Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

The Statistical Accumulators Library

count
covariance
density
error_of<mean>
extended_p_square
extended_p_square_quantile and variants
kurtosis
max
mean and variants
median and variants
min
moment
p_square_cumulative_distribution
p_square_quantile and variants
peaks_over_threshold and variants
pot_quantile and variants
pot_tail_mean
skewness
sum and variants
tail
coherent_tail_mean
non_coherent_tail_mean
tail_quantile
tail_variate
tail_variate_means and variants
variance and variants
weighted_covariance
weighted_density
weighted_extended_p_square
weighted_kurtosis
weighted_mean and variants
weighted_median and variants
weighted_moment
weighted_p_square_cumulative_distribution
weighted_p_square_quantile and variants
weighted_peaks_over_threshold and variants
weighted_skewness
weighted_sum and variants
non_coherent_weighted_tail_mean
weighted_tail_quantile
weighted_tail_variate_means and variants
weighted_variance and variants

The Statistical Accumulators Library defines accumulators for incremental statistial computations. It is built on top of The Accumulator Framework.

The count feature is a simple counter that tracks the number of samples pushed into the accumulator set.

Result Type

std::size_t

Depends On

none

Variants

none

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/count.hpp>

Example

accumulator_set<int, features<tag::count> > acc;
acc(0);
acc(0);
acc(0);
assert(3 == count(acc));

See also

The covariance feature is an iterative Monte Carlo estimator for the covariance. It is specified as tag::covariance<variate-type, variate-tag>.

Result Type

numeric::functional::outer_product<
    numeric::functional::average<sample-type, std::size_t>::result_type
  , numeric::functional::average<variate-type, std::size_t>::result_type
>::result_type

Depends On

count
mean
mean_of_variates<variate-type, variate-tag>

Variants

abstract_covariance

Initialization Parameters

none

Accumulator Parameters

variate-tag

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/covariance.hpp>

Example

accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc;
acc(1., covariate1 = 2.);
acc(1., covariate1 = 4.);
acc(2., covariate1 = 3.);
acc(6., covariate1 = 1.);
assert(covariance(acc) == -1.75);

See also

The tag::density feature returns a histogram of the sample distribution. For more implementation details, see density_impl.

Result Type

iterator_range<
    std::vector<
        std::pair<
            numeric::functional::average<sample-type, std::size_t>::result_type
          , numeric::functional::average<sample-type, std::size_t>::result_type
        >
    >::iterator
>

Depends On

count
min
max

Variants

none

Initialization Parameters

density::cache_size
density::num_bins

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(N), when N is density::num_bins

Header

#include <boost/accumulators/statistics/density.hpp>

See also

The error_of<mean> feature calculates the error of the mean feature. It is equal to sqrt(variance / (count - 1)).

Result Type

numeric::functional::average<sample-type, std::size_t>::result_type

Depends On

count
variance

Variants

error_of<immediate_mean>

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/error_of.hpp>
#include <boost/accumulators/statistics/error_of_mean.hpp>

Example

accumulator_set<double, stats<tag::error_of<tag::mean> > > acc;
acc(1.1);
acc(1.2);
acc(1.3);
assert(0.057735 == error_of<tag::mean>(acc));

See also

Multiple quantile estimation with the extended P^2 algorithm. For further details, see extended_p_square_impl.

Result Type

boost::iterator_range<
    implementation-defined
>

Depends On

count

Variants

none

Initialization Parameters

tag::extended_p_square::probabilities

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/extended_p_square.hpp>

Example

boost::array<double> probs = {0.001,0.01,0.1,0.25,0.5,0.75,0.9,0.99,0.999};
accumulator_set<double, stats<tag::extended_p_square> >
    acc(tag::extended_p_square::probabilities = probs);

boost::lagged_fibonacci607 rng; // a random number generator
for (int i=0; i<10000; ++i)
    acc(rng());

BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], 25);
BOOST_CHECK_CLOSE(extended_p_square(acc)[1], probs[1], 10);
BOOST_CHECK_CLOSE(extended_p_square(acc)[2], probs[2], 5);

for (std::size_t i=3; i < probs.size(); ++i)
{
    BOOST_CHECK_CLOSE(extended_p_square(acc)[i], probs[i], 2);
}

See also

Quantile estimation using the extended P^2 algorithm for weighted and unweighted samples. By default, the calculation is linear and unweighted, but quadratic and weighted variants are also provided. For further implementation details, see extended_p_square_quantile_impl.

