Loading...
Searching...
No Matches
BayesianModelAveraging.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
17
18#ifndef GPR_ML_BAYESIANMODELAVERAGING_H_
19#define GPR_ML_BAYESIANMODELAVERAGING_H_
20
21#include <Eigen/Dense>
22#include <functional>
23#include <vector>
24
26
27namespace gpr {
28
33 Eigen::VectorXd mean; // BMA-averaged mean prediction
34 Eigen::VectorXd
35 variance; // BMA-averaged variance (includes hp uncertainty)
36 int n_models; // number of models in the average
37};
38
51public:
61 std::function<std::tuple<EigenMatrix, EigenMatrix, Eigen::VectorXd>(
62 double lengthscale, double noise)>;
63
74 void setGrid(double log_ls_min, double log_ls_max, int n_ls,
75 double log_noise_min, double log_noise_max, int n_noise);
76
86 void fit(const Eigen::VectorXd& y, KernelBuilder kernel_builder,
87 bool use_tp = false, double tp_a = 1e-20, double tp_b = 1e-20);
88
96 BMAPrediction predict(const Eigen::VectorXd& y,
97 KernelBuilder kernel_builder) const;
98
99 int totalModels() const
100 {
101 return static_cast<int>(weights_.size());
102 }
103
105 const std::vector<double>& getWeights() const
106 {
107 return weights_;
108 }
109
110private:
111 // Grid specification
112 std::vector<double> log_ls_grid_;
113 std::vector<double> log_noise_grid_;
114
115 // Posterior weights (normalized)
116 std::vector<double> weights_;
117
118 // Grid point parameters (flattened: [ls0_n0, ls0_n1, ..., ls1_n0, ...])
119 std::vector<std::pair<double, double>> grid_params_;
120};
121
122} // namespace gpr
123
124#endif // GPR_ML_BAYESIANMODELAVERAGING_H_
@ y
Definition Enums.h:28
Bayesian model averaging over hyperparameter grid.
const std::vector< double > & getWeights() const
Access posterior weights (for testing parallel correctness).
void fit(const Eigen::VectorXd &y, KernelBuilder kernel_builder, bool use_tp=false, double tp_a=1e-20, double tp_b=1e-20)
Evaluate likelihoods on the grid and compute posterior weights.
BMAPrediction predict(const Eigen::VectorXd &y, KernelBuilder kernel_builder) const
BMA-averaged prediction at test points.
std::function< std::tuple< EigenMatrix, EigenMatrix, Eigen::VectorXd >( double lengthscale, double noise)> KernelBuilder
Build function: given (lengthscale, noise) -> covariance matrix K.
void setGrid(double log_ls_min, double log_ls_max, int n_ls, double log_noise_min, double log_noise_max, int n_noise)
Configure the hyperparameter grid.
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Prediction result from Bayesian model averaging.