Skip to content

Commit c7e1c64

Browse files
kriging tests
1 parent 1a3d2c6 commit c7e1c64

4 files changed

Lines changed: 115 additions & 18 deletions

File tree

notebooks/testKriging.ipynb

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,7 @@
143143
"p=2\n",
144144
"S = Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
145145
"S.extract_from_bounds(np.array([1, 2, 3]))\n",
146-
"assert len(S.theta) == n\n",
147-
"\n",
148-
"\n",
149-
"\n"
146+
"assert len(S.theta) == n"
150147
]
151148
},
152149
{
@@ -402,6 +399,73 @@
402399
"assert S.Psi.shape == (n, n)\n"
403400
]
404401
},
402+
{
403+
"cell_type": "markdown",
404+
"metadata": {},
405+
"source": [
406+
"## fun_likelihood"
407+
]
408+
},
409+
{
410+
"cell_type": "code",
411+
"execution_count": 23,
412+
"metadata": {},
413+
"outputs": [
414+
{
415+
"name": "stdout",
416+
"output_type": "stream",
417+
"text": [
418+
"[[0]\n",
419+
" [1]]\n",
420+
"[0 1]\n",
421+
"S.theta: [0.]\n",
422+
"S.theta: [1.72284258]\n",
423+
"S.Psi: [[1.00000001e+00 1.14348852e-23]\n",
424+
" [1.14348852e-23 1.00000001e+00]]\n",
425+
"S.U:[[1.00000001e+00 1.14348851e-23]\n",
426+
" [0.00000000e+00 1.00000001e+00]]\n"
427+
]
428+
},
429+
{
430+
"data": {
431+
"text/plain": [
432+
"-1.3862943611198906"
433+
]
434+
},
435+
"execution_count": 23,
436+
"metadata": {},
437+
"output_type": "execute_result"
438+
}
439+
],
440+
"source": [
441+
"from spotPython.build.kriging import Kriging\n",
442+
"import numpy as np\n",
443+
"nat_X = np.array([[0], [1]])\n",
444+
"nat_y = np.array([0, 1])\n",
445+
"n=1\n",
446+
"p=1\n",
447+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
448+
"S.initialize_variables(nat_X, nat_y)\n",
449+
"S.set_variable_types()\n",
450+
"S.nat_to_cod_init()\n",
451+
"print(S.cod_X)\n",
452+
"print(S.cod_y)\n",
453+
"S.set_theta_values()\n",
454+
"print(f\"S.theta: {S.theta}\")\n",
455+
"S.initialize_matrices()\n",
456+
"S.set_de_bounds()\n",
457+
"new_theta_p_Lambda = S.optimize_model()\n",
458+
"S.extract_from_bounds(new_theta_p_Lambda)\n",
459+
"print(f\"S.theta: {S.theta}\")\n",
460+
"S.build_Psi()\n",
461+
"print(f\"S.Psi: {S.Psi}\")\n",
462+
"S.build_U()\n",
463+
"print(f\"S.U:{S.U}\")\n",
464+
"S.likelihood()\n",
465+
"# assert S.negLnLike < 0\n",
466+
"S.negLnLike"
467+
]
468+
},
405469
{
406470
"cell_type": "code",
407471
"execution_count": null,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotPython"
10-
version = "0.7.10"
10+
version = "0.7.12"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -701,19 +701,31 @@ def fun_likelihood(self, new_theta_p_Lambda: np.ndarray) -> float:
701701
The negative log likelihood of the surface at the specified hyperparameters.
702702
703703
Examples:
704-
705704
>>> from spotPython.build.kriging import Kriging
706-
>>> class MyClass(Kriging):
707-
>>> def __init__(self):
708-
>>> super().__init__()
709-
>>> self.n_p = 2
710-
>>> self.n = 3
711-
>>> self.nat_y = np.array([1, 2, 3])
712-
>>> self.k = 2
713-
>>> self.seed = 1
714-
>>> instance = MyClass()
715-
>>> negLnLike = instance.fun_likelihood(new_theta_p_Lambda)
716-
>>> print(negLnLike)
705+
import numpy as np
706+
nat_X = np.array([[0], [1]])
707+
nat_y = np.array([0, 1])
708+
n=1
709+
p=1
710+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
711+
S.initialize_variables(nat_X, nat_y)
712+
S.set_variable_types()
713+
S.nat_to_cod_init()
714+
print(S.cod_X)
715+
print(S.cod_y)
716+
S.set_theta_values()
717+
print(f"S.theta: {S.theta}")
718+
S.initialize_matrices()
719+
S.set_de_bounds()
720+
new_theta_p_Lambda = S.optimize_model()
721+
S.extract_from_bounds(new_theta_p_Lambda)
722+
print(f"S.theta: {S.theta}")
723+
S.build_Psi()
724+
print(f"S.Psi: {S.Psi}")
725+
S.build_U()
726+
print(f"S.U:{S.U}")
727+
S.likelihood()
728+
S.negLnLike
717729
718730
"""
719731
self.extract_from_bounds(new_theta_p_Lambda)

test/test_kriging.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,25 @@ def test_initialize_matrices():
209209
# if var(self.nat_y) is > 0, then self.pen_val = self.n * log(var(self.nat_y)) + 1e4
210210
# else self.pen_val = self.n * var(self.nat_y) + 1e4
211211
assert S.pen_val == nat_X.shape[0] * (var(S.nat_y)) + 1e4
212-
assert S.Psi.shape == (n, n)
212+
assert S.Psi.shape == (n, n)
213+
214+
def test_fun_likelihood():
215+
from spotPython.build.kriging import Kriging
216+
import numpy as np
217+
nat_X = np.array([[0], [1]])
218+
nat_y = np.array([0, 1])
219+
n=1
220+
p=1
221+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
222+
S.initialize_variables(nat_X, nat_y)
223+
S.set_variable_types()
224+
S.nat_to_cod_init()
225+
S.set_theta_values()
226+
S.initialize_matrices()
227+
S.set_de_bounds()
228+
new_theta_p_Lambda = S.optimize_model()
229+
S.extract_from_bounds(new_theta_p_Lambda)
230+
S.build_Psi()
231+
S.build_U()
232+
assert S.negLnLike < 0
233+

0 commit comments

Comments
 (0)