| 16 | |
| 17 | |
| 18 | class GPRegression: |
| 19 | def __init__(self, kernel="RBFKernel", alpha=1e-10): |
| 20 | """ |
| 21 | A Gaussian Process (GP) regression model. |
| 22 | |
| 23 | .. math:: |
| 24 | |
| 25 | y \mid X, f &\sim \mathcal{N}( [f(x_1), \ldots, f(x_n)], \\alpha I ) \\\\ |
| 26 | f \mid X &\sim \\text{GP}(0, K) |
| 27 | |
| 28 | for data :math:`D = \{(x_1, y_1), \ldots, (x_n, y_n) \}` and a covariance matrix :math:`K_{ij} |
| 29 | = \\text{kernel}(x_i, x_j)` for all :math:`i, j \in \{1, \ldots, n \}`. |
| 30 | |
| 31 | Parameters |
| 32 | ---------- |
| 33 | kernel : str |
| 34 | The kernel to use in fitting the GP prior. Default is 'RBFKernel'. |
| 35 | alpha : float |
| 36 | An isotropic noise term for the diagonal in the GP covariance, `K`. |
| 37 | Larger values correspond to the expectation of greater noise in the |
| 38 | observed data points. Default is 1e-10. |
| 39 | """ |
| 40 | self.kernel = KernelInitializer(kernel)() |
| 41 | self.parameters = {"GP_mean": None, "GP_cov": None, "X": None} |
| 42 | self.hyperparameters = {"kernel": str(self.kernel), "alpha": alpha} |
| 43 | |
| 44 | def fit(self, X, y): |
| 45 | """ |
| 46 | Fit the GP prior to the training data. |
| 47 | |
| 48 | Parameters |
| 49 | ---------- |
| 50 | X : :py:class:`ndarray <numpy.ndarray>` of shape `(N, M)` |
| 51 | A training dataset of `N` examples, each with dimensionality `M`. |
| 52 | y : :py:class:`ndarray <numpy.ndarray>` of shape `(N, O)` |
| 53 | A collection of real-valued training targets for the |
| 54 | examples in `X`, each with dimension `O`. |
| 55 | """ |
| 56 | mu = np.zeros(X.shape[0]) |
| 57 | K = self.kernel(X, X) |
| 58 | |
| 59 | self.parameters["X"] = X |
| 60 | self.parameters["y"] = y |
| 61 | self.parameters["GP_cov"] = K |
| 62 | self.parameters["GP_mean"] = mu |
| 63 | |
| 64 | def predict(self, X, conf_interval=0.95, return_cov=False): |
| 65 | """ |
| 66 | Return the MAP estimate for :math:`y^*`, corresponding the mean/mode of |
| 67 | the posterior predictive distribution, :math:`p(y^* \mid x^*, X, y)`. |
| 68 | |
| 69 | Notes |
| 70 | ----- |
| 71 | Under the GP regression model, the posterior predictive distribution is |
| 72 | |
| 73 | .. math:: |
| 74 | |
| 75 | y^* \mid x^*, X, y \sim \mathcal{N}(\mu^*, \\text{cov}^*) |
no outgoing calls