11import numpy as np
2+ from spotpython .gp .covar import covar_sep , covar_sep_symm
23
34
45def 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-
24384def 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