Skip to content

distances

covar_anisotropic(X1=None, X2=None, d=None, g=None)

Calculate the separable covariance matrix between the rows of X1 and X2 with lengthscale d and nugget g.

Parameters:

Name Type Description Default
X1 ndarray

First input matrix.

None
X2 ndarray

Second input matrix (optional).

None
d ndarray

Array of lengthscale parameters.

None
g float

Nugget parameter.

None

Returns:

Type Description
ndarray

np.ndarray: Covariance matrix K.

Examples:

>>> import numpy as np
>>> from spotpython.gp.distances import covar_anisotropic
>>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
>>> d = np.array([1.0, 1.0])
>>> g = 0.1
>>> K_symm = covar_anisotropic(X1=X1, d=d, g=g)
>>> print(K_symm)
[[1.1        0.36787944]
 [0.36787944 1.1       ]]
>>> K = covar_anisotropic(X1=X1, X2=X2, d=d, g=g)
>>> print(K)
[[0.00012341 0.00033546]
 [0.00033546 0.000911
Source code in spotpython/gp/distances.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def covar_anisotropic(X1=None, X2=None, d=None, g=None) -> np.ndarray:
    """
    Calculate the separable covariance matrix between the rows of X1 and X2 with lengthscale d and nugget g.

    Args:
        X1 (np.ndarray): First input matrix.
        X2 (np.ndarray): Second input matrix (optional).
        d (np.ndarray): Array of lengthscale parameters.
        g (float): Nugget parameter.

    Returns:
        np.ndarray: Covariance matrix K.

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.distances import covar_anisotropic
        >>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
        >>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
        >>> d = np.array([1.0, 1.0])
        >>> g = 0.1
        >>> K_symm = covar_anisotropic(X1=X1, d=d, g=g)
        >>> print(K_symm)
        [[1.1        0.36787944]
         [0.36787944 1.1       ]]
        >>> K = covar_anisotropic(X1=X1, X2=X2, d=d, g=g)
        >>> print(K)
        [[0.00012341 0.00033546]
         [0.00033546 0.000911
    """
    # Convert pandas dataframes to numpy arrays
    X1 = np.asarray(X1)
    if X2 is not None:
        X2 = np.asarray(X2)
    if X1 is None:
        raise ValueError("X1 cannot be None")

    if not isinstance(X1, np.ndarray):
        if X2 is None:
            raise ValueError("X2 cannot be None in this context")
        m = X2.shape[1]
        X1 = np.reshape(X1, (-1, m))
    else:
        m = X1.shape[1]
    n1 = X1.shape[0]

    if len(d) != m:
        raise ValueError("bad d argument")
    if not isinstance(g, float):
        raise ValueError("bad g argument")

    if X2 is None:
        # Calculate K using covar_sep_symm_R
        # K = covar_sep_symm_R(m, X1.flatten(), n1, d, g)
        K = covar_sep_symm(m, X1, n1, d, g)
        return K
    else:
        if X1.shape[1] != X2.shape[1]:
            raise ValueError("col dim mismatch for X1 & X2")

        X2 = np.asarray(X2)
        n2 = X2.shape[0]

        # Calculate K using covar_sep_R
        # K = covar_sep_R(m, X1.flatten(), n1, X2.flatten(), n2, d, g)
        K = covar_sep(m, X1, n1, X2, n2, d, g)
        return K

dist(X1, X2=None)

Calculate the distance matrix between the rows of X1 and X2, or between X1 and itself when X2 is None.

Parameters:

Name Type Description Default
X1 ndarray

First input matrix.

required
X2 ndarray

Second input matrix.

None

Returns:

Type Description
ndarray

np.ndarray: Distance matrix D.

Examples:

>>> import numpy as np
>>> from spotpython.gp.distances import dist
>>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
>>> D_symm = dist(X1)
>>> print(D_symm)
[[ 0.  8.]
 [ 8.  0.]]
>>> D = dist(X1, X2)
>>> print(D)
    [[32.  8.]
    [18.  2.]]
Source code in spotpython/gp/distances.py
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def dist(X1, X2=None) -> np.ndarray:
    """
    Calculate the distance matrix between the rows of X1 and X2, or between X1 and itself when X2 is None.

    Args:
        X1 (np.ndarray): First input matrix.
        X2 (np.ndarray, optional): Second input matrix.

    Returns:
        np.ndarray: Distance matrix D.

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.distances import dist
        >>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
        >>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
        >>> D_symm = dist(X1)
        >>> print(D_symm)
        [[ 0.  8.]
         [ 8.  0.]]
        >>> D = dist(X1, X2)
        >>> print(D)
            [[32.  8.]
            [18.  2.]]
    """
    # Coerce arguments and extract dimensions
    X1 = np.asarray(X1)
    n1, m = X1.shape

    if X2 is None:
        # Calculate D using distance_symm_R
        D = distance_symm_R(X1.flatten(), n1, m)
        return D
    else:
        # Coerce arguments and extract dimensions
        X2 = np.asarray(X2)
        n2 = X2.shape[0]

        # Check inputs
        if X1.shape[1] != X2.shape[1]:
            raise ValueError("col dim mismatch for X1 & X2")

        # Calculate D using distance_R
        D = distance_R(X1.flatten(), n1, X2.flatten(), n2, m)
        return D

distance(X1, n1, X2, n2, m)

Calculate the distance matrix (D) between X1 and X2.

Parameters:

Name Type Description Default
X1 ndarray

First input matrix of shape (n1, m).

required
n1 int

Number of rows in X1.

required
X2 ndarray

Second input matrix of shape (n2, m).

required
n2 int

Number of rows in X2.

required
m int

Number of columns (features).

required

Returns:

Type Description
ndarray

np.ndarray: Distance matrix D of shape (n1, n2).

Examples:

>>> import numpy as np
>>> from spotpython.gp.distances import distance
>>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> n1 = 2
>>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
>>> n2 = 2
>>> m = 2
>>> D_out = distance(X1, n1, X2, n2, m)
>>> print(D_out)
[[32.  8.]
 [18.  2.]]
Source code in spotpython/gp/distances.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def distance(X1, n1, X2, n2, m) -> np.ndarray:
    """
    Calculate the distance matrix (D) between X1 and X2.

    Args:
        X1 (np.ndarray): First input matrix of shape (n1, m).
        n1 (int): Number of rows in X1.
        X2 (np.ndarray): Second input matrix of shape (n2, m).
        n2 (int): Number of rows in X2.
        m (int): Number of columns (features).

    Returns:
        np.ndarray: Distance matrix D of shape (n1, n2).

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.distances import distance
        >>> X1 = np.array([[1.0, 2.0], [3.0, 4.0]])
        >>> n1 = 2
        >>> X2 = np.array([[5.0, 6.0], [7.0, 8.0]])
        >>> n2 = 2
        >>> m = 2
        >>> D_out = distance(X1, n1, X2, n2, m)
        >>> print(D_out)
        [[32.  8.]
         [18.  2.]]
    """
    D = np.zeros((n1, n2))

    for i in range(n1):
        for j in range(n2):
            D[i, j] = 0.0
            for k in range(m):
                D[i, j] += (X1[i, k] - X2[j, k]) ** 2

    return D

distance_R(X1_in, n1_in, X2_in, n2_in, m_in)

Calculate the distance matrix between the rows of X1 and X2.

Parameters:

Name Type Description Default
X1_in ndarray

First input matrix of shape (n1, m).

required
n1_in int

Number of rows in X1.

required
X2_in ndarray

Second input matrix of shape (n2, m).

required
n2_in int

Number of rows in X2.

required
m_in int

Number of columns (features).

required

Returns:

Type Description
ndarray

np.ndarray: Distance matrix D of shape (n1, n2).

Examples:

>>> import numpy as np
>>> from spotpython.gp.distances import distance_R
>>> X1_in = np.array([1.0, 2.0, 3.0, 4.0])
>>> n1_in = 2
>>> X2_in = np.array([5.0, 6.0, 7.0, 8.0])
>>> n2_in = 2
>>> m_in = 2
>>> D_out = distance_R(X1_in, n1_in, X2_in, n2_in, m_in)
>>> print(D_out)
[[32.  8.]
 [18.  2.]]
Source code in spotpython/gp/distances.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
def distance_R(X1_in, n1_in, X2_in, n2_in, m_in) -> np.ndarray:
    """
    Calculate the distance matrix between the rows of X1 and X2.

    Args:
        X1_in (np.ndarray): First input matrix of shape (n1, m).
        n1_in (int): Number of rows in X1.
        X2_in (np.ndarray): Second input matrix of shape (n2, m).
        n2_in (int): Number of rows in X2.
        m_in (int): Number of columns (features).

    Returns:
        np.ndarray: Distance matrix D of shape (n1, n2).

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.distances import distance_R
        >>> X1_in = np.array([1.0, 2.0, 3.0, 4.0])
        >>> n1_in = 2
        >>> X2_in = np.array([5.0, 6.0, 7.0, 8.0])
        >>> n2_in = 2
        >>> m_in = 2
        >>> D_out = distance_R(X1_in, n1_in, X2_in, n2_in, m_in)
        >>> print(D_out)
        [[32.  8.]
         [18.  2.]]
    """
    # Make matrix bones
    X1 = np.reshape(X1_in, (n1_in, m_in))
    X2 = np.reshape(X2_in, (n2_in, m_in))
    D = distance(X1, n1_in, X2, n2_in, m_in)
    return D

distance_symm_R(X_in, n_in, m_in)

Calculate the distance matrix between the rows of X and itself, with output in the symmetric D_out matrix.

Parameters:

Name Type Description Default
X_in ndarray

Input matrix of shape (n, m).

required
n_in int

Number of rows in X.

required
m_in int

Number of columns (features).

required

Returns:

Type Description
ndarray

np.ndarray: Symmetric distance matrix D of shape (n, n).

Examples:

>>> import numpy as np
>>> from spotpython.gp.distances import distance_symm_R
>>> X_in = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
>>> n_in = 3
>>> m_in = 2
>>> D_out = distance_symm_R(X_in, n_in, m_in)
>>> print(D_out)
[[ 0.  8. 32.]
 [ 8.  0.  8.]
 [32.  8.  0.]]
Source code in spotpython/gp/distances.py
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
def distance_symm_R(X_in, n_in, m_in) -> np.ndarray:
    """
    Calculate the distance matrix between the rows of X and itself, with output in the symmetric D_out matrix.

    Args:
        X_in (np.ndarray): Input matrix of shape (n, m).
        n_in (int): Number of rows in X.
        m_in (int): Number of columns (features).

    Returns:
        np.ndarray: Symmetric distance matrix D of shape (n, n).

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.distances import distance_symm_R
        >>> X_in = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        >>> n_in = 3
        >>> m_in = 2
        >>> D_out = distance_symm_R(X_in, n_in, m_in)
        >>> print(D_out)
        [[ 0.  8. 32.]
         [ 8.  0.  8.]
         [32.  8.  0.]]
    """
    n = n_in
    m = m_in

    # Make matrix bones
    X = new_matrix_bones(X_in, n, m)
    D = np.zeros((n, n))

    # For each row of X and itself
    for i in range(n):
        D[i][i] = 0.0
        for j in range(i + 1, n):
            D[i][j] = 0.0
            for k in range(m):
                D[i][j] += (X[i][k] - X[j][k]) ** 2
            D[j][i] = D[i][j]

    return D

new_matrix_bones(data, rows, cols)

Create a matrix view of the given data.

Parameters:

Name Type Description Default
data ndarray

Input data.

required
rows int

Number of rows.

required
cols int

Number of columns.

required

Returns:

Type Description
ndarray

np.ndarray: Matrix view of the input data.

Source code in spotpython/gp/distances.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def new_matrix_bones(data, rows, cols) -> np.ndarray:
    """
    Create a matrix view of the given data.

    Args:
        data (np.ndarray): Input data.
        rows (int): Number of rows.
        cols (int): Number of columns.

    Returns:
        np.ndarray: Matrix view of the input data.
    """
    return np.reshape(data, (rows, cols))