LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
LArDiscreteProbabilityHelper.cc
Go to the documentation of this file.
1 
8 #include "Pandora/PandoraInternal.h"
9 
11 
12 namespace lar_content
13 {
14 
15 template <typename T>
17  const T &t1, const T &t2, std::mt19937 &randomNumberGenerator, const unsigned int nPermutations)
18 {
19  if (1 > nPermutations)
20  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
21 
23 
24  unsigned int nExtreme(0);
25  for (unsigned int iPermutation = 0; iPermutation < nPermutations; ++iPermutation)
26  {
28  LArDiscreteProbabilityHelper::MakeRandomisedSample(t1, randomNumberGenerator),
29  LArDiscreteProbabilityHelper::MakeRandomisedSample(t2, randomNumberGenerator)));
30 
31  if ((rRandomised - rNominal) > std::numeric_limits<float>::epsilon())
32  nExtreme++;
33  }
34 
35  return static_cast<float>(nExtreme) / static_cast<float>(nPermutations);
36 }
37 
38 //------------------------------------------------------------------------------------------------------------------------------------------
39 
40 template <typename T>
42  const T &t1, const T &t2, const unsigned int nIntegrationSteps, const float upperLimit)
43 {
45  const float dof(static_cast<float>(LArDiscreteProbabilityHelper::GetSize(t1)) - 2.f);
46 
47  if (0 > dof)
48  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
49 
50  const float tTestStatisticDenominator(1.f - correlation - correlation);
51 
52  if (tTestStatisticDenominator < std::numeric_limits<float>::epsilon())
53  throw pandora::StatusCodeException(pandora::STATUS_CODE_FAILURE);
54 
55  const float tTestStatistic(correlation * std::sqrt(dof) / (std::sqrt(tTestStatisticDenominator)));
56  const float tDistCoeff(std::tgamma(0.5f * (dof + 1.f)) / std::tgamma(0.5f * dof) / (std::sqrt(dof * M_PI)));
57 
58  const float dx((upperLimit - tTestStatistic) / static_cast<float>(nIntegrationSteps));
59  float integral(tDistCoeff * std::pow(1.f + tTestStatistic * tTestStatistic / dof, -0.5f * (dof + 1.f)) +
60  tDistCoeff * std::pow(1.f + upperLimit * upperLimit / dof, -0.5f * (dof + 1.f)));
61  for (unsigned int iStep = 1; iStep < nIntegrationSteps; ++iStep)
62  {
63  integral += 2.f * tDistCoeff *
64  std::pow(1.f + (tTestStatistic + static_cast<float>(iStep) * dx) * (tTestStatistic + static_cast<float>(iStep) * dx) / dof,
65  -0.5f * (dof + 1.f));
66  }
67 
68  return integral * dx / 2.f;
69 }
70 
71 //------------------------------------------------------------------------------------------------------------------------------------------
72 
73 template <typename T>
75 {
76  const unsigned int size1(LArDiscreteProbabilityHelper::GetSize(t1));
77  const unsigned int size2(LArDiscreteProbabilityHelper::GetSize(t2));
78  if (size1 != size2)
79  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
80 
81  if (2 > size1)
82  throw pandora::StatusCodeException(pandora::STATUS_CODE_INVALID_PARAMETER);
83 
84  const float mean1(LArDiscreteProbabilityHelper::CalculateMean(t1));
85  const float mean2(LArDiscreteProbabilityHelper::CalculateMean(t2));
86 
87  float variance1(0.f), variance2(0.f), covariance(0.f);
88 
89  for (unsigned int iElement = 0; iElement < size1; ++iElement)
90  {
91  const float diff1(LArDiscreteProbabilityHelper::GetElement(t1, iElement) - mean1);
92  const float diff2(LArDiscreteProbabilityHelper::GetElement(t2, iElement) - mean2);
93 
94  variance1 += diff1 * diff1;
95  variance2 += diff2 * diff2;
96  covariance += diff1 * diff2;
97  }
98 
99  if (variance1 < std::numeric_limits<float>::epsilon() || variance2 < std::numeric_limits<float>::epsilon())
100  throw pandora::StatusCodeException(pandora::STATUS_CODE_FAILURE);
101 
102  const float sqrtVars(std::sqrt(variance1 * variance2));
103  if (sqrtVars < std::numeric_limits<float>::epsilon())
104  throw pandora::StatusCodeException(pandora::STATUS_CODE_FAILURE);
105 
106  return covariance / sqrtVars;
107 }
108 
109 //------------------------------------------------------------------------------------------------------------------------------------------
110 
111 template <typename T>
113 {
114  const unsigned int size(LArDiscreteProbabilityHelper::GetSize(t));
115  if (1 > size)
116  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_INITIALIZED);
117 
118  float mean(0.f);
119  for (unsigned int iElement = 0; iElement < size; ++iElement)
120  mean += LArDiscreteProbabilityHelper::GetElement(t, iElement);
121 
122  return mean / static_cast<float>(size);
123 }
124 
125 //------------------------------------------------------------------------------------------------------------------------------------------
126 
128  const DiscreteProbabilityVector &, const DiscreteProbabilityVector &, std::mt19937 &, const unsigned int);
130  const pandora::FloatVector &, const pandora::FloatVector &, std::mt19937 &, const unsigned int);
131 
133  const DiscreteProbabilityVector &, const DiscreteProbabilityVector &, const unsigned int, const float);
135  const pandora::FloatVector &, const pandora::FloatVector &, const unsigned int, const float);
136 
138 template float LArDiscreteProbabilityHelper::CalculateCorrelationCoefficient(const pandora::FloatVector &, const pandora::FloatVector &);
139 
141 template float LArDiscreteProbabilityHelper::CalculateMean(const pandora::FloatVector &);
142 
143 } // namespace lar_content
TTree * t1
Definition: plottest35.C:26
static float CalculateCorrelationCoefficientPValueFromPermutationTest(const T &t1, const T &t2, std::mt19937 &randomNumberGenerator, const unsigned int nPermutations)
Calculate P value for measured correlation coefficient between two datasets via a permutation test...
static float CalculateCorrelationCoefficientPValueFromStudentTDistribution(const T &t1, const T &t2, const unsigned int nIntegrationSteps, const float upperLimit)
Calculate P value for measured correlation coefficient between two datasets via a integrating the stu...
static float CalculateCorrelationCoefficient(const T &t1, const T &t2)
Calculate the correlation coefficient between two datasets.
Header file for the discrete probability helper class.
TFile f
Definition: plotHisto.C:6
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
static T MakeRandomisedSample(const T &t, std::mt19937 &randomNumberGenerator)
Make a randomised copy of a dataset.
static unsigned int GetSize(const T &t)
Get the size the size of a dataset.
static float CalculateMean(const T &t)
Calculate the mean of a dataset.
TTree * t2
Definition: plottest35.C:36
double mean(const std::vector< short > &wf, size_t start, size_t nsample)
Definition: UtilFunc.cxx:13
static float GetElement(const T &t, const unsigned int index)
Get an element in a dataset.