yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
Dbn1D.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of YODA -- Yet more Objects for Data Analysis
4 // Copyright (C) 2008-2017 The YODA collaboration (see AUTHORS for details)
5 //
6 #include "YODA/Dbn1D.h"
7 
8 namespace YODA {
9 
10 
11  double Dbn1D::xMean() const {
12  if (effNumEntries() == 0 || sumW() == 0) {
13  throw LowStatsError("Requested mean of a distribution with no net fill weights");
14  }
15  // This is ok, even for negative sum(w)
16  return sumWX()/sumW();
17  }
18 
19 
20  double Dbn1D::xVariance() const {
21  // Weighted variance defined as
22  // sig2 = ( sum(wx**2) * sum(w) - sum(wx)**2 ) / ( sum(w)**2 - sum(w**2) )
23  // see http://en.wikipedia.org/wiki/Weighted_mean
24  if (effNumEntries() == 0) {
25  throw LowStatsError("Requested variance of a distribution with no net fill weights");
26  } else if (fuzzyLessEquals(effNumEntries(), 1.0)) {
27  throw LowStatsError("Requested variance of a distribution with only one effective entry");
28  }
29  const double num = sumWX2()*sumW() - sqr(sumWX());
30  const double den = sqr(sumW()) - sumW2();
31  if (den==0.) {
32  throw WeightError("Undefined weighted variance");
33  }
37  // if (fabs(num) < 1e-10 && fabs(den) < 1e-10) {
38  // throw WeightError("Numerically unstable weights in width calculation");
39  // }
40  // The weighted variance as defined above
41  const double var = num/den;
44  return fabs(var);
45  }
46 
47 
48  double Dbn1D::xStdErr() const {
49  // Handle zero/negative sum weight
50  if (effNumEntries() == 0) {
51  throw LowStatsError("Requested std error of a distribution with no net fill weights");
52  }
54  return std::sqrt(xVariance() / effNumEntries());
55  }
56 
57 
58  double Dbn1D::xRMS() const {
59  // Weighted RMS defined as
60  // rms = sqrt(sum{w x^2} / sum{w})
61  if (effNumEntries() == 0) {
62  throw LowStatsError("Requested RMS of a distribution with no net fill weights");
63  }
64  const double meansq = sumWX2() / sumW();
65  return std::sqrt(meansq);
66  }
67 
68 
69  Dbn1D& Dbn1D::add(const Dbn1D& d) {
70  _dbnW += d._dbnW;
71  _sumWX += d._sumWX;
72  _sumWX2 += d._sumWX2;
73  return *this;
74  }
75 
76 
78  _dbnW -= d._dbnW;
79  _sumWX -= d._sumWX;
80  _sumWX2 -= d._sumWX2;
81  return *this;
82  }
83 
84 
85 }
double sumWX2() const
The sum of x^2*weight.
Definition: Dbn1D.h:167
Dbn1D & subtract(const Dbn1D &d)
Subtract one dbn from another (internal, explicitly named version)
Definition: Dbn1D.cc:77
double xMean() const
Weighted mean, , of distribution.
Definition: Dbn1D.cc:11
double sumWX() const
The sum of x*weight.
Definition: Dbn1D.h:162
double sumW() const
The sum of weights.
Definition: Dbn1D.h:152
double xVariance() const
Weighted variance, , of distribution.
Definition: Dbn1D.cc:20
double xRMS() const
Weighted RMS, , of distribution.
Definition: Dbn1D.cc:58
double xStdErr() const
Weighted standard error on the mean, , of distribution.
Definition: Dbn1D.cc:48
NUM sqr(NUM a)
Named number-type squaring operation.
Definition: MathUtils.h:207
Errors relating to insufficient (effective) statistics.
Definition: Exceptions.h:72
Errors relating to event/bin weights.
Definition: Exceptions.h:65
Dbn1D & add(const Dbn1D &d)
Add two dbns (internal, explicitly named version)
Definition: Dbn1D.cc:69
double effNumEntries() const
Effective number of entries .
Definition: Dbn1D.h:147
double sumW2() const
The sum of weights squared.
Definition: Dbn1D.h:157
A 1D distribution.
Definition: Dbn1D.h:28
bool fuzzyLessEquals(double a, double b, double tolerance=1E-5)
Compare two floating point numbers for <= with a degree of fuzziness.
Definition: MathUtils.h:115