namespace gpr::bo¶
Overview¶
namespace bo {
// namespaces
namespace gpr::bo::testing;
// typedefs
typedef std::function<Eigen::VectorXd(const Eigen::Ref<const Eigen::VectorXd>&)> MeanBasis;
typedef Eigen::AutoDiffScalar<Eigen::Matrix<Inner, 1, 1>> Dual;
// enums
enum BayesianQuadratureBasis;
enum BayesianQuadratureHyperMode;
enum BayesianQuadratureKernel;
enum BayesianQuadratureMeasureKind;
enum MeanBasisKind;
enum ParameterAcquisition;
enum RadialKind;
// structs
struct BayesianQuadratureCalibration;
struct BayesianQuadratureConfig;
struct BayesianQuadratureCumulative;
struct BayesianQuadratureFitReport;
struct BayesianQuadratureIntegral;
struct BayesianQuadratureIntegralReport;
struct BayesianQuadratureMeasure;
struct BayesianQuadratureSelection;
struct FreeEnergyFitReport;
struct FreeEnergyObservations;
struct FreeEnergyPathPosterior;
struct FreeEnergySurrogateConfig;
struct KramersRateConfig;
struct KramersRateSummary;
struct MaternMixture;
struct ParameterBound;
struct ParameterObservation;
struct ParameterSearchConfig;
struct ParameterSearchResult;
struct ScalarFitBounds;
struct ScalarFitConfig;
struct ScalarFitResult;
struct ScalarHyperparameters;
struct ScalarLikelihoodData;
// classes
class BayesianQuadratureCholeskyError;
class BayesianQuadratureInferenceError;
class BayesianQuadratureModel;
class BayesianQuadratureNumericalError;
class FreeEnergyCholeskyError;
class FreeEnergyInferenceError;
class FreeEnergyRateUnsupportedError;
class FreeEnergySaddleNotFoundError;
class FreeEnergySurrogate;
class Matern32;
class Matern52;
class ParameterSearch;
template <typename Left, typename Right>
class ProductRadial;
class RadialShape;
template <typename Kernel>
class ScalarGaussianProcess;
class ScalarSurrogate;
class ScaledEuclideanDistance;
template <typename Radial>
class ScaledRadial;
class SplitMix64;
class SquaredExponential;
template <typename Distance, typename Radial>
class StationaryKernel;
template <typename Left, typename Right>
class SumRadial;
// global variables
double kMaxProductMixtureNodes = 2.0e5;
// global functions
double squaredExponentialBoxMean(const Eigen::VectorXd& point, const Eigen::VectorXd& lower, const Eigen::VectorXd& upper, const Eigen::VectorXd& lengthscales);
double squaredExponentialBoxDouble(const Eigen::VectorXd& lower, const Eigen::VectorXd& upper, const Eigen::VectorXd& lengthscales);
double squaredExponentialBoxCross(const Eigen::VectorXd& lower, const Eigen::VectorXd& first_upper, const Eigen::VectorXd& second_upper, const Eigen::VectorXd& lengthscales);
double maternMixtureUnit(double squared_distance, double nu, const Eigen::VectorXd& nodes, const Eigen::VectorXd& weights);
double analyticUnitMatern(double squared_distance, double nu);
void gaussLegendreInterval(int order, std::vector<double>& nodes, std::vector<double>& weights);
MaternMixture maternMixture(double nu);
const MaternMixture& matern32Mixture();
const MaternMixture& matern52Mixture();
double mixtureScale(double node, double nu);
double maternMixtureBoxMean(const MaternMixture& rule, const Eigen::VectorXd& point, const Eigen::VectorXd& lower, const Eigen::VectorXd& upper, const Eigen::VectorXd& lengthscales);
double maternMixtureBoxDouble(const MaternMixture& rule, const Eigen::VectorXd& lower, const Eigen::VectorXd& upper, const Eigen::VectorXd& lengthscales);
double maternMixtureBoxCross(const MaternMixture& rule, const Eigen::VectorXd& lower, const Eigen::VectorXd& first_upper, const Eigen::VectorXd& second_upper, const Eigen::VectorXd& lengthscales);
void requireSymmetricCovariance(const Eigen::MatrixXd& covariance);
double squaredExponentialGaussianMean(const Eigen::VectorXd& point, const Eigen::VectorXd& mean, const Eigen::MatrixXd& covariance, const Eigen::VectorXd& lengthscales);
double squaredExponentialGaussianDouble(const Eigen::MatrixXd& covariance, const Eigen::VectorXd& lengthscales);
double squaredExponentialGaussian1D(double point, double mean, double variance, double lengthscale);
double squaredExponentialGaussianDouble1D(double variance, double lengthscale);
std::vector<Eigen::Index> activeMixtureNodes(const MaternMixture& rule);
void requireProductMixtureBudget(const MaternMixture& rule, Eigen::Index dimension);
template <class Visitor>
void forEachProductMixtureNode(const MaternMixture& rule, const std::vector<Eigen::Index>& active, Eigen::Index dimension, Visitor&& visit);
Eigen::VectorXd maternProductGaussianMeans(const MaternMixture& rule, const Eigen::MatrixXd& points, const Eigen::VectorXd& mean, const Eigen::MatrixXd& covariance, const Eigen::VectorXd& lengthscales);
double maternProductGaussianDouble(const MaternMixture& rule, const Eigen::MatrixXd& covariance, const Eigen::VectorXd& lengthscales);
bool covarianceIsDiagonal(const Eigen::MatrixXd& covariance);
double expectedImprovementScore(double mu, double mu_best, double sigma);
double expectedImprovementForMinimum(double mu, double best, double sigma);
double probabilityOfFeasibility(double mu, double sigma, double threshold = 0.0);
double constrainedExpectedImprovementForMinimum(double mu, double best, double sigma, double constraint_mu, double constraint_sigma);
double lowerConfidenceBound(double mu, double sigma, double kappa);
Eigen::VectorXd freeEnergyNoiseFromWeights(const Eigen::VectorXd& weights, double reference_variance, double min_relative_weight);
bool isDiscrete(const ParameterBound& bound);
void validateBound(const ParameterBound& bound);
double unitToParameter(double unit, const ParameterBound& bound);
double parameterToUnit(double parameter, const ParameterBound& bound);
double snapUnit(double unit, const ParameterBound& bound);
template <typename Objective, typename Kernel>
ParameterSearchResult minimize(Objective&& objective, const std::vector<ParameterBound>& bounds, const ParameterSearchConfig& config, const Kernel& kernel);
template <typename Objective>
ParameterSearchResult minimize(Objective&& objective, const std::vector<ParameterBound>& bounds, const ParameterSearchConfig& config);
ScalarFitBounds defaultScalarFitBounds(const ScalarLikelihoodData& data);
ScalarFitResult fitScalarHyperparameters(const ScalarLikelihoodData& data, const ScalarHyperparameters& initial, const ScalarFitConfig& config = {});
Eigen::Index scalarParameterCount(const ScalarLikelihoodData& data);
double scalarLogMarginalLikelihood(const ScalarLikelihoodData& data, const ScalarHyperparameters& hyper, Eigen::VectorXd* gradient = nullptr);
void fillStandardNormal(SplitMix64& generator, Eigen::VectorXd& normal);
double kernelMean1D(const SquaredExponential& radial, double point, double lower, double upper, double lengthscale);
double kernelMean1D(const Matern32& radial, double point, double lower, double upper, double lengthscale);
double kernelMean1D(const Matern52& radial, double point, double lower, double upper, double lengthscale);
double kernelDoubleIntegral1D(const SquaredExponential& radial, double lower, double upper, double lengthscale);
double kernelDoubleIntegral1D(const Matern32& radial, double lower, double upper, double lengthscale);
double kernelDoubleIntegral1D(const Matern52& radial, double lower, double upper, double lengthscale);
template <class Radial>
double kernelMean1DLogLengthDerivative(const Radial& radial, double point, double lower, double upper, double lengthscale);
template <class Radial>
double kernelDoubleIntegral1DLogLengthDerivative(const Radial& radial, double lower, double upper, double lengthscale);
template <typename Radial>
ScaledRadial<Radial> scaledRadial(double scale, Radial radial);
template <typename Left, typename Right>
SumRadial<Left, Right> sumRadial(Left left, Right right);
template <typename Left, typename Right>
ProductRadial<Left, Right> productRadial(Left left, Right right);
template <typename Distance, typename Radial>
StationaryKernel<Distance, Radial> stationaryKernel(Distance distance, Radial radial);
template <typename T, typename Value>
T lift(const Value& value);
} // namespace bo
Detailed Documentation¶
Typedefs¶
typedef std::function<Eigen::VectorXd(const Eigen::Ref<const Eigen::VectorXd>&)> MeanBasis
Basis functions phi(x) of the parametric mean. The returned vector has the same length at every x.
Global Variables¶
double kMaxProductMixtureNodes = 2.0e5
Largest number of joint mixture nodes the product-Matérn moments under a non-diagonal Gaussian will evaluate. With the nodes of the accurate one-dimensional rule this admits two dimensions and no more.
Global Functions¶
MaternMixture maternMixture(double nu)
Plain generalized Gauss–Laguerre at a few hundred nodes leaves a relative kernel error near 5e-5, because exp(-a/t) is not a polynomial in t. The substitution t = exp(u) makes the gamma-mixture integrand smooth, and a fixed Gauss–Legendre panel rule then matches the closed-form Matérn to a small multiple of machine epsilon. The window and panel count are the smallest values in the search below that meet that test.
void requireSymmetricCovariance(const Eigen::MatrixXd& covariance)
A covariance is read through its lower triangle by the Cholesky factor, so an asymmetric matrix would be integrated as a different, symmetric one. Reject it instead. The tolerance is roundoff of the matrix’s own scale.
std::vector<Eigen::Index> activeMixtureNodes(const MaternMixture& rule)
Indices of the mixture nodes whose weight is above the cut that keeps the pruned mass below the kernel error budget.
template <class Visitor>
void forEachProductMixtureNode(const MaternMixture& rule, const std::vector<Eigen::Index>& active, Eigen::Index dimension, Visitor&& visit)
Visits every joint node of a product mixture. visit(weight, scales) receives the product weight and the per-axis scale factors.
Eigen::VectorXd maternProductGaussianMeans(const MaternMixture& rule, const Eigen::MatrixXd& points, const Eigen::VectorXd& mean, const Eigen::MatrixXd& covariance, const Eigen::VectorXd& lengthscales)
Product Matérn kernel mean under a Gaussian with any covariance. Each axis is a gamma scale mixture of squared exponentials, so the product is a joint mixture of squared exponentials with diagonal scales, and each term has the closed full-covariance Gaussian moment.
double expectedImprovementScore(double mu, double mu_best, double sigma)
Jones expected improvement for a maximizer.
EI = (mu - mu_best) Phi(z) + sigma phi(z), with z = (mu - mu_best) / sigma. The score is zero when sigma is not positive or the inputs are not finite.
double expectedImprovementForMinimum(double mu, double best, double sigma)
Expected improvement for a minimizer.
The maximizer score on negated values. best is the smallest loss seen so far.
double probabilityOfFeasibility(double mu, double sigma, double threshold = 0.0)
Probability that a normal constraint value is feasible.
P(c <= threshold) for c normal with mean mu and standard deviation sigma, which is Phi((threshold - mu) / sigma). A zero sigma gives one when mu is at most the threshold and zero otherwise. A non-finite input or a negative sigma gives zero.
double constrainedExpectedImprovementForMinimum(double mu, double best, double sigma, double constraint_mu, double constraint_sigma)
Constrained expected improvement for a minimizer.
Expected improvement of the loss times the probability that an independent constraint model is feasible: Gardner et al. (2014) and Gelbart, Snoek and Adams (2014). The constraint is feasible at or below zero.
double lowerConfidenceBound(double mu, double sigma, double kappa)
Lower confidence bound for a minimizer, mu - kappa * sigma.
A non-finite input returns +infinity so a minimizer skips the point.
Eigen::VectorXd freeEnergyNoiseFromWeights(const Eigen::VectorXd& weights, double reference_variance, double min_relative_weight)
noise[i] = reference * max(w) / max(w[i], min_relative * max(w)). Scale-invariant in the weights. Every weight is positive and finite, reference is positive and finite, and min_relative lies in (0, 1]. Throws std::domain_error when a noise variance overflows.
bool isDiscrete(const ParameterBound& bound)
True for an integer axis or an axis with allowed values.
void validateBound(const ParameterBound& bound)
Throw std::invalid_argument unless the axis is well formed.
double unitToParameter(double unit, const ParameterBound& bound)
Map a unit-cube coordinate to the parameter of its axis.
An axis with k allowed values splits [0, 1] into k equal cells. An integer axis maps as the continuous one does and rounds to the nearest whole number in the interval.
double parameterToUnit(double parameter, const ParameterBound& bound)
Unit-cube coordinate of a parameter, the inverse of unitToParameter().
Allowed value i of k sits at i / (k - 1), and at 0.5 when k is one. A parameter that is not an allowed value takes the coordinate of the nearest one. Other axes are clamped to [0, 1].
double snapUnit(double unit, const ParameterBound& bound)
The unit-cube coordinate of the lattice point a coordinate rounds to. A continuous axis returns the clamped coordinate.
template <typename Objective>
ParameterSearchResult minimize(Objective&& objective, const std::vector<ParameterBound>& bounds, const ParameterSearchConfig& config)
Default kernel: one length scale on every unit-cube axis, squared exponential. A categorical axis is compared for equality.
ScalarFitBounds defaultScalarFitBounds(const ScalarLikelihoodData& data)
Default box of the fit, from the spread of the observations.
Length scale of an axis: 0.02 to 20 times the range of that column of x. Signal variance: 1e-4 to 1e3 times the sample variance of y. Noise variance: 1e-8 to 10 times the sample variance of y. A column or a y without spread uses one as its scale.
ScalarFitResult fitScalarHyperparameters(const ScalarLikelihoodData& data, const ScalarHyperparameters& initial, const ScalarFitConfig& config = {})
Maximize the log marginal likelihood over the hyperparameters.
Every start runs the rgmin L-BFGS solver on the logarithms of the hyperparameters with the analytic gradient, inside the box. The start with the largest likelihood is returned. A noise variance supplied per row is kept and only the length scales and the signal variance move.
Parameters:
initial |
First start, clipped into the box. A single length scale applies to every axis. |
std::invalid_argument |
for inconsistent sizes or an empty box. |
std::runtime_error |
when no start has a finite likelihood. |
Eigen::Index scalarParameterCount(const ScalarLikelihoodData& data)
Number of fitted parameters: the length scales, the signal variance and, without per-row variances, the noise variance.
double scalarLogMarginalLikelihood(const ScalarLikelihoodData& data, const ScalarHyperparameters& hyper, Eigen::VectorXd* gradient = nullptr)
Log marginal likelihood log p(y | X, theta) and its gradient.
With K = k(X, X) + diag(noise), the value is -1/2 y^T K^-1 y - 1/2 log|K| - n/2 log(2 pi). With a basis H of p columns it is -1/2 y^T P y - 1/2 log|K| - 1/2 log|H^T K^-1 H|
(n - p)/2 log(2 pi), P = K^-1 - K^-1 H (H^T K^-1 H)^-1 H^T K^-1.
Parameters:
gradient |
Optional. Receives scalarParameterCount() derivatives with respect to the logarithms of the length scales, of the signal variance and, without per-row variances, of the noise variance, in that order: 1/2 tr((a a^T - P) dK/dtheta), a = P y. |
Returns:
The value, or minus infinity when K or H^T K^-1 H is not positive definite. The gradient is then not written.
void fillStandardNormal(SplitMix64& generator, Eigen::VectorXd& normal)
Independent standard normals, two per pair of uniforms (Box-Muller).