1 #ifndef _theplu_yat_statistics_average_base
2 #define _theplu_yat_statistics_average_base
25 #include <boost/concept_check.hpp>
32 namespace statistics {
68 template<
class Derived>
77 BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Derived>));
90 void add(
double x,
long n=1) {
static_cast<Derived*
>(
this)->add_impl(x,
n);}
98 {
return n_ ?
mean_ : std::numeric_limits<double>::quiet_NaN(); }
105 long n(
void)
const {
return n_; }
112 void rescale(
double a) {
static_cast<Derived*
>(
this)->rescale_impl(a); }
122 *
static_cast<Derived*
>(
this) = tmp;
144 void add1(
double x,
long int n);
148 void add1(
double delta);
169 template<
class Derived>
198 {
return std::sqrt(
variance()/this->
n()); }
236 {
return sum_xx()/this->
n() + m*m - 2*m*this->
mean();}
259 std::numeric_limits<double>::quiet_NaN();
272 void add2(
double mean,
double cm2,
long int n);
277 void add2(
double delta);
295 template<
class Derived>
348 void add3(
double mean,
double cm2,
double cm3,
long int n);
353 void add3(
double delta);
369 template<
class Derived>
423 void add4(
double mean,
double cm2,
double cm3,
double cm4,
long int n);
428 void add4(
double delta);
439 template<
class Derived>
441 template<
class Derived>
443 template<
class Derived>
445 template<
class Derived>
448 template<
class Derived>
451 if (n == -this->n_) {
455 this->mean_ = (this->n_*this->mean_ + n*x) / (this->n_ + n);
460 template<
class Derived>
464 this->mean_ += delta * 1/(this->n_);
468 template<
class Derived>
471 if (n == -this->n_) {
475 const double delta = this->mean_ - m;
476 this->cm2_ += cm2 + this->n_*n*delta*delta/(this->n_+n);
481 template<
class Derived>
484 this->cm2_ += this->n_*delta*delta/(this->n_+1);
489 template<
class Derived>
493 if (n == -this->n_) {
497 const double n_inverse = 1.0/(this->n_+n);
498 const double delta = mean - this->mean_;
499 const double r = n_inverse*delta;
501 this->cm3_ += cm3 + r*r*delta*this->n_*n*(this->n_-n) +
502 3*r*(this->n_*cm2 - n*this->cm2_);
503 this->add2(mean, cm2, n);
507 template<
class Derived>
510 const double r = delta/(this->n_+1);
512 this->cm3_ += r*r*delta*this->n_*(this->n_-1) - 3*r*this->cm2_;
517 template<
class Derived>
519 double cm4,
long int n)
521 if (n == -this->n_) {
525 const double n_inverse = 1.0/(this->n_+n);
526 const double delta = mean - this->mean_;
527 const double r = n_inverse*delta;
530 std::pow(r,3)*delta*this->n_*n*(this->n_*this->n_-this->n_*n+n*n) +
531 6*r*r*(this->n_*this->n_*cm2+n*n*this->cm2_) +
532 4*r*(this->n_*cm3-n*this->cm3_);
533 this->add3(mean, cm2, cm3, n);
537 template<
class Derived>
540 const double r = delta/(this->n_+1);
543 std::pow(r,3)*delta*this->n_*(this->n_*this->n_-this->n_+1) +
551 template<
class Derived>
559 template<
class Derived>
564 this->cm2_ *= factor;
569 template<
class Derived>
572 double factor = x*this->rescale2(x);
573 this->cm3_ *= factor;
578 template<
class Derived>
581 double factor = x*this->rescale3(x);
582 this->cm3_ *= factor;
double rescale1(double x)
Definition: averager_base.h:552
double cm3_
Definition: averager_base.h:342
Definition: averager_base.h:370
averager_base2(void)
Constructor.
Definition: averager_base.h:176
double std(void) const
The standard deviation is defined as the square root of the variance.
Definition: averager_base.h:206
averager_base4(void)
Constructor.
Definition: averager_base.h:376
double cv(void) const
Coeffient of variation.
Definition: averager_base.h:191
virtual ~averager_base4(void)=0
Destructor.
Definition: averager_base.h:446
long int n_
Definition: averager_base.h:134
void add4(double mean, double cm2, double cm3, double cm4, long int n)
Definition: averager_base.h:518
double mean_
mean
Definition: averager_base.h:139
double rescale3(double x)
Definition: averager_base.h:570
Definition: averager_base.h:296
double central_moment3(void) const
Third central moment.
Definition: averager_base.h:315
void rescale(double a)
Rescales the object.
Definition: averager_base.h:112
Base class for averager classes.
Definition: averager_base.h:69
virtual ~averager_base3(void)=0
Destructor.
Definition: averager_base.h:444
void add2(double mean, double cm2, long int n)
Definition: averager_base.h:469
void reset(void)
Reset object.
Definition: averager_base.h:119
double variance(void) const
The estimated variance.
Definition: averager_base.h:246
double sum_x3_centered(void) const
Definition: averager_base.h:336
long n(void) const
number of data points
Definition: averager_base.h:105
Base class for averagers calculating mean and variance.
Definition: averager_base.h:170
void add(double x, long n=1)
add a data point
Definition: averager_base.h:90
double central_moment4(void) const
Fourth central moment.
Definition: averager_base.h:389
double rescale2(double x)
Definition: averager_base.h:560
double variance(double m) const
The variance with known mean.
Definition: averager_base.h:235
double skewness(void) const
Skewness.
Definition: averager_base.h:328
virtual ~averager_base(void)=0
Destructor.
Definition: averager_base.h:440
double sum_x4_centered(void) const
sum quadrupled centered
Definition: averager_base.h:411
void add1(double x, long int n)
Definition: averager_base.h:449
double sum_xx(void) const
Definition: averager_base.h:219
double mean(void) const
mean
Definition: averager_base.h:97
double rescale4(double x)
Definition: averager_base.h:579
double std(double m) const
The standard deviation is defined as the square root of the variance.
Definition: averager_base.h:214
double sum_xx_centered(void) const
Definition: averager_base.h:225
void add3(double mean, double cm2, double cm3, long int n)
Definition: averager_base.h:490
double cm2_
Definition: averager_base.h:266
double variance_unbiased(void) const
Definition: averager_base.h:256
averager_base3(void)
Constructor.
Definition: averager_base.h:302
virtual ~averager_base2(void)=0
Destructor.
Definition: averager_base.h:442
double cm4_
sum quadrupled centered
Definition: averager_base.h:416
double standard_error(void) const
Definition: averager_base.h:197
double kurtosis(void) const
Kurtosis.
Definition: averager_base.h:403
double sum_x(void) const
Definition: averager_base.h:128
averager_base(void)
Constructor.
Definition: averager_base.h:75