Skip to content

Commit 01da155

Browse files
test kriging example
1 parent c7e1c64 commit 01da155

4 files changed

Lines changed: 181 additions & 53 deletions

File tree

notebooks/testKriging.ipynb

Lines changed: 105 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,20 @@
285285
},
286286
{
287287
"cell_type": "code",
288-
"execution_count": null,
288+
"execution_count": 5,
289289
"metadata": {},
290-
"outputs": [],
290+
"outputs": [
291+
{
292+
"data": {
293+
"text/plain": [
294+
"array([ True, True])"
295+
]
296+
},
297+
"execution_count": 5,
298+
"metadata": {},
299+
"output_type": "execute_result"
300+
}
301+
],
291302
"source": [
292303
"from spotPython.build.kriging import Kriging\n",
293304
"import numpy as np\n",
@@ -299,6 +310,21 @@
299310
"S.initialize_variables(nat_X, nat_y)\n",
300311
"S.set_variable_types()\n",
301312
"assert S.var_type == ['num', 'num']\n",
313+
"assert S.num_mask.all() == True\n",
314+
"assert S.factor_mask.all() == False\n",
315+
"assert S.int_mask.all() == False\n",
316+
"assert S.ordered_mask.all() == True\n",
317+
"S.num_mask"
318+
]
319+
},
320+
{
321+
"cell_type": "code",
322+
"execution_count": null,
323+
"metadata": {},
324+
"outputs": [],
325+
"source": [
326+
"\n",
327+
"\n",
302328
"nat_X = np.array([[1, 2, 3], [4, 5, 6]])\n",
303329
"nat_y = np.array([1, 2])\n",
304330
"n=3\n",
@@ -361,7 +387,7 @@
361387
},
362388
{
363389
"cell_type": "code",
364-
"execution_count": 9,
390+
"execution_count": null,
365391
"metadata": {},
366392
"outputs": [],
367393
"source": [
@@ -408,35 +434,9 @@
408434
},
409435
{
410436
"cell_type": "code",
411-
"execution_count": 23,
437+
"execution_count": null,
412438
"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-
],
439+
"outputs": [],
440440
"source": [
441441
"from spotPython.build.kriging import Kriging\n",
442442
"import numpy as np\n",
@@ -466,6 +466,80 @@
466466
"S.negLnLike"
467467
]
468468
},
469+
{
470+
"cell_type": "code",
471+
"execution_count": null,
472+
"metadata": {},
473+
"outputs": [],
474+
"source": [
475+
"from spotPython.build.kriging import Kriging\n",
476+
"import numpy as np\n",
477+
"nat_X = np.array([[0,1], [1, 0]])\n",
478+
"nat_y = np.array([0, 10])\n",
479+
"n=2\n",
480+
"p=1\n",
481+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
482+
"S.initialize_variables(nat_X, nat_y)\n",
483+
"S.set_variable_types()\n",
484+
"S.nat_to_cod_init()\n",
485+
"print(S.cod_X)\n",
486+
"print(S.cod_y)\n",
487+
"S.set_theta_values()\n",
488+
"print(f\"S.theta: {S.theta}\")\n",
489+
"S.initialize_matrices()\n",
490+
"print(f\"S.mu: {S.mu}\")\n",
491+
"S.set_de_bounds()\n",
492+
"new_theta_p_Lambda = S.optimize_model()\n",
493+
"print(f\"S.mu: {S.mu}\")\n",
494+
"S.extract_from_bounds(new_theta_p_Lambda)\n",
495+
"print(f\"S.theta: {S.theta}\")\n",
496+
"S.build_Psi()\n",
497+
"print(f\"S.Psi: {S.Psi}\")\n",
498+
"S.build_U()\n",
499+
"print(f\"S.U:{S.U}\")\n",
500+
"print(f\"S.mu: {S.mu}\")\n",
501+
"S.likelihood()\n",
502+
"print(f\"S.mu: {S.mu}\")\n",
503+
"# assert S.negLnLike < 0\n",
504+
"S.negLnLike"
505+
]
506+
},
507+
{
508+
"cell_type": "markdown",
509+
"metadata": {},
510+
"source": [
511+
"## Kriging Example 1D"
512+
]
513+
},
514+
{
515+
"cell_type": "code",
516+
"execution_count": 4,
517+
"metadata": {},
518+
"outputs": [],
519+
"source": [
520+
"from spotPython.build.kriging import Kriging\n",
521+
"import numpy as np\n",
522+
"nat_X = np.array([[1], [2]])\n",
523+
"nat_y = np.array([5, 10])\n",
524+
"n=2\n",
525+
"p=1\n",
526+
"S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)\n",
527+
"S.initialize_variables(nat_X, nat_y)\n",
528+
"S.set_variable_types()\n",
529+
"S.nat_to_cod_init()\n",
530+
"S.set_theta_values()\n",
531+
"S.initialize_matrices()\n",
532+
"S.build_Psi()\n",
533+
"S.build_U()\n",
534+
"S.likelihood()\n",
535+
"# assert S.mu is close to 7.5 with a tolerance of 1e-6\n",
536+
"assert np.allclose(S.mu, 7.5, atol=1e-6)\n",
537+
"E = np.exp(1)\n",
538+
"sigma2 = E/(E**2 -1) * (25/4 + 25/4*E)\n",
539+
"# asssert S.SigmaSqr is close to sigma2 with a tolerance of 1e-6\n",
540+
"assert np.allclose(S.SigmaSqr, sigma2, atol=1e-6)\n"
541+
]
542+
},
469543
{
470544
"cell_type": "code",
471545
"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.12"
10+
version = "0.7.13"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/build/kriging.py

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ def fit(self, nat_X: np.ndarray, nat_y: np.ndarray) -> object:
480480
print(S.Psi)
481481
[[1.00000001 1. ]
482482
[1. 1.00000001]]
483-
483+
484484
"""
485485
self.initialize_variables(nat_X, nat_y)
486486
self.set_variable_types()
@@ -548,8 +548,9 @@ def set_variable_types(self) -> None:
548548
This method sets the variable types for the class instance based
549549
on the `var_type` attribute. If the length of `var_type` is less
550550
than `k`, all variable types are forced to 'num' and a warning is logged.
551-
The method then creates masks for each variable
552-
type ('num', 'factor', 'int', 'float') using numpy arrays.
551+
The method then creates Boolean masks for each variable
552+
type ('num', 'factor', 'int', 'ordered') using numpy arrays, e.g.,
553+
`num_mask = array([ True, True])` if two numerical variables are present.
553554
554555
Args:
555556
self (object): The Kriging object.
@@ -564,6 +565,11 @@ def set_variable_types(self) -> None:
564565
S.initialize_variables(nat_X, nat_y)
565566
S.set_variable_types()
566567
assert S.var_type == ['num', 'num']
568+
assert S.var_type == ['num', 'num']
569+
assert S.num_mask.all() == True
570+
assert S.factor_mask.all() == False
571+
assert S.int_mask.all() == False
572+
assert S.ordered_mask.all() == True
567573
568574
Returns:
569575
None
@@ -912,21 +918,29 @@ def likelihood(self) -> None:
912918
None
913919
914920
Examples:
915-
916921
>>> from spotPython.build.kriging import Kriging
917-
>>> class MyClass(Kriging):
918-
>>> def __init__(self):
919-
>>> super().__init__()
920-
>>> self.n_p = 2
921-
>>> self.n = 3
922-
>>> self.nat_y = np.array([1, 2, 3])
923-
>>> self.k = 2
924-
>>> self.seed = 1
925-
926-
>>> obj = MyClass()
927-
>>> obj.build_Psi()
928-
>>> obj.build_U()
929-
>>> obj.likelihood()
922+
import numpy as np
923+
nat_X = np.array([[1], [2]])
924+
nat_y = np.array([5, 10])
925+
n=2
926+
p=1
927+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
928+
S.initialize_variables(nat_X, nat_y)
929+
S.set_variable_types()
930+
S.nat_to_cod_init()
931+
S.set_theta_values()
932+
S.initialize_matrices()
933+
S.build_Psi()
934+
S.build_U()
935+
S.likelihood()
936+
# assert S.mu is close to 7.5 with a tolerance of 1e-6
937+
assert np.allclose(S.mu, 7.5, atol=1e-6)
938+
E = np.exp(1)
939+
sigma2 = E/(E**2 -1) * (25/4 + 25/4*E)
940+
# asssert S.SigmaSqr is close to sigma2 with a tolerance of 1e-6
941+
assert np.allclose(S.SigmaSqr, sigma2, atol=1e-6)
942+
print(f"S.LnDetPsi:{S.LnDetPsi}")
943+
print(f"S.self.negLnLike:{S.negLnLike}")
930944
"""
931945
# (2.20) in [Forr08a]:
932946
U_T_inv_one = solve(self.U.T, self.one)
@@ -1367,15 +1381,18 @@ def cod_to_nat_y(self, cod_y: np.ndarray) -> np.ndarray:
13671381

13681382
def nat_to_cod_x(self, nat_X: np.ndarray) -> np.ndarray:
13691383
"""
1370-
Normalizes one point (row) of nat_X array to [0,1]. The internal nat_range_X values are not updated.
1384+
Compute coded X-values from natural (physical or real world) units based on the
1385+
setting of the `cod_type` attribute. If `cod_type` is "norm", the values are
1386+
normalized to [0,1]. If `cod_type` is "std", the values are standardized.
1387+
Otherwise, the values are not modified.
13711388
13721389
Args:
13731390
self (object): The Kriging object.
13741391
nat_X (np.ndarray):
13751392
An array representing one point (self.k long) in natural (physical or real world) units.
13761393
13771394
Returns:
1378-
X (np.ndarray): An array of coded values in the range of [0,1] for each dimension.
1395+
X (np.ndarray): An array of coded values for each dimension.
13791396
13801397
Examples:
13811398
@@ -1417,7 +1434,10 @@ def nat_to_cod_x(self, nat_X: np.ndarray) -> np.ndarray:
14171434

14181435
def nat_to_cod_y(self, nat_y: np.ndarray) -> np.ndarray:
14191436
"""
1420-
Normalizes natural y values to [0,1].
1437+
Compute coded y-values from natural (physical or real world) units based on the
1438+
setting of the `cod_type` attribute. If `cod_type` is "norm", the values are
1439+
normalized to [0,1]. If `cod_type` is "std", the values are standardized.
1440+
Otherwise, the values are not modified.
14211441
14221442
Args:
14231443
self (object): The Kriging object.
@@ -1447,10 +1467,17 @@ def nat_to_cod_y(self, nat_y: np.ndarray) -> np.ndarray:
14471467

14481468
def nat_to_cod_init(self) -> None:
14491469
"""
1450-
Determines max and min of each dimension and normalizes that axis to a range of [0,1].
1470+
Compute coded X- and y-values from natural (physical or real world) units based on the
1471+
setting of the `cod_type` attribute. If `cod_type` is "norm", the values are
1472+
normalized to [0,1]. If `cod_type` is "std", the values are standardized.
1473+
Otherwise, the values are not modified.
14511474
Called when 1) surrogate is initialized and 2) new points arrive, i.e.,
14521475
suggested by the surrogate as infill points.
14531476
This method calls `nat_to_cod_x` and `nat_to_cod_y` and updates the ranges `nat_range_X` and `nat_range_y`.
1477+
Furthermore, the following statistics are computed:
1478+
- `nat_mean_X` and `nat_std_X`
1479+
- `nat_mean_y` and `nat_std_y`
1480+
- `mean_cod_y`
14541481
14551482
Args:
14561483
self (object): The Kriging object.

test/test_kriging.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ def test_set_variable_types():
144144
S.initialize_variables(nat_X, nat_y)
145145
S.set_variable_types()
146146
assert S.var_type == ['num', 'num']
147+
assert S.var_type == ['num', 'num']
148+
assert S.num_mask.all() == True
149+
assert S.factor_mask.all() == False
150+
assert S.int_mask.all() == False
151+
assert S.ordered_mask.all() == True
147152
nat_X = np.array([[1, 2, 3], [4, 5, 6]])
148153
nat_y = np.array([1, 2])
149154
n=3
@@ -230,4 +235,26 @@ def test_fun_likelihood():
230235
S.build_Psi()
231236
S.build_U()
232237
assert S.negLnLike < 0
238+
239+
def test_likelihood():
240+
nat_X = np.array([[1], [2]])
241+
nat_y = np.array([5, 10])
242+
n=2
243+
p=1
244+
S=Kriging(name='kriging', seed=124, n_theta=n, n_p=p, optim_p=True, noise=False)
245+
S.initialize_variables(nat_X, nat_y)
246+
S.set_variable_types()
247+
S.nat_to_cod_init()
248+
S.set_theta_values()
249+
S.initialize_matrices()
250+
S.build_Psi()
251+
S.build_U()
252+
S.likelihood()
253+
# assert S.mu is close to 7.5 with a tolerance of 1e-6
254+
assert np.allclose(S.mu, 7.5, atol=1e-6)
255+
E = np.exp(1)
256+
sigma2 = E/(E**2 -1) * (25/4 + 25/4*E)
257+
# asssert S.SigmaSqr is close to sigma2 with a tolerance of 1e-6
258+
assert np.allclose(S.SigmaSqr, sigma2, atol=1e-6)
259+
233260

0 commit comments

Comments
 (0)