MINT2
Public Member Functions | Private Member Functions | Private Attributes | List of all members
StatisticsFinder Class Reference

#include <StatisticsFinder.h>

Inheritance diagram for StatisticsFinder:
MeanFinder MedianFinder MinMaxFinder WidthErrorFinder WidthFinder

Public Member Functions

 StatisticsFinder (bool mean=1, bool width=1, bool widthError=1, bool keepOrderedEvents=0)
 
double median () const
 
double numEvents () const
 
void add (const double &x, const double &weight=1.0)
 
double mean () const
 
double meanError () const
 
double varience () const
 
double width () const
 
double widthError () const
 
double expX () const
 
double expX2 () const
 
double expX3 () const
 
double expX4 () const
 
double secondCentralMom () const
 
double fourthCentralMom () const
 
const double & getMin () const
 
const double & getMax () const
 
double range () const
 
virtual ~StatisticsFinder ()
 

Private Member Functions

bool needOrderedEvents () const
 
void warnIfWeightedEvents () const
 
bool notEnoughInformation (const double &val) const
 

Private Attributes

double _min
 
double _max
 
double _nEvents
 
double _wSum
 
double _wSum2
 
double _wSum3
 
double _wSum4
 
double _sumW
 
int _keepOrderedEvents
 
std::vector< double > _orderedEvents
 

Detailed Description

HyperPlot, Author: Sam Harnew, sam.h.nosp@m.arne.nosp@m.w@gma.nosp@m.il.c.nosp@m.om , Date: Dec 2015

Used to find statistics for a dataset.

Definition at line 15 of file StatisticsFinder.h.

Constructor & Destructor Documentation

◆ StatisticsFinder()

StatisticsFinder::StatisticsFinder ( bool  mean = 1,
bool  width = 1,
bool  widthError = 1,
bool  keepOrderedEvents = 0 
)

In the constuctor you decide what things you want to be stored once you start adding values. This will determine what statistics you are able to calcuate later

Definition at line 14 of file StatisticsFinder.cpp.

14  :
15  _min (0.0),
16  _max (0.0),
17  _nEvents(0 ),
18  _wSum (0.0),
19  _wSum2 (0.0),
20  _wSum3 (0.0),
21  _wSum4 (0.0),
22  _sumW (0.0),
23  _keepOrderedEvents(keepOrderedEvents)
24 {
26  if( width == 0 ) { _wSum2 = NOT_STORED_VAL; }
27  if( mean == 0 ) { _wSum = NOT_STORED_VAL; _sumW = NOT_STORED_VAL;}
28 }
double widthError() const
#define NOT_STORED_VAL
double width() const
double mean() const

◆ ~StatisticsFinder()

StatisticsFinder::~StatisticsFinder ( )
virtual

Destructor

Definition at line 195 of file StatisticsFinder.cpp.

195  {
196 
197 }

Member Function Documentation

◆ add()

void StatisticsFinder::add ( const double &  x,
const double &  weight = 1.0 
)

Add a value (with optional weight) to the StatisticsFinder

Definition at line 87 of file StatisticsFinder.cpp.

87  {
88  if (_nEvents == 0){
89  _min = x;
90  _max = x;
91  }
92  else{
93  if (x < _min) _min = x;
94  if (x > _max) _max = x;
95  }
96  if (_sumW != NOT_STORED_VAL) _sumW += weight;
97  if (_wSum != NOT_STORED_VAL) _wSum += x*weight;
98  if (_wSum2 != NOT_STORED_VAL) _wSum2 += x*x*weight;
99  if (_wSum3 != NOT_STORED_VAL) _wSum3 += x*x*x*weight;
100  if (_wSum4 != NOT_STORED_VAL) _wSum4 += x*x*x*x*weight;
101 
102  if ( _keepOrderedEvents == 1) _orderedEvents.push_back(x);
103 
104  _nEvents += 1.0;
105 }
std::vector< double > _orderedEvents
#define NOT_STORED_VAL

◆ expX()

double StatisticsFinder::expX ( ) const

