|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Test Functions for Kriging" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "from spotPython.build.kriging import Kriging\n", |
| 17 | + "import numpy as np\n", |
| 18 | + "import matplotlib.pyplot as plt\n", |
| 19 | + "from numpy import linspace, arange\n", |
| 20 | + "rng = np.random.RandomState(1)\n", |
| 21 | + "X = linspace(start=0, stop=10, num=1_000).reshape(-1, 1)\n", |
| 22 | + "y = np.squeeze(X * np.sin(X))\n", |
| 23 | + "training_indices = rng.choice(arange(y.size), size=6, replace=False)\n", |
| 24 | + "X_train, y_train = X[training_indices], y[training_indices]\n", |
| 25 | + "S = Kriging(name='kriging', seed=124)\n", |
| 26 | + "S.fit(X_train, y_train)\n", |
| 27 | + "mean_prediction, std_prediction, s_ei = S.predict(X, return_val=\"all\")\n", |
| 28 | + "plt.plot(X, y, label=r\"$f(x)$\", linestyle=\"dotted\")\n", |
| 29 | + "plt.scatter(X_train, y_train, label=\"Observations\")\n", |
| 30 | + "plt.plot(X, mean_prediction, label=\"Mean prediction\")\n", |
| 31 | + "plt.fill_between(\n", |
| 32 | + " X.ravel(),\n", |
| 33 | + " mean_prediction - 1.96 * std_prediction,\n", |
| 34 | + " mean_prediction + 1.96 * std_prediction,\n", |
| 35 | + " alpha=0.5,\n", |
| 36 | + " label=r\"95% confidence interval\",\n", |
| 37 | + " )\n", |
| 38 | + "plt.legend()\n", |
| 39 | + "plt.xlabel(\"$x$\")\n", |
| 40 | + "plt.ylabel(\"$f(x)$\")\n", |
| 41 | + "_ = plt.title(\"Gaussian process regression on noise-free dataset\")\n", |
| 42 | + "plt.show()" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "from spotPython.build.kriging import Kriging\n", |
| 52 | + "import numpy as np\n", |
| 53 | + "import matplotlib.pyplot as plt\n", |
| 54 | + "from numpy import linspace, arange\n", |
| 55 | + "rng = np.random.RandomState(1)\n", |
| 56 | + "X = linspace(start=0, stop=10, num=1_0).reshape(-1, 1)\n", |
| 57 | + "y = np.squeeze(X * np.sin(X))\n", |
| 58 | + "training_indices = rng.choice(arange(y.size), size=6, replace=False)\n", |
| 59 | + "X_train, y_train = X[training_indices], y[training_indices]\n", |
| 60 | + "S = Kriging(name='kriging', seed=124)\n", |
| 61 | + "S.fit(X_train, y_train)\n", |
| 62 | + "mean_prediction, std_prediction, s_ei = S.predict(X, return_val=\"all\")\n", |
| 63 | + "# Kriging is a interpolator, so the mean prediction should be equal to the training points:\n", |
| 64 | + "# check if the difference between the mean prediction and the true value in the training points is smaller than 1e-6\n", |
| 65 | + "assert np.allclose(mean_prediction[training_indices], y[training_indices], atol=1e-6)\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "## Expected Improvement" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "code", |
| 77 | + "execution_count": null, |
| 78 | + "metadata": {}, |
| 79 | + "outputs": [], |
| 80 | + "source": [ |
| 81 | + "from spotPython.build.kriging import Kriging\n", |
| 82 | + "from math import erf\n", |
| 83 | + "import numpy as np\n", |
| 84 | + "S = Kriging(name='kriging', seed=124)\n", |
| 85 | + "S.mean_cod_y = [0.0, 0.0, 0.0, 0.0, 0.0]\n", |
| 86 | + "# asset that the S.exp_imp(1.0, 0.0) is equal to 0.0\n", |
| 87 | + "assert 0.0 == S.exp_imp(1.0, 0.0)\n", |
| 88 | + "# assert that the S.exp_imp(0.0, 1.0) is equal to 1/sqrt(2 pi)\n", |
| 89 | + "# assert S.exp_imp(0.0, 1.0) == 1/np.sqrt(2*np.pi)\n", |
| 90 | + "# play safe and use np.allclose\n", |
| 91 | + "assert np.allclose(S.exp_imp(0.0, 1.0), 1/np.sqrt(2*np.pi), atol=1e-6)\n", |
| 92 | + "assert np.allclose(S.exp_imp(1.0, 1.0), -(0.5 + 0.5*erf(-1/np.sqrt(2))) + 1/np.sqrt(2*np.pi)*np.exp(-1/2), atol=1e-6)" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "markdown", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "# set_de_bounds" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": null, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "from spotPython.build.kriging import Kriging\n", |
| 109 | + "S = Kriging(name='kriging', seed=124)\n", |
| 110 | + "S.set_de_bounds()\n", |
| 111 | + "assert S.de_bounds == [[-3, 2]]\n", |
| 112 | + "from spotPython.build.kriging import Kriging\n", |
| 113 | + "n = 10\n", |
| 114 | + "S = Kriging(name='kriging', seed=124, n_theta=n)\n", |
| 115 | + "S.set_de_bounds()\n", |
| 116 | + "assert len(S.de_bounds) == n\n", |
| 117 | + "n=2\n", |
| 118 | + "p=4\n", |
| 119 | + "S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True)\n", |
| 120 | + "S.set_de_bounds()\n", |
| 121 | + "assert len(S.de_bounds) == n+p\n", |
| 122 | + "S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=False)\n", |
| 123 | + "S.set_de_bounds()\n", |
| 124 | + "assert len(S.de_bounds) == n" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "metadata": {}, |
| 130 | + "source": [ |
| 131 | + "## extract_from_bounds" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 2, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "name": "stdout", |
| 141 | + "output_type": "stream", |
| 142 | + "text": [ |
| 143 | + "[1 2]\n", |
| 144 | + "[3]\n" |
| 145 | + ] |
| 146 | + } |
| 147 | + ], |
| 148 | + "source": [ |
| 149 | + "import numpy as np\n", |
| 150 | + "from spotPython.build.kriging import Kriging\n", |
| 151 | + "n=2\n", |
| 152 | + "p=4\n", |
| 153 | + "S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n", |
| 154 | + "S.extract_from_bounds(np.array([1, 2, 3]))\n", |
| 155 | + "print(S.theta)\n", |
| 156 | + "print(S.p)\n", |
| 157 | + "\n", |
| 158 | + "\n" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
| 166 | + "source": [] |
| 167 | + } |
| 168 | + ], |
| 169 | + "metadata": { |
| 170 | + "kernelspec": { |
| 171 | + "display_name": "spotCondaEnv", |
| 172 | + "language": "python", |
| 173 | + "name": "python3" |
| 174 | + }, |
| 175 | + "language_info": { |
| 176 | + "codemirror_mode": { |
| 177 | + "name": "ipython", |
| 178 | + "version": 3 |
| 179 | + }, |
| 180 | + "file_extension": ".py", |
| 181 | + "mimetype": "text/x-python", |
| 182 | + "name": "python", |
| 183 | + "nbconvert_exporter": "python", |
| 184 | + "pygments_lexer": "ipython3", |
| 185 | + "version": "3.11.6" |
| 186 | + } |
| 187 | + }, |
| 188 | + "nbformat": 4, |
| 189 | + "nbformat_minor": 2 |
| 190 | +} |
0 commit comments