All the variants share the tag::quantile feature and can be extracted using the quantile() extractor.

Result Type

numeric::functional::average<sample-type, std::size_t>::result_type

Depends On

weighted variants depend on weighted_extended_p_square
unweighted variants depend on extended_p_square

Variants

extended_p_square_quantile_quadratic
weighted_extended_p_square_quantile
weighted_extended_p_square_quantile_quadratic

Initialization Parameters

tag::extended_p_square::probabilities

Accumulator Parameters

weight for the weighted variants

Extractor Parameters

quantile_probability

Accumulator Complexity

TODO

Extractor Complexity

O(N) where N is the count of probabilities.

Header

#include <boost/accumulators/statistics/extended_p_square_quantile.hpp>

Example

typedef accumulator_set<double, stats<tag::extended_p_square_quantile> >
    accumulator_t;
typedef accumulator_set<double, stats<tag::weighted_extended_p_square_quantile>, double >
    accumulator_t_weighted;
typedef accumulator_set<double, stats<tag::extended_p_square_quantile(quadratic)> >
    accumulator_t_quadratic;
typedef accumulator_set<double, stats<tag::weighted_extended_p_square_quantile(quadratic)>, double >
    accumulator_t_weighted_quadratic;

// tolerance
double epsilon = 1;

// a random number generator
boost::lagged_fibonacci607 rng;

boost::array<double> probs = { 0.990, 0.991, 0.992, 0.993, 0.994, 
                               0.995, 0.996, 0.997, 0.998, 0.999 };
accumulator_t acc(extended_p_square_probabilities = probs);
accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs);
accumulator_t_quadratic acc2(extended_p_square_probabilities = probs);
accumulator_t_weighted_quadratic acc_weighted2(extended_p_square_probabilities = probs);

for (int i=0; i<10000; ++i)
{
    double sample = rng();
    acc(sample);
    acc2(sample);
    acc_weighted(sample, weight = 1.);
    acc_weighted2(sample, weight = 1.);
}

for (std::size_t i = 0; i < probs.size() - 1; ++i)
{
    BOOST_CHECK_CLOSE(
        quantile(acc, quantile_probability = 0.99025 + i*0.001)
      , 0.99025 + i*0.001
      , epsilon
    );
    BOOST_CHECK_CLOSE(
        quantile(acc2, quantile_probability = 0.99025 + i*0.001)
      , 0.99025 + i*0.001
      , epsilon
    );
    BOOST_CHECK_CLOSE(
        quantile(acc_weighted, quantile_probability = 0.99025 + i*0.001)
      , 0.99025 + i*0.001
      , epsilon
    );
    BOOST_CHECK_CLOSE(
        quantile(acc_weighted2, quantile_probability = 0.99025 + i*0.001)
      , 0.99025 + i*0.001
      , epsilon
    );
}

See also

The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central moment (the variance) of the samples, minus 3. The term -3 is added in order to ensure that the normal distribution has zero kurtosis. For more implementation details, see kurtosis_impl

Result Type

numeric::functional::average<sample-type, sample-type>::result_type

Depends On

mean
moment<2>
moment<3>
moment<4>

Variants

none

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/kurtosis.hpp>

Example

accumulator_set<int, stats<tag::kurtosis > > acc;
    
acc(2);
acc(7);
acc(4);
acc(9);
acc(3);

BOOST_CHECK_EQUAL( mean(acc), 5 );
BOOST_CHECK_EQUAL( moment<2>(acc), 159./5. );
BOOST_CHECK_EQUAL( moment<3>(acc), 1171./5. );
BOOST_CHECK_EQUAL( moment<4>(acc), 1863 );
BOOST_CHECK_CLOSE( kurtosis(acc), -1.39965397924, 1e-6 );

See also

Calculates the maximum value of all the samples.

Result Type

sample-type

Depends On

none

Variants

none

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/max.hpp>

Example

accumulator_set<int, stats<tag::max> > acc;

acc(1);
BOOST_CHECK_EQUAL(1, (max)(acc));

acc(0);
BOOST_CHECK_EQUAL(1, (max)(acc));

acc(2);
BOOST_CHECK_EQUAL(2, (max)(acc));

See also

Calculates the mean of samples, weights or variates. The calculation is either lazy (in the result extractor), or immediate (in the accumulator). The lazy implementation is the default. For more implementation details, see mean_impl or. immediate_mean_impl

Result Type

