All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Types | Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
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
 
 operator Data_t () const
 
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 55 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 58 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 57 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 68 of file ChiSquareAccumulator.h.

69  : 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 108 of file ChiSquareAccumulator.h.

109  { fChiSq += sqr(y - expected(x)); ++fN; }
process_name opflash particleana ie x
static Data_t sqr(Data_t v)
The usual square function.
unsigned int fN
Number of data entries.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.
process_name opflash particleana ie ie y
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 120 of file ChiSquareAccumulator.h.

121  { fChiSq += sqr(z(y, expected(x), s)); ++fN; }
process_name opflash particleana ie x
static Data_t sqr(Data_t v)
The usual square function.
unsigned int fN
Number of data entries.
Data_t expected(Data_t x) const
Returns the expected value for the specified parameter.
process_name opflash particleana ie ie y
Data_t fChiSq
Accumulated χ value.
static Data_t z(Data_t x, Data_t mu, Data_t sigma)
Normal variable.
then echo File list $list not found else cat $list while read file do echo $file sed s
Definition: file_to_url.sh:60
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 79 of file ChiSquareAccumulator.h.

79 { 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.

124 { fChiSq = Data_t{0}; fN = 0U; }
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 88 of file ChiSquareAccumulator.h.

88 { return fExpected(x); }
process_name opflash particleana ie x
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 85 of file ChiSquareAccumulator.h.

85 { 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

Definition at line 81 of file ChiSquareAccumulator.h.

81 { 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

Definition at line 80 of file ChiSquareAccumulator.h.

80 { 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 140 of file ChiSquareAccumulator.h.

140 { 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 136 of file ChiSquareAccumulator.h.

137  { return (x - mu) / sigma; }
process_name opflash particleana ie x
pdgs mu
Definition: selectors.fcl:22

Member Data Documentation

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

Accumulated χ value.

Definition at line 131 of file ChiSquareAccumulator.h.

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

Function for the expectation.

Definition at line 133 of file ChiSquareAccumulator.h.

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

Number of data entries.

Definition at line 130 of file ChiSquareAccumulator.h.


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