Skip to content

Commit

Permalink
added tests for students tp, sparse gp
Browse files Browse the repository at this point in the history
  • Loading branch information
Emaasit committed Oct 23, 2018
1 parent 594cf18 commit 58c7441
Show file tree
Hide file tree
Showing 3 changed files with 288 additions and 271 deletions.
6 changes: 3 additions & 3 deletions README.rst
Expand Up @@ -262,8 +262,8 @@ project: https://github.com/parsing-science/pymc3_models.
.. |Travis| image:: https://travis-ci.com/pymc-learn/pymc-learn.svg?branch=master
:target: https://travis-ci.com/pymc-learn/pymc-learn

.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg
:target: https://coveralls.io/github/pymc-learn/pymc-learn
.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg?branch=master
:target: https://coveralls.io/github/pymc-learn/pymc-learn?branch=master

.. |Python27| image:: https://img.shields.io/badge/python-2.7-blue.svg
:target: https://badge.fury.io/py/pymc-learn
Expand All @@ -280,4 +280,4 @@ project: https://github.com/parsing-science/pymc3_models.
:target: https://github.com/pymc-learn/pymc-learn/blob/master/LICENSE

.. |Pypi| image:: https://badge.fury.io/py/pymc-learn.svg
:target: https://badge.fury.io/py/pymc-learn
:target: https://badge.fury.io/py/pymc-learn
61 changes: 42 additions & 19 deletions pmlearn/gaussian_process/gpr.py
Expand Up @@ -173,7 +173,8 @@ def load(self, file_prefix):
self.num_training_samples = params['num_training_samples']


class StudentsTProcessRegressor(GaussianProcessRegressor):
class StudentsTProcessRegressor(BayesianModel,
GaussianProcessRegressorMixin):
""" StudentsT Process Regression built using PyMC3.
Fit a StudentsT process model and estimate model parameters using
Expand Down Expand Up @@ -204,8 +205,15 @@ class StudentsTProcessRegressor(GaussianProcessRegressor):
Rasmussen and Williams (2006). Gaussian Processes for Machine Learning.
"""

def __init__(self, prior_mean=0.0):
super(StudentsTProcessRegressor, self).__init__(prior_mean=prior_mean)
def __init__(self, prior_mean=None, kernel=None):
self.ppc = None
self.gp = None
self.num_training_samples = None
self.num_pred = None
self.prior_mean = prior_mean
self.kernel = kernel

super(StudentsTProcessRegressor, self).__init__()

def create_model(self):
""" Creates and returns the PyMC3 model.
Expand Down Expand Up @@ -241,13 +249,17 @@ def create_model(self):
degrees_of_freedom = pm.Gamma('degrees_of_freedom', alpha=2,
beta=0.1, shape=1)

# cov_function = signal_variance**2 * pm.gp.cov.ExpQuad(
# 1, length_scale)
cov_function = signal_variance ** 2 * pm.gp.cov.Matern52(
1, length_scale)
if self.kernel is None:
cov_function = signal_variance ** 2 * RBF(
input_dim=self.num_pred,
ls=length_scale)
else:
cov_function = self.kernel

# mean_function = pm.gp.mean.Zero()
mean_function = pm.gp.mean.Constant(self.prior_mean)
if self.prior_mean is None:
mean_function = pm.gp.mean.Zero()
else:
mean_function = self.prior_mean

self.gp = pm.gp.Latent(mean_func=mean_function,
cov_func=cov_function)
Expand Down Expand Up @@ -277,7 +289,8 @@ def load(self, file_prefix):
self.num_training_samples = params['num_training_samples']


class SparseGaussianProcessRegressor(GaussianProcessRegressor):
class SparseGaussianProcessRegressor(BayesianModel,
GaussianProcessRegressorMixin):
""" Sparse Gaussian Process Regression built using PyMC3.
Fit a Sparse Gaussian process model and estimate model parameters using
Expand Down Expand Up @@ -308,9 +321,15 @@ class SparseGaussianProcessRegressor(GaussianProcessRegressor):
Rasmussen and Williams (2006). Gaussian Processes for Machine Learning.
"""

def __init__(self, prior_mean=0.0):
super(SparseGaussianProcessRegressor, self).__init__(
prior_mean=prior_mean)
def __init__(self, prior_mean=None, kernel=None):
self.ppc = None
self.gp = None
self.num_training_samples = None
self.num_pred = None
self.prior_mean = prior_mean
self.kernel = kernel

super(SparseGaussianProcessRegressor, self).__init__()

def create_model(self):
""" Creates and returns the PyMC3 model.
Expand Down Expand Up @@ -344,13 +363,17 @@ def create_model(self):
noise_variance = pm.HalfCauchy('noise_variance', beta=5,
shape=1)

# cov_function = signal_variance**2 * pm.gp.cov.ExpQuad(
# 1, length_scale)
cov_function = signal_variance ** 2 * pm.gp.cov.Matern52(
1, length_scale)
if self.kernel is None:
cov_function = signal_variance ** 2 * RBF(
input_dim=self.num_pred,
ls=length_scale)
else:
cov_function = self.kernel

# mean_function = pm.gp.mean.Zero()
mean_function = pm.gp.mean.Constant(self.prior_mean)
if self.prior_mean is None:
mean_function = pm.gp.mean.Zero()
else:
mean_function = self.prior_mean

self.gp = pm.gp.MarginalSparse(mean_func=mean_function,
cov_func=cov_function,
Expand Down

0 comments on commit 58c7441

Please sign in to comment.