1+ import numpy as np
2+ import pytest
3+ from spotpython .surrogate .kriging import Kriging # Assuming Kriging class is in this path
4+
5+ # Create a dummy Kriging instance for testing, as _kernel is a method of the class
6+ # We can use default parameters for the Kriging constructor as they don't affect _kernel
7+ @pytest .fixture
8+ def kriging_model ():
9+ """Fixture to create a Kriging model instance."""
10+ return Kriging ()
11+
12+ def test_kernel_simple_1d (kriging_model ):
13+ """Test _kernel with 2 samples, 1 feature."""
14+ X = np .array ([[0.0 ], [1.0 ]])
15+ theta = np .array ([1.0 ])
16+ p = 2.0
17+
18+ expected_psi_upper_triangle = np .array ([
19+ [0.0 , np .exp (- 1.0 )],
20+ [0.0 , 0.0 ]
21+ ])
22+
23+ result = kriging_model ._kernel (X , theta , p )
24+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
25+
26+ def test_kernel_simple_2d (kriging_model ):
27+ """Test _kernel with 2 samples, 2 features."""
28+ X = np .array ([[0.0 , 0.0 ], [1.0 , 1.0 ]])
29+ theta = np .array ([1.0 , 1.0 ])
30+ p = 2.0
31+
32+ # diff for [0,1]: [abs(0-1)^2, abs(0-1)^2] = [1,1]
33+ # dist_matrix[0,1] = sum(theta * diff) = 1*1 + 1*1 = 2
34+ # Psi[0,1] = exp(-2)
35+ expected_psi_upper_triangle = np .array ([
36+ [0.0 , np .exp (- 2.0 )],
37+ [0.0 , 0.0 ]
38+ ])
39+
40+ result = kriging_model ._kernel (X , theta , p )
41+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
42+
43+ def test_kernel_three_samples_1d (kriging_model ):
44+ """Test _kernel with 3 samples, 1 feature."""
45+ X = np .array ([[0.0 ], [1.0 ], [2.0 ]])
46+ theta = np .array ([0.5 ])
47+ p = 2.0
48+
49+ # For X[0] and X[1]: diff = abs(0-1)^2 = 1. dist = 0.5 * 1 = 0.5. val = exp(-0.5)
50+ # For X[0] and X[2]: diff = abs(0-2)^2 = 4. dist = 0.5 * 4 = 2.0. val = exp(-2.0)
51+ # For X[1] and X[2]: diff = abs(1-2)^2 = 1. dist = 0.5 * 1 = 0.5. val = exp(-0.5)
52+ expected_psi_upper_triangle = np .array ([
53+ [0.0 , np .exp (- 0.5 ), np .exp (- 2.0 )],
54+ [0.0 , 0.0 , np .exp (- 0.5 )],
55+ [0.0 , 0.0 , 0.0 ]
56+ ])
57+
58+ result = kriging_model ._kernel (X , theta , p )
59+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
60+
61+ def test_kernel_different_p_value (kriging_model ):
62+ """Test _kernel with a different p value."""
63+ X = np .array ([[0.0 ], [1.0 ]])
64+ theta = np .array ([1.0 ])
65+ p = 1.0 # Changed p value
66+
67+ # diff for [0,1]: abs(0-1)^1 = 1
68+ # dist_matrix[0,1] = sum(theta * diff) = 1*1 = 1
69+ # Psi[0,1] = exp(-1)
70+ expected_psi_upper_triangle = np .array ([
71+ [0.0 , np .exp (- 1.0 )],
72+ [0.0 , 0.0 ]
73+ ])
74+
75+ result = kriging_model ._kernel (X , theta , p )
76+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
77+
78+ def test_kernel_theta_zeros (kriging_model ):
79+ """Test _kernel when a theta value is zero."""
80+ X = np .array ([[0.0 , 0.0 ], [1.0 , 1.0 ]])
81+ theta = np .array ([1.0 , 0.0 ]) # Second feature's theta is 0
82+ p = 2.0
83+
84+ # diff for [0,1]: [abs(0-1)^2, abs(0-1)^2] = [1,1]
85+ # dist_matrix[0,1] = sum(theta * diff) = 1*1 + 0*1 = 1
86+ # Psi[0,1] = exp(-1)
87+ expected_psi_upper_triangle = np .array ([
88+ [0.0 , np .exp (- 1.0 )],
89+ [0.0 , 0.0 ]
90+ ])
91+
92+ result = kriging_model ._kernel (X , theta , p )
93+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
94+
95+ def test_kernel_no_samples (kriging_model ):
96+ """Test _kernel with no samples. Should produce an empty array or handle gracefully."""
97+ X = np .empty ((0 , 2 )) # No samples, 2 features
98+ theta = np .array ([1.0 , 1.0 ])
99+ p = 2.0
100+
101+ # Expected: an empty (0,0) array for the upper triangle
102+ expected_psi_upper_triangle = np .empty ((0 ,0 ))
103+
104+ result = kriging_model ._kernel (X , theta , p )
105+ # The current implementation of _kernel will likely raise an error or behave unexpectedly
106+ # with X.shape[0] == 0 before np.triu.
107+ # np.zeros((0,0)) is valid.
108+ # np.abs(X[:, np.newaxis, :] - X[np.newaxis, :, :]) will result in (0,0,2) shape
109+ # np.sum on axis 2 will result in (0,0) shape
110+ # np.exp will result in (0,0) shape
111+ # np.triu will result in (0,0) shape
112+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
113+
114+ def test_kernel_one_sample (kriging_model ):
115+ """Test _kernel with only one sample."""
116+ X = np .array ([[1.0 , 2.0 ]]) # One sample, 2 features
117+ theta = np .array ([1.0 , 1.0 ])
118+ p = 2.0
119+
120+ # Expected: a (1,1) array with 0 in the upper triangle (as k=1)
121+ expected_psi_upper_triangle = np .array ([[0.0 ]])
122+
123+ result = kriging_model ._kernel (X , theta , p )
124+ np .testing .assert_array_almost_equal (result , expected_psi_upper_triangle )
0 commit comments