Provides the base GaussianProcess class.
Bases: object
Gaussian process.
If called with one argument, an untrained Gaussian process is constructed and data must be added with the add_data() method. If called with the optional keywords, the values given are used as the data. It is always possible to add additional data with add_data().
Note that the attributes have no write protection, but you should always add data with add_data() to ensure internal consistency.
Parameters: | k : Kernel instance
noise_k : Kernel instance
diag_factor : float, optional
mu : MeanFunction instance
NOTE :
X : array, (M, D), optional
y : array, (M,), optional
err_y : array, (M,), optional
n : array, (M, D) or scalar float, optional
T : array, (M, N), optional
|
---|---|
Raises: | GPArgumentError :
ValueError :
|
See also
Attributes
k | Kernel instance | The non-noise portion of the covariance kernel. |
noise_k | Kernel instance | The noise portion of the covariance kernel. |
X | array, (M, D) | The M training input values, each of which is of dimension D. |
y | array, (M,) | The M training target values. |
err_y | array, (M,) | The error in the M training input values. |
n | array, (M, D) | The orders of derivatives that each of the M training points represent, indicating the order of derivative with respect to each of the D dimensions. |
T | array, (M, N) | The transformation matrix applied to the data. If this is not None, X and n will be N-by-D. |
K_up_to_date | bool | True if no data have been added since the last time the internal state was updated with a call to compute_K_L_alpha_ll(). |
K | array, (M, M) | Covariance matrix between all of the training inputs. |
noise_K | array, (M, M) | Noise portion of the covariance matrix between all of the training inputs. Only includes the noise from noise_k, not from err_y. |
L | array, (M, M) | Cholesky decomposition of the combined covariance matrix between all of the training inputs. |
alpha | array, (M, 1) | Solution to ![]() |
ll | float | Log-likelihood of the data given the model. |
diag_factor | float | The factor of sys.float_info.epsilon which is added to the diagonal of the K matrix to improve stability. |
mu | MeanFunction instance | The mean function. |
Combined hyperprior for the kernel, noise kernel and (if present) mean function.
Add data to the training data set of the GaussianProcess instance.
Parameters: | X : array, (M, D)
y : array, (M,)
err_y : array, (M,) or scalar float, optional
n : array, (M, D) or scalar float, optional
T : array, (M, N), optional
|
---|---|
Raises: | ValueError :
|
Condense duplicate points using a transformation matrix.
This is useful if you have multiple non-transformed points at the same location or multiple transformed points that use the same quadrature points.
Won’t change the GP if all of the rows of [X, n] are unique. Will create a transformation matrix T if necessary. Note that the order of the points in [X, n] will be arbitrary after this operation.
Remove outliers from the GP.
Removes points that are more than thresh * err_y away from the GP mean. Note that this is only very rough in that it ignores the uncertainty in the GP mean at any given point. But you should only be using this as a rough way of removing bad channels, anyways!
Returns the values that were removed and a boolean array indicating where the removed points were.
Parameters: | thresh : float, optional
**predict_kwargs : optional kwargs
|
---|---|
Returns: | X_bad : array
y_bad : array
err_y_bad : array
n_bad : array
bad_idxs : array
T_bad : array
|
Optimize the hyperparameters by maximizing the log likelihood.
Leaves the GaussianProcess instance in the optimized state.
If scipy.optimize.minimize() is not available (i.e., if your scipy version is older than 0.11.0) then fmin_slsqp() is used independent of what you set for the method keyword.
Parameters: | method : str, optional
opt_kwargs : dict, optional
verbose : bool, optional
random_starts : non-negative int, optional
num_proc : non-negative int or None
|
---|
Predict the mean and covariance at the inputs Xstar.
The order of the derivative is given by n. The keyword noise sets whether or not noise is included in the prediction.
Parameters: | Xstar : array, (M, D)
n : array, (M, D) or scalar, non-negative int, optional
noise : bool, optional
return_std : bool, optional
return_cov : bool, optional
full_output : bool, optional
return_samples : bool, optional
num_samples : int, optional
samp_kwargs : dict, optional
use_MCMC : bool, optional
full_MC : bool, optional
rejection_func : callable, optional
ddof : int, optional
output_transform: array, (`L`, `M`), optional :
**kwargs : optional kwargs
|
||||||||
---|---|---|---|---|---|---|---|---|---|
Returns: | mean : array, (M,)
std : array, (M,)
cov : array, (M, M)
full_output : dict
|
||||||||
Raises: | ValueError :
|
Plots the Gaussian process using the current hyperparameters. Only for num_dim <= 2.
Parameters: | X : array-like (M,) or (M, num_dim), optional
n : int or list, optional
ax : axis instance, optional
envelopes: list of float, optional :
base_alpha : float, optional
return_prediction : bool, optional
return_std : bool, optional
full_output : bool, optional
plot_kwargs : dict, optional
**kwargs : extra arguments for predict, optional
|
||||||||
---|---|---|---|---|---|---|---|---|---|
Returns: | ax : axis instance
mean : Array, (M,)
std : Array, (M,)
full_output : dict
|
Draw a sample evaluated at the given points Xstar.
Parameters: | Xstar : array, (M, D)
n : array, (M, D) or scalar, non-negative int, optional
noise : bool, optional
num_samp : Positive int, optional
rand_vars : array, (M, P), optional
rand_type : {‘standard normal’, ‘uniform’}, optional
diag_factor : float, optional
method : {‘cholesky’, ‘eig’}, optional
num_eig : int or None, optional
mean : array, (M,)
cov : array, (M, M)
**kwargs : optional kwargs
|
---|---|
Returns: | samples : Array (M, P) or (M, num_samp)
|
Raises: | ValueError :
|
Update the kernel’s hyperparameters to the new parameters.
This will call compute_K_L_alpha_ll() to update the state accordingly.
Parameters: | new_params : Array or other Array-like, length dictated by kernel
exit_on_bounds : bool, optional
inf_on_error : bool, optional
|
---|---|
Returns: | -1*ll : float
|
Compute K, L, alpha and log-likelihood according to the first part of Algorithm 2.1 in R&W.
Computes K and the noise portion of K using compute_Kij(), computes L using scipy.linalg.cholesky(), then computes alpha as L.T\(L\y).
Only does the computation if K_up_to_date is False – otherwise leaves the existing values.
The number of dimensions of the input data.
Returns: | num_dim: int :
|
---|
Compute covariance matrix between datasets Xi and Xj.
Specify the orders of derivatives at each location with the ni, nj
arrays. The include_noise flag is passed to the covariance kernel to
indicate whether noise is to be included (i.e., for evaluation of
versus
).
If Xj is None, the symmetric matrix is formed.
Note that type and dimension checking is NOT performed, as it is assumed the data are from inside the instance and have hence been sanitized by add_data().
Parameters: | Xi : array, (M, D)
Xj : array, (P, D)
ni : array, (M,), non-negative integers
nj : array, (P,), non-negative integers
noise : bool, optional
hyper_deriv : None or non-negative int
|
---|---|
Returns: | Kij : array, (M, P)
|
Compute the log likelihood over the (free) parameter space.
Parameters: | bounds : 2-tuple or list of 2-tuples with length equal to the number of free parameters
num_pts : int or list of ints with length equal to the number of free parameters
|
---|---|
Returns: | ll_vals : Array
|
Produce samples from the posterior for the hyperparameters using MCMC.
Returns the sampler created, because storing it stops the GP from being pickleable. To add more samples to a previous sampler, pass the sampler instance in the sampler keyword.
Parameters: | nwalkers : int, optional
nsamp : int, optional
burn : int, optional
thin : int, optional
num_proc : int or None, optional
sampler : Sampler instance
plot_posterior : bool, optional
plot_chains : bool, optional
sampler_type : str, optional
ntemps : int, optional
sampler_a : float, optional
|
---|
Compute desired quantities from MCMC samples of the hyperparameter posterior.
The return will be a list with a number of rows equal to the number of hyperparameter samples. The columns depend on the state of the boolean flags, but will be some subset of (mean, stddev, cov, samples), in that order. Samples will be the raw output of draw_sample(), so you will need to remember to convert to an array and flatten if you want to work with a single sample.
Parameters: | X : array-like (M,) or (M, num_dim)
n : non-negative int or list, optional
return_mean : bool, optional
return_std : bool, optional
return_cov : bool, optional
return_samples : bool, optional
num_samples : int, optional
noise : bool, optional
samp_kwargs : dict, optional
sampler : Sampler instance or None, optional
flat_trace : array-like (nsamp, ndim) or None, optional
burn : int, optional
thin : int, optional
num_proc : int, optional
**kwargs : extra optional kwargs
|
||||||||
---|---|---|---|---|---|---|---|---|---|
Returns: | out : dict
|
Make a prediction using MCMC samples.
This is essentially a convenient wrapper of compute_from_MCMC(), designed to act more or less interchangeably with predict().
Computes the mean of the GP posterior marginalized over the hyperparameters using iterated expectations. If return_std is True, uses the law of total variance to compute the variance of the GP posterior marginalized over the hyperparameters. If return_cov is True, uses the law of total covariance to compute the entire covariance of the GP posterior marginalized over the hyperparameters. If both return_cov and return_std are True, then both the covariance matrix and standard deviation array will be returned.
Parameters: | X : array-like (M,) or (M, num_dim)
ddof : int, optional
return_std : bool, optional
full_MC : bool, optional
rejection_func : callable, optional
ddof : int, optional **kwargs : optional kwargs
|
---|
Bases: object
Implements an inequality constraint on the value of the mean or its derivatives.
Provides a callable such as can be passed to SLSQP or COBYLA to implement the constraint when using scipy.optimize.minimize().
The function defaults implement a constraint that forces the mean value to be positive everywhere.
Parameters: | gp : GaussianProcess
boundary_val : float, optional
n : non-negative int, optional
loc : {‘min’, ‘max’}, float or Array-like of float (num_dim,), optional
type_ : {‘gt’, ‘lt’}, optional
bounds : 2-tuple of float or 2-tuple Array-like of float (num_dim,) or None, optional
|
---|---|
Raises: | TypeError :
ValueError :
ValueError :
ValueError :
ValueError :
|
Provides convenient utilities for working with the classes and results from gptools.
This module specifically contains utilities that need to interact directly with the GaussianProcess object, and hence can present circular import problems when incorporated in the main utils submodule.
Compute matrix of the log likelihood over the parameter space in parallel.
Parameters: | bounds : 2-tuple or list of 2-tuples with length equal to the number of free parameters
num_pts : int or list of ints with length equal to the number of free parameters
num_proc : Positive int or None, optional
|
---|---|
Returns: | ll_vals : array
param_vals : list of array
|
Constructs a plot that lets you look at slices through a multidimensional array.
Parameters: | vals : array, (M, D, P, ...)
x_vals_1 : array, (M,)
x_vals_2 : array, (D,)
x_vals_3 : array, (P,)
names : list of strings, optional
n : Positive int, optional
|
---|---|
Returns: | f : Figure
|
Raises: | GPArgumentError :
|
Event handler for arrow key events in plot windows.
Pass the slider object to update as a masked argument using a lambda function:
lambda evt: arrow_respond(my_slider, evt)
Parameters: | slider : Slider instance associated with this handler. event : Event to be handled. |
---|
Provides classes for defining explicit, parametric mean functions.
To provide the necessary hooks to optimize/sample the hyperparameters, your mean function must be wrapped with MeanFunction before being passed to GaussianProcess. The function must have the calling fingerprint fun(X, n, p1, p2, ...), where X is an array with shape (M, N), n is a vector with length D and p1, p2, ... are the (hyper)parameters of the mean function, given as individual arguments.
Bases: object
Wrapper to turn a function into a form useable by GaussianProcess.
This lets you define a simple function fun(X, n, p1, p2, ...) that operates on an (M, D) array X, taking the derivatives indicated by the vector n with length D (one derivative order for each dimension). The function should evaluate this derivative at all points in X, returning an array of length M. MeanFunction takes care of looping over the different derivatives requested by GaussianProcess.
Parameters: | fun : callable
num_params : Non-negative int, optional
initial_params : Array, (num_params,), optional
fixed_params : Array of bool, (num_params,), optional
param_bounds : list of 2-tuples (num_params,), optional
param_names : list of str (num_params,), optional
enforce_bounds : bool, optional
hyperprior : JointPrior instance or list, optional
|
---|
Sets the free hyperparameters to the new parameter values in new_params.
Parameters: | new_params : Array or other Array-like, (len(self.params),)
|
---|
Returns the indices of the free parameters in the main arrays of parameters, etc.
Returns the values of the free hyperparameters.
Returns: | free_params : Array
|
---|
Function implementing a constant mean suitable for use with MeanFunction.
Bases: gptools.mean.MeanFunction
Class implementing a constant mean function suitable for use with GaussianProcess.
All kwargs are passed to MeanFunction. If you do not pass hyperprior or param_bounds, the hyperprior for the mean is taken to be uniform over [-1e3, 1e3].
Modified hyperbolic tangent function mtanh(z; alpha).
Parameters: | alpha : float
z : float or array
|
---|
Profile used with the mtanh function to fit profiles, suitable for use with MeanFunction.
Only supports univariate data!
Parameters: | X : array, (M, 1)
n : array, (1,)
x0 : float
delta : float
alpha : float
h : float
b : float
|
---|
Bases: gptools.mean.MeanFunction
Profile with mtanh edge, suitable for use with GaussianProcess.
All kwargs are passed to MeanFunction. If hyperprior and param_bounds are not passed then the hyperprior is taken to be uniform over the following intervals:
x0 0.98 1.1 delta 0.0 0.1 alpha -0.5 0.5 h 0 5 b 0 0.5
Linear mean function of arbitrary dimension, suitable for use with MeanFunction.
The form is .
Parameters: | X : array, (M, D)
n : array of non-negative int, (D)
*args : num_dim+1 floats
|
---|
Bases: gptools.mean.MeanFunction
Linear mean function suitable for use with GaussianProcess.
Parameters: | num_dim : positive int, optional
**kwargs : optional kwargs
|
---|
Provides convenient utilities for working with the classes and results from gptools.
Bases: object
Class to implement a potential to enforce an inequality constraint.
Specifically lets you change the param with l_idx to have a uniform prior between its lower bound and the param with g_idx.
Returns log((ub-lb)/(theta[g_idx]-lb)) if theta[l_idx] <= theta[g_idx], double_min otherwise.
Parameters: | l_idx : int
g_idx : int
|
---|
Bases: object
Class to implement a Jeffreys prior over a finite range. Returns log-density.
Parameters: | idx : int
bounds : 2-tuple
|
---|
Bases: object
Class to implement a linear prior. Returns log-density.
Parameters: | idx : int
bounds : 2-tuple
|
---|
Bases: object
Class to implement a uniform prior. Returns log-density.
Parameters: | idx : int
bounds : 2-tuple
|
---|
Return the log-PDF of the uniform prior.
Parameters: | theta : array-like
|
---|---|
Returns: | f : Array or float
|
Bases: object
Abstract class for objects implementing joint priors over hyperparameters.
Evaluate the prior log-PDF at the given values of the hyperparameters, theta.
Parameters: | theta : array-like, (num_params,)
|
---|
Draw random samples of the hyperparameters.
Parameters: | size : None, int or array-like, optional
|
---|
Multiply two JointPrior instances together.
Bases: object
Object to support reassignment of the bounds from a combined prior.
Bases: object
Object to support reassignment of free parameter bounds.
Bases: gptools.utils.JointPrior
Product of two independent priors.
Parameters: | p1, p2: :py:class:`JointPrior` instances :
|
---|
Bases: gptools.utils.JointPrior
Uniform prior over the specified bounds.
Parameters: | bounds : list of tuples, (num_params,)
|
---|
Bases: gptools.utils.UniformJointPrior
Prior for use with Gibbs kernel warping functions with an inequality constraint between the core and edge length scales.
Bases: gptools.utils.UniformJointPrior
Prior for use with Gibbs kernel warping functions with an inequality constraint between the core, mid and edge length scales and the core-mid and mid-edge joins.
Bases: gptools.utils.JointPrior
Joint prior for which each hyperparameter is independent.
Parameters: | univariate_priors : list of callables or rv_frozen, (num_params,)
|
---|
Bases: gptools.utils.JointPrior
Joint prior for which each hyperparameter has a normal prior with fixed hyper-hyperparameters.
Parameters: | mu : list of float, same size as sigma
sigma : list of float
|
---|
Bases: gptools.utils.JointPrior
Joint prior for which each hyperparameter has a log-normal prior with fixed hyper-hyperparameters.
Parameters: | mu : list of float, same size as sigma
sigma : list of float
|
---|
Bases: gptools.utils.JointPrior
Joint prior for which each hyperparameter has a gamma prior with fixed hyper-hyperparameters.
Parameters: | a : list of float, same size as b
b : list of float
|
---|
Wrapper for fmin_slsqp() to allow it to be called with minimize()-like syntax.
This is included to enable the code to run with scipy versions older than 0.11.0.
Accepts opt_kwargs in the same format as used by scipy.optimize.minimize(), with the additional precondition that the keyword method has already been removed by the calling code.
Parameters: | fun : callable
guess : sequence
opt_kwargs : dict, optional
|
||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns: | Result : namedtuple
|
||||||||||||||||||
Raises: | ValueError :
|
Recursive evaluation of the incomplete Bell polynomial .
Evaluates the incomplete Bell polynomial ,
also known as the partial Bell polynomial or the Bell polynomial of the
second kind. This polynomial is useful in the evaluation of (the univariate)
Faa di Bruno’s formula which generalizes the chain rule to higher order
derivatives.
The implementation here is based on the implementation in: sympy.functions.combinatorial.numbers.bell._bell_incomplete_poly() Following that function’s documentation, the polynomial is computed according to the recurrence formula:
Parameters: | n : scalar int
k : scalar int
x : Array of floats, (p, n - k + 1)
|
---|---|
Returns: | result : Array, (p,)
|
Generate the restricted growth strings for all of the partitions of an n-member set.
Uses Algorithm H from page 416 of volume 4A of Knuth’s The Art of Computer Programming. Returns the partitions in lexicographical order.
Parameters: | n : scalar int, non-negative
|
---|---|
Returns: | partitions : list of Array
|
Generate all of the partitions of a set.
This is a helper function that utilizes the restricted growth strings from generate_set_partition_strings(). The partitions are returned in lexicographic order.
Parameters: | set_ : Array or other Array-like, (m,)
|
---|---|
Returns: | partitions : list of lists of Array
|
From itertools documentation.
Returns a copy of arr with duplicate rows removed.
From Stackoverflow “Find unique rows in numpy.array.”
Parameters: | arr : Array, (m, n)
return_index : bool, optional
return_inverse: bool, optional :
|
---|---|
Returns: | unique : Array, (p, n) where p <= m
|
Compute the average statistics (mean, std dev) for the given values.
Parameters: | vals : array-like, (M, D)
check_nan : bool, optional
robust : bool, optional
axis : int, optional
plot_QQ : bool, optional
bins : int, optional
name : str, optional
|
---|---|
Returns: | mean : ndarray, (M,)
std : ndarray, (M,)
|
Raises: | NotImplementedError :
NotImplementedError :
|
Make a plot of a mean curve with uncertainty envelopes.
Create summary statistics of the flattened chain of the sampler.
The confidence regions are computed from the quantiles of the data.
Parameters: | sampler : emcee.EnsembleSampler instance
burn : int, optional
thin : int, optional
ci : float, optional
|
---|---|
Returns: | mean : array, (num_params,)
ci_l : array, (num_params,)
ci_u : array, (num_params,)
|
Plot the results of MCMC sampler (posterior and chains).
Loosely based on triangle.py.
Parameters: | sampler : emcee.EnsembleSampler instance
labels : list of str, optional
burn : int, optional
chain_mask : (index) array
|
---|