Skip to content

Commit 1ba26a2

Browse files
tests for kriging
1 parent 8a6a118 commit 1ba26a2

4 files changed

Lines changed: 151 additions & 13 deletions

File tree

notebooks/testKriging.ipynb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,89 @@
189189
"assert len(new_theta_p_Lambda) == n+p"
190190
]
191191
},
192+
{
193+
"cell_type": "markdown",
194+
"metadata": {},
195+
"source": [
196+
"## update_log"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 10,
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"name": "stdout",
206+
"output_type": "stream",
207+
"text": [
208+
"{'negLnLike': array([-1.38629436]), 'theta': array([-1.14525993, 1.6123372 ]), 'p': array([1.84444406, 1.74590865]), 'Lambda': array([0.44268472])}\n",
209+
"{'negLnLike': array([-1.38629436, -1.38629436]), 'theta': array([-1.14525993, 1.6123372 , -1.14525993, 1.6123372 ]), 'p': array([1.84444406, 1.74590865, 1.84444406, 1.74590865]), 'Lambda': array([0.44268472, 0.44268472])}\n"
210+
]
211+
}
212+
],
213+
"source": [
214+
"from spotPython.build.kriging import Kriging\n",
215+
"import numpy as np\n",
216+
"nat_X = np.array([[1, 2], [3, 4]])\n",
217+
"nat_y = np.array([1, 2])\n",
218+
"n=2\n",
219+
"p=2\n",
220+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
221+
"S.initialize_variables(nat_X, nat_y)\n",
222+
"S.set_variable_types()\n",
223+
"S.nat_to_cod_init()\n",
224+
"S.set_theta_values()\n",
225+
"S.initialize_matrices()\n",
226+
"S.set_de_bounds()\n",
227+
"new_theta_p_Lambda = S.optimize_model()\n",
228+
"S.update_log()\n",
229+
"print(S.log)\n",
230+
"assert len(S.log[\"negLnLike\"]) == 1\n",
231+
"assert len(S.log[\"theta\"]) == n\n",
232+
"assert len(S.log[\"p\"]) == p\n",
233+
"assert len(S.log[\"Lambda\"]) == 1\n",
234+
"S.update_log()\n",
235+
"print(S.log)\n",
236+
"assert len(S.log[\"negLnLike\"]) == 2\n",
237+
"assert len(S.log[\"theta\"]) == 2*n\n",
238+
"assert len(S.log[\"p\"]) == 2*p\n",
239+
"assert len(S.log[\"Lambda\"]) == 2"
240+
]
241+
},
242+
{
243+
"cell_type": "markdown",
244+
"metadata": {},
245+
"source": [
246+
"## fit"
247+
]
248+
},
249+
{
250+
"cell_type": "code",
251+
"execution_count": 6,
252+
"metadata": {},
253+
"outputs": [
254+
{
255+
"name": "stdout",
256+
"output_type": "stream",
257+
"text": [
258+
"[[1.00000001 1. ]\n",
259+
" [1. 1.00000001]]\n"
260+
]
261+
}
262+
],
263+
"source": [
264+
"from spotPython.build.kriging import Kriging\n",
265+
"import numpy as np\n",
266+
"nat_X = np.array([[1, 0], [1, 0]])\n",
267+
"nat_y = np.array([1, 2])\n",
268+
"S = Kriging()\n",
269+
"S.fit(nat_X, nat_y)\n",
270+
"assert S.Psi.shape == (2, 2)\n",
271+
"assert len(S.log[\"negLnLike\"]) == 1\n",
272+
"print(S.Psi)"
273+
]
274+
},
192275
{
193276
"cell_type": "code",
194277
"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.8"
10+
version = "0.7.9"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def extract_from_bounds(self, new_theta_p_Lambda: np.ndarray) -> None:
303303
print(S.theta)
304304
print(S.p)
305305
[1 2]
306-
[3]
306+
[3]
307307
308308
Returns:
309309
None
@@ -395,13 +395,27 @@ def update_log(self) -> None:
395395
None
396396
397397
Examples:
398-
399398
>>> from spotPython.build.kriging import Kriging
400-
>>> MyClass = Kriging(name='kriging', seed=124)
401-
>>> obj = MyClass()
402-
>>> obj.update_log()
403-
>>> print(obj.log)
404-
{'negLnLike': [0.5], 'theta': [0.1], 'p': [0.2], 'Lambda': [0.3]}
399+
import numpy as np
400+
nat_X = np.array([[1, 2], [3, 4]])
401+
nat_y = np.array([1, 2])
402+
n=2
403+
p=2
404+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
405+
S.initialize_variables(nat_X, nat_y)
406+
S.set_variable_types()
407+
S.nat_to_cod_init()
408+
S.set_theta_values()
409+
S.initialize_matrices()
410+
S.set_de_bounds()
411+
new_theta_p_Lambda = S.optimize_model()
412+
S.update_log()
413+
print(S.log)
414+
{'negLnLike': array([-1.38629436]),
415+
'theta': array([-1.14525993, 1.6123372 ]),
416+
'p': array([1.84444406, 1.74590865]),
417+
'Lambda': array([0.44268472])}
418+
405419
"""
406420
self.log["negLnLike"] = append(self.log["negLnLike"], self.negLnLike)
407421
self.log["theta"] = append(self.log["theta"], self.theta)
@@ -457,12 +471,16 @@ def fit(self, nat_X: np.ndarray, nat_y: np.ndarray) -> object:
457471
Lambda (float): lambda noise value.
458472
459473
Examples:
460-
461474
>>> from spotPython.build.kriging import Kriging
462-
>>> nat_X = np.array([[1, 2], [3, 4]])
463-
>>> nat_y = np.array([1, 2])
464-
>>> surrogate = Kriging()
465-
>>> surrogate.fit(nat_X, nat_y)
475+
import numpy as np
476+
nat_X = np.array([[1, 0], [1, 0]])
477+
nat_y = np.array([1, 2])
478+
S = Kriging()
479+
S.fit(nat_X, nat_y)
480+
print(S.Psi)
481+
[[1.00000001 1. ]
482+
[1. 1.00000001]]
483+
466484
"""
467485
self.initialize_variables(nat_X, nat_y)
468486
self.set_variable_types()

test/test_kriging.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,40 @@ def test_optimize_model():
9090
S.set_de_bounds()
9191
new_theta_p_Lambda = S.optimize_model()
9292
assert len(new_theta_p_Lambda) == n+p
93+
94+
def test_update_log():
95+
from spotPython.build.kriging import Kriging
96+
import numpy as np
97+
nat_X = np.array([[1, 2], [3, 4]])
98+
nat_y = np.array([1, 2])
99+
n=2
100+
p=2
101+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
102+
S.initialize_variables(nat_X, nat_y)
103+
S.set_variable_types()
104+
S.nat_to_cod_init()
105+
S.set_theta_values()
106+
S.initialize_matrices()
107+
S.set_de_bounds()
108+
new_theta_p_Lambda = S.optimize_model()
109+
S.update_log()
110+
assert len(S.log["negLnLike"]) == 1
111+
assert len(S.log["theta"]) == n
112+
assert len(S.log["p"]) == p
113+
assert len(S.log["Lambda"]) == 1
114+
S.update_log()
115+
# now that we have log iterations two, there should be two entries in the log
116+
assert len(S.log["negLnLike"]) == 2
117+
assert len(S.log["theta"]) == 2*n
118+
assert len(S.log["p"]) == 2*p
119+
assert len(S.log["Lambda"]) == 2
120+
121+
def test_fit():
122+
from spotPython.build.kriging import Kriging
123+
import numpy as np
124+
nat_X = np.array([[1, 0], [1, 0]])
125+
nat_y = np.array([1, 2])
126+
S = Kriging()
127+
S.fit(nat_X, nat_y)
128+
assert S.Psi.shape == (2, 2)
129+
assert len(S.log["negLnLike"]) == 1

0 commit comments

Comments
 (0)