From charlesreid1

Line 38: Line 38:


<pre>
<pre>
kernel = ConstantKernel() + Matern(length_scale=2, nu=3/2) + WhiteKernel(noise_level=1)
kernel = ConstantKernel()  
    + Matern(length_scale=2, nu=3/2)  
    + WhiteKernel(noise_level=1)
</pre>
</pre>



Revision as of 00:18, 6 November 2017

GPM = Gaussian Process Modeling

Notes

Link to nice notebook explaining how to implement this in Python: https://blog.dominodatalab.com/fitting-gaussian-process-models-python/

Python Libraries

To do Gaussian Process Modeling, can use several libraries:

  • GPFlow (Gaussian Process Modeling using TensorFlow under the hood)
  • PyMC3 (PyMC implements Markov-chain Monte Carlo methods for GPM)
  • scikit-learn (implements several GPM routines/objects/functions)

These packages principally implement covariance functions that can be used to describe non-linearity in data, and methods for fitting GPM parameters.

Scikit-Learn

scikit-learn has several relevant functions/implementations of GPMs appropriate for different applications:

  • GaussianProcessRegressor - create this by specifying an appropriate covariance function (kernel). Fitting is accomplished by maximizing log marginal likelihood (avoids computationally intensive cross-validation strategy). Does not allow for specification of mean function (assumed to be zero). This is because the mean is not as important when computing the posterior.
  • GaussianProcessClassifier - useful for classification tasks and fitting categorical/binary data. Uses a latent Gaussian response variable, transforms it to the unit interval (or simplex). Soft probabilisitic classification task, rather than a hard classification prediction. User provides a kernel to describe the covariance in the data set. The posterior is non-normal. Laplace approximation used in place of maximizing marginal likelihood.

Kernels:

  • from sklearn import gaussian_process will import code/functions related to Gaussian process modeling
  • from sklearn.gaussian_process.kernels import Matern will import one of about a dozen GPM kernels
  • Matern covariance is a good, flexible first-choice:

$ k_M(x) = \dfrac{ \sigma^2 }{\Gamma(\nu) 2^{\nu - 1}} \left( \dfrac{ \sqrt{2 \nu} x }{ l } \right)^{\nu} K_{\nu} \left( \dfrac{ \sqrt{2 \nu } x }{ l } \right) $

  • $ \sigma $ is amplitude, scalar multiplier that controls y-axis scaling
  • $ l $ is length scale, scales realizations laong x-axis
  • $ \nu $ is roughness, controls sharpness/smoothing of ridges in covariance function

Example usage:

  • A single GPM kernel can be a combination of multiple kernels
  • Start by using a Matern component (Matern), then use an amplitude factor (ConstantKernel), then use an observation noise (WhiteKernel) kernel
kernel = ConstantKernel() 
    + Matern(length_scale=2, nu=3/2) 
    + WhiteKernel(noise_level=1)

Then create a GaussianProcessRegressor object:

gp = gaussian_process.GaussianProcessRegressor(kernel=kernel)
gp.fit(X, y)

Or, to do this all in a single call:

GaussianProcessRegressor(alpha=1e-10, copy_X_train=True,
    kernel=1**2 + Matern(length_scale=2, nu=1.5) + WhiteKernel(noise_level=1),
    n_restarts_optimizer=0, normalize_y=False,
    optimizer='fmin_l_bfgs_b', random_state=None)