7 std::shared_ptr<LikelihoodContext> ctx,
12 covariance_inv_(
std::move(covariance_inv))
16 if (theta.size() !=
p_dim) {
17 throw std::invalid_argument(
"ChiSquaredLikelihood::nll: theta size must equal p_dim");
21 const std::vector<double> eta_empty;
22 std::vector<double> pred =
model(theta, eta_empty);
24 if (pred.size() !=
ctx->exp_obs_values.size()) {
25 throw std::runtime_error(
"ChiSquaredLikelihood::nll: prediction/observation size mismatch");
27 if (covariance_inv_.
rows() != pred.size() || covariance_inv_.
cols() != pred.size()) {
28 throw std::runtime_error(
"ChiSquaredLikelihood::nll: covariance inverse dimension mismatch");
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])) {
40 for (std::size_t i = 0; i < r.size(); ++i) {
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)) {
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";
58 std::cout <<
"[CHI2DBG] nll unknown exception\n";
68 const std::vector<double>&
70 return covariance_inv_;
74 const std::vector<double>& eta
77 throw std::invalid_argument(
"ChiSquaredLikelihood::nuisance_curvature: eta must be empty");
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.
double & at(size_t i, size_t j)
Returns a mutable reference to element (i,j) with bounds checking.
std::size_t cols() const
Returns the number of columns.
Hash specialization for SymbolId<Tag>.