Tools/CIC/utl/Math.cc
Go to the documentation of this file.
1 #include <utl/Math.h>
2 #include <utl/Accumulator.h>
3 #include <numeric>
4 
5 using namespace std;
6 using namespace utl::Accumulator;
7 
8 
9 namespace utl {
10 
11 inline
12 TRandom3*
14 {
15  gRandom->SetSeed(0);
16  return (TRandom3*)(gRandom);
17 }
18 
19 
20 TRandom3* const gRandom3 = GetRandom3();
21 
22 
23 // Kolmogorov-Smirnov measure of uniformity
24 /*double
25 UniformKolmogorovSmirnov(const vector<double>& cdf)
26 {
27  // cdf is sorted list of discrete points where jump occurs
28  // the values are assumed to be normalized into [0, 1]
29  const double n = cdf.size();
30  Max<double> maxm(0);
31  Max<double> maxp(0);
32  unsigned int i = 0;
33  double c = 0;
34  for (const auto x : cdf) {
35  maxm(x - c);
36  c = (++i) / n;
37  maxp(c - x);
38  }
39  return sqrt(n) * (maxp.GetMax() + maxm.GetMax());
40 }*/
41 
42 
43 double
44 UniformKolmogorovSmirnov(const vector<pair<double, double>>& wcdf,
45  double wtot)
46 {
47  // wcdf a list of (x,w) points, sorted in x, assuming x in [0,1].
48  // weights w can be arbitrary but positive
49  if (wcdf.empty())
50  return 0;
51  if (!wtot) {
52  for (const auto& xw : wcdf)
53  wtot += xw.second;
54  if (!wtot)
55  return 0;
56  }
57  Max<double> maxm(0);
58  Max<double> maxp(0);
59  double wsum = 0;
60  for (const auto& xw : wcdf) {
61  maxm(xw.first - wsum/wtot);
62  wsum += xw.second;
63  maxp(wsum/wtot - xw.first);
64  }
65  return sqrt(wtot) * (maxp.GetMax() + maxm.GetMax());
66 }
67 
68 
69 double
70 UniformAndersonDarling(const vector<pair<double, double>>& wcdf,
71  double wtot)
72 {
73  // wcdf a list of (x,w) points, sorted in x, assuming x in [0,1].
74  // weights w can be arbitrary but positive
75  if (wcdf.empty())
76  return 0;
77  if (!wtot) {
78  for (const auto& xw : wcdf)
79  wtot += xw.second;
80  if (!wtot)
81  return 0;
82  }
83  double wsum = 0;
84  double a = wcdf.front().first;
85  double a1 = 1 - a;
86  double anderson = -log(a1);
87  for (unsigned int i = 0, n = wcdf.size() - 1; i < n; ++i) {
88  const double b = wcdf[i+1].first;
89  const double b1 = 1 - b;
90  wsum += wcdf[i].second;
91  const double d = wsum/wtot;
92  anderson += Sqr(1 - d)*log(a1/b1) + Sqr(d)*log(b/a);
93  a = b;
94  a1 = b1;
95  }
96  anderson -= log(a) + 1;
97  return wtot * anderson;
98 }
99 
100 }
constexpr T Sqr(const T &x)
double UniformKolmogorovSmirnov(const vector< pair< double, double >> &wcdf, double wtot)
TRandom3 * GetRandom3()
double UniformAndersonDarling(const vector< pair< double, double >> &wcdf, double wtot)
TRandom3 *const gRandom3

, generated on Tue Sep 26 2023.