LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar::util::ChiSquareAccumulator< F, T > Class Template Reference

Computes a χ² from expectation function and data points. More...

#include "ChiSquareAccumulator.h"

Public Types

using Function_t = F
 Type of function for the expectation. More...
 
using Data_t = T
 Type of parameter and observed values. More...
 

Public Member Functions

unsigned int N () const
 Returns the number of added points (it's not degrees of freedom yet!). More...
 
Data_t expected (Data_t x) const
 Returns the expected value for the specified parameter. More...
 
 ChiSquareAccumulator (Function_t const &expected)
 Constructor: uses the specified expectation function. More...
 
Access to results
Data_t chiSquare () const
 Returns the value of χ² currently accumulated. More...
 
Data_t operator() () const
 Returns the value of χ² currently accumulated. More...
 
 operator Data_t () const
 Returns the value of χ² currently accumulated. More...
 
Data manipulation
void add (Data_t x, Data_t y)
 Adds a data point to the χ². More...
 
void add (Data_t x, Data_t y, Data_t s)
 Adds a data point to the χ². More...
 
void clear ()
 Resets all the counts, starting from no data. More...
 

Static Private Member Functions

static Data_t z (Data_t x, Data_t mu, Data_t sigma)
 Normal variable. More...
 
static Data_t sqr (Data_t v)
 The usual square function. More...
 

Private Attributes

unsigned int fN = 0U
 Number of data entries. More...
 
Data_t fChiSq = 0.0
 Accumulated χ² value. More...
 
Function_t fExpected
 Function for the expectation. More...
 

Detailed Description

template<typename F, typename T = double>
class lar::util::ChiSquareAccumulator< F, T >

Computes a χ² from expectation function and data points.

Template Parameters
Ftype of the function
Ttype of data

The formula used is the simple $ \chi^{2} = \sum_{i} \left(\frac{y_{i} - e(x_{i})}{\sigma_{i}}\right)^{2} $ with each observed point being $ ( x_{i}, y_{i} \pm \sigma_{i} ) $ and with $ e() $ the function describing the expectation (e.g. a fit result).

The parameter F must be usable as a unary functor, that is it must accept a single argument convertible from type Data_t (that is T), and return a value convertible back to type Data_t.

Example of usage:

double const a = 2.0;
double const b = -1.0;
auto f = [a,b](double x){ return a + b * x; };
chiSquare.add(0.0, 1.0, 0.5); // add ( 0 ; 1.0 +/- 0.5 )
chiSquare.add(1.0, 1.0, 0.5); // add ( 1 ; 1.0 +/- 0.5 )
chiSquare.add(2.0, 1.0, 0.5); // add ( 2 ; 1.0 +/- 0.5 )
double const chi2value = chiSquare();
int degreesOfFreedom = int(chiSquare.N()) - 3;

will check three observations against the prediction of 2 - x, returning a chi2value of 8.0 and a degreesOfFreedom of 0 (note that the 3 degrees are manually subtracted).

Definition at line 53 of file ChiSquareAccumulator.h.

Member Typedef Documentation

template<typename F, typename T = double>
using lar::util::ChiSquareAccumulator< F, T >::Data_t = T

Type of parameter and observed values.

Definition at line 56 of file ChiSquareAccumulator.h.

template<typename F, typename T = double>
using lar::util::ChiSquareAccumulator< F, T >::Function_t = F

Type of function for the expectation.

Definition at line 55 of file ChiSquareAccumulator.h.

Constructor & Destructor Documentation

template<typename F, typename T = double>
lar::util::ChiSquareAccumulator< F, T >::ChiSquareAccumulator ( Function_t const &  expected)
inline

Constructor: uses the specified expectation function.

Parameters
expectedexpectation function

The expectation function domain must be a single dimension of type Data_t.

Definition at line 66 of file ChiSquareAccumulator.h.

66 : fExpected(expected) {}
Function_t fExpected
Function for the expectation.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.

Member Function Documentation

template<typename F, typename T = double>
void lar::util::ChiSquareAccumulator< F, T >::add ( Data_t  x,
Data_t  y 
)
inline

Adds a data point to the χ².

Parameters
xparameter
yobserved data with the x parameter

The χ² is increased by $ \left(y - e(x)\right)^{2} $ where e is the expectation function (expected()). The observed values are considered to have nominal uncertainty 1.

Definition at line 102 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::expected(), lar::util::ChiSquareAccumulator< F, T >::fChiSq, lar::util::ChiSquareAccumulator< F, T >::fN, and lar::util::ChiSquareAccumulator< F, T >::sqr().

103  {
104  fChiSq += sqr(y - expected(x));
105  ++fN;
106  }
Float_t x
Definition: compare.C:6
static Data_t sqr(Data_t v)
The usual square function.
Float_t y
Definition: compare.C:6
unsigned int fN
Number of data entries.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.
Data_t fChiSq
Accumulated χ² value.
template<typename F, typename T = double>
void lar::util::ChiSquareAccumulator< F, T >::add ( Data_t  x,
Data_t  y,
Data_t  s 
)
inline

Adds a data point to the χ².

Parameters
xparameter
yobserved data with the x parameter
suncertainty on the observed data (default: 1.0)

The χ² is increased by $ \left(\frac{y - e(x)}{s}\right)^{2} $ where e is the expectation function (expected()).

Definition at line 117 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::expected(), lar::util::ChiSquareAccumulator< F, T >::fChiSq, lar::util::ChiSquareAccumulator< F, T >::fN, lar::util::ChiSquareAccumulator< F, T >::sqr(), and lar::util::ChiSquareAccumulator< F, T >::z().

118  {
119  fChiSq += sqr(z(y, expected(x), s));
120  ++fN;
121  }
Float_t x
Definition: compare.C:6
static Data_t sqr(Data_t v)
The usual square function.
Float_t y
Definition: compare.C:6
unsigned int fN
Number of data entries.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.
Data_t fChiSq
Accumulated χ² value.
static Data_t z(Data_t x, Data_t mu, Data_t sigma)
Normal variable.
template<typename F, typename T = double>
Data_t lar::util::ChiSquareAccumulator< F, T >::chiSquare ( ) const
inline

Returns the value of χ² currently accumulated.

Definition at line 75 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::fChiSq.

Referenced by lar::util::ChiSquareAccumulator< F, T >::operator Data_t(), and lar::util::ChiSquareAccumulator< F, T >::operator()().

75 { return fChiSq; }
Data_t fChiSq
Accumulated χ² value.
template<typename F, typename T = double>
void lar::util::ChiSquareAccumulator< F, T >::clear ( )
inline

Resets all the counts, starting from no data.

Definition at line 124 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::fChiSq, and lar::util::ChiSquareAccumulator< F, T >::fN.

125  {
126  fChiSq = Data_t{0};
127  fN = 0U;
128  }
unsigned int fN
Number of data entries.
Data_t fChiSq
Accumulated χ² value.
T Data_t
Type of parameter and observed values.
template<typename F, typename T = double>
Data_t lar::util::ChiSquareAccumulator< F, T >::expected ( Data_t  x) const
inline

Returns the expected value for the specified parameter.

Definition at line 84 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::fExpected.

Referenced by lar::util::ChiSquareAccumulator< F, T >::add().

84 { return fExpected(x); }
Float_t x
Definition: compare.C:6
Function_t fExpected
Function for the expectation.
template<typename F, typename T = double>
unsigned int lar::util::ChiSquareAccumulator< F, T >::N ( ) const
inline

Returns the number of added points (it's not degrees of freedom yet!).

Definition at line 81 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::fN.

81 { return fN; }
unsigned int fN
Number of data entries.
template<typename F, typename T = double>
lar::util::ChiSquareAccumulator< F, T >::operator Data_t ( ) const
inline

Returns the value of χ² currently accumulated.

Definition at line 77 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::chiSquare().

77 { return chiSquare(); }
Data_t chiSquare() const
Returns the value of χ² currently accumulated.
template<typename F, typename T = double>
Data_t lar::util::ChiSquareAccumulator< F, T >::operator() ( ) const
inline

Returns the value of χ² currently accumulated.

Definition at line 76 of file ChiSquareAccumulator.h.

References lar::util::ChiSquareAccumulator< F, T >::chiSquare().

76 { return chiSquare(); }
Data_t chiSquare() const
Returns the value of χ² currently accumulated.
template<typename F, typename T = double>
static Data_t lar::util::ChiSquareAccumulator< F, T >::sqr ( Data_t  v)
inlinestaticprivate

The usual square function.

Definition at line 143 of file ChiSquareAccumulator.h.

Referenced by lar::util::ChiSquareAccumulator< F, T >::add().

143 { return v * v; }
template<typename F, typename T = double>
static Data_t lar::util::ChiSquareAccumulator< F, T >::z ( Data_t  x,
Data_t  mu,
Data_t  sigma 
)
inlinestaticprivate

Normal variable.

Definition at line 140 of file ChiSquareAccumulator.h.

Referenced by lar::util::ChiSquareAccumulator< F, T >::add().

140 { return (x - mu) / sigma; }
Float_t x
Definition: compare.C:6

Member Data Documentation

template<typename F, typename T = double>
Data_t lar::util::ChiSquareAccumulator< F, T >::fChiSq = 0.0
private
template<typename F, typename T = double>
Function_t lar::util::ChiSquareAccumulator< F, T >::fExpected
private

Function for the expectation.

Definition at line 137 of file ChiSquareAccumulator.h.

Referenced by lar::util::ChiSquareAccumulator< F, T >::expected().

template<typename F, typename T = double>
unsigned int lar::util::ChiSquareAccumulator< F, T >::fN = 0U
private

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