ATLAS Offline Software
Loading...
Searching...
No Matches
CaloClusterMLCalib Namespace Reference

Typedefs

using TransformFunc = std::function<float(float, const std::vector<float> &)>

Functions

float standard (float x, const std::vector< float > &params)
float log (float x, const std::vector< float > &)
float logTen (float x, const std::vector< float > &)
float logStandard (float x, const std::vector< float > &params)
float logTenStandard (float x, const std::vector< float > &params)
float maxAbsolute (float x, const std::vector< float > &params)
float minMax (float x, const std::vector< float > &params)
float log_sum_exp (const std::vector< float > &vec)
float log_likelihood (float x, const std::vector< float > &mus, const std::vector< float > &log_sigma2s, const std::vector< float > &alphas)
float modes (const std::vector< float > &mus, const std::vector< float > &log_sigma2s, const std::vector< float > &alphas)
float sigma_stoch (const std::vector< float > &mus, const std::vector< float > &log_sigma2s, const std::vector< float > &alphas)

Variables

const std::map< std::string, TransformFuncTRANSFORMATIONS
constexpr float epsilon = 1e-9

Typedef Documentation

◆ TransformFunc

using CaloClusterMLCalib::TransformFunc = std::function<float(float, const std::vector<float> &)>

Definition at line 79 of file CaloClusterMLCalibFeatureTransform.h.

Function Documentation

◆ log()

float CaloClusterMLCalib::log ( float x,
const std::vector< float > &  )

Definition at line 27 of file CaloClusterMLCalibFeatureTransform.h.

28 {
29 if (x <= 0)
30 return -INFINITY; // mimic numpy's log(0) behavior
31 return std::log(x);
32 }
#define x

◆ log_likelihood()

float CaloClusterMLCalib::log_likelihood ( float x,
const std::vector< float > & mus,
const std::vector< float > & log_sigma2s,
const std::vector< float > & alphas )

Definition at line 34 of file CaloClusterMLGaussianMixture.h.

35 {
36 // Ensure vectors have the correct size
37 assert(mus.size() == 3 && log_sigma2s.size() == 3 && alphas.size() == 3);
38
39 std::vector<float> log_likelihood_components(3);
40
41 for (int i = 0; i < 3; ++i)
42 {
43 // Calculate one-dimensional negative log-Gaussian
44 float neg_log_gauss = std::pow(mus[i] - x, 2.0) / (2.0 * std::exp(log_sigma2s[i])) + 0.5 * log_sigma2s[i];
45 neg_log_gauss += 0.5 * std::log(2.0 * M_PI); // M_PI is a common constant for pi
46
47 // log_prob_components = -neg_log_gauss_i + log(alpha_i)
48 log_likelihood_components[i] = -neg_log_gauss + std::log(alphas[i] + epsilon); // Added epsilon to avoid FPE DIVBYZERO warning
49 }
50
51 // log_prob = log(sum_i exp(log_prob_components_i))
52 return log_sum_exp(log_likelihood_components);
53 }
#define M_PI
float log_sum_exp(const std::vector< float > &vec)

◆ log_sum_exp()

float CaloClusterMLCalib::log_sum_exp ( const std::vector< float > & vec)

Definition at line 19 of file CaloClusterMLGaussianMixture.h.

20 {
21 if (vec.empty())
22 {
23 return -std::numeric_limits<float>::infinity();
24 }
25 float max_val = *std::max_element(vec.begin(), vec.end());
26 float sum = 0.0;
27 for (float val : vec)
28 {
29 sum += std::exp(val - max_val);
30 }
31 return max_val + std::log(sum);
32 }
std::vector< size_t > vec

◆ logStandard()

float CaloClusterMLCalib::logStandard ( float x,
const std::vector< float > & params )

Definition at line 41 of file CaloClusterMLCalibFeatureTransform.h.

42 {
43 if (params.size() != 2)
44 throw std::invalid_argument("LogStandard expects 2 parameters [mean, std_dev]");
45 float log_x = log(x, {});
46 return standard(log_x, params);
47 }
float standard(float x, const std::vector< float > &params)
float log(float x, const std::vector< float > &)

◆ logTen()

float CaloClusterMLCalib::logTen ( float x,
const std::vector< float > &  )

Definition at line 34 of file CaloClusterMLCalibFeatureTransform.h.

35 {
36 if (x <= 0)
37 return -INFINITY;
38 return std::log10(x);
39 }

◆ logTenStandard()

float CaloClusterMLCalib::logTenStandard ( float x,
const std::vector< float > & params )

Definition at line 49 of file CaloClusterMLCalibFeatureTransform.h.

50 {
51 if (params.size() != 4)
52 throw std::invalid_argument("LogTenStandard expects 4 parameters [xmin, epsilon, mean, std_dev]");
53 float xmin = params[0], epsilon = params[1], mean = params[2], std_dev = params[3];
54 float x_shifted = x - xmin + epsilon;
55 float log_x = logTen(x_shifted, {});
56 return standard(log_x, {mean, std_dev});
57 }
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
double xmin
Definition listroot.cxx:60
float logTen(float x, const std::vector< float > &)

◆ maxAbsolute()

float CaloClusterMLCalib::maxAbsolute ( float x,
const std::vector< float > & params )

