Skip to content

Commit 1a3d2c6

Browse files
kriging tests
1 parent 1ba26a2 commit 1a3d2c6

4 files changed

Lines changed: 279 additions & 60 deletions

File tree

notebooks/testKriging.ipynb

Lines changed: 155 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
},
134134
{
135135
"cell_type": "code",
136-
"execution_count": 21,
136+
"execution_count": null,
137137
"metadata": {},
138138
"outputs": [],
139139
"source": [
@@ -158,7 +158,7 @@
158158
},
159159
{
160160
"cell_type": "code",
161-
"execution_count": 19,
161+
"execution_count": null,
162162
"metadata": {},
163163
"outputs": [],
164164
"source": [
@@ -198,18 +198,9 @@
198198
},
199199
{
200200
"cell_type": "code",
201-
"execution_count": 10,
201+
"execution_count": null,
202202
"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-
],
203+
"outputs": [],
213204
"source": [
214205
"from spotPython.build.kriging import Kriging\n",
215206
"import numpy as np\n",
@@ -248,22 +239,13 @@
248239
},
249240
{
250241
"cell_type": "code",
251-
"execution_count": 6,
242+
"execution_count": null,
252243
"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-
],
244+
"outputs": [],
263245
"source": [
264246
"from spotPython.build.kriging import Kriging\n",
265247
"import numpy as np\n",
266-
"nat_X = np.array([[1, 0], [1, 0]])\n",
248+
"nat_X = np.array([[.1, 0], [0, .1]])\n",
267249
"nat_y = np.array([1, 2])\n",
268250
"S = Kriging()\n",
269251
"S.fit(nat_X, nat_y)\n",
@@ -272,6 +254,154 @@
272254
"print(S.Psi)"
273255
]
274256
},
257+
{
258+
"cell_type": "markdown",
259+
"metadata": {},
260+
"source": [
261+
"## initialize_variables"
262+
]
263+
},
264+
{
265+
"cell_type": "code",
266+
"execution_count": null,
267+
"metadata": {},
268+
"outputs": [],
269+
"source": [
270+
"from spotPython.build.kriging import Kriging\n",
271+
"import numpy as np\n",
272+
"nat_X = np.array([[1, 2], [3, 4]])\n",
273+
"nat_y = np.array([1, 2])\n",
274+
"S = Kriging()\n",
275+
"S.initialize_variables(nat_X, nat_y)\n",
276+
"assert S.nat_X.all() == nat_X.all()\n",
277+
"assert S.nat_y.all() == nat_y.all()\n",
278+
"assert S.cod_X.shape == (2, 2)\n",
279+
"assert S.cod_y.shape == (2,)"
280+
]
281+
},
282+
{
283+
"cell_type": "markdown",
284+
"metadata": {},
285+
"source": [
286+
"## set_variable_types"
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": null,
292+
"metadata": {},
293+
"outputs": [],
294+
"source": [
295+
"from spotPython.build.kriging import Kriging\n",
296+
"import numpy as np\n",
297+
"nat_X = np.array([[1, 2], [3, 4]])\n",
298+
"nat_y = np.array([1, 2])\n",
299+
"n=2\n",
300+
"p=2\n",
301+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
302+
"S.initialize_variables(nat_X, nat_y)\n",
303+
"S.set_variable_types()\n",
304+
"assert S.var_type == ['num', 'num']\n",
305+
"nat_X = np.array([[1, 2, 3], [4, 5, 6]])\n",
306+
"nat_y = np.array([1, 2])\n",
307+
"n=3\n",
308+
"p=1\n",
309+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
310+
"S.initialize_variables(nat_X, nat_y)\n",
311+
"S.set_variable_types()\n",
312+
"S.var_type\n",
313+
"assert S.var_type == ['num', 'num', 'num']"
314+
]
315+
},
316+
{
317+
"cell_type": "markdown",
318+
"metadata": {},
319+
"source": [
320+
"## set_theta_values"
321+
]
322+
},
323+
{
324+
"cell_type": "code",
325+
"execution_count": null,
326+
"metadata": {},
327+
"outputs": [],
328+
"source": [
329+
"from spotPython.build.kriging import Kriging\n",
330+
"import numpy as np\n",
331+
"from numpy import array\n",
332+
"nat_X = np.array([[1, 2], [3, 4]])\n",
333+
"nat_y = np.array([1, 2])\n",
334+
"n=2\n",
335+
"p=2\n",
336+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
337+
"S.initialize_variables(nat_X, nat_y)\n",
338+
"S.set_variable_types()\n",
339+
"S.nat_to_cod_init()\n",
340+
"S.set_theta_values()\n",
341+
"assert S.theta.all() == array([0., 0.]).all()\n",
342+
"nat_X = np.array([[1, 2], [3, 4]])\n",
343+
"nat_y = np.array([1, 2])\n",
344+
"# n is set to 3, but the number of columns of nat_X is 2\n",
345+
"n=3\n",
346+
"p=2\n",
347+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
348+
"S.initialize_variables(nat_X, nat_y)\n",
349+
"S.set_variable_types()\n",
350+
"S.nat_to_cod_init()\n",
351+
"snt = S.n_theta\n",
352+
"S.set_theta_values()\n",
353+
"# since snt == 3, it is not equal to S.n_theta, which is 2 because \n",
354+
"# of the correction in the set_theta_values method\n",
355+
"assert S.n_theta != snt\n"
356+
]
357+
},
358+
{
359+
"cell_type": "markdown",
360+
"metadata": {},
361+
"source": [
362+
"## initialize_matrices"
363+
]
364+
},
365+
{
366+
"cell_type": "code",
367+
"execution_count": 9,
368+
"metadata": {},
369+
"outputs": [],
370+
"source": [
371+
"from spotPython.build.kriging import Kriging\n",
372+
"import numpy as np\n",
373+
"from numpy import log, var\n",
374+
"nat_X = np.array([[1, 2], [3, 4], [5, 6]])\n",
375+
"nat_y = np.array([1, 2, 3])\n",
376+
"n=3\n",
377+
"p=1\n",
378+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
379+
"S.initialize_variables(nat_X, nat_y)\n",
380+
"S.set_variable_types()\n",
381+
"S.nat_to_cod_init()\n",
382+
"S.set_theta_values()\n",
383+
"S.initialize_matrices()\n",
384+
"# if var(self.nat_y) is > 0, then self.pen_val = self.n * log(var(self.nat_y)) + 1e4\n",
385+
"# else self.pen_val = self.n * var(self.nat_y) + 1e4\n",
386+
"assert S.pen_val == nat_X.shape[0] * log(var(S.nat_y)) + 1e4\n",
387+
"assert S.Psi.shape == (n, n)\n",
388+
"#\n",
389+
"# use a zero variance, then the penalty should be computed without log()\n",
390+
"nat_y = np.array([1, 1, 1])\n",
391+
"n=3\n",
392+
"p=1\n",
393+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)\n",
394+
"S.initialize_variables(nat_X, nat_y)\n",
395+
"S.set_variable_types()\n",
396+
"S.nat_to_cod_init()\n",
397+
"S.set_theta_values()\n",
398+
"S.initialize_matrices()\n",
399+
"# if var(self.nat_y) is > 0, then self.pen_val = self.n * log(var(self.nat_y)) + 1e4\n",
400+
"# else self.pen_val = self.n * var(self.nat_y) + 1e4\n",
401+
"assert S.pen_val == nat_X.shape[0] * (var(S.nat_y)) + 1e4\n",
402+
"assert S.Psi.shape == (n, n)\n"
403+
]
404+
},
275405
{
276406
"cell_type": "code",
277407
"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.9"
10+
version = "0.7.10"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -555,16 +555,15 @@ def set_variable_types(self) -> None:
555555
self (object): The Kriging object.
556556
557557
Examples:
558-
559558
>>> from spotPython.build.kriging import Kriging
560-
>>> class MyClass(Kriging):
561-
>>> def __init__(self):
562-
>>> super().__init__()
563-
>>> self.var_type = ["num", "factor"]
564-
>>> instance = MyClass()
565-
>>> instance.set_variable_types()
566-
>>> instance.num_mask
567-
array([ True, False])
559+
nat_X = np.array([[1, 2], [3, 4]])
560+
nat_y = np.array([1, 2])
561+
n=2
562+
p=2
563+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
564+
S.initialize_variables(nat_X, nat_y)
565+
S.set_variable_types()
566+
assert S.var_type == ['num', 'num']
568567
569568
Returns:
570569
None
@@ -597,17 +596,19 @@ def set_theta_values(self) -> None:
597596
None
598597
599598
Examples:
600-
601599
>>> from spotPython.build.kriging import Kriging
602-
>>> class MyClass(Kriging):
603-
>>> def __init__(self):
604-
>>> super().__init__()
605-
>>> self.n_theta = 3
606-
>>> self.k = 2
607-
>>> instance = MyClass()
608-
>>> instance.set_theta_values()
609-
>>> instance.theta
610-
array([0., 0., 0.])
600+
import numpy as np
601+
from numpy import array
602+
nat_X = np.array([[1, 2], [3, 4]])
603+
nat_y = np.array([1, 2])
604+
n=2
605+
p=2
606+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
607+
S.initialize_variables(nat_X, nat_y)
608+
S.set_variable_types()
609+
S.nat_to_cod_init()
610+
S.set_theta_values()
611+
assert S.theta.all() == array([0., 0.]).all()
611612
"""
612613
if self.n_theta > self.k:
613614
self.n_theta = self.k
@@ -634,18 +635,23 @@ def initialize_matrices(self) -> None:
634635
self (object): The Kriging object.
635636
636637
Examples:
637-
638638
>>> from spotPython.build.kriging import Kriging
639-
>>> class MyClass(Kriging):
640-
>>> def __init__(self):
641-
>>> super().__init__()
642-
>>> self.n_p = 2
643-
>>> self.n = 3
644-
>>> self.nat_y = np.array([1, 2, 3])
645-
>>> self.k = 2
646-
>>> self.seed = 1
647-
>>> instance = MyClass()
648-
>>> instance.initialize_matrices()
639+
import numpy as np
640+
from numpy import log, var
641+
nat_X = np.array([[1, 2], [3, 4], [5, 6]])
642+
nat_y = np.array([1, 2, 3])
643+
n=3
644+
p=1
645+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=True)
646+
S.initialize_variables(nat_X, nat_y)
647+
S.set_variable_types()
648+
S.nat_to_cod_init()
649+
S.set_theta_values()
650+
S.initialize_matrices()
651+
# if var(self.nat_y) is > 0, then self.pen_val = self.n * log(var(self.nat_y)) + 1e4
652+
# else self.pen_val = self.n * var(self.nat_y) + 1e4
653+
assert S.pen_val == nat_X.shape[0] * log(var(S.nat_y)) + 1e4
654+
assert S.Psi.shape == (n, n)
649655
650656
Returns:
651657
None

0 commit comments

Comments
 (0)