Skip to content

Commit 0bba6ea

Browse files
0.25.9
1 parent e12d3ee commit 0bba6ea

10 files changed

Lines changed: 1264 additions & 727 deletions

File tree

notebooks/00_spotPython_tests.ipynb

Lines changed: 961 additions & 220 deletions
Large diffs are not rendered by default.

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.25.8"
10+
version = "0.25.9"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotpython/gp/covar.py

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,12 @@
11
import numpy as np
2+
import pandas as pd
23

34

4-
def covar_sep_symm(col, X, n, d, g, K=None) -> np.ndarray:
5-
"""
6-
Calculate the correlation (K) between X1 and X2 with a separable power exponential correlation function with range d and nugget g.
7-
8-
Args:
9-
col (int): Number of columns in the input matrix X.
10-
X (ndarray): Input matrix of shape (n, col).
11-
n (int): Number of rows in the input matrix X.
12-
d (ndarray): Array of length col representing the range parameters.
13-
g (float): Nugget parameter.
14-
K (ndarray): The covariance matrix of shape (n, n).
15-
16-
Returns:
17-
ndarray: The calculated covariance matrix K of shape (n, n).
18-
19-
Examples:
20-
>>> col = 2
21-
>>> X = np.array([[1, 2], [3, 4], [5, 6]])
22-
>>> n = 3
23-
>>> d = np.array([1.0, 1.0])
24-
>>> g = 0.1
25-
>>> K = covar_sep_symm(col, X, n, d, g)
26-
>>> print(K)
27-
[[1.1 0.01831564 0.00012341]
28-
[0.01831564 1.1 0.01831564]
29-
[0.00012341 0.01831564 1.1 ]]
30-
"""
31-
K = np.zeros((n, n))
32-
33-
for i in range(n):
34-
K[i, i] = 1.0 + g
35-
for j in range(i + 1, n):
36-
K[i, j] = 0.0
37-
for k in range(col):
38-
K[i, j] += (X[i, k] - X[j, k]) ** 2 / d[k]
39-
K[i, j] = np.exp(-K[i, j])
40-
K[j, i] = K[i, j]
41-
42-
return K
5+
def prepare_X(X: np.ndarray) -> np.ndarray:
6+
# check if X is a dataframe and convert to numpy array
7+
if isinstance(X, pd.DataFrame):
8+
X = X.to_numpy()
9+
return X
4310

4411

4512
def covar_sep(col, X1, n1, X2, n2, d, g) -> np.ndarray:
@@ -61,6 +28,8 @@ def covar_sep(col, X1, n1, X2, n2, d, g) -> np.ndarray:
6128
ndarray: The calculated covariance matrix K of shape (n1, n2).
6229
6330
Examples:
31+
>>> import numpy as np
32+
>>> from spotpython.gp.covar import covar_sep
6433
>>> col = 2
6534
>>> X1 = np.array([[1, 2], [3, 4], [5, 6]])
6635
>>> n1 = 3
@@ -75,13 +44,62 @@ def covar_sep(col, X1, n1, X2, n2, d, g) -> np.ndarray:
7544
[1.38389653e-87 5.14820022e-131]]
7645
"""
7746
K = np.zeros((n1, n2))
47+
X1 = prepare_X(X1)
48+
X2 = prepare_X(X2)
7849

7950
for i in range(n1):
8051
for j in range(n2):
8152
K[i, j] = 0.0
8253
for k in range(col):
8354
K[i, j] += (X1[i, k] - X2[j, k]) ** 2 / d[k]
55+
if i == j and K[i, j] == 0.0:
56+
K[i, j] = 1.0 + g
57+
else:
58+
K[i, j] = np.exp(0.0 - K[i, j])
59+
60+
return K
61+
62+
63+
def covar_sep_symm(col, X, n, d, g) -> np.ndarray:
64+
"""
65+
Calculate the correlation (K) between X1 and X2 with a separable power exponential correlation function with range d and nugget g.
66+
67+
Args:
68+
col (int): Number of columns in the input matrix X (features).
69+
X (ndarray): Input matrix of shape (n, col).
70+
n (int): Number of rows in the input matrix X.
71+
d (ndarray): Array of length col representing the range parameters, shape (col,).
72+
g (float): Nugget parameter.
73+
74+
Returns:
75+
ndarray: The calculated covariance matrix K of shape (n, n).
76+
77+
Examples:
78+
>>> from spotpython.gp.covar import covar_sep_symm
79+
>>> import numpy as np
80+
>>> col = 2
81+
>>> X = np.array([[1, 2], [3, 4], [5, 6]])
82+
>>> n = 3
83+
>>> d = np.array([1.0, 1.0])
84+
>>> g = 0.1
85+
>>> K = covar_sep_symm(col, X, n, d, g)
86+
>>> print(K)
87+
[[1.1 0.01831564 0.00012341]
88+
[0.01831564 1.1 0.01831564]
89+
[0.00012341 0.01831564 1.1 ]]
90+
"""
91+
K = np.zeros((n, n))
92+
X = prepare_X(X)
93+
94+
# calculate the covariance matrix K
95+
for i in range(n):
96+
K[i, i] = 1.0 + g
97+
for j in range(i + 1, n):
98+
K[i, j] = 0.0
99+
for k in range(col):
100+
K[i, j] += (X[i, k] - X[j, k]) ** 2 / d[k]
84101
K[i, j] = np.exp(-K[i, j])
102+
K[j, i] = K[i, j]
85103

86104
return K
87105

@@ -121,6 +139,8 @@ def diff_covar_sep(col, X1, n1, X2, n2, d, K) -> np.ndarray:
121139
[3.72007598e-44 1.38389653e-87]
122140
[1.38389653e-87 5.14820022e-131]]]
123141
"""
142+
X1 = prepare_X(X1)
143+
X2 = prepare_X(X2)
124144
dK = np.zeros((col, n1, n2))
125145

