The Priors Module¶
-
class
mogp_emulator.Priors.GPPriors(mean=None, corr=None, cov=None, nugget=None, n_corr=None, nugget_type='fit')¶ Class representing prior distributions on GP Hyperparameters
This class combines together the prior distributions over the hyperparameters for a GP. These are separated out into
mean(which is a separateMeanPriorsobject),corr(a list of distributions for the correlation length parameters),covfor the covariance, andnuggetfor the nugget. These can be specified when initializing a new object using the appropriate kwargs, or can be set once aGPPriorsobject has already been created.In addition to kwargs for the distributions, an additional kwarg
n_corrcan be used to specify the number of correlation lengths (in the event thatcorris not provided). Ifcorris specified, then this will overriden_corrin determining the number of correlation lengths, so if both are provided thencorris used preferrentially. If neithercorrorn_corris provided, an exception will be raised.Finally, the nugget type is required to be specified when initializing a new object.
Parameters: - mean (MeanPriors) – Priors on mean, must be a
MeanPriorsobject. Optional, default isNone(indicating weak prior information). - corr (list) – Priors on correlation lengths, must be a list
of prior distributions (objects derived from
WeakPriors). Optional, default isNone(indicating weak prior information, which will automatically create an appropriate list ofWeakPriorsobjects of the length specified byn_corr). - cov (WeakPriors) – Priors on covariance. Must be a
WeakPriorsderived object. Optional, default isNone(indicating weak prior information). - nugget (WeakPriors) – Priors on nugget. Only valid if the nugget
is fit. Must be a
WeakPriorsderived object. Optional, default isNone(indicating weak prior information). - n_corr (int) – Integer specifying number of correlation lengths.
Only used if
corris not specified. Optional, default isNoneto indicate number of correlation lengths is specified bycorr. - nugget_type (str) – String indicating nugget type. Must be
"fixed","adaptive","fit", or"pivot". Optional, default is"fit"
-
corr¶ Correlation Length Priors
Must be a list of distributions/None. When class object is initialized, must either set number of correlation parameters explicitly or pass a list of prior objects. If only number of parameters, will generate a list of NoneTypes of that length (assumes weak prior information). If list provided, will use that and override the value of number of correlation parameters.
Can change the length by setting this attribute. n_corr will automatically update.
-
cov¶ Covariance Scale Priors
Prior distribution on Covariance Scale. Can be set using a
WeakPriorsderived object.
-
d2logpdtheta2(theta)¶ Compute the second derivative of the log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the second derivative of the log probability of all of the sub-distributions with respect to the raw hyperparameter values. Returns a numpy array of lengthn_params(the number of fitting parameters in theGPParamsobject).Parameters: theta (GPParams) – Hyperparameter values at which the log prior second derivative is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Hessian of the log probability. Length will be the value of n_paramsof theGPParamsobject. (Note that since all mixed partials are zero, this returns the diagonal of the Hessian as an array)Return type: ndarray
-
classmethod
default_priors(inputs, n_corr, nugget_type='fit', dist='invgamma')¶ Class Method to create a
GPPriorsobject with default valuesClass method that creates priors with defaults for correlation length priors and nugget. For the correlation lengths, the values of the inputs are used to infer a distribution that puts 99% of the mass between the minimum and maximum grid spacing. For the nugget (if fit), a default is used that preferrentially uses a small nugget. The mean and covariance priors are kept as weak prior information.
Parameters: - inputs (ndarray) – Input values on which the GP will be fit. Must be a 2D numpy array with the same restrictions as the inputs to the GP class.
- n_corr (int) – Number of correlation lengths. Because some kernels only use a single correlation length, this parameter specifies how to treat the inputs to derive the default correlation length priors. Must be a positive integer.
- nugget_type (str) – String indicating nugget type. Must be
"fixed","adaptive","fit", or"pivot". Optional, default is"fit" - dist (str or WeakPriors derived class) – Distribution to fit to the correlation lengths.
Must be either a class derived from
WeakPriorswith adefault_priorclass method, or"lognormal","gamma", or"invgamma". Default is"invgamma".
:
-
dlogpdtheta(theta)¶ Compute derivative of the log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the derivative of the log probability of all of the sub-distributions with respect to the raw hyperparameter values. Returns a numpy array of lengthn_params(the number of fitting parameters in theGPParamsobject).Parameters: theta (GPParams) – Hyperparameter values at which the log prior derivative is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Gradient of the log probability. Length will be the value of n_paramsof theGPParamsobject.Return type: ndarray
-
logp(theta)¶ Compute log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the sum of the log probability of all of the sub-distributions. Returns a float.Parameters: theta (GPParams) – Hyperparameter values at which the log prior is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Sum of the log probability of all prior distributions Return type: float
-
mean¶ Mean Prior information
The mean prior information is held in a
MeanPriorsobject. Can be set using aMeanPriorsobject orNone
-
n_corr¶ Number of correlation length parameters
-
n_mean¶ Number of mean parameters
Returns: Number of parameters for the MeanPriorobject. If the mean prior is weak or there is no mean function, returnsNone.Return type: int or None
-
nugget¶ Nugget prior distribution
If a nugget is fit, this determines the prior used. If the nugget is not fit, will automatically set this to
None.
-
nugget_type¶ Nugget fitting method for the parent GP.
-
sample()¶ Draw a set of samples from the prior distributions
Draws a set of samples from the prior distributions associated with this GPPriors object. Used in fitting to initialize the minimization algorithm.
Returns: Random draw from each distribution, transformed to the raw hyperparameter values. Will be a numpy array with length n_paramsof the associatedGPParamsobject.
- mean (MeanPriors) – Priors on mean, must be a
-
class
mogp_emulator.Priors.GammaPrior(shape, scale)¶ Gamma Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape \({\alpha}\) and scale \({\beta}\). Both must be positive, and they are defined such that
\({p(x) = \frac{\beta^{-\alpha}x^{\alpha - 1}}{\Gamma(/alpha)} \exp(-x/\beta)}\)
-
d2logpdx2(x)¶ Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x)¶ Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x)¶ Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x()¶ Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.InvGammaPrior(shape, scale)¶ Inverse Gamma Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape \({\alpha}\) and scale \({\beta}\). Both must be positive, and they are defined such that
\({p(x) = \frac{\beta^{\alpha}x^{-\alpha - 1}}{\Gamma(/alpha)} \exp(-\beta/x)}\)
Note that
InvGammaPriorsupports both the usual distribution finding methodsdefault_prioras well as some fallback methods that use the mode of the distribution to set the parameters.-
d2logpdx2(x)¶ Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
classmethod
default_prior_corr_mode(inputs)¶ Compute default priors on a set of inputs for the correlation length
Takes a set of inputs and computes the min and max spacing before calling the
default_prior_modemethod. This method is more stable than the standard default and is used as a fallback in the event that the usual method fails (which can happen if the inputs have too broad a range of spacing values).Parameters: inputs (ndarray) – Input values on which the distribution will be fit. Must be a 1D numpy array (note that 2D arrays will be flattened). Returns: InvGammaPrior distribution with fit parameters Return type: InvGammaPrior
-
classmethod
default_prior_mode(min_val, max_val)¶ Compute default priors on a set of inputs for the correlation length
In some cases, the default correlation prior can fail to fit the distribution to the provided values. This method is more stable as it does not attempt to fit the lower bound of the distribution but instead fits the mode (which can be analytically related to the distribution parameters). The mode is chosen to be the the geometric mean of the min/max values and 99.5% of the mass is below the max value. This approach can fit distributions to wider ranges of parameters and is used as a fallback for correlation lengths and the default for the nugget (if the nugget is fit)
Parameters: - min_val (float) – Minimum value of the input spacing
- max_val (float) – Maximum value of the input spacing
Returns: InvGammaPrior distribution with fit parameters
Return type:
-
classmethod
default_prior_nugget(min_val=1e-08, max_val=1e-06)¶ Compute default priors on a set of inputs for the nugget
Computes a distribution with given bounds using the
default_prior_modemethod. This method is more stable than the standard default and is used as a fallback in the event that the usual method fails (which can happen if the inputs have too broad a range of spacing values). Is well suited for the nugget, which in most cases is desired to be small.Parameters: - min_val (float) – Minimum value of the input spacing. Optional,
default is
1.e-8 - max_val (float) – Maximum value of the input spacing. Optional,
default is
1.e-6
Returns: InvGammaPrior distribution with fit parameters
Return type: - min_val (float) – Minimum value of the input spacing. Optional,
default is
-
dlogpdx(x)¶ Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x)¶ Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x()¶ Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.LogNormalPrior(shape, scale)¶ Normal Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape and scale, both of which must be positive
-
d2logpdx2(x)¶ Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x)¶ Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x)¶ Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x()¶ Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.MeanPriors(mean=None, cov=None)¶ Object holding mean priors (mean vector and covariance float/vector/matrix assuming a multivariate normal distribution). Includes methods for computing the inverse and determinant of the covariance and the inverse of the covariance multiplied by the mean.
Note that if weak prior information is provided, or if there is no mean function, the methods here will still work correctly given the desired calling context.
Parameters: - mean (ndarray) – Mean vector of the multivariate normal prior distribution
- cov (float or ndarray) – Scalar variance, vector variance, or covariance matrix of the covariance of the prior distribution. Must be a float or 1D or 2D numpy array.
-
dm_dot_b(dm)¶ Take dot product of mean with a design matrix
Returns the dot product of a design matrix with the prior distribution mean vector. If prior information is weak or there is no mean function, returns zeros of the appropriate shape.
Parameters: dm (ndarray or patsy.DesignMatrix) – Design matrix, array with shape (n, n_mean)Returns: dot product of design matrix with prior distribution mean vector. Return type: ndarray
-
has_weak_priors¶ Property indicating if the Mean has weak prior information
Returns: Boolean indicating if prior information is weak Return type: bool
-
inv_cov()¶ Compute the inverse of the covariance matrix
Returns the inverse covariance matrix or zero if prior information is weak. Returns a float or a 2D numpy array with shape
(n_mean, n_mean).Returns: Inverse of the covariance matrix or zero if prior information is weak. If the inverse is returned, it will be a numpy array of shape (n_mean, n_mean).Return type: ndarray or float
-
inv_cov_b()¶ Compute the inverse of the covariance matrix times the mean vector
In the log posterior computations, the inverse of the covariance matrix multiplied by the mean is required. This method correctly returns zero in the event mean prior information is weak.
Returns: Inverse covariance matrix multiplied by the mean of the prior distribution. Returns an array with length of the number of mean parameters or a float (in the event of weak prior information) Return type: ndarray or float
-
logdet_cov()¶ Compute the log of the determinant of the covariance
Computes the log determininant of the mean prior covariance. Correctly returns zero if the prior information on the mean is weak.
Returns: Log determinant of the covariance matrix Return type: float
-
n_params¶ Number of parameters associated with the mean
Returns: number of mean parameters (or zero if prior information is weak) Return type: int
-
class
mogp_emulator.Priors.NormalPrior(mean, std)¶ Normal Distribution Prior object
Admits input values from -inf/+inf.
Take two parameters: mean and std. Mean can take any numeric value, while std must be positive.
-
d2logpdx2(x)¶ Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x)¶ Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x)¶ Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x()¶ Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.PriorDist¶ Generic Prior Distribution Object
This implements the generic methods for all non-weak prior distributions such as default priors and sampling methods. Requires a derived method to implement
logp,dlogpdx,d2logpdx2, andsample_x.-
classmethod
default_prior(min_val, max_val)¶ Computes default priors given a min and max val between which 99% of the mass should be found.
Both min and max must be positive as the supported distributions are defined over \([0, +\inf]\)
This stabilizes the solution, as it prevents the algorithm from getting stuck outside these ranges as the likelihood tends to be flat on those areas.
Optionally, can change the distribution to be a lognormal or gamma distribution by specifying the
distargument.Note that the function assumes only a single input dimension is provided. Thus, any input array will be flattened before processing.
If the root-finding algorithm fails, then the function will return
Noneto revert to a flat prior.Parameters: - min_val (float) – Minimum value of the input spacing
- max_val (float) – Maximum value of the input spacing
Returns: Distribution with fit parameters
Return type: Type derived from
PriorDist
-
classmethod
default_prior_corr(inputs)¶ Compute default priors on a set of inputs for the correlation length
Takes a set of inputs and computes the min and max spacing before calling the
default_priormethod of the class in question to generate a distribution. Used in computing the correlation length default prior.Parameters: inputs (ndarray) – Input values on which the distribution will be fit. Must be a 1D numpy array (note that 2D arrays will be flattened). Returns: Prior distribution with fit parameters Return type: PriorDist derived object
-
sample(transform)¶ Draws a random sample from the distribution and transform to the raw parameter values
Parameters: transform (CorrTransform or CovTransform) – Transform to apply to the sample. Must be one of CorrTransformorCovTransform.Returns: Raw random sample from the distribution Return type: float
-
sample_x()¶ Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
classmethod
-
class
mogp_emulator.Priors.WeakPrior¶ Base Prior class implementing weak prior information
This was implemented to avoid using
Noneto signify weak prior information, which required many different conditionals that made the code clunky. In this implementation, all parameters have a prior distribution to simplify implementation and clarify the methods for computing the log probabilities.-
d2logpdtheta2(x, transform)¶ Computes second derivative of log probability with respect to the raw variable at a given value. Requires passing the transform to apply to the variable to correctly compute the derivative.
Parameters: - x (float) – Value of (transformed) variable
- transform (CorrTransform or CovTransform) – Transform to apply to the derivative
to use the chain rule to compute the
derivative. Must be one of
CorrTransformorCovTransform.
Returns: Derivative of Log probability
Return type: float
-
d2logpdx2(x)¶ Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdtheta(x, transform)¶ Computes derivative of log probability with respect to the raw variable at a given value. Requires passing the transform to apply to the variable to correctly compute the derivative.
Parameters: - x (float) – Value of (transformed) variable
- transform (CorrTransform or CovTransform) – Transform to apply to the derivative
to use the chain rule to compute the
derivative. Must be one of
CorrTransformorCovTransform.
Returns: Derivative of Log probability
Return type: float
-
dlogpdx(x)¶ Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x)¶ Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample(transform=None)¶ Draws a random sample from the distribution and transform to the raw parameter values
Parameters: transform (CorrTransform or CovTransform) – Transform to apply to the sample. Must be one of CorrTransformorCovTransform. Note that for aWeakPriorobject this argument is optional as it is ignored, though derived classes require this argument.Returns: Raw random sample from the distribution Return type: float
-
-
mogp_emulator.Priors.max_spacing(input)¶ Computes the maximum spacing of a particular input
Parameters: input (ndarray) – Input values over which the maximum is to be computed. Must be a numpy array (will be flattened). Returns: Maximum difference between any pair of values Return type: float
-
mogp_emulator.Priors.min_spacing(input)¶ Computes the median spacing of a particular input
Parameters: input (ndarray) – Input values over which the median is to be computed. Must be a numpy array (will be flattened). Returns: Median spacing of the sorted inputs Return type: float
-
class
mogp_emulator.Priors.GPPriors(mean=None, corr=None, cov=None, nugget=None, n_corr=None, nugget_type='fit') Class representing prior distributions on GP Hyperparameters
This class combines together the prior distributions over the hyperparameters for a GP. These are separated out into
mean(which is a separateMeanPriorsobject),corr(a list of distributions for the correlation length parameters),covfor the covariance, andnuggetfor the nugget. These can be specified when initializing a new object using the appropriate kwargs, or can be set once aGPPriorsobject has already been created.In addition to kwargs for the distributions, an additional kwarg
n_corrcan be used to specify the number of correlation lengths (in the event thatcorris not provided). Ifcorris specified, then this will overriden_corrin determining the number of correlation lengths, so if both are provided thencorris used preferrentially. If neithercorrorn_corris provided, an exception will be raised.Finally, the nugget type is required to be specified when initializing a new object.
Parameters: - mean (MeanPriors) – Priors on mean, must be a
MeanPriorsobject. Optional, default isNone(indicating weak prior information). - corr (list) – Priors on correlation lengths, must be a list
of prior distributions (objects derived from
WeakPriors). Optional, default isNone(indicating weak prior information, which will automatically create an appropriate list ofWeakPriorsobjects of the length specified byn_corr). - cov (WeakPriors) – Priors on covariance. Must be a
WeakPriorsderived object. Optional, default isNone(indicating weak prior information). - nugget (WeakPriors) – Priors on nugget. Only valid if the nugget
is fit. Must be a
WeakPriorsderived object. Optional, default isNone(indicating weak prior information). - n_corr (int) – Integer specifying number of correlation lengths.
Only used if
corris not specified. Optional, default isNoneto indicate number of correlation lengths is specified bycorr. - nugget_type (str) – String indicating nugget type. Must be
"fixed","adaptive","fit", or"pivot". Optional, default is"fit"
-
corr Correlation Length Priors
Must be a list of distributions/None. When class object is initialized, must either set number of correlation parameters explicitly or pass a list of prior objects. If only number of parameters, will generate a list of NoneTypes of that length (assumes weak prior information). If list provided, will use that and override the value of number of correlation parameters.
Can change the length by setting this attribute. n_corr will automatically update.
-
cov Covariance Scale Priors
Prior distribution on Covariance Scale. Can be set using a
WeakPriorsderived object.
-
d2logpdtheta2(theta) Compute the second derivative of the log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the second derivative of the log probability of all of the sub-distributions with respect to the raw hyperparameter values. Returns a numpy array of lengthn_params(the number of fitting parameters in theGPParamsobject).Parameters: theta (GPParams) – Hyperparameter values at which the log prior second derivative is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Hessian of the log probability. Length will be the value of n_paramsof theGPParamsobject. (Note that since all mixed partials are zero, this returns the diagonal of the Hessian as an array)Return type: ndarray
-
classmethod
default_priors(inputs, n_corr, nugget_type='fit', dist='invgamma') Class Method to create a
GPPriorsobject with default valuesClass method that creates priors with defaults for correlation length priors and nugget. For the correlation lengths, the values of the inputs are used to infer a distribution that puts 99% of the mass between the minimum and maximum grid spacing. For the nugget (if fit), a default is used that preferrentially uses a small nugget. The mean and covariance priors are kept as weak prior information.
Parameters: - inputs (ndarray) – Input values on which the GP will be fit. Must be a 2D numpy array with the same restrictions as the inputs to the GP class.
- n_corr (int) – Number of correlation lengths. Because some kernels only use a single correlation length, this parameter specifies how to treat the inputs to derive the default correlation length priors. Must be a positive integer.
- nugget_type (str) – String indicating nugget type. Must be
"fixed","adaptive","fit", or"pivot". Optional, default is"fit" - dist (str or WeakPriors derived class) – Distribution to fit to the correlation lengths.
Must be either a class derived from
WeakPriorswith adefault_priorclass method, or"lognormal","gamma", or"invgamma". Default is"invgamma".
:
-
dlogpdtheta(theta) Compute derivative of the log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the derivative of the log probability of all of the sub-distributions with respect to the raw hyperparameter values. Returns a numpy array of lengthn_params(the number of fitting parameters in theGPParamsobject).Parameters: theta (GPParams) – Hyperparameter values at which the log prior derivative is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Gradient of the log probability. Length will be the value of n_paramsof theGPParamsobject.Return type: ndarray
-
logp(theta) Compute log probability given a
GPParamsobjectTakes a
GPParamsobject, this method computes the sum of the log probability of all of the sub-distributions. Returns a float.Parameters: theta (GPParams) – Hyperparameter values at which the log prior is to be computed. Must be a GPParamsobject whose attributes match thisGPPriorsobject.Returns: Sum of the log probability of all prior distributions Return type: float
-
mean Mean Prior information
The mean prior information is held in a
MeanPriorsobject. Can be set using aMeanPriorsobject orNone
-
n_corr Number of correlation length parameters
-
n_mean Number of mean parameters
Returns: Number of parameters for the MeanPriorobject. If the mean prior is weak or there is no mean function, returnsNone.Return type: int or None
-
nugget Nugget prior distribution
If a nugget is fit, this determines the prior used. If the nugget is not fit, will automatically set this to
None.
-
nugget_type Nugget fitting method for the parent GP.
-
sample() Draw a set of samples from the prior distributions
Draws a set of samples from the prior distributions associated with this GPPriors object. Used in fitting to initialize the minimization algorithm.
Returns: Random draw from each distribution, transformed to the raw hyperparameter values. Will be a numpy array with length n_paramsof the associatedGPParamsobject.
- mean (MeanPriors) – Priors on mean, must be a
-
class
mogp_emulator.Priors.MeanPriors(mean=None, cov=None) Object holding mean priors (mean vector and covariance float/vector/matrix assuming a multivariate normal distribution). Includes methods for computing the inverse and determinant of the covariance and the inverse of the covariance multiplied by the mean.
Note that if weak prior information is provided, or if there is no mean function, the methods here will still work correctly given the desired calling context.
Parameters: - mean (ndarray) – Mean vector of the multivariate normal prior distribution
- cov (float or ndarray) – Scalar variance, vector variance, or covariance matrix of the covariance of the prior distribution. Must be a float or 1D or 2D numpy array.
-
dm_dot_b(dm) Take dot product of mean with a design matrix
Returns the dot product of a design matrix with the prior distribution mean vector. If prior information is weak or there is no mean function, returns zeros of the appropriate shape.
Parameters: dm (ndarray or patsy.DesignMatrix) – Design matrix, array with shape (n, n_mean)Returns: dot product of design matrix with prior distribution mean vector. Return type: ndarray
-
has_weak_priors Property indicating if the Mean has weak prior information
Returns: Boolean indicating if prior information is weak Return type: bool
-
inv_cov() Compute the inverse of the covariance matrix
Returns the inverse covariance matrix or zero if prior information is weak. Returns a float or a 2D numpy array with shape
(n_mean, n_mean).Returns: Inverse of the covariance matrix or zero if prior information is weak. If the inverse is returned, it will be a numpy array of shape (n_mean, n_mean).Return type: ndarray or float
-
inv_cov_b() Compute the inverse of the covariance matrix times the mean vector
In the log posterior computations, the inverse of the covariance matrix multiplied by the mean is required. This method correctly returns zero in the event mean prior information is weak.
Returns: Inverse covariance matrix multiplied by the mean of the prior distribution. Returns an array with length of the number of mean parameters or a float (in the event of weak prior information) Return type: ndarray or float
-
logdet_cov() Compute the log of the determinant of the covariance
Computes the log determininant of the mean prior covariance. Correctly returns zero if the prior information on the mean is weak.
Returns: Log determinant of the covariance matrix Return type: float
-
n_params Number of parameters associated with the mean
Returns: number of mean parameters (or zero if prior information is weak) Return type: int
-
class
mogp_emulator.Priors.WeakPrior Base Prior class implementing weak prior information
This was implemented to avoid using
Noneto signify weak prior information, which required many different conditionals that made the code clunky. In this implementation, all parameters have a prior distribution to simplify implementation and clarify the methods for computing the log probabilities.-
d2logpdtheta2(x, transform) Computes second derivative of log probability with respect to the raw variable at a given value. Requires passing the transform to apply to the variable to correctly compute the derivative.
Parameters: - x (float) – Value of (transformed) variable
- transform (CorrTransform or CovTransform) – Transform to apply to the derivative
to use the chain rule to compute the
derivative. Must be one of
CorrTransformorCovTransform.
Returns: Derivative of Log probability
Return type: float
-
d2logpdx2(x) Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdtheta(x, transform) Computes derivative of log probability with respect to the raw variable at a given value. Requires passing the transform to apply to the variable to correctly compute the derivative.
Parameters: - x (float) – Value of (transformed) variable
- transform (CorrTransform or CovTransform) – Transform to apply to the derivative
to use the chain rule to compute the
derivative. Must be one of
CorrTransformorCovTransform.
Returns: Derivative of Log probability
Return type: float
-
dlogpdx(x) Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x) Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample(transform=None) Draws a random sample from the distribution and transform to the raw parameter values
Parameters: transform (CorrTransform or CovTransform) – Transform to apply to the sample. Must be one of CorrTransformorCovTransform. Note that for aWeakPriorobject this argument is optional as it is ignored, though derived classes require this argument.Returns: Raw random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.NormalPrior(mean, std) Normal Distribution Prior object
Admits input values from -inf/+inf.
Take two parameters: mean and std. Mean can take any numeric value, while std must be positive.
-
d2logpdx2(x) Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x) Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x) Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x() Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.LogNormalPrior(shape, scale) Normal Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape and scale, both of which must be positive
-
d2logpdx2(x) Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x) Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x) Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x() Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.GammaPrior(shape, scale) Gamma Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape \({\alpha}\) and scale \({\beta}\). Both must be positive, and they are defined such that
\({p(x) = \frac{\beta^{-\alpha}x^{\alpha - 1}}{\Gamma(/alpha)} \exp(-x/\beta)}\)
-
d2logpdx2(x) Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
dlogpdx(x) Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x) Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x() Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-
-
class
mogp_emulator.Priors.InvGammaPrior(shape, scale) Inverse Gamma Distribution Prior object
Admits input values from 0/+inf.
Take two parameters: shape \({\alpha}\) and scale \({\beta}\). Both must be positive, and they are defined such that
\({p(x) = \frac{\beta^{\alpha}x^{-\alpha - 1}}{\Gamma(/alpha)} \exp(-\beta/x)}\)
Note that
InvGammaPriorsupports both the usual distribution finding methodsdefault_prioras well as some fallback methods that use the mode of the distribution to set the parameters.-
d2logpdx2(x) Computes second derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Second derivative of Log probability Return type: float
-
classmethod
default_prior_corr_mode(inputs) Compute default priors on a set of inputs for the correlation length
Takes a set of inputs and computes the min and max spacing before calling the
default_prior_modemethod. This method is more stable than the standard default and is used as a fallback in the event that the usual method fails (which can happen if the inputs have too broad a range of spacing values).Parameters: inputs (ndarray) – Input values on which the distribution will be fit. Must be a 1D numpy array (note that 2D arrays will be flattened). Returns: InvGammaPrior distribution with fit parameters Return type: InvGammaPrior
-
classmethod
default_prior_mode(min_val, max_val) Compute default priors on a set of inputs for the correlation length
In some cases, the default correlation prior can fail to fit the distribution to the provided values. This method is more stable as it does not attempt to fit the lower bound of the distribution but instead fits the mode (which can be analytically related to the distribution parameters). The mode is chosen to be the the geometric mean of the min/max values and 99.5% of the mass is below the max value. This approach can fit distributions to wider ranges of parameters and is used as a fallback for correlation lengths and the default for the nugget (if the nugget is fit)
Parameters: - min_val (float) – Minimum value of the input spacing
- max_val (float) – Maximum value of the input spacing
Returns: InvGammaPrior distribution with fit parameters
Return type:
-
classmethod
default_prior_nugget(min_val=1e-08, max_val=1e-06) Compute default priors on a set of inputs for the nugget
Computes a distribution with given bounds using the
default_prior_modemethod. This method is more stable than the standard default and is used as a fallback in the event that the usual method fails (which can happen if the inputs have too broad a range of spacing values). Is well suited for the nugget, which in most cases is desired to be small.Parameters: - min_val (float) – Minimum value of the input spacing. Optional,
default is
1.e-8 - max_val (float) – Maximum value of the input spacing. Optional,
default is
1.e-6
Returns: InvGammaPrior distribution with fit parameters
Return type: - min_val (float) – Minimum value of the input spacing. Optional,
default is
-
dlogpdx(x) Computes derivative of log probability with respect to the transformed variable at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Derivative of Log probability Return type: float
-
logp(x) Computes log probability at a given value
Parameters: x (float) – Value of (transformed) variable Returns: Log probability Return type: float
-
sample_x() Draws a random sample from the distribution
Returns: Transformed random sample from the distribution Return type: float
-