class gpr::SexpatCF¶
Overview¶
Squared exponential covariance function. More…
#include <SexpatCF.h>
class SexpatCF {
public:
// structs
struct CrossBlockGeometry;
// construction
SexpatCF();
virtual ~SexpatCF();
// methods
double calculateLogPrior();
FieldMatrixd calculateLogPriorGradient();
void calculateCovarianceMatrix(const Coord& x1, Coord& x2, FieldMatrixd& C);
void calculateTrainingCovarianceMatrix(const Coord& x, FieldMatrixd& C);
void calculateGradOfCovMatrix(const Coord& x1, Coord& x2, std::vector<FieldMatrixd>& DKff);
void calculateGradOfCovMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff);
void calculateGradOfCovMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff);
void calculateDirectionalCovarianceMatrix(const Coord& x1, Coord& x2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
void calculateDirectionalCovarianceMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
void calculateDirectionalCovarianceMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second);
bool buildCrossBlockGeometry(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const;
bool evaluateCrossBlockGeometry(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, FieldMatrixd& out) const;
bool applyCrossBlockGeometryToVector(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, const Eigen::VectorXd& weights, bool transpose_field, Eigen::VectorXd& out) const;
void ginput2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff);
void ginput3(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff);
void ginput4(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff);
void setMagnSigma2(double value);
void setLengthScale(double value);
void setConfInfo(const AtomsConfiguration& value);
void setParameters(const Eigen::VectorXd& w);
double getMagnSigma2();
FieldMatrixd& getLengthScaleRef();
const FieldMatrixd& getLengthScaleRef() const;
void clear();
void setPriorParametersGaussian(const PriorBase& prior);
void setPriorParametersSqrtt(const PriorBase& prior);
PriorBase getPriorParametersGaussian();
PriorBase getPriorParametersSqrtt();
Eigen::VectorXd combineParameters();
double calculateDirectionalLogPriorCurvature(const Eigen::VectorXd& direction) const;
void cacheDistances(const Coord& x1, const Coord& x2);
void clearDistanceCache();
bool hasDistanceCache() const;
};
Detailed Documentation¶
Squared exponential covariance function.
Methods¶
double calculateLogPrior()
Evaluate the log prior density of the parameters.
This function takes a covariance function structure GPCF and returns log(p(th)), where th collects the parameters. This is a mandatory subfunction used for example in energy computations.
Evaluate the prior contribution to the error. The parameters that are sampled are transformed, e.g., W = log(w) where w is all the “real” samples. On the other hand errors are evaluated in the W-space so we need take into account also the Jacobian of transformation, e.g., W -> w = exp(W). See Gelman et al. (2013), Bayesian Data Analysis, third edition, p. 21.
FieldMatrixd calculateLogPriorGradient()
Evaluate the gradient of the log prior density of the parameters with respect to parameters.
This function takes a covariance function structure GPCF and returns LPG = d log (p(th))/dth, where th is the vector of parameters. This is a mandatory subfunction used in gradient computations.
Parameters:
lpg |
void calculateCovarianceMatrix(const Coord& x1, Coord& x2, FieldMatrixd& C)
Evaluate covariance matrix between two input vectors.
The function takes in covariance function of a Gaussian process GP and two matrixes TX and X that contain input vectors to GP. Returns covariance matrix C. Every element ij of C contains covariance between inputs i in TX and j in X. This is a mandatory subfunction used for example in prediction and energy computations.
Parameters:
x1 |
??? |
x2 |
??? |
C |
Covariance matrix |
void calculateTrainingCovarianceMatrix(const Coord& x, FieldMatrixd& C)
Evaluate training variance vector.
Function takes in covariance function of a Gaussian process GPCF and matrix TX that contains training inputs. Returns variance vector C. Every element i of C contains variance of input i in TX. This is a mandatory subfunction used for example in prediction and energy computations.
Parameters:
x |
??? |
C |
??? |
void calculateGradOfCovMatrix(const Coord& x1, Coord& x2, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function with respect to the parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of covariance matrix Kff = k(X,X) with respect to th (cell array with matrix elements). This is a mandatory subfunction used for example in gradient computations.
Parameters:
x2 |
|
x2 |
|
DKff |
void calculateGradOfCovMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function, of which has been taken partial derivative with respect to x1, with respect to parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of derivatived covariance matrix dK(df,f)/dhyp = d(d k(X,X)/dx)/dhyp, with respect to the parameters.
- Evaluate: DKff{1:m} = d Kff / d magnSigma2 DKff{m+1:2m} = d Kff / d lengthScale_m m is the dimension of inputs. If ARD is used, then multiple lengthScales. This subfunction is needed when using derivative observations. dims - is a vector of input dimensions with respect to which the
derivatives of the covariance function have been calculated [by default dims=1:size(x,2)]
Note! When coding the derivatives of the covariance function, remember to double check them. See gp_cov for lines of code to check the matrices
Parameters:
x1 |
|
x2 |
|
dims |
|
DKff |
void calculateGradOfCovMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function, of which has been taken partial derivatives with respect to both input variables x1 and x2 with respect to parameters.
The function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of derivative covariance matrix dK(df,df)/dhyp = d(d^2 k(X1,X2)/dX1dX2)/dhyp with respect to the parameters.
Evaluate: DKff{1-m} = d Kff / d magnSigma2 DKff{m+1-2m} = d Kff / d lengthScale_m m is the dimension of inputs. If ARD is used, then multiple lengthScales. This subfunction is needed when using derivative observations.
Parameters:
x1 |
|
x2 |
|
dims1 |
|
dims2 |
|
DKff |
void calculateDirectionalCovarianceMatrix(const Coord& x1, Coord& x2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate the directional first and second derivatives of the covariance matrix along a parameter-space direction.
The direction is expressed in the packed log-parameter coordinates returned by combineParameters().
void calculateDirectionalCovarianceMatrixWithDerivatives(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate directional first and second derivatives of the derivative-vs-value covariance block.
void calculateDirectionalCovarianceMatrixWithDerivatives2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, const Eigen::VectorXd& direction, FieldMatrixd& first, FieldMatrixd& second)
Evaluate directional first and second derivatives of the derivative-vs-derivative covariance block.
bool buildCrossBlockGeometry(const Coord& x1, const Coord& x2, uint8_t derivative_options, const FieldMatrixIdx& dims1, const FieldMatrixIdx& dims2, CrossBlockGeometry& geometry) const
Build theta-independent pairtype geometry for a covariance block.
The stored fields are raw pairtype contributions. They can be evaluated at any packed Sexpat log-parameter vector without re-walking atomic coordinates.
bool evaluateCrossBlockGeometry(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, FieldMatrixd& out) const
Evaluate a cross-block geometry at packed Sexpat log parameters.
bool applyCrossBlockGeometryToVector(const CrossBlockGeometry& geometry, const Eigen::VectorXd& theta, const Eigen::VectorXd& weights, bool transpose_field, Eigen::VectorXd& out) const
Apply a cross-block geometry directly to a vector.
When transpose_field is false, computes out = A(theta)^T * weights. When it is true, computes out = A(theta) * weights.
void ginput2(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff)
Calculates covariances between the derivatives.
Parameters:
x1 |
Matrix of coordinates |
x2 |
Matrix of coordinates |
dims |
|
DKff |
Covariances between the derivatives |
void ginput3(const Coord& x1, Coord& x2, FieldMatrixIdx& dims1, FieldMatrixIdx& dims2, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function with respect to both input variables x1 and x2 (in different dimensions).
This function takes a covariance function structure GPCF, a matrix X of input vectors and returns DKff, the gradients of twice derivatived covariance matrix K(df,df) = dk(X1,X2)/dX1dX2 (cell array with matrix elements). The derivative is calculated in multidimensional problem between input’s observation dimensions which are not same. This subfunction is needed when using derivative observations.
Parameters:
x1 |
|
x2 |
|
dims1 |
|
dims2 |
|
DKff |
void ginput4(const Coord& x1, Coord& x2, FieldMatrixIdx& dims, std::vector<FieldMatrixd>& DKff)
Evaluate gradient of covariance function with respect to x.
Simplified and faster version of sexp_ginput, returns full matrices.
Returns DKff, the gradients of covariance matrix Kff = k(X,X2) with respect to dimensions DIMS of X.
Parameters:
x1 |
|
x2 |
|
dims |
|
DKff |
void setParameters(const Eigen::VectorXd& w)
Set parameters of this covariance function using the provided vector.
Parameters:
w |
Vector of combined parameters from covariance functions and likelyhood |
Eigen::VectorXd combineParameters()
Combine parameters of the covariance function into one Eigen vector.
double calculateDirectionalLogPriorCurvature(const Eigen::VectorXd& direction) const
Evaluate the second directional derivative of log p(theta) along the packed log-parameter direction.
Pre-compute and cache inverse distances for the given positions.
Call once before an optimization loop. Subsequent covariance matrix evaluations will use the cached 1/r values, avoiding redundant coordinate extractions and norm computations.
void clearDistanceCache()
Invalidate the distance cache.
Call when positions change (e.g. after optimization completes and new observations are added).