Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
ChiSquaredLikelihood.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
6 const ModelFn& model,
7 std::shared_ptr<LikelihoodContext> ctx,
8 std::size_t p_dim,
9 RealMatrix covariance_inv
10)
11 : BaseLikelihood(model, std::move(ctx), p_dim),
12 covariance_inv_(std::move(covariance_inv))
13{}
14
15double ChiSquaredLikelihood::nll(const std::vector<double>& theta) const {
16 if (theta.size() != p_dim) {
17 throw std::invalid_argument("ChiSquaredLikelihood::nll: theta size must equal p_dim");
18 }
19
20 try {
21 const std::vector<double> eta_empty;
22 std::vector<double> pred = model(theta, eta_empty);
23
24 if (pred.size() != ctx->exp_obs_values.size()) {
25 throw std::runtime_error("ChiSquaredLikelihood::nll: prediction/observation size mismatch");
26 }
27 if (covariance_inv_.rows() != pred.size() || covariance_inv_.cols() != pred.size()) {
28 throw std::runtime_error("ChiSquaredLikelihood::nll: covariance inverse dimension mismatch");
29 }
30
31 std::vector<double> r(pred.size(), 0.0);
32 for (std::size_t i = 0; i < pred.size(); ++i) {
33 r[i] = pred[i] - ctx->exp_obs_values[i];
34 if (!std::isfinite(r[i])) {
35 return 1e100;
36 }
37 }
38
39 double q = 0.0;
40 for (std::size_t i = 0; i < r.size(); ++i) {
41 double row = 0.0;
42 for (std::size_t j = 0; j < r.size(); ++j) {
43 const double cij = covariance_inv_.at(i, j);
44 if (!std::isfinite(cij)) {
45 return 1e100;
46 }
47 row += cij * r[j];
48 }
49 q += r[i] * row;
50 }
51
52 const double out = 0.5 * q;
53 return std::isfinite(out) ? out : 1e100;
54 } catch (const std::exception& e) {
55 std::cout << "[CHI2DBG] nll exception: " << e.what() << "\n";
56 return 1e100;
57 } catch (...) {
58 std::cout << "[CHI2DBG] nll unknown exception\n";
59 return 1e100;
60 }
61}
62
63std::size_t ChiSquaredLikelihood::dim() const {
64 return p_dim;
65}
66
68 const std::vector<double>&
69) const {
70 return covariance_inv_;
71}
72
74 const std::vector<double>& eta
75) const {
76 if (!eta.empty()) {
77 throw std::invalid_argument("ChiSquaredLikelihood::nuisance_curvature: eta must be empty");
78 }
79 return RealMatrix(0, 0);
80}
std::function< std::vector< double >(const std::vector< double > &p, const std::vector< double > &eta)> ModelFn
Model function signature used by BaseLikelihood.
Chi-square likelihood implementation for models without explicit nuisance parameters.
Default implementation of a profileable negative log-likelihood.
std::shared_ptr< LikelihoodContext > ctx
Shared statistical context used by the likelihood.
ModelFn model
Model function evaluated by the likelihood.
std::size_t p_dim
Dimension of the fitted-parameter block.
double nll(const std::vector< double > &theta) const override
Evaluates the chi-square negative log-likelihood.
RealMatrix observable_curvature(const std::vector< double > &r) const override
Returns the observable-space curvature matrix.
ChiSquaredLikelihood(const ModelFn &model, std::shared_ptr< LikelihoodContext > ctx, std::size_t p_dim, RealMatrix covariance_inv)
Constructs a chi-square likelihood.
std::size_t dim() const override
Returns the total optimization dimension.
RealMatrix nuisance_curvature(const std::vector< double > &eta) const override
Returns the nuisance-space curvature matrix.
std::size_t rows() const
Returns the number of rows.
Definition Matrix.cpp:601
double & at(size_t i, size_t j)
Returns a mutable reference to element (i,j) with bounds checking.
Definition Matrix.cpp:587
std::size_t cols() const
Returns the number of columns.
Definition Matrix.cpp:605
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353