LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::LArDiscreteProbabilityHelper Class Reference

LArDiscreteProbabilityHelper class. More...

#include "LArDiscreteProbabilityHelper.h"

Public Member Functions

template<>
DiscreteProbabilityVector MakeRandomisedSample (const DiscreteProbabilityVector &t, std::mt19937 &randomNumberGenerator)
 
template<>
unsigned int GetSize (const DiscreteProbabilityVector &t)
 
template<>
float GetElement (const DiscreteProbabilityVector &t, const unsigned int index)
 

Static Public Member Functions

template<typename T >
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. More...
 
template<typename T >
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 student T dist. More...
 
template<typename T >
static float CalculateCorrelationCoefficient (const T &t1, const T &t2)
 Calculate the correlation coefficient between two datasets. More...
 
template<typename T >
static float CalculateMean (const T &t)
 Calculate the mean of a dataset. More...
 

Static Private Member Functions

template<typename T >
static T MakeRandomisedSample (const T &t, std::mt19937 &randomNumberGenerator)
 Make a randomised copy of a dataset. More...
 
template<typename T >
static std::vector< T > MakeRandomisedSample (const std::vector< T > &t, std::mt19937 &randomNumberGenerator)
 Make a randomised copy of dataset (dataset is an std::vector) More...
 
template<typename T >
static unsigned int GetSize (const T &t)
 Get the size the size of a dataset. More...
 
template<typename T >
static unsigned int GetSize (const std::vector< T > &t)
 Get the size of a dataset (dataset is an std::vector) More...
 
template<typename T >
static float GetElement (const T &t, const unsigned int index)
 Get an element in a dataset. More...
 
template<typename T >
static float GetElement (const std::vector< T > &t, const unsigned int index)
 Get an element in a dataset (dataset is an std::vector) More...
 

Detailed Description

Member Function Documentation

template<typename T >
template float lar_content::LArDiscreteProbabilityHelper::CalculateCorrelationCoefficient ( const T &  t1,
const T &  t2 
)
static

Calculate the correlation coefficient between two datasets.

Parameters
t1the first input dataset
t2the second input dataset
Returns
the correlation coefficient

Definition at line 74 of file LArDiscreteProbabilityHelper.cc.

References CalculateMean(), f, GetElement(), and GetSize().

Referenced by CalculateCorrelationCoefficientPValueFromPermutationTest(), CalculateCorrelationCoefficientPValueFromStudentTDistribution(), CalculateMean(), and lar_content::TwoViewTransverseTracksAlgorithm::TwoViewTransverseTracksAlgorithm().

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 
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 }
TTree * t1
Definition: plottest35.C:26
TFile f
Definition: plotHisto.C:6
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
static float GetElement(const T &t, const unsigned int index)
Get an element in a dataset.
template<typename T >
template float lar_content::LArDiscreteProbabilityHelper::CalculateCorrelationCoefficientPValueFromPermutationTest ( const T &  t1,
const T &  t2,
std::mt19937 &  randomNumberGenerator,
const unsigned int  nPermutations 
)
static

Calculate P value for measured correlation coefficient between two datasets via a permutation test.

Parameters
t1the first input dataset
t2the second input dataset
randomNumberGeneratorthe random number generator to shuffle the datasets
nPermutationsthe number of permutations to run
Returns
the p-value

Definition at line 16 of file LArDiscreteProbabilityHelper.cc.

References CalculateCorrelationCoefficient(), and MakeRandomisedSample().

Referenced by CalculateMean(), lar_content::TwoViewTransverseTracksAlgorithm::CalculateNumberOfLocallyMatchingSamplingPoints(), and lar_content::TwoViewTransverseTracksAlgorithm::TwoViewTransverseTracksAlgorithm().

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  {
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 }
TTree * t1
Definition: plottest35.C:26
static float CalculateCorrelationCoefficient(const T &t1, const T &t2)
Calculate the correlation coefficient between two datasets.
static T MakeRandomisedSample(const T &t, std::mt19937 &randomNumberGenerator)
Make a randomised copy of a dataset.
TTree * t2
Definition: plottest35.C:36
template<typename T >
template float lar_content::LArDiscreteProbabilityHelper::CalculateCorrelationCoefficientPValueFromStudentTDistribution ( const T &  t1,
const T &  t2,
const unsigned int  nIntegrationSteps,
const float  upperLimit 
)
static

Calculate P value for measured correlation coefficient between two datasets via a integrating the student T dist.

Parameters
t1the first input dataset
t2the second input dataset
nIntegrationStepshow many steps to use in the trapezium integration
upperLimitthe upper limit of the integration
Returns
the p-value

Definition at line 41 of file LArDiscreteProbabilityHelper.cc.

References CalculateCorrelationCoefficient(), f, and GetSize().