calculate and return the expectation value of X

Definition at line 145 of file StatisticsFinder.cpp.

145  {
146  if ( notEnoughInformation(_wSum) == 0 ) return 0.0;
147  return _wSum/_sumW;
148 }
bool notEnoughInformation(const double &val) const

◆ expX2()

double StatisticsFinder::expX2 ( ) const

calculate and return the expectation value of X^2

Definition at line 153 of file StatisticsFinder.cpp.

153  {
154  if ( notEnoughInformation(_wSum2) == 0 ) return 0.0;
155  return _wSum2/_sumW;
156 }
bool notEnoughInformation(const double &val) const

◆ expX3()

double StatisticsFinder::expX3 ( ) const

calculate and return the expectation value of X^3

Definition at line 161 of file StatisticsFinder.cpp.

161  {
162  if ( notEnoughInformation(_wSum3) == 0 ) return 0.0;
163  return _wSum3/_sumW;
164 }
bool notEnoughInformation(const double &val) const

◆ expX4()

double StatisticsFinder::expX4 ( ) const

calculate and return the expectation value of X^4

Definition at line 169 of file StatisticsFinder.cpp.

169  {
170  if ( notEnoughInformation(_wSum4) == 0 ) return 0.0;
171  return _wSum4/_sumW;
172 }
bool notEnoughInformation(const double &val) const

◆ fourthCentralMom()

double StatisticsFinder::fourthCentralMom ( ) const

calculate and return the second central moment

E[(X - E(X))^4] = E[X^4] - 4E[X]E[X^3] + 6E[X]^2 E[X^2] - 3 E[X]^4

Definition at line 188 of file StatisticsFinder.cpp.

188  {
189  return expX4() - 4.0*expX()*expX3() + 6.0*expX()*expX()*expX2() - 3.0*expX()*expX()*expX()*expX();
190 }
double expX4() const
double expX3() const
double expX() const
double expX2() const

◆ getMax()

const double& StatisticsFinder::getMax ( ) const
inline

max value added to the StatisticsFinder

Definition at line 62 of file StatisticsFinder.h.

◆ getMin()

const double& StatisticsFinder::getMin ( ) const
inline

min value added to the StatisticsFinder

Definition at line 61 of file StatisticsFinder.h.

◆ mean()

double StatisticsFinder::mean ( ) const

calculate and return the mean

Definition at line 110 of file StatisticsFinder.cpp.

110  {
111  return expX();
112 }
double expX() const

◆ meanError()

double StatisticsFinder::meanError ( ) const

calculate and return the error on the mean

Definition at line 117 of file StatisticsFinder.cpp.

117  {
118  return width() /sqrt(_nEvents);
119 }
double width() const

◆ median()

double StatisticsFinder::median ( ) const

The median

Definition at line 54 of file StatisticsFinder.cpp.

54  {
55  if ( needOrderedEvents() == 0) return 0.0;
57 
58  std::sort(_orderedEvents.begin(),_orderedEvents.end());
59 
60  int nEntries = _orderedEvents.size();
61 
62  if (nEntries % 2 == 0){
63  int lowi = (nEntries/2) - 1;
64  int highi = (nEntries/2);
65  return 0.5*(_orderedEvents.at(lowi) + _orderedEvents.at(highi));
66  }
67 
68  return _orderedEvents.at( (nEntries - 1)/2 );
69 
70 }
void warnIfWeightedEvents() const
std::vector< double > _orderedEvents
bool needOrderedEvents() const

◆ needOrderedEvents()

bool StatisticsFinder::needOrderedEvents ( ) const
private

