1# Copyright (c) 2012 - 2014 the GPy Austhors (see AUTHORS.txt)
2# Licensed under the BSD 3-clause license (see LICENSE.txt)
3
4import numpy as np
5from ..core import GP
6from .. import likelihoods
7from .. import kern
8
9class GPRegression(GP):
10    """
11    Gaussian Process model for regression
12
13    This is a thin wrapper around the models.GP class, with a set of sensible defaults
14
15    :param X: input observations
16    :param Y: observed values
17    :param kernel: a GPy kernel, defaults to rbf
18    :param Norm normalizer: [False]
19    :param noise_var: the noise variance for Gaussian likelhood, defaults to 1.
20
21        Normalize Y with the norm given.
22        If normalizer is False, no normalization will be done
23        If it is None, we use GaussianNorm(alization)
24
25    .. Note:: Multiple independent outputs are allowed using columns of Y
26
27    """
28
29    def __init__(self, X, Y, kernel=None, Y_metadata=None, normalizer=None, noise_var=1., mean_function=None):
30
31        if kernel is None:
32            kernel = kern.RBF(X.shape[1])
33
34        likelihood = likelihoods.Gaussian(variance=noise_var)
35
36        super(GPRegression, self).__init__(X, Y, kernel, likelihood, name='GP regression', Y_metadata=Y_metadata, normalizer=normalizer, mean_function=mean_function)
37
38    @staticmethod
39    def from_gp(gp):
40        from copy import deepcopy
41        gp = deepcopy(gp)
42        return GPRegression(gp.X, gp.Y, gp.kern, gp.Y_metadata, gp.normalizer, gp.likelihood.variance.values, gp.mean_function)
43
44    def to_dict(self, save_data=True):
45        model_dict = super(GPRegression,self).to_dict(save_data)
46        model_dict["class"] = "GPy.models.GPRegression"
47        return model_dict
48
49    @staticmethod
50    def _from_dict(input_dict, data=None):
51        import GPy
52        input_dict["class"] = "GPy.core.GP"
53        m = GPy.core.GP.from_dict(input_dict, data)
54        return GPRegression.from_gp(m)
55
56    def save_model(self, output_filename, compress=True, save_data=True):
57        self._save_model(output_filename, compress=True, save_data=True)
58