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.
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
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
Built Distribution
Hashes for Constrained_GaussianProcess-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb654f7b5cdde012a6d6f813d00ac829569c73d71c4dc1f8f798ad5744c66656 |
|
MD5 | d2e179e1f6fd3ef9a6c32ac1e2c0512e |
|
BLAKE2b-256 | 323c4dc54f18560b982ba680ed8e37e2bdb7b9f9b5c46d9578bb8ffc8af66606 |
Hashes for Constrained_GaussianProcess-0.0.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c33969be44e5339cb0fdce3ecff118820d6f62a222c68f267e8d6ccaf267f28 |
|
MD5 | 7598fbb0b3e97dd43c811818aca77a64 |
|
BLAKE2b-256 | 0b484e38a58a52aa7f6f9e7ee92ef27bbc8c3dc3e6f5733d00d744a6e3e83aaa |