A warning that is shown if ordered events are needed (but haven't been stored)

Definition at line 33 of file StatisticsFinder.cpp.

33  {
34  if (_keepOrderedEvents == 0){
35  ERROR_LOG << "You need to keep ordered events to calculate this value";
36  return 0;
37  }
38  return 1;
39 }
#define ERROR_LOG

◆ notEnoughInformation()

bool StatisticsFinder::notEnoughInformation ( const double &  val) const
private

A warning that is shown if not enough information has been provided to calculate what you want

Definition at line 76 of file StatisticsFinder.cpp.

76  {
77  if (val == NOT_STORED_VAL) {
78  ERROR_LOG << "You are not storing the correct information to calculate this value. Consider changing the constuctor";
79  return 0;
80  }
81  return 1;
82 }
#define ERROR_LOG
#define NOT_STORED_VAL

◆ numEvents()

double StatisticsFinder::numEvents ( ) const
inline

numer of events added to the StatisticsFinder

Definition at line 43 of file StatisticsFinder.h.

◆ range()

double StatisticsFinder::range ( ) const
inline

max - min value added to the StatisticsFinder

Definition at line 64 of file StatisticsFinder.h.

◆ secondCentralMom()

double StatisticsFinder::secondCentralMom ( ) const

calculate and return the second central moment

E[(X - E(X))^2] = E[X^2] - E[X]^2

Definition at line 179 of file StatisticsFinder.cpp.

179  {
180  return expX2() - expX()*expX();
181 }
double expX() const
double expX2() const

◆ varience()

double StatisticsFinder::varience ( ) const

calculate and return the varience

Definition at line 124 of file StatisticsFinder.cpp.

124  {
125  return secondCentralMom();
126 }
double secondCentralMom() const

◆ warnIfWeightedEvents()

void StatisticsFinder::warnIfWeightedEvents ( ) const
private

A warning that is shown if the value being returned is meaningless because weights have been used

Definition at line 45 of file StatisticsFinder.cpp.

45  {
46  if ((int)_sumW != _nEvents){
47  INFO_LOG << "You have used weighted events. This may make this function meaningless";
48  }
49 }
#define INFO_LOG

◆ width()

double StatisticsFinder::width ( ) const

calculate and return the width

Definition at line 131 of file StatisticsFinder.cpp.

131  {
132  return sqrt(secondCentralMom());
133 }
double secondCentralMom() const

◆ widthError()

double StatisticsFinder::widthError ( ) const

calculate and return the error on the width

Definition at line 138 of file StatisticsFinder.cpp.

138  {
139  return sqrt( sqrt( ( fourthCentralMom() - varience()*varience() )*(1.0/(8.0*_nEvents*_nEvents)) ) );
140 }
double fourthCentralMom() const
double varience() const

Member Data Documentation

◆ _keepOrderedEvents

int StatisticsFinder::_keepOrderedEvents
private

Keep a list of the values added (so the median can be found)

Definition at line 30 of file StatisticsFinder.h.

◆ _max

double StatisticsFinder::_max
private

The largest member added to the StatisticsFinder

Definition at line 18 of file StatisticsFinder.h.

◆ _min

double StatisticsFinder::_min
private

The smallest member added to the StatisticsFinder

Definition at line 17 of file StatisticsFinder.h.

◆ _nEvents

double StatisticsFinder::_nEvents
private

The number of events added to the StatisticsFinder

Definition at line 20 of file StatisticsFinder.h.

◆ _orderedEvents

std::vector<double> StatisticsFinder::_orderedEvents
mutableprivate

list of the values added

Definition at line 31 of file StatisticsFinder.h.

◆ _sumW

double StatisticsFinder::_sumW
private

The sum of weights

Definition at line 25 of file StatisticsFinder.h.

◆ _wSum

double StatisticsFinder::_wSum
private

The weighted sum of values added to the StatisticsFinder

Definition at line 21 of file StatisticsFinder.h.

◆ _wSum2

double StatisticsFinder::_wSum2
private

The weighted sum of values^2 added to the StatisticsFinder

Definition at line 22 of file StatisticsFinder.h.

◆ _wSum3

double StatisticsFinder::_wSum3
private

The weighted sum of values^3 added to the StatisticsFinder

Definition at line 23 of file StatisticsFinder.h.

◆ _wSum4

double StatisticsFinder::_wSum4
private

The weighted sum of values^4 added to the StatisticsFinder

Definition at line 24 of file StatisticsFinder.h.


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