Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
FitAbstraction.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <functional>
6#include <limits>
7#include <memory>
8#include <optional>
9#include <sstream>
10#include <stdexcept>
11#include <string>
12#include <utility>
13#include <vector>
14
15#include "Matrix.h"
16
17namespace fit_app {
18
20 std::string name;
21 double value = 0.0;
22 double step_hint = 0.0;
23 std::optional<std::pair<double, double>> limits;
24 bool fixed = false;
25};
26
27struct FitOptions {
28 double up = 0.5;
29 unsigned strategy = 2;
30 unsigned max_fcn = 100000;
31 double tolerance = 0.2;
32 bool run_hesse = true;
33 unsigned hesse_maxcalls = 0;
34 bool verbose = true;
35};
36
38 double up = 0.5;
39 unsigned npoints = 80;
40 unsigned strategy = 2;
41 unsigned max_fcn = 30000;
42 double tolerance = 0.2;
43};
44
45// struct FitDiagnostics {
46// double fmin = std::numeric_limits<double>::quiet_NaN();
47// double edm = std::numeric_limits<double>::quiet_NaN();
48// int nfcn = -1;
49
50// bool ok = false;
51// bool has_valid_covar = false;
52// bool has_posdef_covar = false;
53// bool has_accurate_covar = false;
54// bool made_posdef = false;
55
56// std::vector<double> cov_eigs;
57// double cond_number = std::numeric_limits<double>::infinity();
58// };
59
61 double fmin = std::numeric_limits<double>::quiet_NaN();
62 double edm = std::numeric_limits<double>::quiet_NaN();
63 int nfcn = -1;
64
65 bool ok = false;
66
68 bool has_valid_covar = false;
69 bool has_posdef_covar = false;
70 bool has_accurate_covar = false;
71 bool made_posdef = false;
72 bool hesse_failed = false;
73
74 bool reached_call_limit = false;
75 bool above_max_edm = false;
76
77 std::vector<double> cov_eigs;
78 double cond_number = std::numeric_limits<double>::infinity();
79};
80
82public:
83 virtual ~BackendState() = default;
84};
85
87 std::vector<double>values;
88 std::vector<double> errors;
91 std::shared_ptr<const BackendState> state;
92};
93
95 bool success = false;
96 std::vector<std::pair<double, double>> points;
97};
98
100public:
101 virtual ~IObjectiveFunction() = default;
102 virtual double operator()(const std::vector<double>& x) const = 0;
103 virtual double error_definition() const = 0;
104};
105
107public:
108 LambdaObjectiveFunction(std::function<double(const std::vector<double>&)> fn, double up)
109 : fn_(std::move(fn)), up_(up) {}
110
111 double operator()(const std::vector<double>& x) const override {
112 return fn_(x);
113 }
114
115 double error_definition() const override {
116 return up_;
117 }
118
119private:
120 std::function<double(const std::vector<double>&)> fn_;
121 double up_ = 0.5;
122};
123
125public:
126 virtual ~IFitBackend() = default;
127
129 const std::vector<ParameterDefinition>& parameters,
130 const FitOptions& options) const = 0;
131
133 const std::vector<ParameterDefinition>& parameters,
134 const FitOptions& options,
135 const std::vector<std::size_t>& fixed_indices,
136 const std::vector<double>& fixed_values) const = 0;
137
139 const BackendFitResult& reference_fit,
140 std::size_t x_index,
141 std::size_t y_index,
142 const ContourOptionsBackEnd& options) const = 0;
143};
144
145std::unique_ptr<IFitBackend> make_minuit_backend();
146
147template <class T>
148std::string to_string_any(const T& x) {
149 std::ostringstream oss;
150 oss << x;
151 return oss.str();
152}
153
154inline std::vector<double> linspace(double a, double b, std::size_t n) {
155 std::vector<double> out(n);
156 if (n == 0) return out;
157 if (n == 1) {
158 out[0] = a;
159 return out;
160 }
161 for (std::size_t i = 0; i < n; ++i) {
162 out[i] = a + (b - a) * double(i) / double(n - 1);
163 }
164 return out;
165}
166
167inline double safe_step(double value, double scale_hint) {
168 const double abs_value = std::fabs(value);
169 const double abs_scale = std::fabs(scale_hint);
170
171 double step = 0.0;
172 if (std::isfinite(abs_scale) && abs_scale > 0.0) step = 0.05 * abs_scale;
173 if (std::isfinite(abs_value) && abs_value > 0.0) step = std::max(step, 0.01 * abs_value);
174 if (!std::isfinite(step) || step <= 0.0) step = 1e-3;
175 return step;
176}
177
178} // namespace fit_app
Lightweight dense real-matrix utilities built on top of STL storage and GSL backends.
virtual ~BackendState()=default
virtual ~IFitBackend()=default
virtual BackendFitResult minimize_with_fixed(const IObjectiveFunction &objective, const std::vector< ParameterDefinition > &parameters, const FitOptions &options, const std::vector< std::size_t > &fixed_indices, const std::vector< double > &fixed_values) const =0
virtual BackendFitResult minimize(const IObjectiveFunction &objective, const std::vector< ParameterDefinition > &parameters, const FitOptions &options) const =0
virtual BackendContourResult contour(const IObjectiveFunction &objective, const BackendFitResult &reference_fit, std::size_t x_index, std::size_t y_index, const ContourOptionsBackEnd &options) const =0
virtual ~IObjectiveFunction()=default
virtual double operator()(const std::vector< double > &x) const =0
virtual double error_definition() const =0
double error_definition() const override
LambdaObjectiveFunction(std::function< double(const std::vector< double > &)> fn, double up)
double operator()(const std::vector< double > &x) const override
std::unique_ptr< IFitBackend > make_minuit_backend()
std::string to_string_any(const T &x)
std::vector< double > linspace(double a, double b, std::size_t n)
double safe_step(double value, double scale_hint)
Hash specialization for SymbolId<Tag>.
Definition BlockName.h:353
double T(double x)
Wilson coefficient T(x).
std::vector< std::pair< double, double > > points
std::vector< double > errors
std::vector< double > values
std::shared_ptr< const BackendState > state
std::vector< double > cov_eigs
std::optional< std::pair< double, double > > limits