Skip to content

regressor

GPRegressor

Source code in spotpython/gp/regressor.py
 8
 9
10
11
12
13
14
15
16
17
18
19
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
86
class GPRegressor:
    def __init__(self):
        self.eps = np.sqrt(np.finfo(float).eps)
        self.fitted = False

    def fit(self, X, y) -> "GPRegressor":
        """
        Fit the Gaussian Process model.

        Args:
            X (np.ndarray): Training input matrix of shape (n, m).
            y (np.ndarray): Training response vector of shape (n,).

        Returns:
            self: Fitted model.
        """
        self.n, self.m = X.shape

        # Optimize parameters
        outg = minimize(
            lambda par: nlsep(par, X, y),
            x0=np.concatenate([np.repeat(0.1, self.m), [0.1 * np.var(y)]]),
            jac=lambda par: gradnlsep(par, X, y),
            method="L-BFGS-B",
            bounds=[(self.eps, 10)] * self.m + [(self.eps, np.var(y))],
        )

        # Compute covariance matrices
        K = covar_anisotropic(X, d=outg.x[: self.m], g=outg.x[self.m])
        Ki = inv(K)
        tau2hat = (y.T @ Ki @ y) / len(X)

        self.X = X
        self.y = y
        self.outg = outg
        self.Ki = Ki
        self.tau2hat = tau2hat
        self.fitted = True

        return self

    def predict(self, XX) -> tuple:
        """
        Predict using the Gaussian Process model.

        Args:
            XX (np.ndarray): Test input matrix of shape (n_test, m).

        Returns:
            tuple: Predicted mean (mup2) and covariance (Sigmap2).

        Raises:
            RuntimeError: If the model is not fitted.

        Examples:
            >>> import numpy as np
            >>> from spotpython.gp.regressor import GPRegressor
            >>> X = np.array([[1, 2], [3, 4], [5, 6]])
            >>> y = np.array([1, 2, 3])
            >>> XX = np.array([[1, 2], [3, 4]])
            >>> gp_model = GPRegressor()
            >>> gp_model.fit(X, y)
            >>> mup2, Sigmap2 = gp_model.predict(XX)
            >>> print(mup2)
            [1. 2.]
            >>> print(Sigmap2)
            [[1. 1.]
             [1. 1.]]

        """
        if not self.fitted:
            raise RuntimeError("The model must be fitted before calling predict.")

        KXX = covar_anisotropic(XX, d=self.outg.x[: self.m], g=self.outg.x[self.m])
        KX = covar_anisotropic(XX, self.X, d=self.outg.x[: self.m], g=0.0)
        mup2 = KX @ self.Ki @ self.y
        Sigmap2 = self.tau2hat * (KXX - KX @ self.Ki @ KX.T)

        return mup2, Sigmap2

fit(X, y)

Fit the Gaussian Process model.

Parameters:

Name Type Description Default
X ndarray

Training input matrix of shape (n, m).

required
y ndarray

Training response vector of shape (n,).

required

Returns:

Name Type Description
self GPRegressor

Fitted model.

Source code in spotpython/gp/regressor.py
13
14
15
16
17
18
19
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
def fit(self, X, y) -> "GPRegressor":
    """
    Fit the Gaussian Process model.

    Args:
        X (np.ndarray): Training input matrix of shape (n, m).
        y (np.ndarray): Training response vector of shape (n,).

    Returns:
        self: Fitted model.
    """
    self.n, self.m = X.shape

    # Optimize parameters
    outg = minimize(
        lambda par: nlsep(par, X, y),
        x0=np.concatenate([np.repeat(0.1, self.m), [0.1 * np.var(y)]]),
        jac=lambda par: gradnlsep(par, X, y),
        method="L-BFGS-B",
        bounds=[(self.eps, 10)] * self.m + [(self.eps, np.var(y))],
    )

    # Compute covariance matrices
    K = covar_anisotropic(X, d=outg.x[: self.m], g=outg.x[self.m])
    Ki = inv(K)
    tau2hat = (y.T @ Ki @ y) / len(X)

    self.X = X
    self.y = y
    self.outg = outg
    self.Ki = Ki
    self.tau2hat = tau2hat
    self.fitted = True

    return self

predict(XX)

Predict using the Gaussian Process model.

Parameters:

Name Type Description Default
XX ndarray

Test input matrix of shape (n_test, m).

required

Returns:

Name Type Description
tuple tuple

Predicted mean (mup2) and covariance (Sigmap2).

Raises:

Type Description
RuntimeError

If the model is not fitted.

Examples:

>>> import numpy as np
>>> from spotpython.gp.regressor import GPRegressor
>>> X = np.array([[1, 2], [3, 4], [5, 6]])
>>> y = np.array([1, 2, 3])
>>> XX = np.array([[1, 2], [3, 4]])
>>> gp_model = GPRegressor()
>>> gp_model.fit(X, y)
>>> mup2, Sigmap2 = gp_model.predict(XX)
>>> print(mup2)
[1. 2.]
>>> print(Sigmap2)
[[1. 1.]
 [1. 1.]]
Source code in spotpython/gp/regressor.py
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
86
def predict(self, XX) -> tuple:
    """
    Predict using the Gaussian Process model.

    Args:
        XX (np.ndarray): Test input matrix of shape (n_test, m).

    Returns:
        tuple: Predicted mean (mup2) and covariance (Sigmap2).

    Raises:
        RuntimeError: If the model is not fitted.

    Examples:
        >>> import numpy as np
        >>> from spotpython.gp.regressor import GPRegressor
        >>> X = np.array([[1, 2], [3, 4], [5, 6]])
        >>> y = np.array([1, 2, 3])
        >>> XX = np.array([[1, 2], [3, 4]])
        >>> gp_model = GPRegressor()
        >>> gp_model.fit(X, y)
        >>> mup2, Sigmap2 = gp_model.predict(XX)
        >>> print(mup2)
        [1. 2.]
        >>> print(Sigmap2)
        [[1. 1.]
         [1. 1.]]

    """
    if not self.fitted:
        raise RuntimeError("The model must be fitted before calling predict.")

    KXX = covar_anisotropic(XX, d=self.outg.x[: self.m], g=self.outg.x[self.m])
    KX = covar_anisotropic(XX, self.X, d=self.outg.x[: self.m], g=0.0)
    mup2 = KX @ self.Ki @ self.y
    Sigmap2 = self.tau2hat * (KXX - KX @ self.Ki @ KX.T)

    return mup2, Sigmap2