Referenced by CalculateMean().

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 }
TTree * t1
Definition: plottest35.C:26
static float CalculateCorrelationCoefficient(const T &t1, const T &t2)
Calculate the correlation coefficient between two datasets.
TFile f
Definition: plotHisto.C:6
static unsigned int GetSize(const T &t)
Get the size the size of a dataset.
TTree * t2
Definition: plottest35.C:36
template<typename T >
template float lar_content::LArDiscreteProbabilityHelper::CalculateMean ( const T &  t)
static

Calculate the mean of a dataset.

Parameters
tthe dataset
Returns
the mean

Definition at line 112 of file LArDiscreteProbabilityHelper.cc.

References CalculateCorrelationCoefficient(), CalculateCorrelationCoefficientPValueFromPermutationTest(), CalculateCorrelationCoefficientPValueFromStudentTDistribution(), f, GetElement(), GetSize(), pmtana::mean(), and util::size().

Referenced by CalculateCorrelationCoefficient().

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)
121 
122  return mean / static_cast<float>(size);
123 }
TFile f
Definition: plotHisto.C:6
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
static unsigned int GetSize(const T &t)
Get the size the size of a dataset.
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.
template<typename T >
static float lar_content::LArDiscreteProbabilityHelper::GetElement ( const T &  t,
const unsigned int  index 
)
staticprivate

Get an element in a dataset.

Parameters
tthe dataset
indexthe index of the element
Returns
the dataset element

Referenced by CalculateCorrelationCoefficient(), and CalculateMean().

template<typename T >
float lar_content::LArDiscreteProbabilityHelper::GetElement ( const std::vector< T > &  t,
const unsigned int  index 
)
inlinestaticprivate

Get an element in a dataset (dataset is an std::vector)

Parameters
tthe std::vector dataset
indexthe index of the element
Returns
the std::vector-based dataset element

Definition at line 174 of file LArDiscreteProbabilityHelper.h.

175 {
176  return static_cast<float>(t.at(index));
177 }
template<>
float lar_content::LArDiscreteProbabilityHelper::GetElement ( const DiscreteProbabilityVector t,
const unsigned int  index 
)
inline

Definition at line 180 of file LArDiscreteProbabilityHelper.h.

References lar_content::DiscreteProbabilityVector::GetProbability().

181 {
182  return static_cast<float>(t.GetProbability(index));
183 }
template<typename T >
static unsigned int lar_content::LArDiscreteProbabilityHelper::GetSize ( const T &  t)
staticprivate

Get the size the size of a dataset.

Parameters
tthe dataset
Returns
the dataset size

Referenced by CalculateCorrelationCoefficient(), CalculateCorrelationCoefficientPValueFromStudentTDistribution(), and CalculateMean().

template<typename T >
unsigned int lar_content::LArDiscreteProbabilityHelper::GetSize ( const std::vector< T > &  t)
inlinestaticprivate

Get the size of a dataset (dataset is an std::vector)

Parameters
tthe std::vector dataset
Returns
the std::vector-based dataset size

Definition at line 160 of file LArDiscreteProbabilityHelper.h.

161 {
162  return t.size();
163 }
template<>
unsigned int lar_content::LArDiscreteProbabilityHelper::GetSize ( const DiscreteProbabilityVector t)
inline

Definition at line 166 of file LArDiscreteProbabilityHelper.h.

References lar_content::DiscreteProbabilityVector::GetSize().

167 {
168  return t.GetSize();
169 }
template<typename T >
static T lar_content::LArDiscreteProbabilityHelper::MakeRandomisedSample ( const T &  t,
std::mt19937 &  randomNumberGenerator 
)
staticprivate

Make a randomised copy of a dataset.

Parameters
tthe dataset to be shuffled
randomNumberGeneratorthe random number generator
Returns
the reshuffled dataset

Referenced by CalculateCorrelationCoefficientPValueFromPermutationTest().

template<typename T >
std::vector< T > lar_content::LArDiscreteProbabilityHelper::MakeRandomisedSample ( const std::vector< T > &  t,
std::mt19937 &  randomNumberGenerator 
)
inlinestaticprivate

Make a randomised copy of dataset (dataset is an std::vector)

Parameters
tthe std::vector-based dataset to be shuffled
randomNumberGeneratorthe random number generator
Returns
the reshuffled std::vector

Definition at line 143 of file LArDiscreteProbabilityHelper.h.

144 {
145  std::vector<T> randomisedVector(t);
146  std::shuffle(randomisedVector.begin(), randomisedVector.end(), randomNumberGenerator);
147 
148  return randomisedVector;
149 }
template<>
DiscreteProbabilityVector lar_content::LArDiscreteProbabilityHelper::MakeRandomisedSample ( const DiscreteProbabilityVector t,
std::mt19937 &  randomNumberGenerator 
)
inline

Definition at line 152 of file LArDiscreteProbabilityHelper.h.

153 {
154  return DiscreteProbabilityVector(t, randomNumberGenerator);
155 }

The documentation for this class was generated from the following files: