2013-06-05 14:11:49 +01:00
|
|
|
# Copyright (c) 2012, James Hensman
|
|
|
|
|
# Licensed under the BSD 3-clause license (see LICENSE.txt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
from ..core import SparseGP
|
|
|
|
|
from .. import likelihoods
|
|
|
|
|
from .. import kern
|
2014-02-13 08:53:14 +00:00
|
|
|
from ..inference.latent_function_inference import VarDTC
|
2014-02-24 19:31:13 +00:00
|
|
|
from ..util.misc import param_to_array
|
2014-02-28 16:18:47 +00:00
|
|
|
from ..core.parameterization.variational import NormalPosterior
|
2013-06-05 14:11:49 +01:00
|
|
|
|
|
|
|
|
class SparseGPRegression(SparseGP):
|
|
|
|
|
"""
|
|
|
|
|
Gaussian Process model for regression
|
|
|
|
|
|
|
|
|
|
This is a thin wrapper around the SparseGP class, with a set of sensible defalts
|
|
|
|
|
|
|
|
|
|
:param X: input observations
|
|
|
|
|
:param Y: observed values
|
|
|
|
|
:param kernel: a GPy kernel, defaults to rbf+white
|
2013-09-13 12:28:04 +01:00
|
|
|
:param Z: inducing inputs (optional, see note)
|
|
|
|
|
:type Z: np.ndarray (num_inducing x input_dim) | None
|
2014-01-29 09:40:22 +00:00
|
|
|
:param num_inducing: number of inducing points (ignored if Z is passed, see note)
|
|
|
|
|
:type num_inducing: int
|
2013-06-05 14:11:49 +01:00
|
|
|
:rtype: model object
|
|
|
|
|
|
2014-01-29 09:40:22 +00:00
|
|
|
.. Note:: If no Z array is passed, num_inducing (default 10) points are selected from the data. Other wise num_inducing is ignored
|
2013-06-05 14:11:49 +01:00
|
|
|
.. Note:: Multiple independent outputs are allowed using columns of Y
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2014-08-27 12:05:13 -07:00
|
|
|
def __init__(self, X, Y, kernel=None, Z=None, num_inducing=10, X_variance=None, normalizer=None):
|
2014-01-29 09:40:22 +00:00
|
|
|
num_data, input_dim = X.shape
|
|
|
|
|
|
2013-06-05 14:11:49 +01:00
|
|
|
# kern defaults to rbf (plus white for stability)
|
|
|
|
|
if kernel is None:
|
2014-02-24 19:31:13 +00:00
|
|
|
kernel = kern.RBF(input_dim)# + kern.white(input_dim, variance=1e-3)
|
2013-06-05 14:11:49 +01:00
|
|
|
|
|
|
|
|
# Z defaults to a subset of the data
|
|
|
|
|
if Z is None:
|
2014-01-29 09:40:22 +00:00
|
|
|
i = np.random.permutation(num_data)[:min(num_inducing, num_data)]
|
2014-02-24 19:31:13 +00:00
|
|
|
Z = param_to_array(X)[i].copy()
|
2013-06-05 14:11:49 +01:00
|
|
|
else:
|
2014-01-29 09:40:22 +00:00
|
|
|
assert Z.shape[1] == input_dim
|
2013-06-05 14:11:49 +01:00
|
|
|
|
2014-01-29 09:40:22 +00:00
|
|
|
likelihood = likelihoods.Gaussian()
|
2014-03-11 10:25:21 +00:00
|
|
|
|
2014-02-26 15:46:14 +00:00
|
|
|
if not (X_variance is None):
|
2014-02-28 16:18:47 +00:00
|
|
|
X = NormalPosterior(X,X_variance)
|
2014-03-11 10:25:21 +00:00
|
|
|
|
2014-08-27 12:05:13 -07:00
|
|
|
SparseGP.__init__(self, X, Y, Z, kernel, likelihood, inference_method=VarDTC(), normalizer=normalizer)
|
2013-06-26 16:48:48 +01:00
|
|
|
|
2014-01-29 09:40:22 +00:00
|
|
|
class SparseGPRegressionUncertainInput(SparseGP):
|
|
|
|
|
"""
|
|
|
|
|
Gaussian Process model for regression with Gaussian variance on the inputs (X_variance)
|
|
|
|
|
|
|
|
|
|
This is a thin wrapper around the SparseGP class, with a set of sensible defalts
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
2014-08-27 12:05:13 -07:00
|
|
|
def __init__(self, X, X_variance, Y, kernel=None, Z=None, num_inducing=10, normalizer=None):
|
2014-01-29 09:40:22 +00:00
|
|
|
"""
|
|
|
|
|
:param X: input observations
|
|
|
|
|
:type X: np.ndarray (num_data x input_dim)
|
|
|
|
|
:param X_variance: The uncertainty in the measurements of X (Gaussian variance, optional)
|
|
|
|
|
:type X_variance: np.ndarray (num_data x input_dim)
|
|
|
|
|
:param Y: observed values
|
|
|
|
|
:param kernel: a GPy kernel, defaults to rbf+white
|
|
|
|
|
:param Z: inducing inputs (optional, see note)
|
|
|
|
|
:type Z: np.ndarray (num_inducing x input_dim) | None
|
|
|
|
|
:param num_inducing: number of inducing points (ignored if Z is passed, see note)
|
|
|
|
|
:type num_inducing: int
|
|
|
|
|
:rtype: model object
|
|
|
|
|
|
|
|
|
|
.. Note:: If no Z array is passed, num_inducing (default 10) points are selected from the data. Other wise num_inducing is ignored
|
|
|
|
|
.. Note:: Multiple independent outputs are allowed using columns of Y
|
|
|
|
|
"""
|
|
|
|
|
num_data, input_dim = X.shape
|
|
|
|
|
|
|
|
|
|
# kern defaults to rbf (plus white for stability)
|
|
|
|
|
if kernel is None:
|
2014-03-03 15:08:54 +00:00
|
|
|
kernel = kern.RBF(input_dim) + kern.White(input_dim, variance=1e-3)
|
2014-01-29 09:40:22 +00:00
|
|
|
|
|
|
|
|
# Z defaults to a subset of the data
|
|
|
|
|
if Z is None:
|
|
|
|
|
i = np.random.permutation(num_data)[:min(num_inducing, num_data)]
|
|
|
|
|
Z = X[i].copy()
|
|
|
|
|
else:
|
|
|
|
|
assert Z.shape[1] == input_dim
|
|
|
|
|
|
|
|
|
|
likelihood = likelihoods.Gaussian()
|
|
|
|
|
|
2014-08-27 12:05:13 -07:00
|
|
|
SparseGP.__init__(self, X, Y, Z, kernel, likelihood, X_variance=X_variance, inference_method=VarDTC(), normalizer=normalizer)
|
2014-01-29 09:40:22 +00:00
|
|
|
self.ensure_default_constraints()
|