Skip to main content

Implementation of Python package for Fitting and Inference of Linearly Constrained Gaussian Processes

Project description

Constrained_GaussianProcess

Constrained_GaussianProcess is able to deal with linear inequality constraints in Gaussian Process frameworks. Check out the paper Finite-Dimensional Gaussian Approximation with Linear Inequality Constraints for a detail explanation.

A toy example

There are also Hamiltonian Monte Carlo method and Gibbs sampling method to sample from truncated multivariate Gaussian.

Requirement

The code requires Python 3.7 , as well as the following python libraries:

  • cvxpy==1.0.25
  • numpy==1.17.3
  • scipy==1.2.1

Those modules can be installed using: pip install numpy scipy cvxpy or pip install -r requirements.txt.

Installation

Use the package manager pip to install Constrained_GaussianProcess.

pip install Constrained-GaussianProcess

Usage

from Constrained_GaussianProcess import ConstrainedGP
m=30
# specify the constraints
constraints={'increasing': True, 'bounded': [0,1], 'convex': False}  
interval=[0,1]
Gp = ConstrainedGP(m, constraints=constraints, interval=interval)

# Training data
x_train = np.array([0.25, 0.5, 0.75])
y_train = norm().cdf((x-0.5)/0.2)

# the MCMC methods are used to approximate the posterior distribution, 
# so apart from training data, 'method' ('HMC' or 'Gibbs'), required number of samples 
# 'n' and the burn in numbers 'burn_in' should be specified when fitting the data.
Gp.fit_gp(x_train, y_train, n=100, burn_in=100, method='HMC')

x_test = np.arange(0, 1 + 0.01, 0.5)
y_pred = Gp.mean(x_test)  # get the conditional mean

Sampling from $X\sim \mathcal{N}(\mu, \Sigma)$ with constraints $f\cdot X+g\ge 0$

from Constrained_GaussianProcess import tmg

# set the number of samples and number in burn in phase
n = 150  
burn_in = 30

#Define the covariance matrix and mean vector
M = np.array([[0.5, -0.4], [-0.4, 0.5]])  
mu = np.array([0,0])

# Set initial point for the Markov chain
initial = np.array([4,1])

# Define two linear constraints
f = np.array([[1,1],[1,0]])
g = np.array([0,0])

# Sample 
samples = tmg(n, mu, M, initial, f, g, burn_in=burn_in)

Acknowledment

The HMC method for MCMC is based on the R package tmg.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Constrained_GaussianProcess-0.0.5.tar.gz (14.1 kB view hashes)

Uploaded Source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page