Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
example_manager.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <fstream>
3#include <iomanip>
4#include <iostream>
5#include <map>
6#include <memory>
7#include <set>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "StatisticManager.h"
13#include "ObservableInterface.h"
16#include "StatParameterProxy.h"
19#include "FitAbstraction.h"
20#include "NuisanceReader.h"
22
23namespace {
24
25using Path = std::vector<std::pair<double, double>>;
26
27void print_fit_result(const FitResultWithMaps& fit) {
28 std::cout << std::setprecision(17);
29 std::cout << "fit_ok = " << fit.fit_ok << "\n";
30 std::cout << "ell_hat = " << fit.ell_hat << "\n";
31
32 std::cout << "\np_hat:\n";
33 for (const auto& [pid, val] : fit.p_hat) {
34 std::cout << " " << fit_app::to_string_any(pid) << " = " << val << "\n";
35 }
36
37 std::cout << "\neta_hat:\n";
38 for (const auto& [pid, val] : fit.eta_hat) {
39 std::cout << " " << fit_app::to_string_any(pid) << " = " << val << "\n";
40 }
41
42 std::cout << "\np_hat_std:\n";
43 for (const auto& [pid, val] : fit.p_hat_std) {
44 std::cout << " " << fit_app::to_string_any(pid) << " = " << val << "\n";
45 }
46
47 std::cout << "\np_correlations:\n";
48 for (const auto& [pi, row] : fit.p_correlations) {
49 for (const auto& [pj, corr] : row) {
50 std::cout << " corr(" << fit_app::to_string_any(pi)
51 << ", " << fit_app::to_string_any(pj)
52 << ") = " << corr << "\n";
53 }
54 }
55}
56
57void save_bestfit_csv(const std::string& path, const FitResultWithMaps& fit) {
58 std::ofstream out(path);
59 out << "name,value,error\n";
60
61 for (const auto& [pid, val] : fit.p_hat) {
62 double err = 0.0;
63 auto it = fit.p_hat_std.find(pid);
64 if (it != fit.p_hat_std.end()) {
65 err = it->second;
66 }
67
68 out << fit_app::to_string_any(pid) << ","
69 << std::setprecision(17) << val << ","
70 << err << "\n";
71 }
72}
73
74void save_contour_csv(const std::string& path,
75 const std::string& xname,
76 const std::string& yname,
77 const std::set<Path>& contour68,
78 const std::set<Path>& contour95) {
79 std::ofstream out(path);
80 out << "# x=" << xname << "\n";
81 out << "# y=" << yname << "\n";
82 out << "cl,path_id,x,y\n";
83
84 std::size_t path_id = 0;
85 for (const auto& path_pts : contour68) {
86 for (const auto& p : path_pts) {
87 out << "0.683," << path_id << ","
88 << std::setprecision(17) << p.first << ","
89 << p.second << "\n";
90 }
91 ++path_id;
92 }
93
94 path_id = 0;
95 for (const auto& path_pts : contour95) {
96 for (const auto& p : path_pts) {
97 out << "0.95," << path_id << ","
98 << std::setprecision(17) << p.first << ","
99 << p.second << "\n";
100 }
101 ++path_id;
102 }
103}
104
105}
106
107int main() {
108 using namespace fit_app;
109 // Logger::getInstance()->setLevel(Logger::LogLevel::VERBOSE);
110 HyperisoMaster hyp;
111 HyperisoConfig config_hyp;
112 config_hyp.model = Model::SM;
113 hyp.init("lha/si_input.flha", config_hyp);
114
115 auto oint = std::make_shared<ObservableInterface>();
118
119 std::shared_ptr<IStatParamOptimizerProxy> spop = std::make_shared<StatParamOptimizerProxy>();
120
121 auto model = std::make_shared<ObservableInterfaceProxy>(oint, spop);
122
123 StatisticConfig config;
125 config.MC_draws = 100;
126 config.advanced.MLE_max_iter = 120000;
127 config.advanced.MLE_tol = 0.2;
128
129 std::vector<ParamId> p_specs = {
130 ParamId{ParameterType::FLAVOR, "FCONST", {511, 1}},
131 ParamId{ParameterType::FLAVOR, "FCONST", {531, 1}}
132 };
133
134 std::shared_ptr<INuisancePathsProvider> npp = std::make_shared<DefaultNuisancePathsProvider>();
135
136 StatisticManager stat(
137 config,
138 model,
139 std::make_shared<StatCorrelationProxy>(),
140 std::make_shared<StatParameterProxy>(),
141 std::make_shared<StatParamSourcesProxy>(),
142 std::make_shared<StatDependencyPruner>(),
143 std::make_shared<NuisanceReader>(npp),
144 spop
145 );
146
147 auto t0 = std::chrono::steady_clock::now();
148 // auto unc = stat.compute_uncertainties();
149 auto t1 = std::chrono::steady_clock::now();
150 std::cout << "Uncertainty pass done in "
151 << std::chrono::duration_cast<std::chrono::milliseconds>(t1 - t0).count()
152 << " ms\n";
153 // std::cout << "Number of summarized observables = " << unc.size() << "\n";
154
155 auto t2 = std::chrono::steady_clock::now();
156 FitResultWithMaps fit = stat.compute_MLE(p_specs);
157 auto t3 = std::chrono::steady_clock::now();
158
159 std::cout << "\nMLE done in "
160 << std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2).count()
161 << " ms\n\n";
162
163 if (!fit.fit_ok) {
164 std::cerr << "[ERROR] MLE fit failed.\n";
165 return 5;
166 }
167
168 print_fit_result(fit);
169 save_bestfit_csv("bestfit.csv", fit);
170 std::cout << "[INFO] Wrote bestfit.csv\n";
171
172 if (p_specs.size() == 2) {
173 const ParamId p1 = p_specs[0];
174 const ParamId p2 = p_specs[1];
175
176 std::array<double, 4> bounds = {0.05, 0.35, 0.05, 0.35};
177
178 // 68.3% -> delta_chi2 = 2.30 -> delta_nll = 1.15 -> z = sqrt(2.30)
179 // 95% -> delta_chi2 = 5.99 -> delta_nll = 2.995 -> z = sqrt(5.99)
180 const double z68_2d = std::sqrt(2.30);
181 const double z95_2d = std::sqrt(5.99);
182
183 ContourOptions opt;
184
187 // opt.profile_backend= ProfileBackend::LAPLACE_NUISANCE;
188
189 auto c68 = stat.confidence_contour(p1, p2, z68_2d, bounds, opt);
190
191 std::cout << "first contour done" << std::endl;
192
193 auto t4 = std::chrono::steady_clock::now();
194 auto c95 = stat.confidence_contour(p1, p2, z95_2d, bounds, opt);
195 auto t5 = std::chrono::steady_clock::now();
196
197 std::cout << "\nContour done in "
198 << std::chrono::duration_cast<std::chrono::milliseconds>(t5 - t4).count()
199 << " ms\n\n";
200
201
202
203 std::cout << "[INFO] contour 68% paths = " << c68.level << "\n";
204 std::cout << "[INFO] contour 95% paths = " << c95.level << "\n";
205
206 save_contour_csv(
207 "contours.csv",
210 c68.paths,
211 c95.paths
212 );
213 std::cout << "[INFO] Wrote contours.csv\n";
214 }
215
216 return 0;
217}
@ AMS
Adaptive/marching-squares contour extractor.
@ MINUIT
Minuit contour extractor.
Default nuisance-configuration path provider.
Concrete reader for nuisance-parameter definition files.
Adapter from ObservableInterface to the statistical model interface.
High-level, user-facing entry point to compute flavor observables.
Concrete statistical proxy forwarding correlation queries to CorrelationProvider.
Statistics-layer adapter over the core DependencyPruner service.
Statistics-layer adapter for retrieving leaf parameter sources.
Statistics-layer proxy for read-only access to parameters and observables.
High-level orchestration of statistical uncertainty propagation, likelihood construction and fit scan...
@ CHI2_MC_COVARIANCE
Fast chi-square likelihood using MC theory covariance plus experimental covariance.
static IdOf< ObservableTag > to_id(Observables e)
Converts an enum value to an IdOf<Tag>.
High-level interface to initialize and monitor the main framework configuration.
void init(const std::string &lhaFile, HyperisoConfig config)
Initializes Hyperiso using a LHA file and a full Config object.
Coordinates statistical inputs, nuisance distributions, MLE fits and contour/scan computations.
Contour confidence_contour(ParamId p1, ParamId p2, double z, std::array< double, 4 > bounds, ContourOptions options)
Computes a two-dimensional confidence contour for the last successful MLE.
FitResultWithMaps compute_MLE(const std::vector< ParamId > &p_specs)
Computes the maximum-likelihood fit for a selected set of fit parameters.
std::vector< Point > Path
Definition contour.h:16
int main()
std::string to_string_any(const T &x)
double MLE_tol
Minimizer tolerance passed to the backend.
std::size_t MLE_max_iter
Maximum number of minimizer function calls/iterations.
StatisticLikelihoodMode likelihood_mode
Likelihood mode used by compute_MLE().
Runtime options controlling 2D contour computation.
Definition Fit.h:93
ContourAlgorithm primary_contour_method
Definition Fit.h:101
std::optional< ContourAlgorithm > fallback_contour_method
Definition Fit.h:104
User-facing MLE result keyed by physics parameter identifiers.
std::map< ParamId, double > p_hat_std
Profiled standard deviations of fitted parameters.
double ell_hat
Minimum negative log-likelihood value.
bool fit_ok
True when the fit returned a usable parameter estimate.
Configuration object controlling model, input flags and optional MARTY resources.
Definition Config.h:24
Model model
Current model.
Definition Config.h:33
Composite identifier for a single parameter.
Definition ParamID.h:57
AdvancedStatisticConfig advanced
Advanced fit/pruning/covariance configuration.
std::size_t MC_draws
Number of accepted MC draws used for uncertainty propagation.