Definition at line 59 of file CaloClusterMLCalibFeatureTransform.h.

60 {
61 if (params.size() != 1)
62 throw std::invalid_argument("MaxAbsolute expects 1 parameter [max_value]");
63 float max_val = params[0];
64 if (max_val == 0)
65 return x;
66 return x / std::abs(max_val);
67 }

◆ minMax()

float CaloClusterMLCalib::minMax ( float x,
const std::vector< float > & params )

Definition at line 69 of file CaloClusterMLCalibFeatureTransform.h.

70 {
71 if (params.size() != 2)
72 throw std::invalid_argument("MinMaxNorm expects 2 parameters [min_val, max_val]");
73 float min_val = params[0], max_val = params[1];
74 if (min_val == max_val)
75 return 0.0; // Avoid division by zero
76 return (x - min_val) / (max_val - min_val);
77 }

◆ modes()

float CaloClusterMLCalib::modes ( const std::vector< float > & mus,
const std::vector< float > & log_sigma2s,
const std::vector< float > & alphas )

Definition at line 55 of file CaloClusterMLGaussianMixture.h.

56 {
57 // Select range to check for maxima
58 float x_min = 0.9 * (*std::min_element(mus.begin(), mus.end()));
59 float x_max = 1.1 * (*std::max_element(mus.begin(), mus.end()));
60
61 // Create array of ranges to check for maxima
62 const int num_points = 1000;
63 std::vector<float> x_test(num_points);
64 float step = (x_max - x_min) / (num_points - 1);
65 for (int i = 0; i < num_points; ++i)
66 {
67 x_test[i] = x_min + i * step;
68 }
69
70 float max_log_likelihood = -std::numeric_limits<float>::infinity();
71 float mode = x_test[0]; // Initialize to first test point instead of 0
72
73 // Find the x-point with the largest likelihood value
74 for (float x : x_test)
75 {
76 float ll = log_likelihood(x, mus, log_sigma2s, alphas);
77 if (ll > max_log_likelihood)
78 {
79 max_log_likelihood = ll;
80 mode = x;
81 }
82 }
83
84 return mode;
85 }
float log_likelihood(float x, const std::vector< float > &mus, const std::vector< float > &log_sigma2s, const std::vector< float > &alphas)

◆ sigma_stoch()

float CaloClusterMLCalib::sigma_stoch ( const std::vector< float > & mus,
const std::vector< float > & log_sigma2s,
const std::vector< float > & alphas )

Definition at line 87 of file CaloClusterMLGaussianMixture.h.

90 {
91 float sigma_stoch2 = 0.0;
92 float sum_alphas_mus = 0.0;
93
94 // This loop combines the first three lines of the Python function.
95 for (size_t i = 0; i < mus.size(); ++i)
96 {
97 // Corresponds to: np.sum(alphas * sigma2s, axis=-1)
98 sigma_stoch2 += alphas[i] * std::exp(log_sigma2s[i]);
99 // Corresponds to: np.sum(alphas * np.power(mus, 2), axis=-1)
100 sigma_stoch2 += alphas[i] * std::pow(mus[i], 2);
101 // Accumulates sum for the final term: np.sum(alphas * mus, axis=-1)
102 sum_alphas_mus += alphas[i] * mus[i];
103 }
104
105 // Corresponds to: sigma_stoch2 -= np.power(np.sum(alphas * mus, axis=-1), 2)
106 sigma_stoch2 -= std::pow(sum_alphas_mus, 2);
107
108 // Corresponds to: return np.sqrt(sigma_stoch2)
109 return std::sqrt(sigma_stoch2);
110 }

◆ standard()

float CaloClusterMLCalib::standard ( float x,
const std::vector< float > & params )

Definition at line 17 of file CaloClusterMLCalibFeatureTransform.h.

18 {
19 if (params.size() != 2)
20 throw std::invalid_argument("Standard expects 2 parameters [mean, std_dev]");
21 float mean = params[0], std_dev = params[1];
22 if (std_dev == 0)
23 return x - mean;
24 return (x - mean) / std_dev;
25 }

Variable Documentation

◆ epsilon

float CaloClusterMLCalib::epsilon = 1e-9
constexpr

Definition at line 16 of file CaloClusterMLGaussianMixture.h.

◆ TRANSFORMATIONS

const std::map<std::string, TransformFunc> CaloClusterMLCalib::TRANSFORMATIONS
Initial value:
= {
{"Standard", standard},
{"Log", log},
{"LogTen", logTen},
{"LogStandard", logStandard},
{"LogTenStandard", logTenStandard},
{"MaxAbsolute", maxAbsolute},
{"MinMaxNorm", minMax}}
float minMax(float x, const std::vector< float > &params)
float logStandard(float x, const std::vector< float > &params)
float logTenStandard(float x, const std::vector< float > &params)
float maxAbsolute(float x, const std::vector< float > &params)

Definition at line 81 of file CaloClusterMLCalibFeatureTransform.h.

81 {
82 {"Standard", standard},
83 {"Log", log},
84 {"LogTen", logTen},
85 {"LogStandard", logStandard},
86 {"LogTenStandard", logTenStandard},
87 {"MaxAbsolute", maxAbsolute},
88 {"MinMaxNorm", minMax}};