126146
for k in range(col):
@@ -163,6 +183,7 @@ def diff_covar_sep_symm(col, X, n, d, K) -> np.ndarray:
163183
[0.36787944 0. 0.36787944]
164184
[0.01831564 0.36787944 0. ]]]
165185
"""
186+
X = prepare_X(X)
166187
dK = np.zeros((col, n, n))
167188

168189
for k in range(col):

src/spotpython/gp/distances.py

Lines changed: 5 additions & 164 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
from spotpython.gp.covar import covar_sep, covar_sep_symm
23

34

45
def new_matrix_bones(data, rows, cols) -> np.ndarray:
@@ -64,7 +65,8 @@ def covar_anisotropic(X1=None, X2=None, d=None, g=None) -> np.ndarray:
6465

6566
if X2 is None:
6667
# Calculate K using covar_sep_symm_R
67-
K = covar_sep_symm_R(m, X1.flatten(), n1, d, g)
68+
# K = covar_sep_symm_R(m, X1.flatten(), n1, d, g)
69+
K = covar_sep_symm(m, X1, n1, d, g)
6870
return K
6971
else:
7072
if X1.shape[1] != X2.shape[1]:
@@ -74,172 +76,11 @@ def covar_anisotropic(X1=None, X2=None, d=None, g=None) -> np.ndarray:
7476
n2 = X2.shape[0]
7577

7678
# Calculate K using covar_sep_R
77-
K = covar_sep_R(m, X1.flatten(), n1, X2.flatten(), n2, d, g)
79+
# K = covar_sep_R(m, X1.flatten(), n1, X2.flatten(), n2, d, g)
80+
K = covar_sep(m, X1, n1, X2, n2, d, g)
7881
return K
7982

8083

81-
def covar_sep_R(col_in, X1_in, n1_in, X2_in, n2_in, d_in, g_in) -> np.ndarray:
82-
"""
83-
Calculate a covariance matrix (K) using a separable power exponential covariance function with range d[col] and nugget g.
84-
85-
Args:
86-
col_in (int): Number of columns (features).
87-
X1_in (np.ndarray): First input matrix of shape (n1, col).
88-
n1_in (int): Number of rows in X1.
89-
X2_in (np.ndarray): Second input matrix of shape (n2, col).
90-
n2_in (int): Number of rows in X2.
91-
d_in (np.ndarray): Array of range parameters of shape (col,).
92-
g_in (float): Nugget parameter.
93-
94-
Returns:
95-
np.ndarray: Covariance matrix K of shape (n1, n2).
96-
97-
Examples:
98-
>>> import numpy as np
99-
>>> from spotpython.gp.distances import covar_sep_R
100-
>>> col_in = 2
101-
>>> X1_in = np.array([1.0, 2.0, 3.0, 4.0])
102-
>>> n1_in = 2
103-
>>> X2_in = np.array([5.0, 6.0, 7.0, 8.0])
104-
>>> n2_in = 2
105-
>>> d_in = np.array([1.0, 1.0])
106-
>>> g_in = 0.1
107-
>>> K_out = covar_sep_R(col_in, X1_in, n1_in, X2_in, n2_in, d_in, g_in)
108-
>>> print(K_out)
109-
[[0.00012341 0.00033546]
110-
[0.00033546 0.0009118]]
111-
"""
112-
col = col_in
113-
n1 = n1_in
114-
n2 = n2_in
115-
g = g_in
116-
117-
# Make matrix bones
118-
X1 = new_matrix_bones(X1_in, n1, col)
119-
X2 = new_matrix_bones(X2_in, n2, col)
120-
121-
# Calculate the covariance
122-
K = covar_sep(col, X1, n1, X2, n2, d_in, g)
123-
124-
return K
125-
126-
127-
def covar_sep(col, X1, n1, X2, n2, d, g) -> np.ndarray:
128-
"""
129-
Calculate the correlation (K) between X1 and X2 with a separable power exponential correlation function
130-
with range d and nugget g.
131-
132-
Args:
133-
col (int): Number of columns (features).
134-
X1 (np.ndarray): First input matrix of shape (n1, col).
135-
n1 (int): Number of rows in X1.
136-
X2 (np.ndarray): Second input matrix of shape (n2, col).
137-
n2 (int): Number of rows in X2.
138-
d (np.ndarray): Array of range parameters of shape (col,).
139-
g (float): Nugget parameter.
140-
141-
Returns:
142-
np.ndarray: Covariance matrix K of shape (n1, n2).
143-
144-
Examples:
145-
>>> import numpy as np
146-
>>> from spotpython.gp.distances import covar_sep
147-
>>> col = 2
148-
>>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
149-
>>> n1 = 2
150-
>>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
151-
>>> n2 = 2
152-
>>> d = np.array([1.0, 1.0])
153-
>>> g = 0.1
154-
>>> K_out = covar_sep(col, X1, n1, X2, n2, d, g)
155-
>>> print(K_out)
156-
[[0.00012341 0.00033546]
157-
[0.00033546 0.0009118]]
158-
"""
159-
K = np.zeros((n1, n2))
160-
161-
for i in range(n1):
162-
for j in range(n2):
163-
K[i, j] = 0.0
164-
for k in range(col):
165-
K[i, j] += (X1[i, k] - X2[j, k]) ** 2 / d[k]
166-
if i == j and K[i, j] == 0.0:
167-
K[i, j] = 1.0 + g
168-
else:
169-
K[i, j] = np.exp(0.0 - K[i, j])
170-
171-
return K
172-
173-
174-
def covar_sep_symm_R(col_in, X_in, n_in, d_in, g_in) -> np.ndarray:
175-
"""
176-
Calculate a symmetric covariance matrix (K) using a separable power exponential covariance function with range d[col] and nugget g.
177-
178-
Args:
179-
col_in (int): Number of columns (features).
180-
X_in (np.ndarray): Input matrix of shape (n, col).
181-
n_in (int): Number of rows in X.
182-
d_in (np.ndarray): Array of range parameters of shape (col,).
183-
g_in (float): Nugget parameter.
184-
185-
Returns:
186-
np.ndarray: Covariance matrix K of shape (n, n).
187-
188-
Examples:
189-
>>> import numpy as np
190-
>>> from spotpython.gp.distances import covar_sep_symm_R
191-
>>> col_in = 2
192-
>>> X_in = np.array([1.0, 2.0, 3.0, 4.0])
193-
>>> n_in = 2
194-
>>> d_in = np.array([1.0, 1.0])
195-
>>> g_in = 0.1
196-
>>> K_out = covar_sep_symm_R(col_in, X_in, n_in, d_in, g_in)
197-
>>> print(K_out)
198-
[[1.1 0.36787944]
199-
[0.36787944 1.1 ]]
200-
"""
201-
col = col_in
202-
n = n_in
203-
g = g_in
204-
205-
# Make matrix bones
206-
X = new_matrix_bones(X_in, n, col)
207-
208-
# Calculate the covariance
209-
K = covar_sep_symm(col, X, n, d_in, g)
210-
211-
return K
212-
213-
214-
def covar_sep_symm(col, X, n, d, g) -> np.ndarray:
215-
"""
216-
Calculate the correlation (K) between X1 and X2 with a separable power exponential correlation function
217-
with range d and nugget g.
218-
219-
Args:
220-
col (int): Number of columns (features).
221-
X (np.ndarray): Input matrix of shape (n, col).
222-
n (int): Number of rows in X.
223-
d (np.ndarray): Array of range parameters of shape (col,).
224-
g (float): Nugget parameter.
225-
226-
Returns:
227-
np.ndarray: Covariance matrix K of shape (n, n).
228-
"""
229-
K = np.zeros((n, n))
230-
231-
for i in range(n):
232-
K[i, i] = 1.0 + g
233-
for j in range(i + 1, n):
234-
K[i, j] = 0.0
235-
for k in range(col):
236-
K[i, j] += (X[i, k] - X[j, k]) ** 2 / d[k]
237-
K[i, j] = np.exp(0.0 - K[i, j])
238-
K[j, i] = K[i, j]
239-
240-
return K
241-
242-
24384
def dist(X1, X2=None) -> np.ndarray:
24485
"""
24586
Calculate the distance matrix between the rows of X1 and X2, or between X1 and itself when X2 is None.

0 commit comments

Comments
 (0)