For samples, numeric::functional::average<sample-type, std::size_t>::result_type
For weights, numeric::functional::average<weight-type, std::size_t>::result_type
For variates, numeric::functional::average<variate-type, std::size_t>::result_type

Depends On

count
The lazy mean of samples depends on sum
The lazy mean of weights depends on sum_of_weights
The lazy mean of variates depends on sum_of_variates<>

Variants

mean_of_weights
mean_of_variates<variate-type, variate-tag>
immediate_mean
immediate_mean_of_weights
immediate_mean_of_variates<variate-type, variate-tag>

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/mean.hpp>

Example

accumulator_set<
    int
  , stats<
        tag::mean
      , tag::mean_of_weights
      , tag::mean_of_variates<int, tag::covariate1>
    >
  , int 
> acc;

acc(1, weight = 2, covariate1 = 3);
BOOST_CHECK_CLOSE(1., mean(acc), 1e-5);
BOOST_CHECK_EQUAL(1u, count(acc));
BOOST_CHECK_EQUAL(2, sum(acc));
BOOST_CHECK_CLOSE(2., mean_of_weights(acc), 1e-5);
BOOST_CHECK_CLOSE(3., (mean_of_variates<int, tag::covariate1>(acc)), 1e-5);

acc(0, weight = 4, covariate1 = 4);
BOOST_CHECK_CLOSE(0.33333333333333333, mean(acc), 1e-5);
BOOST_CHECK_EQUAL(2u, count(acc));
BOOST_CHECK_EQUAL(2, sum(acc));
BOOST_CHECK_CLOSE(3., mean_of_weights(acc), 1e-5);
BOOST_CHECK_CLOSE(3.5, (mean_of_variates<int, tag::covariate1>(acc)), 1e-5);

acc(2, weight = 9, covariate1 = 8);
BOOST_CHECK_CLOSE(1.33333333333333333, mean(acc), 1e-5);
BOOST_CHECK_EQUAL(3u, count(acc));
BOOST_CHECK_EQUAL(20, sum(acc));
BOOST_CHECK_CLOSE(5., mean_of_weights(acc), 1e-5);
BOOST_CHECK_CLOSE(5., (mean_of_variates<int, tag::covariate1>(acc)), 1e-5);

accumulator_set<
    int
  , stats<
        tag::mean(immediate)
      , tag::mean_of_weights(immediate)
      , tag::mean_of_variates<int, tag::covariate1>(immediate)
    >
  , int
> acc2;

