def spwen30(sigma, cts): ''' Wendland 3-D C0 covariance function. Parameters ---------- sigma [mm] : Standard deviation of displacements cts [yr] : Characteristic time-scale ''' return gauss.gpiso(rbf.basis.spwen30, (0.0, sigma**2, cts), dim=1)
def mat32(sigma, cts): ''' Matern covariance function with nu=3/2 Parameters ---------- sigma [mm] : Standard deviation of displacements cts [yr] : Characteristic time-scale ''' return gauss.gpiso(rbf.basis.mat32, (0.0, sigma**2, cts), dim=1)
def mat32(sigma,cts): ''' Matern covariance function with nu=3/2 Parameters ---------- sigma [mm] : Standard deviation of displacements cts [yr] : Characteristic time-scale ''' return gauss.gpiso(rbf.basis.mat32,(0.0,sigma**2,cts),dim=1)
def spwen30(sigma,cts): ''' Wendland 3-D C0 covariance function. Parameters ---------- sigma [mm] : Standard deviation of displacements cts [yr] : Characteristic time-scale ''' return gauss.gpiso(rbf.basis.spwen30,(0.0,sigma**2,cts),dim=1)
import logging from rbf.basis import se from rbf.gauss import gpiso logging.basicConfig(level=logging.DEBUG) np.random.seed(1) y = np.linspace(-7.5,7.5,25) # observation points x = np.linspace(-7.5,7.5,1000) # interpolation points u_true = np.exp(-0.3*np.abs(x))*np.sin(x) # true signal sigma = 0.1*np.ones(25) # observation uncertainty # noisy observations of the signal d = np.exp(-0.3*np.abs(y))*np.sin(y) + np.random.normal(0.0,sigma) # form a prior Gaussian process which has a squared exponential # covariance function (rbf.basis.se), 0.0 for the mean, 1.0 for the # standard deviation, and 1.0 for the characteristic length scale. gp = gpiso(se, (0.0, 1.0, 1.0)) sample = gp.sample(x[:,None]) # generate random sample mean,std = gp(x[:,None]) # find the mean and standard dev. at x gp_cond = gp.condition(y[:,None],d,sigma=sigma) # condition with data sample_cond = gp_cond.sample(x[:,None]) mean_cond,std_cond = gp_cond(x[:,None]) ## Plotting ##################################################################### fig,axs = plt.subplots(2,1,figsize=(6,6)) ax = axs[0] ax.tick_params(labelsize=10) ax.set_title('Prior Gaussian Process',fontsize=10) ax.plot(x,mean,'b-',label='mean') ax.fill_between(x,mean-std,mean+std,color='b', alpha=0.2,edgecolor='none',label='standard deviation')
# matrices. We can perform Gaussian process regression on large # datasets with this choice of prior, provided that the lengthscale of # the prior is much less than the size of the domain (which is not # true for this demo) basis = spwen32 # define hyperparameters for the prior. Tune these parameters to get a # satisfactory interpolant. These can also be chosen with maximum # likelihood methods. prior_mean = 0.0 prior_sigma = 1.0 prior_lengthscale = 0.8 # this controls the sparsity # create the prior Gaussian process params = (prior_mean, prior_sigma, prior_lengthscale) prior_gp = gpiso(basis, params) # add a first order polynomial to the prior to make it suitable for # data with linear trends prior_gp += gppoly(1) # condition the prior on the observations, creating a new Gaussian # process for the posterior. posterior_gp = prior_gp.condition(xobs, uobs, sigma=sobs) # differentiate the posterior with respect to x derivative_gp = posterior_gp.differentiate((1, 0)) # evaluate the posterior and posterior derivative at the interpolation # points. calling the GaussianProcess instances will return their mean # and standard deviation at the provided points. post_mean, post_std = posterior_gp(xitp)
from rbf.basis import get_r, get_eps, RBF from rbf.gauss import gpiso np.random.seed(1) period = 5.0 cls = 0.5 # characteristic length scale var = 1.0 # variance r = get_r() # get symbolic variables eps = get_eps() # create a symbolic expression of the periodic covariance function expr = exp(-sin(r*pi/period)**2/eps**2) # define a periodic RBF using the symbolic expression basis = RBF(expr) # define a Gaussian process using the periodic RBF gp = gpiso(basis, (0.0, var, cls)) t = np.linspace(-10, 10, 1000)[:,None] sample = gp.sample(t) # draw a sample mu,sigma = gp(t) # evaluate mean and std. dev. # plot the results fig,ax = plt.subplots(figsize=(6,4)) ax.grid(True) ax.plot(t[:,0],mu,'b-',label='mean') ax.fill_between(t[:,0],mu-sigma,mu+sigma,color='b',alpha=0.2,edgecolor='none',label='std. dev.') ax.plot(t,sample,'k',label='sample') ax.set_xlim((-10.0,10.0)) ax.set_ylim((-2.5*var,2.5*var)) ax.legend(loc=4,fontsize=10) ax.tick_params(labelsize=10) ax.set_xlabel('time',fontsize=10)
def negative_likelihood(params): log_variance, log_lengthscale = params gp = gpiso('se', (0.0, 10**log_variance, 10**log_lengthscale)) return -gp.likelihood(y[:, None], d, sigma=sigma)
d = np.exp(-0.3 * np.abs(y)) * np.sin(y) + np.random.normal(0.0, sigma) # form a prior Gaussian process which has a squared exponential covariance # function (rbf.basis.se), 0.0 for the mean, and variance and lengthscales that # are chosen with maximum likelihood. The variance and lengthscale are given # positivity contraints by optimizing them in log space def negative_likelihood(params): log_variance, log_lengthscale = params gp = gpiso('se', (0.0, 10**log_variance, 10**log_lengthscale)) return -gp.likelihood(y[:, None], d, sigma=sigma) log_variance, log_lengthscale = minimize(negative_likelihood, [0.0, 0.0]).x # create a prior GaussianProcess using the most likely variance and lengthscale gp_prior = gpiso('se', (0.0, 10**log_variance, 10**log_lengthscale)) # generate a sample of the prior sample_prior = gp_prior.sample(x[:, None]) # find the mean and standard deviation of the prior mean_prior, std_prior = gp_prior(x[:, None]) # condition the prior on the observations gp_post = gp_prior.condition(y[:, None], d, sigma=sigma) sample_post = gp_post.sample(x[:, None]) mean_post, std_post = gp_post(x[:, None]) ## Plotting ##################################################################### fig, axs = plt.subplots(2, 1, figsize=(6, 6)) ax = axs[0] ax.grid(ls=':') ax.tick_params(labelsize=10)
from rbf.gauss import gpiso from rbf.basis import spwen12 import logging logging.basicConfig(level=logging.DEBUG) np.random.seed(1) # create synthetic data n = 10000 y = np.linspace(-20.0, 20.0, n) # observation points sigma = 0.5*np.ones(n) d = np.exp(-0.3*np.abs(y))*np.sin(y) + np.random.normal(0.0, sigma) # evaluate the output at a subset of the observation points x = np.linspace(-20.0, 20.0, 1000) # interpolation points u_true = np.exp(-0.3*np.abs(x))*np.sin(x) # true signal # create a sparse GP gp = gpiso(spwen12, (0.0, 1.0, 4.0)) # condition with the observations gpc = gp.condition(y[:,None], d, sigma) # find the mean and std of the conditioned GP. Chunk size controls the # trade off between speed and memory consumption. It should be tuned # by the user. u, us = gpc.meansd(x[:,None], chunk_size=1000) fig,ax = plt.subplots() ax.plot(x, u_true, 'k-', label='true signal') ax.plot(y, d, 'k.', alpha=0.1, mec='none', label='observations') ax.plot(x, u, 'b-', label='post. mean') ax.fill_between(x, u-us, u+us, color='b', alpha=0.2, label='post. std. dev.') ax.set_xlim((-20.0, 20.0)) ax.set_ylim((-2.0, 2.0)) ax.legend(loc=2, fontsize=10) plt.show()
''' This script demonstrates how to make a custom *GaussianProcess* by combining *GaussianProcess* instances. The resulting Gaussian process has two distinct length-scales. ''' import numpy as np import matplotlib.pyplot as plt from rbf.basis import se from rbf.gauss import gpiso np.random.seed(1) dx = np.linspace(0.0, 5.0, 1000)[:, None] x = np.linspace(-5, 5.0, 1000)[:, None] gp_long = gpiso(se, (0.0, 1.0, 2.0)) gp_short = gpiso(se, (0.0, 0.5, 0.25)) gp = gp_long + gp_short # compute the autocovariances acov_long = gp_long.covariance(dx, [[0.0]]) acov_short = gp_short.covariance(dx, [[0.0]]) acov = gp.covariance(dx, [[0.0]]) # draw 3 samples sample = gp.sample(x) # mean and uncertainty of the new gp mean, sigma = gp(x) # plot the autocovariance functions fig, axs = plt.subplots(2, 1, figsize=(6, 6)) axs[0].plot(dx, acov_long, 'r--', label='long component') axs[0].plot(dx, acov_short, 'b--', label='short component') axs[0].plot(dx, acov, 'k-', label='sum') axs[0].set_xlabel('$\mathregular{\Delta x}$', fontsize=10) axs[0].set_ylabel('auto-covariance', fontsize=10)
def spwen32(sigma,cls): ''' Sparse Wendland space covariance function ''' return gauss.gpiso(rbf.basis.spwen32,(0.0,sigma**2,cls),dim=2)
def mat52(sigma,cls): ''' Matern space covariance function with nu=5/2 ''' return gauss.gpiso(rbf.basis.mat52,(0.0,sigma**2,cls),dim=2)