Hyperiso 1.0.3
Modular flavour-physics calculations, Wilson coefficients and statistical inference
Loading...
Searching...
No Matches
Statistics.h
Go to the documentation of this file.
1#ifndef STATISTICS_H
2#define STATISTICS_H
3
4#include <vector>
5#include <cmath>
6#include <stdexcept>
7#include <map>
8#include <algorithm>
9
10#include "Include.h"
11#include "Math.h"
12
22using Vec = std::vector<double>;
23
25using ObsSamples = std::vector<std::map<BinnedObservableId, double>>; // shape: N x D (N samples of D-dim vector)
26
28using NuisanceSamples = std::vector<std::map<ParamId, double>>; // shape: N x D (N samples of D-dim vector)
29
35 double mean {0.0};
36 double std_unbiased {0.0};
37 double b1_skew {0.0};
38 double std_p {0.0};
39 double std_m {0.0};
40 double mode {0.0};
41};
42
53inline std::pair<std::vector<double>, std::vector<double>>
54split_vector(const std::vector<double>& x, double mu)
55{
56 std::vector<double> below;
57 std::vector<double> above;
58
59 below.reserve(x.size());
60 above.reserve(x.size());
61
62 std::partition_copy(
63 x.begin(), x.end(),
64 std::back_inserter(below),
65 std::back_inserter(above),
66 [mu](double v) { return v <= mu; }
67 );
68
69 return {below, above};
70}
71
86inline std::map<BinnedObservableId, ColumnStats> summarize_columns_obs(const ObsSamples& S) {
87 if (S.empty()) throw std::invalid_argument("No samples");
88
89 const std::size_t N = S.size();
90 const std::size_t D = S[0].size();
91
92 for (const auto& v : S) {
93 if (v.size() != D) throw std::invalid_argument("Jagged samples");
94 }
95
96 std::vector<BinnedObservableId> ids;
97 for (const auto& v : S[0]) {
98 ids.push_back(v.first);
99 }
100
101 std::map<BinnedObservableId, ColumnStats> out;
102
103 std::vector<double> x;
104 x.reserve(N);
105
106 for (size_t d = 0; d < D; d++) {
107 x.clear();
108 for (const auto& v : S) {
109 x.push_back(v.at(ids[d]));
110 }
111 std::sort(x.begin(), x.end());
112
113 double mean = 0.0;
114 for (double x_i : x) mean += x_i;
115 mean /= static_cast<double>(N);
116
117 double s = 0.0, m3 = 0.0;
118 for (double x_i : x) {
119 const double r = x_i - mean;
120 s += r * r;
121 m3 += r * r * r;
122 }
123
124 out[ids[d]].mean = mean;
125 out[ids[d]].std_unbiased = std::sqrt(s / static_cast<double>(N - 1));
126
127 const double m2 = s / static_cast<double>(N);
128 if (m2 > 0.0) {
129 const double m3bar = m3 / static_cast<double>(N);
130 out[ids[d]].b1_skew = m3bar / std::pow(m2, 1.5);
131 } else {
132 out[ids[d]].b1_skew = 0.0;
133 }
134
135 auto obj = [x, N] (std::size_t i) {
136 double s_m = 0;
137 double s_p = 0;
138 for (size_t j = 0; j < i; j++) s_m += (x[j] - x[i]) * (x[j] - x[i]);
139 for (size_t j = i; j < N; j++) s_p += (x[j] - x[i]) * (x[j] - x[i]);
140 return std::cbrt(s_m) + std::cbrt(s_p);
141 };
142
143 double mu_hat = 0;
144 double min_obj = std::numeric_limits<double>::infinity();
145
146 for (std::size_t i = 0; i < N; ++i) {
147 double obj_i = obj(i);
148
149 if (obj_i < min_obj) {
150 min_obj = obj_i;
151 mu_hat = x[i];
152 }
153 }
154
155 auto [x_m, x_p] = split_vector(x, mu_hat);
156 double s_m = 0;
157 double s_p = 0;
158 for (double x_i : x_m) s_m += (x_i - mu_hat) * (x_i - mu_hat);
159 for (double x_i : x_p) s_p += (x_i - mu_hat) * (x_i - mu_hat);
160
161 out[ids[d]].mode = mu_hat;
162 out[ids[d]].std_m = std::sqrt(min_obj / N) * std::cbrt(s_m);
163 out[ids[d]].std_p = std::sqrt(min_obj / N) * std::cbrt(s_p);
164 }
165
166 return out;
167}
168
169#endif
std::vector< std::map< BinnedObservableId, double > > ObsSamples
Definition Statistics.h:25
std::pair< std::vector< double >, std::vector< double > > split_vector(const std::vector< double > &x, double mu)
Splits a vector around a reference value.
Definition Statistics.h:54
std::map< BinnedObservableId, ColumnStats > summarize_columns_obs(const ObsSamples &S)
Computes per-observable summary statistics from Monte Carlo samples.
Definition Statistics.h:86
std::vector< std::map< ParamId, double > > NuisanceSamples
Definition Statistics.h:28
Summary statistics for one sampled observable column.
Definition Statistics.h:34
double mean
Arithmetic mean of the sampled values.
Definition Statistics.h:35
double std_p
Right-side split standard deviation estimate.
Definition Statistics.h:38
double b1_skew
Moment skewness coefficient.
Definition Statistics.h:37
double std_unbiased
Unbiased standard deviation.
Definition Statistics.h:36
double mode
Discrete mode-like central estimator.
Definition Statistics.h:40
double std_m
Left-side split standard deviation estimate.
Definition Statistics.h:39
std::vector< double > Vec