acc2(1, weight = 2, covariate1 = 3);
BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5);
BOOST_CHECK_EQUAL(1u, count(acc2));
BOOST_CHECK_CLOSE(2., mean_of_weights(acc2), 1e-5);
BOOST_CHECK_CLOSE(3., (mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);
    
acc2(0, weight = 4, covariate1 = 4);
BOOST_CHECK_CLOSE(0.33333333333333333, mean(acc2), 1e-5);
BOOST_CHECK_EQUAL(2u, count(acc2));
BOOST_CHECK_CLOSE(3., mean_of_weights(acc2), 1e-5);
BOOST_CHECK_CLOSE(3.5, (mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);

acc2(2, weight = 9, covariate1 = 8);
BOOST_CHECK_CLOSE(1.33333333333333333, mean(acc2), 1e-5);
BOOST_CHECK_EQUAL(3u, count(acc2));
BOOST_CHECK_CLOSE(5., mean_of_weights(acc2), 1e-5);
BOOST_CHECK_CLOSE(5., (mean_of_variates<int, tag::covariate1>(acc2)), 1e-5);

See also

Median estimation based on the P^2 quantile estimator, the density estimator, or the P^2 cumulative distribution estimator. For more implementation details, see median_impl, with_density_median_impl, and with_p_square_cumulative_distribution_median_impl.

The three median accumulators all satisfy the tag::median feature, and can all be extracted with the median() extractor.

Result Type

numeric::functional::average<sample-type, std::size_t>::result_type

Depends On

median depends on p_square_quantile_for_median
with_density_median depends on count and density
with_p_square_cumulative_distribution_median depends on p_square_cumulative_distribution

Variants

with_density_median
with_p_square_cumulative_distribution_median

Initialization Parameters

with_density_median requires tag::density::cache_size and tag::density::num_bins
with_p_square_cumulative_distribution_median requires tag::p_square_cumulative_distribution::num_cells

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

TODO

Header

#include <boost/accumulators/statistics/median.hpp>

Example

// two random number generators
double mu = 1.;
boost::lagged_fibonacci607 rng;
boost::normal_distribution<> mean_sigma(mu,1);
boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> >
    normal(rng, mean_sigma);

accumulator_set<double, stats<tag::median(with_p_square_quantile) > > acc;
accumulator_set<double, stats<tag::median(with_density) > > 
    acc_dens( density_cache_size = 10000, density_num_bins = 1000 );
accumulator_set<double, stats<tag::median(with_p_square_cumulative_distribution) > > 
    acc_cdist( p_square_cumulative_distribution_num_cells = 100 );
    
for (std::size_t i=0; i<100000; ++i)
{
    double sample = normal();
    acc(sample);
    acc_dens(sample);
    acc_cdist(sample);
}

BOOST_CHECK_CLOSE(1., median(acc), 1.);
BOOST_CHECK_CLOSE(1., median(acc_dens), 1.);
BOOST_CHECK_CLOSE(1., median(acc_cdist), 3.);

See also

Calculates the minimum value of all the samples.

Result Type

sample-type

Depends On

none

Variants

none

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/min.hpp>

Example

accumulator_set<int, stats<tag::min> > acc;

acc(1);
BOOST_CHECK_EQUAL(1, (min)(acc));

acc(0);
BOOST_CHECK_EQUAL(0, (min)(acc));

acc(2);
BOOST_CHECK_EQUAL(0, (min)(acc));

See also

Calculates the N-th moment of the samples, which is defined as the sum of the N-th power of the samples over the count of samples.

Result Type

numeric::functional::average<sample-type, std::size_t>::result_type

Depends On

count

Variants

none

Initialization Parameters

none

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

O(1)

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/moment.hpp>

Example

accumulator_set<int, stats<tag::moment<2> > > acc1;

acc1(2); //    4
acc1(4); //   16
acc1(5); // + 25
         // = 45 / 3 = 15

BOOST_CHECK_CLOSE(15., moment<2>(acc1), 1e-5);

accumulator_set<int, stats<tag::moment<5> > > acc2;

acc2(2); //     32
acc2(3); //    243
acc2(4); //   1024
acc2(5); // + 3125
         // = 4424 / 4 = 1106

BOOST_CHECK_CLOSE(1106., moment<5>(acc2), 1e-5);

See also

Histogram calculation of the cumulative distribution with the P^2 algorithm. For more implementation details, see p_square_cumulative_distribution_impl

Result Type

iterator_range<
    std::vector<
        std::pair<
            numeric::functional::average<sample-type, std::size_t>::result_type
          , numeric::functional::average<sample-type, std::size_t>::result_type
        >
    >::iterator
>

Depends On

count

Variants

none

Initialization Parameters

tag::p_square_cumulative_distribution::num_cells

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(N) where N is num_cells

Header

#include <boost/accumulators/statistics/p_square_cumulative_distribution.hpp>

Example

// tolerance in %
double epsilon = 3;

typedef accumulator_set<double, stats<tag::p_square_cumulative_distribution> > accumulator_t;
    
accumulator_t acc(tag::p_square_cumulative_distribution::num_cells = 100);

// two random number generators
boost::lagged_fibonacci607 rng;
boost::normal_distribution<> mean_sigma(0,1);
boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma);

for (std::size_t i=0; i<100000; ++i)
{
    acc(normal());
}

typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type;
histogram_type histogram = p_square_cumulative_distribution(acc);

for (std::size_t i = 0; i < histogram.size(); ++i)
{   
    // problem with small results: epsilon is relative (in percent), not absolute!
    if ( histogram[i].second > 0.001 )    
        BOOST_CHECK_CLOSE( 0.5 * (1.0 + erf( histogram[i].first / sqrt(2.0) )), histogram[i].second, epsilon );
}        

See also

Single quantile estimation with the P^2 algorithm. For more implementation details, see p_square_quantile_impl

Result Type

numeric::functional::average<sample-type, std::size_t>::result_type

Depends On

count

Variants

p_square_quantile_for_median

Initialization Parameters

quantile_probability, which defaults to 0.5. (Note: for p_square_quantile_for_median, the quantile_probability parameter is ignored and is always 0.5.)

Accumulator Parameters

none

Extractor Parameters

none

Accumulator Complexity

TODO

Extractor Complexity

O(1)

Header

#include <boost/accumulators/statistics/p_square_quantile.hpp>

Example

typedef accumulator_set<double, stats<tag::p_square_quantile