diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:24:20 +0000 |
commit | 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch) | |
tree | e5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/accumulators/test | |
parent | Initial commit. (diff) | |
download | ceph-upstream.tar.xz ceph-upstream.zip |
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/accumulators/test')
52 files changed, 5434 insertions, 0 deletions
diff --git a/src/boost/libs/accumulators/test/Jamfile.v2 b/src/boost/libs/accumulators/test/Jamfile.v2 new file mode 100644 index 00000000..6d2dbf06 --- /dev/null +++ b/src/boost/libs/accumulators/test/Jamfile.v2 @@ -0,0 +1,139 @@ +# (C) Copyright 2005: Eric Niebler +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# bring in rules for testing +import testing ; +import feature ; + +feature.feature iterator_debugging + : on off + : composite propagated optional + ; + +feature.compose <iterator_debugging>off + : <define>_HAS_ITERATOR_DEBUGGING=0 + ; + +project + : requirements + <library>/boost/test//boost_unit_test_framework + <link>static + <include>../../.. + <toolset>msvc:<define>_SCL_SECURE_NO_DEPRECATE + # MSVC's iterator debugging causes some tests to run forever. + <toolset>msvc:<iterator_debugging>off + <toolset>intel-win:<iterator_debugging>off + <cxxflags>"-Wno-deprecated-declarations" + <library>/boost/serialization + ; + +alias accumulators_regular_tests + : [ run count.cpp ] + [ run covariance.cpp ] + [ run droppable.cpp ] + [ run error_of.cpp ] + [ run extended_p_square.cpp ] + [ run extended_p_square_quantile.cpp ] + [ run external_accumulator.cpp ] + [ run external_weights.cpp ] + [ run kurtosis.cpp ] + [ run max.cpp ] + [ run mean.cpp ] + [ run median.cpp ] + [ run min.cpp ] + [ run moment.cpp ] + [ run p_square_cumul_dist.cpp ] + [ run p_square_quantile.cpp ] + [ run reference.cpp ] + [ run rolling_count.cpp ] + [ run rolling_sum.cpp ] + [ run rolling_mean.cpp ] + [ run rolling_variance.cpp ] + [ run rolling_moment.cpp ] + [ run skewness.cpp ] + [ run sum.cpp ] + [ run sum_kahan.cpp ] + [ run tail.cpp ] + [ run tail_mean.cpp ] + [ run tail_quantile.cpp ] + [ run variance.cpp ] + [ run vector.cpp ] + [ run weighted_covariance.cpp ] + [ run weighted_extended_p_square.cpp ] + [ run weighted_kurtosis.cpp ] + [ run weighted_mean.cpp ] + [ run weighted_median.cpp ] + [ run weighted_moment.cpp ] + [ run weighted_p_square_cumul_dist.cpp ] + [ run weighted_p_square_quantile.cpp ] + [ run weighted_skewness.cpp ] + [ run weighted_sum.cpp ] + [ run weighted_sum_kahan.cpp ] + [ run weighted_variance.cpp ] + ; + +alias accumulators_test_valarray + : + : <target-os>linux + <toolset>clang +# TODO: Find the correct attribute that detects libc++. + ; + +alias accumulators_test_valarray + : + : <target-os>darwin + ; + +alias accumulators_test_valarray + : [ run valarray.cpp ] + ; + +alias accumulators_test_tail_variate_means + : + : <toolset>msvc + <toolset-msvc:version>14.0 + ; + +alias accumulators_test_tail_variate_means + : [ run tail_variate_means.cpp ] + ; + +alias accumulators_tests_pot_quantile_and_weighted_tail + : + : <target-os>linux + <toolset>gcc + <toolset-gcc:version>4.4.7 + ; + +alias accumulators_tests_pot_quantile_and_weighted_tail + : [ run pot_quantile.cpp ] + [ run weighted_pot_quantile.cpp ] + [ run weighted_tail_mean.cpp ] + [ run weighted_tail_quantile.cpp ] + ; + +alias accumulators_test_weighted_tail_variate_means + : + : <target-os>linux + <toolset>gcc + <toolset-gcc:version>4.4.7 + ; + +alias accumulators_test_weighted_tail_variate_means + : + : <toolset>msvc + <toolset-msvc:version>14.0 + ; + +alias accumulators_test_weighted_tail_variate_means + : [ run weighted_tail_variate_means.cpp ] + ; + +test-suite "accumulators" + : accumulators_regular_tests + accumulators_test_valarray + accumulators_test_tail_variate_means + accumulators_tests_pot_quantile_and_weighted_tail + accumulators_test_weighted_tail_variate_means + ; diff --git a/src/boost/libs/accumulators/test/count.cpp b/src/boost/libs/accumulators/test/count.cpp new file mode 100644 index 00000000..e2f4436b --- /dev/null +++ b/src/boost/libs/accumulators/test/count.cpp @@ -0,0 +1,77 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/count.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::count> > acc; + + acc(1); + BOOST_CHECK_EQUAL(1u, count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(2u, count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(3u, count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(4u, count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(5u, count(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + { + accumulator_set<int, stats<tag::count> > acc; + acc(1); + acc(1); + acc(1); + acc(1); + BOOST_CHECK_EQUAL(4u, count(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::count> > acc; + BOOST_CHECK_EQUAL(0u, count(acc)); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(4u, count(acc)); + +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("count test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/covariance.cpp b/src/boost/libs/accumulators/test/covariance.cpp new file mode 100644 index 00000000..abdbc9e8 --- /dev/null +++ b/src/boost/libs/accumulators/test/covariance.cpp @@ -0,0 +1,135 @@ +// (C) Copyright 2005 Daniel Egloff, Eric Niebler +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/covariance.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + std::vector<double> dummy; + dummy.push_back(0); + dummy.push_back(0); + + accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc; + accumulator_set<std::vector<double>, stats<tag::covariance<double, tag::covariate1> > > acc2(sample = dummy); + accumulator_set<double, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc3(covariate1 = dummy); + accumulator_set<std::vector<double>, stats<tag::covariance<std::vector<double>, tag::covariate1> > > acc4(sample = dummy, covariate1 = dummy); + + std::vector<double> a; + a.push_back(1.); + a.push_back(2.); + std::vector<double> b; + b.push_back(3.); + b.push_back(4.); + std::vector<double> c; + c.push_back(2.); + c.push_back(5.); + std::vector<double> d; + d.push_back(4.); + d.push_back(2.); + + // double - double + { + acc(1., covariate1 = 2.); + acc(1., covariate1 = 4.); + acc(2., covariate1 = 3.); + acc(6., covariate1 = 1.); + } + + // vector - double + { + acc2(a, covariate1 = 1.); + acc2(b, covariate1 = 1.); + acc2(c, covariate1 = 2.); + acc2(d, covariate1 = 6.); + } + + // double - vector + { + acc3(1., covariate1 = a); + acc3(1., covariate1 = b); + acc3(2., covariate1 = c); + acc3(6., covariate1 = d); + } + + // vector - vector + { + acc4(a, covariate1 = b); + acc4(b, covariate1 = c); + acc4(a, covariate1 = c); + acc4(d, covariate1 = b); + } + + double epsilon = 1e-6; + + BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon); + BOOST_CHECK_CLOSE((covariance(acc2))[0], 1.75, epsilon); + BOOST_CHECK_CLOSE((covariance(acc2))[1], -1.125, epsilon); + BOOST_CHECK_CLOSE((covariance(acc3))[0], 1.75, epsilon); + BOOST_CHECK_CLOSE((covariance(acc3))[1], -1.125, epsilon); + BOOST_CHECK_CLOSE((covariance(acc4))(0,0), 0.125, epsilon); + BOOST_CHECK_CLOSE((covariance(acc4))(0,1), -0.25, epsilon); + BOOST_CHECK_CLOSE((covariance(acc4))(1,0), -0.125, epsilon); + BOOST_CHECK_CLOSE((covariance(acc4))(1,1), 0.25, epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance + double epsilon = 1e-6; + { + + 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.); + + + BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<double, stats<tag::covariance<double, tag::covariate1> > > acc; + BOOST_CHECK_CLOSE((covariance(acc)), 0., epsilon); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE((covariance(acc)), -1.75, epsilon); + +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("covariance test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/droppable.cpp b/src/boost/libs/accumulators/test/droppable.cpp new file mode 100644 index 00000000..1ab1f140 --- /dev/null +++ b/src/boost/libs/accumulators/test/droppable.cpp @@ -0,0 +1,142 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<droppable<tag::mean> > > acc, test_acc(sample = 0); + + acc(1); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc.drop<tag::mean>(); + + acc(2); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + assert_is_double(mean(acc)); + + + accumulator_set<int, stats<droppable<tag::mean(immediate)> > > acc2, test_acc2(sample = 0); + + acc2(1); + BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc2)); + + acc2(0); + BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc2)); + + acc2.drop<tag::mean>(); + + acc2(2); + BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc2)); + + assert_is_double(mean(acc2)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat2 +// +void test_stat2() +{ + accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::mean> > > acc, test_acc(sample = 0); + + acc(1); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc.drop<tag::mean>(); + acc.drop<tag::sum>(); + + acc(2); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + assert_is_double(mean(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat3 +// +void test_stat3() +{ + accumulator_set<int, stats<droppable<tag::sum>, droppable<tag::count>, droppable<tag::mean> > > acc, test_acc(sample = 0); + + acc(1); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc.drop<tag::mean>(); + acc.drop<tag::sum>(); + + acc(2); + BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc.drop<tag::count>(); + acc(3); + BOOST_CHECK_CLOSE(1./3., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + assert_is_double(mean(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("droppable test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_stat2)); + test->add(BOOST_TEST_CASE(&test_stat3)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/error_of.cpp b/src/boost/libs/accumulators/test/error_of.cpp new file mode 100644 index 00000000..05890239 --- /dev/null +++ b/src/boost/libs/accumulators/test/error_of.cpp @@ -0,0 +1,57 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/with_error.hpp> +#include <boost/accumulators/statistics/error_of_mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<double, stats<tag::error_of<tag::mean(lazy)> > > acc; + acc(1.1); + acc(1.2); + acc(1.3); + BOOST_CHECK_CLOSE(0.057735, accumulators::error_of<tag::mean(lazy)>(acc), 1e-4); + + accumulator_set<double, stats<tag::error_of<tag::mean(immediate)> > > acc2; + acc2(1.1); + acc2(1.2); + acc2(1.3); + BOOST_CHECK_CLOSE(0.057735, accumulators::error_of<tag::mean(immediate)>(acc2), 1e-4); + + accumulator_set<double, stats<with_error<tag::mean(lazy)> > > acc3; + acc3(1.1); + acc3(1.2); + acc3(1.3); + BOOST_CHECK_CLOSE(0.057735, accumulators::error_of<tag::mean(lazy)>(acc3), 1e-4); + + accumulator_set<double, stats<with_error<tag::mean(immediate)> > > acc4; + acc4(1.1); + acc4(1.2); + acc4(1.3); + BOOST_CHECK_CLOSE(0.057735, accumulators::error_of<tag::mean(immediate)>(acc4), 1e-4); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("mean test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/extended_p_square.cpp b/src/boost/libs/accumulators/test/extended_p_square.cpp new file mode 100644 index 00000000..09f50cf8 --- /dev/null +++ b/src/boost/libs/accumulators/test/extended_p_square.cpp @@ -0,0 +1,110 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for extended_p_square.hpp + +#include <iostream> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/extended_p_square.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +typedef accumulator_set<double, stats<tag::extended_p_square> > accumulator_t; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + + // tolerance + double epsilon = 3; + + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector<double> probs; + + probs.push_back(0.001); + probs.push_back(0.01 ); + probs.push_back(0.1 ); + probs.push_back(0.25 ); + probs.push_back(0.5 ); + probs.push_back(0.75 ); + probs.push_back(0.9 ); + probs.push_back(0.99 ); + probs.push_back(0.999); + + accumulator_t acc(extended_p_square_probabilities = probs); + + for (int i=0; i<10000; ++i) + acc(rng()); + + BOOST_CHECK_GE(extended_p_square(acc)[0], 0.0005); + BOOST_CHECK_LE(extended_p_square(acc)[0], 0.0015); + BOOST_CHECK_CLOSE(extended_p_square(acc)[1], probs[1], 15); + 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], epsilon); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance + double epsilon = 3.; + std::vector<double> probs; + probs.push_back(0.75); + { + accumulator_t acc(extended_p_square_probabilities = probs); + // a random number generator + boost::lagged_fibonacci607 rng; + + for (int i=0; i<10000; ++i) + acc(rng()); + + BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_t acc(extended_p_square_probabilities = probs); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(extended_p_square(acc)[0], probs[0], epsilon); + +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("extended_p_square test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/extended_p_square_quantile.cpp b/src/boost/libs/accumulators/test/extended_p_square_quantile.cpp new file mode 100644 index 00000000..7d602012 --- /dev/null +++ b/src/boost/libs/accumulators/test/extended_p_square_quantile.cpp @@ -0,0 +1,174 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for extended_p_square_quantile.hpp + +#include <iostream> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/extended_p_square_quantile.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +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; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance + double epsilon = 1; + + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector<double> probs; + + probs.push_back(0.990); + probs.push_back(0.991); + probs.push_back(0.992); + probs.push_back(0.993); + probs.push_back(0.994); + probs.push_back(0.995); + probs.push_back(0.996); + probs.push_back(0.997); + probs.push_back(0.998); + probs.push_back(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 + ); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + + // tolerance + double epsilon = 1.; + + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector<double> probs; + probs.push_back(0.990); + probs.push_back(0.991); + probs.push_back(0.992); + probs.push_back(0.993); + probs.push_back(0.994); + probs.push_back(0.995); + probs.push_back(0.996); + probs.push_back(0.997); + probs.push_back(0.998); + probs.push_back(0.999); + + { + accumulator_t acc(extended_p_square_probabilities = probs); + accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs); + + for (int i=0; i<10000; ++i) + { + double sample = rng(); + acc(sample); + acc_weighted(sample, weight = 1.); + } + + BOOST_CHECK_CLOSE( + quantile(acc, quantile_probability = 0.99025) + , 0.99025 + , epsilon + ); + BOOST_CHECK_CLOSE( + quantile(acc_weighted, quantile_probability = 0.99025) + , 0.99025 + , epsilon + ); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + acc_weighted.serialize(oa, 0); + } + + accumulator_t acc(extended_p_square_probabilities = probs); + accumulator_t_weighted acc_weighted(extended_p_square_probabilities = probs); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + acc_weighted.serialize(ia, 0); + BOOST_CHECK_CLOSE( + quantile(acc, quantile_probability = 0.99025) + , 0.99025 + , epsilon + ); + BOOST_CHECK_CLOSE( + quantile(acc_weighted, quantile_probability = 0.99025) + , 0.99025 + , epsilon + ); +} +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("extended_p_square_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/external_accumulator.cpp b/src/boost/libs/accumulators/test/external_accumulator.cpp new file mode 100644 index 00000000..ad8d5bd0 --- /dev/null +++ b/src/boost/libs/accumulators/test/external_accumulator.cpp @@ -0,0 +1,129 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +namespace my +{ + BOOST_PARAMETER_KEYWORD(tag, sum_acc) +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::mean, tag::external<tag::sum, my::tag::sum_acc> > > acc; + accumulator_set<int, stats<tag::sum> > sum_acc; + + acc(1); + sum_acc(1); + BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(sum_acc)); + + acc(0); + sum_acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc, my::sum_acc = sum_acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc, my::sum_acc = sum_acc)); + + acc(2); + sum_acc(2); + BOOST_CHECK_CLOSE(1., mean(acc, my::sum_acc = sum_acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(3, sum(acc, my::sum_acc = sum_acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_reference +// +void test_reference() +{ + typedef accumulator_set<int, stats<tag::sum> > sum_acc_type; + sum_acc_type sum_acc; + accumulator_set< + int + , stats< + tag::mean + , tag::external<tag::sum, my::tag::sum_acc> + , tag::reference<sum_acc_type, my::tag::sum_acc> + > + > acc(my::sum_acc = sum_acc); + + acc(1); + sum_acc(1); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(sum_acc)); + + acc(0); + sum_acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc(2); + sum_acc(2); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(3, sum(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_reference2 +// +void test_reference2() +{ + typedef accumulator_set<int, stats<tag::sum> > sum_acc_type; + sum_acc_type sum_acc; + accumulator_set< + int + , stats< + tag::mean + , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type> + > + > acc(my::sum_acc = sum_acc); + + acc(1); + sum_acc(1); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(sum_acc)); + + acc(0); + sum_acc(0); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + + acc(2); + sum_acc(2); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(3, sum(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("external_accumulator test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_reference)); + test->add(BOOST_TEST_CASE(&test_reference2)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/external_weights.cpp b/src/boost/libs/accumulators/test/external_weights.cpp new file mode 100644 index 00000000..4a904b2e --- /dev/null +++ b/src/boost/libs/accumulators/test/external_weights.cpp @@ -0,0 +1,54 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::weighted_mean>, external<int> > acc; + accumulator_set<void, stats<tag::sum_of_weights>, int> weight_acc; + + acc(10, weight = 2); // 20 + weight_acc(weight = 2); // + BOOST_CHECK_EQUAL(2, sum_of_weights(weight_acc)); // + // + acc(6, weight = 3); // 18 + weight_acc(weight = 3); // + BOOST_CHECK_EQUAL(5, sum_of_weights(weight_acc)); // + // + acc(4, weight = 4); // 16 + weight_acc(weight = 4); // + BOOST_CHECK_EQUAL(9, sum_of_weights(weight_acc)); // + // + acc(6, weight = 5); //+ 30 + weight_acc(weight = 5); // + BOOST_CHECK_EQUAL(14, sum_of_weights(weight_acc)); // + //= 84 / 14 = 6 + + BOOST_CHECK_CLOSE(6., weighted_mean(acc, weights = weight_acc), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("external_weights test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/kurtosis.cpp b/src/boost/libs/accumulators/test/kurtosis.cpp new file mode 100644 index 00000000..92c5cfe9 --- /dev/null +++ b/src/boost/libs/accumulators/test/kurtosis.cpp @@ -0,0 +1,102 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for kurtosis.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/kurtosis.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + // double epsilon = 1; + + accumulator_set<double, stats<tag::kurtosis > > acc1; + accumulator_set<int, stats<tag::kurtosis > > acc2; + + // 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) + { + acc1(normal()); + } + + // This check fails because epsilon is relative and not absolute + // BOOST_CHECK_CLOSE( kurtosis(acc1), 0., epsilon ); + + acc2(2); + acc2(7); + acc2(4); + acc2(9); + acc2(3); + + BOOST_CHECK_EQUAL( mean(acc2), 5 ); + BOOST_CHECK_EQUAL( accumulators::moment<2>(acc2), 159./5. ); + BOOST_CHECK_EQUAL( accumulators::moment<3>(acc2), 1171./5. ); + BOOST_CHECK_EQUAL( accumulators::moment<4>(acc2), 1863 ); + BOOST_CHECK_CLOSE( kurtosis(acc2), -1.39965397924, 1e-6 ); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance + double epsilon = 1e-6; + double kurtosis_value = -1.39965397924; + { + accumulator_set<int, stats<tag::kurtosis > > acc; + acc(2); + acc(7); + acc(4); + acc(9); + acc(3); + BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + + accumulator_set<double, stats<tag::kurtosis > > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon); + +} +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("kurtosis test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/max.cpp b/src/boost/libs/accumulators/test/max.cpp new file mode 100644 index 00000000..00a97936 --- /dev/null +++ b/src/boost/libs/accumulators/test/max.cpp @@ -0,0 +1,67 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/max.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + 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)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::max> > acc; + acc(1); + acc(0); + acc(2); + BOOST_CHECK_EQUAL(2u, max(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::max> > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(2u, max(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("max test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/mean.cpp b/src/boost/libs/accumulators/test/mean.cpp new file mode 100644 index 00000000..29d48776 --- /dev/null +++ b/src/boost/libs/accumulators/test/mean.cpp @@ -0,0 +1,123 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_mean.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +typedef accumulator_set<int, stats<tag::mean, + tag::mean_of_variates<int, tag::covariate1> > > mean_t; + +typedef accumulator_set<int, stats<tag::mean(immediate), + tag::mean_of_variates<int, tag::covariate1>(immediate) > > immediate_mean_t; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + mean_t acc, test_acc(sample = 0); + + acc(1, covariate1 = 3); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + acc(0, covariate1 = 4); + BOOST_CHECK_CLOSE(0.5, mean(acc), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc)); + BOOST_CHECK_EQUAL(1, sum(acc)); + BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + acc(2, covariate1 = 8); + BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc)); + BOOST_CHECK_EQUAL(3, sum(acc)); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + assert_is_double(mean(acc)); + + immediate_mean_t acc2, test_acc2(sample = 0); + + acc2(1, covariate1 = 3); + BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc2)); + BOOST_CHECK_CLOSE(3., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + acc2(0, covariate1 = 4); + BOOST_CHECK_CLOSE(0.5, mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc2)); + BOOST_CHECK_CLOSE(3.5, (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + acc2(2, covariate1 = 8); + BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc2)); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + assert_is_double(mean(acc2)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + mean_t acc1; + immediate_mean_t acc2; + + acc1(1, covariate1 = 3); + acc1(0, covariate1 = 4); + acc1(2, covariate1 = 8); + acc2(1, covariate1 = 3); + acc2(0, covariate1 = 4); + acc2(2, covariate1 = 8); + BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc1)), 1e-5); + BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + boost::archive::text_oarchive oa(ss); + acc1.serialize(oa, 0); + acc2.serialize(oa, 0); + } + mean_t acc1; + immediate_mean_t acc2; + boost::archive::text_iarchive ia(ss); + acc1.serialize(ia, 0); + acc2.serialize(ia, 0); + BOOST_CHECK_CLOSE(1., mean(acc1), 1e-5); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("mean test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/median.cpp b/src/boost/libs/accumulators/test/median.cpp new file mode 100644 index 00000000..e3483932 --- /dev/null +++ b/src/boost/libs/accumulators/test/median.cpp @@ -0,0 +1,110 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/random.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/median.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + + +typedef accumulator_set<double, stats<tag::median(with_p_square_quantile) > > p_square_median_t; +typedef accumulator_set<double, stats<tag::median(with_density) > > dist_median_t; +typedef accumulator_set<double, stats<tag::median(with_p_square_cumulative_distribution) > > p_square_dist_median_t; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // 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); + + p_square_median_t acc; + dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); + p_square_dist_median_t 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.); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // 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); + std::stringstream ss; + { + p_square_median_t acc; + dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); + p_square_dist_median_t 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.); + + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + acc_dens.serialize(oa, 0); + acc_cdist.serialize(oa, 0); + } + + p_square_median_t acc; + dist_median_t acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); + p_square_dist_median_t acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + acc_dens.serialize(ia, 0); + acc_cdist.serialize(ia, 0); + BOOST_CHECK_CLOSE(1., median(acc), 1.); + BOOST_CHECK_CLOSE(1., median(acc_dens), 1.); + BOOST_CHECK_CLOSE(1., median(acc_cdist), 3.); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("median test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/min.cpp b/src/boost/libs/accumulators/test/min.cpp new file mode 100644 index 00000000..614fab4f --- /dev/null +++ b/src/boost/libs/accumulators/test/min.cpp @@ -0,0 +1,67 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/min.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + 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)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::min> > acc; + acc(1); + acc(0); + acc(2); + BOOST_CHECK_EQUAL(0, (min)(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::min> > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(0, (min)(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("min test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/moment.cpp b/src/boost/libs/accumulators/test/moment.cpp new file mode 100644 index 00000000..44fa2a53 --- /dev/null +++ b/src/boost/libs/accumulators/test/moment.cpp @@ -0,0 +1,79 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/moment.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::moment<2> > > acc1; + + acc1(2); // 4 + acc1(4); // 16 + acc1(5); // + 25 + // = 45 / 3 = 15 + + BOOST_CHECK_CLOSE(15., accumulators::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., accumulators::moment<5>(acc2), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-5; + std::stringstream ss; + { + accumulator_set<int, stats<tag::moment<2> > > acc; + acc(2); // 4 + acc(4); // 16 + acc(5); // + 25 + // = 45 / 3 = 15 + BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::moment<2> > > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("moment test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/p_square_cumul_dist.cpp b/src/boost/libs/accumulators/test/p_square_cumul_dist.cpp new file mode 100644 index 00000000..33e8109a --- /dev/null +++ b/src/boost/libs/accumulators/test/p_square_cumul_dist.cpp @@ -0,0 +1,124 @@ +// (C) Copyright Eric Niebler, Olivier Gygi 2006. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for p_square_cumul_dist.hpp + +#include <cmath> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/p_square_cumul_dist.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// erf() not known by VC++ compiler! +// my_erf() computes error function by numerically integrating with trapezoidal rule +// +double my_erf(double const& x, int const& n = 1000) +{ + double sum = 0.; + double delta = x/n; + for (int i = 1; i < n; ++i) + sum += std::exp(-i*i*delta*delta) * delta; + sum += 0.5 * delta * (1. + std::exp(-x*x)); + return sum * 2. / std::sqrt(3.141592653); +} + +typedef accumulator_set<double, stats<tag::p_square_cumulative_distribution> > accumulator_t; +typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 3; + + accumulator_t acc(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<1000000; ++i) + { + acc(normal()); + } + + 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 + my_erf( histogram[i].first / std::sqrt(2.0) )), histogram[i].second, epsilon ); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance in % + double epsilon = 3; + { + accumulator_t acc(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<1000000; ++i) + { + acc(normal()); + } + + histogram_type histogram = p_square_cumulative_distribution(acc); + + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[25].first / std::sqrt(2.0))), histogram[25].second, epsilon); + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[50].first / std::sqrt(2.0))), histogram[50].second, epsilon); + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[75].first / std::sqrt(2.0))), histogram[75].second, epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_t acc(p_square_cumulative_distribution_num_cells = 100); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + histogram_type histogram = p_square_cumulative_distribution(acc); + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[25].first / std::sqrt(2.0))), histogram[25].second, epsilon); + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[50].first / std::sqrt(2.0))), histogram[50].second, epsilon); + BOOST_CHECK_CLOSE(0.5 * (1.0 + my_erf(histogram[75].first / std::sqrt(2.0))), histogram[75].second, epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("p_square_cumulative_distribution test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/p_square_quantile.cpp b/src/boost/libs/accumulators/test/p_square_quantile.cpp new file mode 100644 index 00000000..ba678237 --- /dev/null +++ b/src/boost/libs/accumulators/test/p_square_quantile.cpp @@ -0,0 +1,122 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for p_square_quantile.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/p_square_quantile.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +typedef accumulator_set<double, stats<tag::p_square_quantile> > accumulator_t; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1; + + // a random number generator + boost::lagged_fibonacci607 rng; + + accumulator_t acc0(quantile_probability = 0.001); + accumulator_t acc1(quantile_probability = 0.01 ); + accumulator_t acc2(quantile_probability = 0.1 ); + accumulator_t acc3(quantile_probability = 0.25 ); + accumulator_t acc4(quantile_probability = 0.5 ); + accumulator_t acc5(quantile_probability = 0.75 ); + accumulator_t acc6(quantile_probability = 0.9 ); + accumulator_t acc7(quantile_probability = 0.99 ); + accumulator_t acc8(quantile_probability = 0.999); + + for (int i=0; i<100000; ++i) + { + double sample = rng(); + acc0(sample); + acc1(sample); + acc2(sample); + acc3(sample); + acc4(sample); + acc5(sample); + acc6(sample); + acc7(sample); + acc8(sample); + } + + BOOST_CHECK_CLOSE( p_square_quantile(acc0), 0.001, 18*epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc1), 0.01 , 7*epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc2), 0.1 , 3*epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc3), 0.25 , 2*epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc4), 0.5 , epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc5), 0.75 , epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc6), 0.9 , epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc7), 0.99 , epsilon ); + BOOST_CHECK_CLOSE( p_square_quantile(acc8), 0.999, epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance in % + double epsilon = 1; + // a random number generator + boost::lagged_fibonacci607 rng; + { + accumulator_t acc1(quantile_probability = 0.75 ); + accumulator_t acc2(quantile_probability = 0.999); + + for (int i=0; i<100000; ++i) + { + double sample = rng(); + acc1(sample); + acc2(sample); + } + + BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon); + BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon); + boost::archive::text_oarchive oa(ss); + acc1.serialize(oa, 0); + acc2.serialize(oa, 0); + } + accumulator_t acc1(quantile_probability = 0.75); + accumulator_t acc2(quantile_probability = 0.999); + boost::archive::text_iarchive ia(ss); + acc1.serialize(ia, 0); + acc2.serialize(ia, 0); + BOOST_CHECK_CLOSE(p_square_quantile(acc1), 0.75 , epsilon); + BOOST_CHECK_CLOSE(p_square_quantile(acc2), 0.999, epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("p_square_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/p_square_quantile_extended.cpp b/src/boost/libs/accumulators/test/p_square_quantile_extended.cpp new file mode 100644 index 00000000..debe33ce --- /dev/null +++ b/src/boost/libs/accumulators/test/p_square_quantile_extended.cpp @@ -0,0 +1,117 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for p_square_quantile_extended.hpp + +#include <iostream> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/p_square_quantile_extended.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +typedef accumulator_set<double, stats<tag::p_square_quantile_extended> > accumulator_t; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance + double epsilon = 1e-6; + + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector<double> probs; + + probs.push_back(0.001); + probs.push_back(0.01 ); + probs.push_back(0.1 ); + probs.push_back(0.25 ); + probs.push_back(0.5 ); + probs.push_back(0.75 ); + probs.push_back(0.9 ); + probs.push_back(0.99 ); + probs.push_back(0.999); + + accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); + + for (int i=0; i<10000; ++i) + acc(rng()); + + for (std::size_t i=0; i<probs.size(); ++i) + { + BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance in % + double epsilon = 1; + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector<double> probs; + probs.push_back(0.001); + probs.push_back(0.01 ); + probs.push_back(0.1 ); + probs.push_back(0.25 ); + probs.push_back(0.5 ); + probs.push_back(0.75 ); + probs.push_back(0.9 ); + probs.push_back(0.99 ); + probs.push_back(0.999); + { + accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); + + for (int i=0; i<10000; ++i) + acc(rng()); + + for (std::size_t i=0; i<probs.size(); ++i) + { + BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); + } + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_t acc(tag::p_square_quantile_extended::probabilities = probs); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + for (std::size_t i=0; i<probs.size(); ++i) + { + BOOST_CHECK_CLOSE(p_square_quantile_extended(acc)[i], probs[i], epsilon); + } +} +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("p_square_quantile_extended test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/pot_quantile.cpp b/src/boost/libs/accumulators/test/pot_quantile.cpp new file mode 100644 index 00000000..4e12ad61 --- /dev/null +++ b/src/boost/libs/accumulators/test/pot_quantile.cpp @@ -0,0 +1,131 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for pot_quantile.hpp + +#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics.hpp> +#include <boost/accumulators/statistics/peaks_over_threshold.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1.; + + // two random number generators + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma(0,1); + boost::exponential_distribution<> lambda(1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal(rng, mean_sigma); + boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda); + + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc1( + pot_threshold_value = 3. + ); + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc2( + right_tail_cache_size = 2000 + , pot_threshold_probability = 0.99 + ); + accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_value)> > acc3( + pot_threshold_value = -3. + ); + accumulator_set<double, stats<tag::pot_quantile<left>(with_threshold_probability)> > acc4( + left_tail_cache_size = 2000 + , pot_threshold_probability = 0.01 + ); + + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc5( + pot_threshold_value = 5. + ); + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_probability)> > acc6( + right_tail_cache_size = 2000 + , pot_threshold_probability = 0.995 + ); + + for (std::size_t i = 0; i < 100000; ++i) + { + double sample = normal(); + acc1(sample); + acc2(sample); + acc3(sample); + acc4(sample); + } + + for (std::size_t i = 0; i < 100000; ++i) + { + double sample = exponential(); + acc5(sample); + acc6(sample); + } + + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, 3*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, 2*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, 2*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, 2*epsilon ); + + BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, 3*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, 3*epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // tolerance in % + double epsilon = 1.; + // "persistent" storage + std::stringstream ss; + { + // 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); + + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.); + + for (std::size_t i = 0; i < 100000; ++i) + acc(normal()); + + BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<double, stats<tag::pot_quantile<right>(with_threshold_value)> > acc(pot_threshold_value = 3.); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(quantile(acc, quantile_probability = 0.999), 3.090232, 3*epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("pot_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/reference.cpp b/src/boost/libs/accumulators/test/reference.cpp new file mode 100644 index 00000000..bf9b6c60 --- /dev/null +++ b/src/boost/libs/accumulators/test/reference.cpp @@ -0,0 +1,95 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +namespace my +{ + BOOST_PARAMETER_KEYWORD(tag, int_ref) + BOOST_PARAMETER_KEYWORD(tag, sum_acc) +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + int i = 0; + accumulator_set<double, stats<tag::reference<int, my::tag::int_ref> > > acc( + my::int_ref = i); + + int &ref1 = accumulators::reference<int, my::tag::int_ref>(acc); + int &ref2 = accumulators::reference_tag<my::tag::int_ref>(acc); + + BOOST_CHECK_EQUAL(&i, &ref1); + BOOST_CHECK_EQUAL(&i, &ref2); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_external +// +void test_external() +{ + typedef accumulator_set<int, stats<tag::sum> > sum_acc_type; + sum_acc_type sum_acc; // the sum accumulator + accumulator_set< + int + , stats< + tag::mean + , tag::external<tag::sum, my::tag::sum_acc> // make sum external + , tag::reference<sum_acc_type, my::tag::sum_acc> // and hold a reference to it + > + > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum + + sum_acc(1); + sum_acc(2); // sum is now 3 for both + + BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc)); + BOOST_CHECK_EQUAL(sum(acc_with_ref), 3); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_external2 +// +void test_external2() +{ + typedef accumulator_set<int, stats<tag::sum> > sum_acc_type; + sum_acc_type sum_acc; // the sum accumulator + accumulator_set< + int + , stats< + tag::mean + // make sum external and hold a reference to it + , tag::external<tag::sum, my::tag::sum_acc, sum_acc_type> + > + > acc_with_ref(my::sum_acc = sum_acc); // initialize the reference sum + + sum_acc(1); + sum_acc(2); // sum is now 3 for both + + BOOST_CHECK_EQUAL(sum(acc_with_ref), sum(sum_acc)); + BOOST_CHECK_EQUAL(sum(acc_with_ref), 3); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("reference_accumulator test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_external)); + test->add(BOOST_TEST_CASE(&test_external2)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/rolling_count.cpp b/src/boost/libs/accumulators/test/rolling_count.cpp new file mode 100644 index 00000000..a1b6b6fe --- /dev/null +++ b/src/boost/libs/accumulators/test/rolling_count.cpp @@ -0,0 +1,78 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/rolling_count.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3); + + BOOST_CHECK_EQUAL(0u, rolling_count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(1u, rolling_count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(2u, rolling_count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(3u, rolling_count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(3u, rolling_count(acc)); + + acc(1); + BOOST_CHECK_EQUAL(3u, rolling_count(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3); + acc(1); + acc(1); + acc(1); + acc(1); + acc(1); + acc(1); + BOOST_CHECK_EQUAL(3u, rolling_count(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::rolling_count> > acc(tag::rolling_window::window_size = 3); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(3u, rolling_count(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("rolling count test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/rolling_mean.cpp b/src/boost/libs/accumulators/test/rolling_mean.cpp new file mode 100644 index 00000000..f8037b4b --- /dev/null +++ b/src/boost/libs/accumulators/test/rolling_mean.cpp @@ -0,0 +1,219 @@ +// Copyright (C) Eric Niebler 2008. +// Copyright (C) Pieter Bastiaan Ober 2014. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/mpl/assert.hpp> +#include <boost/type_traits/is_same.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/rolling_mean.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +// test_rolling_mean_test_impl +// implements a test for window_size = 5 +size_t window_size = 5; + +template<typename accumulator_set_type> +void +test_rolling_mean_test_impl(accumulator_set_type& acc) +{ + acc(1); + BOOST_CHECK_CLOSE(1., rolling_mean(acc), 1e-5); + + acc(2); + BOOST_CHECK_CLOSE(1.5, rolling_mean(acc), 1e-5); + + acc(3); + BOOST_CHECK_CLOSE(2., rolling_mean(acc), 1e-5); + + acc(4); + BOOST_CHECK_CLOSE(2.5, rolling_mean(acc), 1e-5); + + acc(5); + BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5); + + acc(6); + BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5); + + acc(7); + BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5); + + assert_is_double(rolling_mean(acc)); +} + +template<typename accumulator_set_type> +void +test_rolling_mean_unsigned_test_impl(accumulator_set_type& acc) +{ + acc(7U); + BOOST_CHECK_CLOSE(7., rolling_mean(acc), 1e-5); + + acc(6U); + BOOST_CHECK_CLOSE(6.5, rolling_mean(acc), 1e-5); + + acc(5U); + BOOST_CHECK_CLOSE(6., rolling_mean(acc), 1e-5); + + acc(4U); + BOOST_CHECK_CLOSE(5.5, rolling_mean(acc), 1e-5); + + acc(3U); + BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5); + + acc(2U); + BOOST_CHECK_CLOSE(4., rolling_mean(acc), 1e-5); + + acc(1U); + BOOST_CHECK_CLOSE(3., rolling_mean(acc), 1e-5); + + assert_is_double(rolling_mean(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency_impl +// +template<typename accumulator_set_type> +void test_persistency_impl(accumulator_set_type& acc) +{ + std::stringstream ss; + { + acc(1); + acc(2); + acc(3); + acc(4); + acc(5); + acc(6); + acc(7); + BOOST_CHECK_CLOSE(5., rolling_mean(acc), 1e-5); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + // initialize from acc to make sure all values are passed + accumulator_set_type other_acc = acc; + // accumulate more, to make sure that deserialization set the right value + // and not the copy ctor + other_acc(100); + other_acc(100); + other_acc(100); + other_acc(100); + other_acc(100); + boost::archive::text_iarchive ia(ss); + other_acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(5., rolling_mean(other_acc), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_rolling_mean +void test_rolling_mean() +{ + accumulator_set<int,stats<tag::immediate_rolling_mean> > + acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size), + acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0); + + accumulator_set<int,stats<tag::rolling_mean(immediate)> > + acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size); + + accumulator_set<int,stats<tag::lazy_rolling_mean> > + acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size), + acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0); + + accumulator_set<int,stats<tag::rolling_mean(lazy)> > + acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size); + + accumulator_set<int,stats<tag::rolling_mean> > + acc_default_rolling_mean(tag::rolling_mean::window_size = window_size), + acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0); + + //// test the different implementations + test_rolling_mean_test_impl(acc_lazy_rolling_mean); + test_rolling_mean_test_impl(acc_default_rolling_mean); + test_rolling_mean_test_impl(acc_immediate_rolling_mean); + + test_rolling_mean_test_impl(acc_lazy_rolling_mean2); + test_rolling_mean_test_impl(acc_default_rolling_mean2); + test_rolling_mean_test_impl(acc_immediate_rolling_mean2); + + test_rolling_mean_test_impl(acc_lazy_rolling_mean3); + test_rolling_mean_test_impl(acc_immediate_rolling_mean3); + + //// test that the default implementation is the 'immediate' computation + BOOST_REQUIRE(sizeof(acc_lazy_rolling_mean) != sizeof(acc_immediate_rolling_mean)); + BOOST_CHECK (sizeof(acc_default_rolling_mean) == sizeof(acc_immediate_rolling_mean)); + + //// test the equivalence of the different ways to indicate a feature + BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean2)); + BOOST_CHECK (sizeof(acc_lazy_rolling_mean) == sizeof(acc_lazy_rolling_mean3)); + BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean2)); + BOOST_CHECK (sizeof(acc_immediate_rolling_mean) == sizeof(acc_immediate_rolling_mean3)); + + //// test unsigned int with both implementations + accumulator_set<unsigned int,stats<tag::immediate_rolling_mean> > + acc_immediate_rolling_mean4(tag::immediate_rolling_mean::window_size = window_size), + acc_immediate_rolling_mean5(tag::immediate_rolling_mean::window_size = window_size, sample = 0); + + test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean4); + test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean5); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +void test_persistency() +{ + accumulator_set<int,stats<tag::immediate_rolling_mean> > + acc_immediate_rolling_mean(tag::immediate_rolling_mean::window_size = window_size), + acc_immediate_rolling_mean2(tag::immediate_rolling_mean::window_size = window_size, sample = 0); + + accumulator_set<int,stats<tag::rolling_mean(immediate)> > + acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size); + + accumulator_set<int,stats<tag::lazy_rolling_mean> > + acc_lazy_rolling_mean(tag::lazy_rolling_mean::window_size = window_size), + acc_lazy_rolling_mean2(tag::lazy_rolling_mean::window_size = window_size, sample = 0); + + accumulator_set<int,stats<tag::rolling_mean(lazy)> > + acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size); + + accumulator_set<int,stats<tag::rolling_mean> > + acc_default_rolling_mean(tag::rolling_mean::window_size = window_size), + acc_default_rolling_mean2(tag::rolling_mean::window_size = window_size, sample = 0); + + //// test the different implementations + test_persistency_impl(acc_lazy_rolling_mean); + test_persistency_impl(acc_default_rolling_mean); + test_persistency_impl(acc_immediate_rolling_mean); + + test_persistency_impl(acc_lazy_rolling_mean2); + test_persistency_impl(acc_default_rolling_mean2); + test_persistency_impl(acc_immediate_rolling_mean2); + + test_persistency_impl(acc_lazy_rolling_mean3); + test_persistency_impl(acc_immediate_rolling_mean3); +} +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("rolling mean test"); + + test->add(BOOST_TEST_CASE(&test_rolling_mean)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/rolling_moment.cpp b/src/boost/libs/accumulators/test/rolling_moment.cpp new file mode 100644 index 00000000..30d0c88b --- /dev/null +++ b/src/boost/libs/accumulators/test/rolling_moment.cpp @@ -0,0 +1,119 @@ +// Copyright (C) Eric Niebler 2005. +// Copyright (C) Pieter Bastiaan Ober 2014. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/mpl/assert.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/rolling_moment.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_rolling_moment +// + +void test_rolling_second_moment() +{ + accumulator_set<int, stats<tag::rolling_moment<2> > > acc(tag::rolling_moment<2>::window_size = 3); + + acc(2); + + BOOST_CHECK_CLOSE(rolling_moment<2>(acc), 4.0/1 ,1e-5); + + acc(4); + + BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0)/2, 1e-5); + + acc(5); + + BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (4.0 + 16.0 + 25.0)/3, 1e-5); + + acc(6); + + BOOST_CHECK_CLOSE(rolling_moment<2>(acc), (16.0 + 25.0 + 36.0)/3, 1e-5); + + assert_is_double(rolling_moment<2>(acc)); +} + +void test_rolling_fifth_moment() +{ + accumulator_set<int, stats<tag::rolling_moment<5> > > acc(tag::rolling_moment<2>::window_size = 3); + + acc(2); + + BOOST_CHECK_CLOSE(rolling_moment<5>(acc), 32.0/1, 1e-5); + + acc(3); + + BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0)/2, 1e-5); + + acc(4); + + BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (32.0 + 243.0 + 1024.0)/3, 1e-5); + + acc(5); + + BOOST_CHECK_CLOSE(rolling_moment<5>(acc), (243.0 + 1024.0 + 3125.0)/3, 1e-5); + + assert_is_double(rolling_moment<5>(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::rolling_moment<2> > > acc2(tag::rolling_moment<2>::window_size = 3); + accumulator_set<int, stats<tag::rolling_moment<5> > > acc5(tag::rolling_moment<5>::window_size = 3); + + acc2(2); acc5(2); + acc2(4); acc5(3); + acc2(5); acc5(4); + acc2(6); acc5(5); + + BOOST_CHECK_CLOSE(rolling_moment<2>(acc2), (16.0 + 25.0 + 36.0)/3, 1e-5); + BOOST_CHECK_CLOSE(rolling_moment<5>(acc5), (243.0 + 1024.0 + 3125.0)/3, 1e-5); + boost::archive::text_oarchive oa(ss); + acc2.serialize(oa, 0); + acc5.serialize(oa, 0); + } + accumulator_set<int, stats<tag::rolling_moment<2> > > acc2(tag::rolling_moment<2>::window_size = 3); + accumulator_set<int, stats<tag::rolling_moment<5> > > acc5(tag::rolling_moment<5>::window_size = 3); + boost::archive::text_iarchive ia(ss); + acc2.serialize(ia, 0); + acc5.serialize(ia, 0); + BOOST_CHECK_CLOSE(rolling_moment<2>(acc2), (16.0 + 25.0 + 36.0)/3, 1e-5); + BOOST_CHECK_CLOSE(rolling_moment<5>(acc5), (243.0 + 1024.0 + 3125.0)/3, 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("rolling moment test"); + + test->add(BOOST_TEST_CASE(&test_rolling_second_moment)); + test->add(BOOST_TEST_CASE(&test_rolling_fifth_moment)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/rolling_sum.cpp b/src/boost/libs/accumulators/test/rolling_sum.cpp new file mode 100644 index 00000000..374514c0 --- /dev/null +++ b/src/boost/libs/accumulators/test/rolling_sum.cpp @@ -0,0 +1,77 @@ +// (C) Copyright Eric Niebler 2008. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/rolling_sum.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::rolling_sum> > acc(tag::rolling_window::window_size = 3); + + BOOST_CHECK_EQUAL(0, rolling_sum(acc)); + + acc(1); + BOOST_CHECK_EQUAL(1, rolling_sum(acc)); + + acc(2); + BOOST_CHECK_EQUAL(3, rolling_sum(acc)); + + acc(3); + BOOST_CHECK_EQUAL(6, rolling_sum(acc)); + + acc(4); + BOOST_CHECK_EQUAL(9, rolling_sum(acc)); + + acc(5); + BOOST_CHECK_EQUAL(12, rolling_sum(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::rolling_sum> > acc(tag::rolling_window::window_size = 3); + acc(1); + acc(2); + acc(3); + acc(4); + acc(5); + BOOST_CHECK_EQUAL(12, rolling_sum(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::rolling_sum> > acc(tag::rolling_window::window_size = 3); + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(12, rolling_sum(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("rolling sum test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/rolling_variance.cpp b/src/boost/libs/accumulators/test/rolling_variance.cpp new file mode 100644 index 00000000..d7f00759 --- /dev/null +++ b/src/boost/libs/accumulators/test/rolling_variance.cpp @@ -0,0 +1,208 @@ +// (C) Copyright Pieter Bastiaan Ober 2014. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/mpl/assert.hpp> +#include <boost/type_traits/is_same.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> +#include <boost/accumulators/statistics/rolling_variance.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +/* +REFERENCE VALUES PROVIDED BY OCTAVE: + +x=[1.2 2.3 3.4 4.5 0.4 2.2 7.1 4.0] + +v1_2 = var(x(1:2)) +v1_3 = var(x(1:3)) +v1_4 = var(x(1:4)) +v2_5 = var(x(2:5)) +v3_6 = var(x(3:6)) +v4_7 = var(x(4:7)) +v5_8 = var(x(5:8)) + +GIVES: + +v1_2 = 0.605000000000000 +v1_3 = 1.21000000000000 +v1_4 = 2.01666666666667 +v2_5 = 3.05666666666667 +v3_6 = 3.08250000000000 +v4_7 = 8.41666666666667 +v5_8 = 8.16250000000000 +*/ + +/////////////////////////////////////////////////////////////////////////////// +// rolling_variance_test_impl +// implements a test for window_size = 4 + +size_t window_size = 4; + +template<typename accumulator_set_type> +void rolling_variance_test_impl(accumulator_set_type& acc) +{ + // Window contains x(1), value is zero + acc(1.2); + BOOST_CHECK_CLOSE(rolling_variance(acc),0.0,1e-10); + + // Window contains x(1)...x(2) + acc(2.3); + BOOST_CHECK_CLOSE(rolling_variance(acc),0.605,1e-10); + + // Window contains x(1)...x(3) + acc(3.4); + BOOST_CHECK_CLOSE(rolling_variance(acc),1.21,1e-10); + + // Window contains x(1)...x(4) + acc(4.5); + BOOST_CHECK_CLOSE(rolling_variance(acc),2.01666666666667,1e-10); + + // Window contains x(2)...x(5) + acc(0.4); + BOOST_CHECK_CLOSE(rolling_variance(acc),3.05666666666667,1e-10); + + // Window contains x(3)...x(6) + acc(2.2); + BOOST_CHECK_CLOSE(rolling_variance(acc),3.08250000000000,1e-10); + + // Window contains x(4)...x(7) + acc(7.1); + BOOST_CHECK_CLOSE(rolling_variance(acc),8.41666666666667,1e-10); + + // Window contains x(5)...x(8) + acc(4.0); + BOOST_CHECK_CLOSE(rolling_variance(acc),8.16250000000000,1e-10); + + assert_is_double(rolling_variance(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_rolling_variance +// +void test_rolling_variance() +{ + // tag::rolling_window::window_size + accumulator_set<double, stats<tag::immediate_rolling_variance> > + acc_immediate_rolling_variance(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::immediate_rolling_variance, tag::rolling_mean> > + acc_immediate_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance(immediate)> > + acc_immediate_rolling_variance3(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::lazy_rolling_variance> > + acc_lazy_rolling_variance(tag::lazy_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance(lazy)> > + acc_lazy_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance> > + acc_default_rolling_variance(tag::rolling_variance::window_size = window_size); + + //// test the different implementations + rolling_variance_test_impl(acc_immediate_rolling_variance); + rolling_variance_test_impl(acc_immediate_rolling_variance2); + rolling_variance_test_impl(acc_immediate_rolling_variance3); + rolling_variance_test_impl(acc_lazy_rolling_variance); + rolling_variance_test_impl(acc_lazy_rolling_variance2); + rolling_variance_test_impl(acc_default_rolling_variance); + + //// test that the default implementation is the 'immediate' computation + BOOST_REQUIRE(sizeof(acc_lazy_rolling_variance) != sizeof(acc_immediate_rolling_variance)); + BOOST_CHECK (sizeof(acc_default_rolling_variance) == sizeof(acc_immediate_rolling_variance)); + + //// test the equivalence of the different ways to indicate a feature + BOOST_CHECK (sizeof(acc_immediate_rolling_variance) == sizeof(acc_immediate_rolling_variance2)); + BOOST_CHECK (sizeof(acc_immediate_rolling_variance) == sizeof(acc_immediate_rolling_variance3)); + BOOST_CHECK (sizeof(acc_lazy_rolling_variance) == sizeof(acc_lazy_rolling_variance2)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency_impl +// +template<typename accumulator_set_type> +void test_persistency_impl(accumulator_set_type& acc) +{ + std::stringstream ss; + { + acc(1.2); + acc(2.3); + acc(3.4); + acc(4.5); + acc(0.4); + acc(2.2); + acc(7.1); + acc(4.0); + BOOST_CHECK_CLOSE(rolling_variance(acc),8.16250000000000,1e-10); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set_type other_acc = acc; + boost::archive::text_iarchive ia(ss); + other_acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(rolling_variance(acc),8.16250000000000,1e-10); + +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // tag::rolling_window::window_size + accumulator_set<double, stats<tag::immediate_rolling_variance> > + acc_immediate_rolling_variance(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::immediate_rolling_variance, tag::rolling_mean> > + acc_immediate_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance(immediate)> > + acc_immediate_rolling_variance3(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::lazy_rolling_variance> > + acc_lazy_rolling_variance(tag::lazy_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance(lazy)> > + acc_lazy_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set<double, stats<tag::rolling_variance> > + acc_default_rolling_variance(tag::rolling_variance::window_size = window_size); + + //// test the different implementations + test_persistency_impl(acc_immediate_rolling_variance); + test_persistency_impl(acc_immediate_rolling_variance2); + test_persistency_impl(acc_immediate_rolling_variance3); + test_persistency_impl(acc_lazy_rolling_variance); + test_persistency_impl(acc_lazy_rolling_variance2); + test_persistency_impl(acc_default_rolling_variance); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("rolling variance test"); + + test->add(BOOST_TEST_CASE(&test_rolling_variance)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/skewness.cpp b/src/boost/libs/accumulators/test/skewness.cpp new file mode 100644 index 00000000..8a824688 --- /dev/null +++ b/src/boost/libs/accumulators/test/skewness.cpp @@ -0,0 +1,100 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for skewness.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/skewness.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + // double epsilon = 1; + + accumulator_set<double, stats<tag::skewness > > acc1; + accumulator_set<int, stats<tag::skewness > > acc2; + + // 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) + { + acc1(normal()); + } + + // This check fails because epsilon is relative and not absolute + // BOOST_CHECK_CLOSE( skewness(acc1), 0., epsilon ); + + acc2(2); + acc2(7); + acc2(4); + acc2(9); + acc2(3); + + BOOST_CHECK_EQUAL( mean(acc2), 5 ); + BOOST_CHECK_EQUAL( accumulators::moment<2>(acc2), 159./5. ); + BOOST_CHECK_EQUAL( accumulators::moment<3>(acc2), 1171./5. ); + BOOST_CHECK_CLOSE( skewness(acc2), 0.406040288214, 1e-6 ); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-6; + std::stringstream ss; + { + accumulator_set<int, stats<tag::skewness > > acc; + acc(2); + acc(7); + acc(4); + acc(9); + acc(3); + + BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.); + BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::skewness > > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(accumulators::moment<3>(acc), 1171./5.); + BOOST_CHECK_CLOSE(skewness(acc), 0.406040288214, epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("skewness test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/sum.cpp b/src/boost/libs/accumulators/test/sum.cpp new file mode 100644 index 00000000..c0c8f8c0 --- /dev/null +++ b/src/boost/libs/accumulators/test/sum.cpp @@ -0,0 +1,80 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/sum.hpp> +#include <boost/accumulators/statistics/weighted_sum.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc; + + acc(1, weight = 2, covariate1 = 3); + BOOST_CHECK_EQUAL(2, sum(acc)); + BOOST_CHECK_EQUAL(2, sum_of_weights(acc)); + BOOST_CHECK_EQUAL(3, sum_of_variates(acc)); + + acc(2, weight = 4, covariate1 = 6); + BOOST_CHECK_EQUAL(10, sum(acc)); + BOOST_CHECK_EQUAL(6, sum_of_weights(acc)); + BOOST_CHECK_EQUAL(9, sum_of_variates(acc)); + + acc(3, weight = 6, covariate1 = 9); + BOOST_CHECK_EQUAL(28, sum(acc)); + BOOST_CHECK_EQUAL(12, sum_of_weights(acc)); + BOOST_CHECK_EQUAL(18, sum_of_variates(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc; + acc(1, weight = 2, covariate1 = 3); + acc(2, weight = 4, covariate1 = 6); + acc(3, weight = 6, covariate1 = 9); + BOOST_CHECK_EQUAL(28, sum(acc)); + BOOST_CHECK_EQUAL(12, sum_of_weights(acc)); + BOOST_CHECK_EQUAL(18, sum_of_variates(acc)); + + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set<int, stats<tag::sum, tag::sum_of_weights, tag::sum_of_variates<int, tag::covariate1> >, int> acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(28, sum(acc)); + BOOST_CHECK_EQUAL(12, sum_of_weights(acc)); + BOOST_CHECK_EQUAL(18, sum_of_variates(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("sum test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/sum_kahan.cpp b/src/boost/libs/accumulators/test/sum_kahan.cpp new file mode 100644 index 00000000..85edb116 --- /dev/null +++ b/src/boost/libs/accumulators/test/sum_kahan.cpp @@ -0,0 +1,120 @@ +// (C) Copyright Gaetano Mendola 2010, Simon West 2011. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/sum_kahan.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_sum_kahan +// +void test_sum_kahan() +{ + accumulator_set<float, stats<tag::sum_kahan> > acc; + + BOOST_CHECK_EQUAL(0.0f, sum_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, sum_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_sum_of_weights_kahan +// +void test_sum_of_weights_kahan() +{ + accumulator_set<float, stats<tag::sum_of_weights_kahan>, float > acc; + + BOOST_CHECK_EQUAL(0.0f, sum_of_weights_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(0, weight = 1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, sum_of_weights_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_sum_of_variates_kahan +// +void test_sum_of_variates_kahan() +{ + accumulator_set< + float, + stats<tag::sum_of_variates_kahan<float, tag::covariate1> >, + float + > + acc; + + BOOST_CHECK_EQUAL(0.0f, sum_of_variates_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(0, covariate1 = 1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set< + float, + stats<tag::sum_of_variates_kahan<float, tag::covariate1> >, + float + > + acc; + + BOOST_CHECK_EQUAL(0.0f, sum_of_variates_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(0, covariate1 = 1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc)); + boost::archive::text_oarchive oa(ss); + acc.serialize(oa, 0); + } + accumulator_set< + float, + stats<tag::sum_of_variates_kahan<float, tag::covariate1> >, + float + > + acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("sum kahan tests"); + + test->add(BOOST_TEST_CASE(&test_sum_kahan)); + test->add(BOOST_TEST_CASE(&test_sum_of_weights_kahan)); + test->add(BOOST_TEST_CASE(&test_sum_of_variates_kahan)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/tail.cpp b/src/boost/libs/accumulators/test/tail.cpp new file mode 100644 index 00000000..f3e248ec --- /dev/null +++ b/src/boost/libs/accumulators/test/tail.cpp @@ -0,0 +1,126 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/foreach.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/tail.hpp> +#include <boost/accumulators/statistics/tail_variate.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename Range> +void check_tail(Range const &rng, char const *expected) +{ + BOOST_FOREACH(int i, rng) + { + if(!*expected) + { + BOOST_CHECK(false); + return; + } + BOOST_CHECK_EQUAL(i, *expected++); + } + BOOST_CHECK(!*expected); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_right_tail +// +void test_right_tail() +{ + accumulator_set<int, stats<tag::tail_weights<right>, tag::tail_variate<int, tag::covariate1, right> >, int > acc( + right_tail_cache_size = 4 + ); + + acc(010, weight = 2, covariate1 = 3); + check_tail(tail(acc), "\10"); + check_tail(tail_variate(acc), "\3"); + check_tail(tail_weights(acc), "\2"); + + acc(020, weight = 7, covariate1 = 1); + check_tail(tail(acc), "\20\10"); + check_tail(tail_variate(acc), "\1\3"); + check_tail(tail_weights(acc), "\7\2"); + + acc(014, weight = 6, covariate1 = 4); + check_tail(tail(acc), "\20\14\10"); + check_tail(tail_variate(acc), "\1\4\3"); + check_tail(tail_weights(acc), "\7\6\2"); + + acc(030, weight = 4, covariate1 = 5); + check_tail(tail(acc), "\30\20\14\10"); + check_tail(tail_variate(acc), "\5\1\4\3"); + check_tail(tail_weights(acc), "\4\7\6\2"); + + acc(001, weight = 1, covariate1 = 9); + check_tail(tail(acc), "\30\20\14\10"); + check_tail(tail_variate(acc), "\5\1\4\3"); + check_tail(tail_weights(acc), "\4\7\6\2"); + + acc(011, weight = 3, covariate1 = 7); + check_tail(tail(acc), "\30\20\14\11"); + check_tail(tail_variate(acc), "\5\1\4\7"); + check_tail(tail_weights(acc), "\4\7\6\3"); + +} + +/////////////////////////////////////////////////////////////////////////////// +// test_left_tail +// +void test_left_tail() +{ + accumulator_set<int, stats<tag::tail_weights<left>, tag::tail_variate<int, tag::covariate1, left> >, int > acc( + left_tail_cache_size = 4 + ); + + acc(010, weight = 2, covariate1 = 3); + check_tail(tail(acc), "\10"); + check_tail(tail_variate(acc), "\3"); + check_tail(tail_weights(acc), "\2"); + + acc(020, weight = 7, covariate1 = 1); + check_tail(tail(acc), "\10\20"); + check_tail(tail_variate(acc), "\3\1"); + check_tail(tail_weights(acc), "\2\7"); + + acc(014, weight = 6, covariate1 = 4); + check_tail(tail(acc), "\10\14\20"); + check_tail(tail_variate(acc), "\3\4\1"); + check_tail(tail_weights(acc), "\2\6\7"); + + acc(030, weight = 4, covariate1 = 5); + check_tail(tail(acc), "\10\14\20\30"); + check_tail(tail_variate(acc), "\3\4\1\5"); + check_tail(tail_weights(acc), "\2\6\7\4"); + + acc(001, weight = 1, covariate1 = 9); + check_tail(tail(acc), "\1\10\14\20"); + check_tail(tail_variate(acc), "\x9\3\4\1"); + check_tail(tail_weights(acc), "\1\2\6\7"); + + acc(011, weight = 3, covariate1 = 7); + check_tail(tail(acc), "\1\10\11\14"); + check_tail(tail_variate(acc), "\x9\3\7\4"); + check_tail(tail_weights(acc), "\1\2\3\6"); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("tail test"); + + test->add(BOOST_TEST_CASE(&test_right_tail)); + test->add(BOOST_TEST_CASE(&test_left_tail)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/tail_mean.cpp b/src/boost/libs/accumulators/test/tail_mean.cpp new file mode 100644 index 00000000..5132eb24 --- /dev/null +++ b/src/boost/libs/accumulators/test/tail_mean.cpp @@ -0,0 +1,86 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for tail_mean.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/tail_mean.hpp> +#include <boost/accumulators/statistics/tail_quantile.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1; + + std::size_t n = 100000; // number of MC steps + std::size_t c = 10000; // cache size + + typedef accumulator_set<double, stats<tag::non_coherent_tail_mean<right>, tag::tail_quantile<right> > > accumulator_t_right1; + typedef accumulator_set<double, stats<tag::non_coherent_tail_mean<left>, tag::tail_quantile<left> > > accumulator_t_left1; + typedef accumulator_set<double, stats<tag::coherent_tail_mean<right>, tag::tail_quantile<right> > > accumulator_t_right2; + typedef accumulator_set<double, stats<tag::coherent_tail_mean<left>, tag::tail_quantile<left> > > accumulator_t_left2; + + accumulator_t_right1 acc0( right_tail_cache_size = c ); + accumulator_t_left1 acc1( left_tail_cache_size = c ); + accumulator_t_right2 acc2( right_tail_cache_size = c ); + accumulator_t_left2 acc3( left_tail_cache_size = c ); + + // a random number generator + boost::lagged_fibonacci607 rng; + + for (std::size_t i = 0; i < n; ++i) + { + double sample = rng(); + acc0(sample); + acc1(sample); + acc2(sample); + acc3(sample); + } + + // check uniform distribution + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.95), 0.975, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.975), 0.9875, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.99), 0.995, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc0, quantile_probability = 0.999), 0.9995, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.05), 0.025, 5*epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.025), 0.0125, 6*epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.01), 0.005, 8*epsilon ); + BOOST_CHECK_CLOSE( non_coherent_tail_mean(acc1, quantile_probability = 0.001), 0.0005, 25*epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.95), 0.975, epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.975), 0.9875, epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.99), 0.995, epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc2, quantile_probability = 0.999), 0.9995, epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.05), 0.025, 5*epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.025), 0.0125, 6*epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.01), 0.005, 8*epsilon ); + BOOST_CHECK_CLOSE( tail_mean(acc3, quantile_probability = 0.001), 0.0005, 25*epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("tail_mean test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/tail_quantile.cpp b/src/boost/libs/accumulators/test/tail_quantile.cpp new file mode 100644 index 00000000..1f539d85 --- /dev/null +++ b/src/boost/libs/accumulators/test/tail_quantile.cpp @@ -0,0 +1,85 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for tail_quantile.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/tail_quantile.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1; + + std::size_t n = 100000; // number of MC steps + std::size_t c = 10000; // cache size + + typedef accumulator_set<double, stats<tag::tail_quantile<right> > > accumulator_t_right; + typedef accumulator_set<double, stats<tag::tail_quantile<left> > > accumulator_t_left; + + accumulator_t_right acc0( right_tail_cache_size = c ); + accumulator_t_right acc1( right_tail_cache_size = c ); + accumulator_t_left acc2( left_tail_cache_size = c ); + accumulator_t_left acc3( left_tail_cache_size = c ); + + // 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 < n; ++i) + { + double sample1 = rng(); + double sample2 = normal(); + acc0(sample1); + acc1(sample2); + acc2(sample1); + acc3(sample2); + } + + // check uniform distribution + BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.95 ), 0.95, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.975), 0.975, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.99 ), 0.99, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc0, quantile_probability = 0.999), 0.999, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.05 ), 0.05, 4*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.025), 0.025, 5*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.01 ), 0.01, 7*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.001), 0.001, 22*epsilon ); + + // check standard normal distribution + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.975), 1.959963, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, 3*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.025), -1.959963, 2*epsilon ); + BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, 3*epsilon ); + +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("tail_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/tail_variate_means.cpp b/src/boost/libs/accumulators/test/tail_variate_means.cpp new file mode 100644 index 00000000..6d003199 --- /dev/null +++ b/src/boost/libs/accumulators/test/tail_variate_means.cpp @@ -0,0 +1,131 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for tail_variate_means.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/tail_variate_means.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + std::size_t c = 5; // cache size + + typedef double variate_type; + typedef std::vector<variate_type> variate_set_type; + + typedef accumulator_set<double, stats<tag::tail_variate_means<right, variate_set_type, tag::covariate1>(relative)> > accumulator_t1; + typedef accumulator_set<double, stats<tag::tail_variate_means<right, variate_set_type, tag::covariate1>(absolute)> > accumulator_t2; + typedef accumulator_set<double, stats<tag::tail_variate_means<left, variate_set_type, tag::covariate1>(relative)> > accumulator_t3; + typedef accumulator_set<double, stats<tag::tail_variate_means<left, variate_set_type, tag::covariate1>(absolute)> > accumulator_t4; + + accumulator_t1 acc1( right_tail_cache_size = c ); + accumulator_t2 acc2( right_tail_cache_size = c ); + accumulator_t3 acc3( left_tail_cache_size = c ); + accumulator_t4 acc4( left_tail_cache_size = c ); + + variate_set_type cov1, cov2, cov3, cov4, cov5; + double c1[] = { 10., 20., 30., 40. }; // 100 + double c2[] = { 26., 4., 17., 3. }; // 50 + double c3[] = { 46., 64., 40., 50. }; // 200 + double c4[] = { 1., 3., 70., 6. }; // 80 + double c5[] = { 2., 2., 2., 14. }; // 20 + cov1.assign(c1, c1 + sizeof(c1)/sizeof(variate_type)); + cov2.assign(c2, c2 + sizeof(c2)/sizeof(variate_type)); + cov3.assign(c3, c3 + sizeof(c3)/sizeof(variate_type)); + cov4.assign(c4, c4 + sizeof(c4)/sizeof(variate_type)); + cov5.assign(c5, c5 + sizeof(c5)/sizeof(variate_type)); + + acc1(100., covariate1 = cov1); + acc1( 50., covariate1 = cov2); + acc1(200., covariate1 = cov3); + acc1( 80., covariate1 = cov4); + acc1( 20., covariate1 = cov5); + + acc2(100., covariate1 = cov1); + acc2( 50., covariate1 = cov2); + acc2(200., covariate1 = cov3); + acc2( 80., covariate1 = cov4); + acc2( 20., covariate1 = cov5); + + acc3(100., covariate1 = cov1); + acc3( 50., covariate1 = cov2); + acc3(200., covariate1 = cov3); + acc3( 80., covariate1 = cov4); + acc3( 20., covariate1 = cov5); + + acc4(100., covariate1 = cov1); + acc4( 50., covariate1 = cov2); + acc4(200., covariate1 = cov3); + acc4( 80., covariate1 = cov4); + acc4( 20., covariate1 = cov5); + + // check relative risk contributions + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), 14./75. ); // (10 + 46) / 300 = 14/75 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), 7./25. ); // (20 + 64) / 300 = 7/25 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), 7./30. ); // (30 + 40) / 300 = 7/30 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), 3./10. ); // (40 + 50) / 300 = 3/10 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() ), 14./35. ); // (26 + 2) / 70 = 14/35 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 1), 3./35. ); // ( 4 + 2) / 70 = 3/35 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 2), 19./70. ); // (17 + 2) / 70 = 19/70 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 3), 17./70. ); // ( 3 + 14) / 70 = 17/70 + + // check absolute risk contributions + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() ), 28 ); // (10 + 46) / 2 = 28 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 1), 42 ); // (20 + 64) / 2 = 42 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 2), 35 ); // (30 + 40) / 2 = 35 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.7).begin() + 3), 45 ); // (40 + 50) / 2 = 45 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() ), 14 ); // (26 + 2) / 2 = 14 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 1), 3 ); // ( 4 + 2) / 2 = 3 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 2),9.5 ); // (17 + 2) / 2 = 9.5 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.3).begin() + 3),8.5 ); // ( 3 + 14) / 2 = 8.5 + + // check relative risk contributions + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() ), 23./100. ); // 46/200 = 23/100 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 1), 8./25. ); // 64/200 = 8/25 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 2), 1./5. ); // 40/200 = 1/5 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 3), 1./4. ); // 50/200 = 1/4 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() ), 1./10. ); // 2/ 20 = 1/10 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 1), 1./10. ); // 2/ 20 = 1/10 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 2), 1./10. ); // 2/ 20 = 1/10 + BOOST_CHECK_EQUAL( *(relative_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 3), 7./10. ); // 14/ 20 = 7/10 + + // check absolute risk contributions + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() ), 46 ); // 46 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 1), 64 ); // 64 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 2), 40 ); // 40 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc2, quantile_probability = 0.9).begin() + 3), 50 ); // 50 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() ), 2 ); // 2 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 1), 2 ); // 2 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 2), 2 ); // 2 + BOOST_CHECK_EQUAL( *(tail_variate_means(acc4, quantile_probability = 0.1).begin() + 3), 14 ); // 14 +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("tail_variate_means test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/valarray.cpp b/src/boost/libs/accumulators/test/valarray.cpp new file mode 100644 index 00000000..ca0842de --- /dev/null +++ b/src/boost/libs/accumulators/test/valarray.cpp @@ -0,0 +1,180 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <iostream> +#include <valarray> +#include <boost/utility/enable_if.hpp> +#include <boost/type_traits/is_floating_point.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/min.hpp> +#include <boost/accumulators/statistics/max.hpp> +#include <boost/accumulators/statistics/mean.hpp> +#include <boost/accumulators/statistics/weighted_mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +typename boost::enable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) +{ + BOOST_CHECK_CLOSE(left, right, 1e-5); +} + +template<typename T> +typename boost::disable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) +{ + BOOST_CHECK_EQUAL(left, right); +} + +template<typename T> +void is_equal(std::valarray<T> const &left, std::valarray<T> const &right) +{ + BOOST_CHECK_EQUAL(left.size(), right.size()); + if(left.size() == right.size()) + { + for(std::size_t i = 0; i < left.size(); ++i) + { + is_equal_or_close(left[i], right[i]); + } + } +} + +namespace std +{ + template<typename T> + inline std::ostream &operator <<(std::ostream &sout, std::valarray<T> const &arr) + { + sout << '('; + for(std::size_t i = 0; i < arr.size(); ++i) + { + sout << arr[i] << ','; + } + sout << ')' << std::endl; + return sout; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + typedef std::valarray<int> sample_t; + + // test sum + { + accumulator_set<sample_t, stats<tag::sum> > acc(sample = sample_t(0,3)); + + acc(sample_t(1,3)); + acc(sample_t(2,3)); + acc(sample_t(3,3)); + + is_equal(sample_t(6,3), sum(acc)); + } + + // test min and max + { + int s1[] = {1,2,3}, s2[] = {0,3,4}, s3[] = {2,1,4}, min_res[] = {0,1,3}, max_res[] = {2,3,4}; + accumulator_set<sample_t, stats<tag::min, tag::max> > acc(sample = sample_t(0,3)); + + acc(sample_t(s1,3)); + acc(sample_t(s2,3)); + acc(sample_t(s3,3)); + + is_equal(sample_t(min_res,3), (min)(acc)); + is_equal(sample_t(max_res,3), (max)(acc)); + } + + // test mean(lazy) and mean(immediate) + { + accumulator_set<sample_t, stats<tag::mean> > acc(sample = sample_t(0,3)); + + acc(sample_t(1,3)); + is_equal(std::valarray<double>(1., 3), mean(acc)); + BOOST_CHECK_EQUAL(1u, count(acc)); + is_equal(sample_t(1, 3), sum(acc)); + + acc(sample_t(0,3)); + is_equal(std::valarray<double>(0.5, 3), mean(acc)); + BOOST_CHECK_EQUAL(2u, count(acc)); + is_equal(sample_t(1, 3), sum(acc)); + + acc(sample_t(2,3)); + is_equal(std::valarray<double>(1., 3), mean(acc)); + BOOST_CHECK_EQUAL(3u, count(acc)); + is_equal(sample_t(3, 3), sum(acc)); + + + accumulator_set<sample_t, stats<tag::mean(immediate)> > acc2(sample = sample_t(0,3)); + + acc2(sample_t(1,3)); + is_equal(std::valarray<double>(1., 3), mean(acc2)); + BOOST_CHECK_EQUAL(1u, count(acc2)); + + acc2(sample_t(0,3)); + is_equal(std::valarray<double>(0.5, 3), mean(acc2)); + BOOST_CHECK_EQUAL(2u, count(acc2)); + + acc2(sample_t(2,3)); + is_equal(std::valarray<double>(1., 3), mean(acc2)); + BOOST_CHECK_EQUAL(3u, count(acc2)); + } + + // test weighted_mean + { + accumulator_set<sample_t, stats<tag::weighted_mean>, int> acc(sample = sample_t(0,3)); + + acc(sample_t(10,3), weight = 2); // 20 + BOOST_CHECK_EQUAL(2, sum_of_weights(acc)); // + // + acc(sample_t(6,3), weight = 3); // 18 + BOOST_CHECK_EQUAL(5, sum_of_weights(acc)); // + // + acc(sample_t(4,3), weight = 4); // 16 + BOOST_CHECK_EQUAL(9, sum_of_weights(acc)); // + // + acc(sample_t(6,3), weight = 5); //+ 30 + BOOST_CHECK_EQUAL(14, sum_of_weights(acc)); // + //= 84 / 14 = 6 + + is_equal(std::valarray<double>(6.,3), weighted_mean(acc)); + + + accumulator_set<sample_t, stats<tag::weighted_mean(immediate)>, int> acc2(sample = sample_t(0,3)); + + acc2(sample_t(10,3), weight = 2); // 20 + BOOST_CHECK_EQUAL(2, sum_of_weights(acc2)); // + // + acc2(sample_t(6,3), weight = 3); // 18 + BOOST_CHECK_EQUAL(5, sum_of_weights(acc2)); // + // + acc2(sample_t(4,3), weight = 4); // 16 + BOOST_CHECK_EQUAL(9, sum_of_weights(acc2)); // + // + acc2(sample_t(6,3), weight = 5); //+ 30 + BOOST_CHECK_EQUAL(14, sum_of_weights(acc2));// + //= 84 / 14 = 6 + + is_equal(std::valarray<double>(6.,3), weighted_mean(acc2)); + + } +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("valarray test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/value.cpp b/src/boost/libs/accumulators/test/value.cpp new file mode 100644 index 00000000..3f059ce2 --- /dev/null +++ b/src/boost/libs/accumulators/test/value.cpp @@ -0,0 +1,45 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +namespace my +{ + BOOST_PARAMETER_KEYWORD(tag, int_val) +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + int i = 42; + accumulator_set<double, stats<tag::value<int, my::tag::int_val> > > acc2( + my::int_val = i); + + int val1 = value<int, my::tag::int_val>(acc2); + int val2 = value_tag<my::tag::int_val>(acc2); + + BOOST_CHECK_EQUAL(i, val1); + BOOST_CHECK_EQUAL(i, val2); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("value_accumulator test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/variance.cpp b/src/boost/libs/accumulators/test/variance.cpp new file mode 100644 index 00000000..8c48713f --- /dev/null +++ b/src/boost/libs/accumulators/test/variance.cpp @@ -0,0 +1,107 @@ +// (C) Copyright 2005 Daniel Egloff, Eric Niebler +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/variance.hpp> +#include <sstream> +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // matlab + // >> samples = [1:5]; + // >> mean(samples) + // ans = 3 + // >> sum(samples .* samples) / length(samples) + // ans = 11 + // >> sum(samples .* samples) / length(samples) - mean(samples)^2 + // ans = 2 + + // lazy variance, now lazy with syntactic sugar, thanks to Eric + accumulator_set<int, stats<tag::variance(lazy)> > acc1; + + acc1(1); + acc1(2); + acc1(3); + acc1(4); + acc1(5); + + BOOST_CHECK_EQUAL(5u, count(acc1)); + BOOST_CHECK_CLOSE(3., mean(acc1), 1e-5); + BOOST_CHECK_CLOSE(11., accumulators::moment<2>(acc1), 1e-5); + BOOST_CHECK_CLOSE(2., variance(acc1), 1e-5); + + // immediate variance + accumulator_set<int, stats<tag::variance> > acc2; + + acc2(1); + acc2(2); + acc2(3); + acc2(4); + acc2(5); + + BOOST_CHECK_EQUAL(5u, count(acc2)); + BOOST_CHECK_CLOSE(3., mean(acc2), 1e-5); + BOOST_CHECK_CLOSE(2., variance(acc2), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-5; + std::stringstream ss; + { + accumulator_set<int, stats<tag::variance(lazy)> > acc1; + accumulator_set<int, stats<tag::variance> > acc2; + acc1(1); + acc1(2); + acc1(3); + acc1(4); + acc1(5); + acc2(1); + acc2(2); + acc2(3); + acc2(4); + acc2(5); + BOOST_CHECK_CLOSE(2., variance(acc2), epsilon); + BOOST_CHECK_CLOSE(2., variance(acc1), epsilon); + boost::archive::text_oarchive oa(ss); + acc1.serialize(oa, 0); + acc2.serialize(oa, 0); + } + accumulator_set<int, stats<tag::variance(lazy)> > acc1; + accumulator_set<int, stats<tag::variance> > acc2; + boost::archive::text_iarchive ia(ss); + acc1.serialize(ia, 0); + acc2.serialize(ia, 0); + BOOST_CHECK_CLOSE(2., variance(acc2), epsilon); + BOOST_CHECK_CLOSE(2., variance(acc1), epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("variance test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/vector.cpp b/src/boost/libs/accumulators/test/vector.cpp new file mode 100644 index 00000000..d0dee2bf --- /dev/null +++ b/src/boost/libs/accumulators/test/vector.cpp @@ -0,0 +1,180 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <iostream> +#include <vector> +#include <boost/utility/enable_if.hpp> +#include <boost/type_traits/is_floating_point.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/min.hpp> +#include <boost/accumulators/statistics/max.hpp> +#include <boost/accumulators/statistics/mean.hpp> +#include <boost/accumulators/statistics/weighted_mean.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +typename boost::enable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) +{ + BOOST_CHECK_CLOSE(left, right, 1e-5); +} + +template<typename T> +typename boost::disable_if<is_floating_point<T> >::type is_equal_or_close(T const &left, T const &right) +{ + BOOST_CHECK_EQUAL(left, right); +} + +template<typename T> +void is_equal(std::vector<T> const &left, std::vector<T> const &right) +{ + BOOST_CHECK_EQUAL(left.size(), right.size()); + if(left.size() == right.size()) + { + for(std::size_t i = 0; i < left.size(); ++i) + { + is_equal_or_close(left[i], right[i]); + } + } +} + +namespace std +{ + template<typename T> + inline std::ostream &operator <<(std::ostream &sout, std::vector<T> const &arr) + { + sout << '('; + for(std::size_t i = 0; i < arr.size(); ++i) + { + sout << arr[i] << ','; + } + sout << ')' << std::endl; + return sout; + } +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + typedef std::vector<int> sample_t; + + // test sum + { + accumulator_set<sample_t, stats<tag::sum> > acc(sample = sample_t(3,0)); + + acc(sample_t(3,1)); + acc(sample_t(3,2)); + acc(sample_t(3,3)); + + is_equal(sample_t(3,6), sum(acc)); + } + + // test min and max + { + int s1[] = {1,2,3}, s2[] = {0,3,4}, s3[] = {2,1,4}, min_res[] = {0,1,3}, max_res[] = {2,3,4}; + accumulator_set<sample_t, stats<tag::min, tag::max> > acc(sample = sample_t(3,0)); + + acc(sample_t(s1,s1+3)); + acc(sample_t(s2,s2+3)); + acc(sample_t(s3,s3+3)); + + is_equal(sample_t(min_res,min_res+3), (min)(acc)); + is_equal(sample_t(max_res,max_res+3), (max)(acc)); + } + + // test mean(lazy) and mean(immediate) + { + accumulator_set<sample_t, stats<tag::mean> > acc(sample = sample_t(3,0)); + + acc(sample_t(3,1)); + is_equal(std::vector<double>(3, 1.), mean(acc)); + BOOST_CHECK_EQUAL(1u, count(acc)); + is_equal(sample_t(3,1), sum(acc)); + + acc(sample_t(3,0)); + is_equal(std::vector<double>(3, 0.5), mean(acc)); + BOOST_CHECK_EQUAL(2u, count(acc)); + is_equal(sample_t(3,1), sum(acc)); + + acc(sample_t(3,2)); + is_equal(std::vector<double>(3, 1.), mean(acc)); + BOOST_CHECK_EQUAL(3u, count(acc)); + is_equal(sample_t(3,3), sum(acc)); + + + accumulator_set<sample_t, stats<tag::mean(immediate)> > acc2(sample = sample_t(3,0)); + + acc2(sample_t(3,1)); + is_equal(std::vector<double>(3,1.), mean(acc2)); + BOOST_CHECK_EQUAL(1u, count(acc2)); + + acc2(sample_t(3,0)); + is_equal(std::vector<double>(3,0.5), mean(acc2)); + BOOST_CHECK_EQUAL(2u, count(acc2)); + + acc2(sample_t(3,2)); + is_equal(std::vector<double>(3,1.), mean(acc2)); + BOOST_CHECK_EQUAL(3u, count(acc2)); + } + + // test weighted_mean + { + accumulator_set<sample_t, stats<tag::weighted_mean>, int> acc(sample = sample_t(3,0)); + + acc(sample_t(3,10), weight = 2); // 20 + BOOST_CHECK_EQUAL(2, sum_of_weights(acc)); // + // + acc(sample_t(3,6), weight = 3); // 18 + BOOST_CHECK_EQUAL(5, sum_of_weights(acc)); // + // + acc(sample_t(3,4), weight = 4); // 16 + BOOST_CHECK_EQUAL(9, sum_of_weights(acc)); // + // + acc(sample_t(3,6), weight = 5); //+ 30 + BOOST_CHECK_EQUAL(14, sum_of_weights(acc)); // + //= 84 / 14 = 6 + + is_equal(std::vector<double>(3,6.), weighted_mean(acc)); + + + accumulator_set<sample_t, stats<tag::weighted_mean(immediate)>, int> acc2(sample = sample_t(3,0)); + + acc2(sample_t(3,10), weight = 2); // 20 + BOOST_CHECK_EQUAL(2, sum_of_weights(acc2)); // + // + acc2(sample_t(3,6), weight = 3); // 18 + BOOST_CHECK_EQUAL(5, sum_of_weights(acc2)); // + // + acc2(sample_t(3,4), weight = 4); // 16 + BOOST_CHECK_EQUAL(9, sum_of_weights(acc2)); // + // + acc2(sample_t(3,6), weight = 5); //+ 30 + BOOST_CHECK_EQUAL(14, sum_of_weights(acc2));// + //= 84 / 14 = 6 + + is_equal(std::vector<double>(3,6.), weighted_mean(acc2)); + + } +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("vector test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_covariance.cpp b/src/boost/libs/accumulators/test/weighted_covariance.cpp new file mode 100644 index 00000000..be4abf08 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_covariance.cpp @@ -0,0 +1,101 @@ +// (C) Copyright 2005 Daniel Egloff, Eric Niebler +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_covariance.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + std::vector<double> dummy; + dummy.push_back(0); + dummy.push_back(0); + + accumulator_set<double, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc; + accumulator_set<std::vector<double>, stats<tag::weighted_covariance<double, tag::covariate1> >, double > acc2(sample = dummy); + accumulator_set<double, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc3(covariate1 = dummy); + accumulator_set<std::vector<double>, stats<tag::weighted_covariance<std::vector<double>, tag::covariate1> >, double > acc4(sample = dummy, covariate1 = dummy); + + std::vector<double> a; + a.push_back(1.); + a.push_back(2.); + std::vector<double> b; + b.push_back(3.); + b.push_back(4.); + std::vector<double> c; + c.push_back(2.); + c.push_back(5.); + std::vector<double> d; + d.push_back(4.); + d.push_back(2.); + + // double - double + { + acc(1., weight = 1.1, covariate1 = 2.); + acc(1., weight = 2.2, covariate1 = 4.); + acc(2., weight = 3.3, covariate1 = 3.); + acc(6., weight = 4.4, covariate1 = 1.); + } + + // vector - double + { + acc2(a, weight = 1.1, covariate1 = 1.); + acc2(b, weight = 2.2, covariate1 = 1.); + acc2(c, weight = 3.3, covariate1 = 2.); + acc2(d, weight = 4.4, covariate1 = 6.); + } + + // double - vector + { + acc3(1., weight = 1.1, covariate1 = a); + acc3(1., weight = 2.2, covariate1 = b); + acc3(2., weight = 3.3, covariate1 = c); + acc3(6., weight = 4.4, covariate1 = d); + } + + // vector - vector + { + acc4(a, weight = 1.1, covariate1 = b); + acc4(b, weight = 2.2, covariate1 = c); + acc4(a, weight = 3.3, covariate1 = c); + acc4(d, weight = 4.4, covariate1 = b); + } + + double epsilon = 1e-6; + + BOOST_CHECK_CLOSE((weighted_covariance(acc)), -2.39, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc2))[0], 1.93, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc2))[1], -2.09, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc3))[0], 1.93, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc3))[1], -2.09, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,0), 0.4, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc4))(0,1), -0.2, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,0), -0.4, epsilon); + BOOST_CHECK_CLOSE((weighted_covariance(acc4))(1,1), 0.2, epsilon); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_covariance test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_extended_p_square.cpp b/src/boost/libs/accumulators/test/weighted_extended_p_square.cpp new file mode 100644 index 00000000..e13b0e3b --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_extended_p_square.cpp @@ -0,0 +1,100 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_extended_p_square.hpp + +#include <iostream> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_extended_p_square.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + typedef accumulator_set<double, stats<tag::weighted_extended_p_square>, double> accumulator_t; + + // problem with small results: epsilon is relative (in percent), not absolute + + // tolerance in % + double epsilon = 1; + + // some random number generators + double mu1 = -1.0; + double mu2 = 1.0; + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma1(mu1, 1); + boost::normal_distribution<> mean_sigma2(mu2, 1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2); + + std::vector<double> probs_uniform, probs_normal1, probs_normal2, probs_normal_exact1, probs_normal_exact2; + + double p1[] = {/*0.001,*/ 0.01, 0.1, 0.5, 0.9, 0.99, 0.999}; + probs_uniform.assign(p1, p1 + sizeof(p1) / sizeof(double)); + + double p2[] = {0.001, 0.025}; + double p3[] = {0.975, 0.999}; + probs_normal1.assign(p2, p2 + sizeof(p2) / sizeof(double)); + probs_normal2.assign(p3, p3 + sizeof(p3) / sizeof(double)); + + double p4[] = {-3.090232, -1.959963}; + double p5[] = {1.959963, 3.090232}; + probs_normal_exact1.assign(p4, p4 + sizeof(p4) / sizeof(double)); + probs_normal_exact2.assign(p5, p5 + sizeof(p5) / sizeof(double)); + + accumulator_t acc_uniform(extended_p_square_probabilities = probs_uniform); + accumulator_t acc_normal1(extended_p_square_probabilities = probs_normal1); + accumulator_t acc_normal2(extended_p_square_probabilities = probs_normal2); + + for (std::size_t i = 0; i < 100000; ++i) + { + acc_uniform(rng(), weight = 1.); + + double sample1 = normal1(); + double sample2 = normal2(); + acc_normal1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1))); + acc_normal2(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2))); + } + + // check for uniform distribution + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[0], probs_uniform[0], 6*epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[1], probs_uniform[1], 3*epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[2], probs_uniform[2], epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[3], probs_uniform[3], epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[4], probs_uniform[4], epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_uniform)[5], probs_uniform[5], epsilon); + + // check for standard normal distribution + for (std::size_t i = 0; i < probs_normal1.size(); ++i) + { + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_normal1)[i], probs_normal_exact1[i], epsilon); + BOOST_CHECK_CLOSE(weighted_extended_p_square(acc_normal2)[i], probs_normal_exact2[i], epsilon); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_extended_p_square test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_kurtosis.cpp b/src/boost/libs/accumulators/test/weighted_kurtosis.cpp new file mode 100644 index 00000000..3cedd5a2 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_kurtosis.cpp @@ -0,0 +1,70 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_kurtosis.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_kurtosis.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + // double epsilon = 1; + + accumulator_set<double, stats<tag::weighted_kurtosis>, double > acc1; + accumulator_set<int, stats<tag::weighted_kurtosis>, int > acc2; + + // 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) + { + acc1(normal(), weight = rng()); + } + + // This check fails because epsilon is relative and not absolute + // BOOST_CHECK_CLOSE( weighted_kurtosis(acc1), 0., epsilon ); + + acc2(2, weight = 4); + acc2(7, weight = 1); + acc2(4, weight = 3); + acc2(9, weight = 1); + acc2(3, weight = 2); + + BOOST_CHECK_EQUAL( weighted_mean(acc2), 42./11. ); + BOOST_CHECK_EQUAL( accumulators::weighted_moment<2>(acc2), 212./11. ); + BOOST_CHECK_EQUAL( accumulators::weighted_moment<3>(acc2), 1350./11. ); + BOOST_CHECK_EQUAL( accumulators::weighted_moment<4>(acc2), 9956./11. ); + BOOST_CHECK_CLOSE( weighted_kurtosis(acc2), 0.58137026432, 1e-6 ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_kurtosis test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_mean.cpp b/src/boost/libs/accumulators/test/weighted_mean.cpp new file mode 100644 index 00000000..ae71e8df --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_mean.cpp @@ -0,0 +1,102 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_mean.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +template<typename T> +void assert_is_double(T const &) +{ + BOOST_MPL_ASSERT((is_same<T, double>)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set< + int + , stats< + tag::weighted_mean + , tag::mean_of_weights + , tag::weighted_mean_of_variates<int, tag::covariate1> + > + , int + > acc, test_acc(sample = 0); + + acc(1, weight = 2, covariate1 = 3); + BOOST_CHECK_CLOSE(1., weighted_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., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + acc(0, weight = 4, covariate1 = 4); + BOOST_CHECK_CLOSE(1./3., weighted_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(11./3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + acc(2, weight = 9, covariate1 = 8); + BOOST_CHECK_CLOSE(4./3., weighted_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(94./15., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc)), 1e-5); + + assert_is_double(mean(acc)); + + accumulator_set< + int + , stats< + tag::weighted_mean(immediate) + , tag::mean_of_weights(immediate) + , tag::weighted_mean_of_variates<int, tag::covariate1>(immediate) + > + , int + > acc2, test_acc2(sample = 0); + + acc2(1, weight = 2, covariate1 = 3); + BOOST_CHECK_CLOSE(1., weighted_mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(1u, count(acc2)); + BOOST_CHECK_CLOSE(2., mean_of_weights(acc2), 1e-5); + BOOST_CHECK_CLOSE(3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + acc2(0, weight = 4, covariate1 = 4); + BOOST_CHECK_CLOSE(1./3., weighted_mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(2u, count(acc2)); + BOOST_CHECK_CLOSE(3., mean_of_weights(acc2), 1e-5); + BOOST_CHECK_CLOSE(11./3., (accumulators::weighted_mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + acc2(2, weight = 9, covariate1 = 8); + BOOST_CHECK_CLOSE(4./3., weighted_mean(acc2), 1e-5); + BOOST_CHECK_EQUAL(3u, count(acc2)); + BOOST_CHECK_CLOSE(5., mean_of_weights(acc2), 1e-5); + BOOST_CHECK_CLOSE(94./15., (accumulators::mean_of_variates<int, tag::covariate1>(acc2)), 1e-5); + + assert_is_double(mean(acc2)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_mean test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_median.cpp b/src/boost/libs/accumulators/test/weighted_median.cpp new file mode 100644 index 00000000..60321d1c --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_median.cpp @@ -0,0 +1,69 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/random.hpp> +#include <boost/range/iterator_range.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_median.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // Median estimation of normal distribution N(1,1) using samples from a narrow normal distribution N(1,0.01) + // The weights equal to the likelihood ratio of the corresponding samples + + // two random number generators + double mu = 1.; + double sigma_narrow = 0.01; + double sigma = 1.; + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma_narrow(mu,sigma_narrow); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_narrow(rng, mean_sigma_narrow); + + accumulator_set<double, stats<tag::weighted_median(with_p_square_quantile) >, double > acc; + accumulator_set<double, stats<tag::weighted_median(with_density) >, double > + acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); + accumulator_set<double, stats<tag::weighted_median(with_p_square_cumulative_distribution) >, double > + acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); + + + for (std::size_t i=0; i<1000000; ++i) + { + double sample = normal_narrow(); + double w = std::exp( + 0.5 * (sample - mu) * (sample - mu) * ( + 1./sigma_narrow/sigma_narrow - 1./sigma/sigma + ) + ); + acc(sample, weight = w); + acc_dens(sample, weight = w); + acc_cdist(sample, weight = w); + } + + BOOST_CHECK_CLOSE(1., weighted_median(acc), 2); + BOOST_CHECK_CLOSE(1., weighted_median(acc_dens), 3); + BOOST_CHECK_CLOSE(1., weighted_median(acc_cdist), 3); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_median test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_moment.cpp b/src/boost/libs/accumulators/test/weighted_moment.cpp new file mode 100644 index 00000000..4aec80d3 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_moment.cpp @@ -0,0 +1,47 @@ +// (C) Copyright Eric Niebler, Olivier Gygi 2006. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_moment.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<double, stats<tag::weighted_moment<2> >, double> acc2; + accumulator_set<double, stats<tag::weighted_moment<7> >, double> acc7; + + acc2(2.1, weight = 0.7); + acc2(2.7, weight = 1.4); + acc2(1.8, weight = 0.9); + + acc7(2.1, weight = 0.7); + acc7(2.7, weight = 1.4); + acc7(1.8, weight = 0.9); + + BOOST_CHECK_CLOSE(5.403, accumulators::weighted_moment<2>(acc2), 1e-5); + BOOST_CHECK_CLOSE(548.54182, accumulators::weighted_moment<7>(acc7), 1e-5); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_moment test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_p_square_cumul_dist.cpp b/src/boost/libs/accumulators/test/weighted_p_square_cumul_dist.cpp new file mode 100644 index 00000000..3fb5b57f --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_p_square_cumul_dist.cpp @@ -0,0 +1,103 @@ +// (C) Copyright Eric Niebler, Olivier Gygi 2006. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_p_square_cumul_dist.hpp + +#include <cmath> +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// erf() not known by VC++ compiler! +// my_erf() computes error function by numerically integrating with trapezoidal rule +// +double my_erf(double const& x, int const& n = 1000) +{ + double sum = 0.; + double delta = x/n; + for (int i = 1; i < n; ++i) + sum += std::exp(-i*i*delta*delta) * delta; + sum += 0.5 * delta * (1. + std::exp(-x*x)); + return sum * 2. / std::sqrt(3.141592653); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 4; + + typedef accumulator_set<double, stats<tag::weighted_p_square_cumulative_distribution>, double > accumulator_t; + + accumulator_t acc_upper(p_square_cumulative_distribution_num_cells = 100); + accumulator_t acc_lower(p_square_cumulative_distribution_num_cells = 100); + + // two random number generators + double mu_upper = 1.0; + double mu_lower = -1.0; + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma_upper(mu_upper,1); + boost::normal_distribution<> mean_sigma_lower(mu_lower,1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_upper(rng, mean_sigma_upper); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal_lower(rng, mean_sigma_lower); + + for (std::size_t i=0; i<100000; ++i) + { + double sample = normal_upper(); + acc_upper(sample, weight = std::exp(-mu_upper * (sample - 0.5 * mu_upper))); + } + + for (std::size_t i=0; i<100000; ++i) + { + double sample = normal_lower(); + acc_lower(sample, weight = std::exp(-mu_lower * (sample - 0.5 * mu_lower))); + } + + typedef iterator_range<std::vector<std::pair<double, double> >::iterator > histogram_type; + histogram_type histogram_upper = weighted_p_square_cumulative_distribution(acc_upper); + histogram_type histogram_lower = weighted_p_square_cumulative_distribution(acc_lower); + + // Note that applying importance sampling results in a region of the distribution + // to be estimated more accurately and another region to be estimated less accurately + // than without importance sampling, i.e., with unweighted samples + + for (std::size_t i = 0; i < histogram_upper.size(); ++i) + { + // problem with small results: epsilon is relative (in percent), not absolute! + + // check upper region of distribution + if ( histogram_upper[i].second > 0.1 ) + BOOST_CHECK_CLOSE( 0.5 * (1.0 + my_erf( histogram_upper[i].first / std::sqrt(2.0) )), histogram_upper[i].second, epsilon ); + // check lower region of distribution + if ( histogram_lower[i].second < -0.1 ) + BOOST_CHECK_CLOSE( 0.5 * (1.0 + my_erf( histogram_lower[i].first / std::sqrt(2.0) )), histogram_lower[i].second, epsilon ); + } +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_p_square_cumulative_distribution test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_p_square_quantile.cpp b/src/boost/libs/accumulators/test/weighted_p_square_quantile.cpp new file mode 100644 index 00000000..cf4fdeae --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_p_square_quantile.cpp @@ -0,0 +1,100 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_p_square_quantile.hpp + +#include <cmath> // for std::exp() +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_p_square_quantile.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + typedef accumulator_set<double, stats<tag::weighted_p_square_quantile>, double> accumulator_t; + + // tolerance in % + double epsilon = 1; + + // some random number generators + double mu4 = -1.0; + double mu5 = -1.0; + double mu6 = 1.0; + double mu7 = 1.0; + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma4(mu4, 1); + boost::normal_distribution<> mean_sigma5(mu5, 1); + boost::normal_distribution<> mean_sigma6(mu6, 1); + boost::normal_distribution<> mean_sigma7(mu7, 1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal4(rng, mean_sigma4); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal5(rng, mean_sigma5); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal6(rng, mean_sigma6); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal7(rng, mean_sigma7); + + accumulator_t acc0(quantile_probability = 0.001); + accumulator_t acc1(quantile_probability = 0.025); + accumulator_t acc2(quantile_probability = 0.975); + accumulator_t acc3(quantile_probability = 0.999); + + accumulator_t acc4(quantile_probability = 0.001); + accumulator_t acc5(quantile_probability = 0.025); + accumulator_t acc6(quantile_probability = 0.975); + accumulator_t acc7(quantile_probability = 0.999); + + + for (std::size_t i=0; i<100000; ++i) + { + double sample = rng(); + acc0(sample, weight = 1.); + acc1(sample, weight = 1.); + acc2(sample, weight = 1.); + acc3(sample, weight = 1.); + + double sample4 = normal4(); + double sample5 = normal5(); + double sample6 = normal6(); + double sample7 = normal7(); + acc4(sample4, weight = std::exp(-mu4 * (sample4 - 0.5 * mu4))); + acc5(sample5, weight = std::exp(-mu5 * (sample5 - 0.5 * mu5))); + acc6(sample6, weight = std::exp(-mu6 * (sample6 - 0.5 * mu6))); + acc7(sample7, weight = std::exp(-mu7 * (sample7 - 0.5 * mu7))); + } + // check for uniform distribution with weight = 1 + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc0), 0.001, 28 ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc1), 0.025, 5 ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc2), 0.975, epsilon ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc3), 0.999, epsilon ); + + // check for shifted standard normal distribution ("importance sampling") + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc4), -3.090232, epsilon ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc5), -1.959963, epsilon ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc6), 1.959963, epsilon ); + BOOST_CHECK_CLOSE( weighted_p_square_quantile(acc7), 3.090232, epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_p_square_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_pot_quantile.cpp b/src/boost/libs/accumulators/test/weighted_pot_quantile.cpp new file mode 100644 index 00000000..28d424ac --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_pot_quantile.cpp @@ -0,0 +1,104 @@ +// (C) Copyright Eric Niebler 2005. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for pot_quantile.hpp (weighted feature) + +#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1.; + + double mu1, mu2, l; + + mu1 = 1.; + mu2 = -1.; + l = 0.5; + + // two random number generators + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma1(mu1,1); + boost::normal_distribution<> mean_sigma2(mu2,1); + boost::exponential_distribution<> lambda(l); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2); + boost::variate_generator<boost::lagged_fibonacci607&, boost::exponential_distribution<> > exponential(rng, lambda); + + accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc1( + pot_threshold_value = 3. + ); + accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc2( + right_tail_cache_size = 10000 + , pot_threshold_probability = 0.99 + ); + accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_value)>, double > acc3( + pot_threshold_value = -3. + ); + accumulator_set<double, stats<tag::weighted_pot_quantile<left>(with_threshold_probability)>, double > acc4( + left_tail_cache_size = 10000 + , pot_threshold_probability = 0.01 + ); + + accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_value)>, double > acc5( + pot_threshold_value = 5. + ); + accumulator_set<double, stats<tag::weighted_pot_quantile<right>(with_threshold_probability)>, double > acc6( + right_tail_cache_size = 10000 + , pot_threshold_probability = 0.995 + ); + + for (std::size_t i = 0; i < 100000; ++i) + { + double sample1 = normal1(); + double sample2 = normal2(); + acc1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1))); + acc2(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1))); + acc3(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2))); + acc4(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2))); + } + + for (std::size_t i = 0; i < 100000; ++i) + { + double sample = exponential(); + acc5(sample, weight = 1./l * std::exp(-sample * (1. - l))); + acc6(sample, weight = 1./l * std::exp(-sample * (1. - l))); + } + + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.999), 3.090232, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc3, quantile_probability = 0.001), -3.090232, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc4, quantile_probability = 0.001), -3.090232, epsilon ); + + BOOST_CHECK_CLOSE( quantile(acc5, quantile_probability = 0.999), 6.908, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc6, quantile_probability = 0.999), 6.908, epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_pot_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_skewness.cpp b/src/boost/libs/accumulators/test/weighted_skewness.cpp new file mode 100644 index 00000000..b5ca1aab --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_skewness.cpp @@ -0,0 +1,69 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_skewness.hpp + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/numeric/functional/vector.hpp> +#include <boost/accumulators/numeric/functional/complex.hpp> +#include <boost/accumulators/numeric/functional/valarray.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_skewness.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + // double epsilon = 1; + + accumulator_set<double, stats<tag::weighted_skewness>, double > acc1; + accumulator_set<int, stats<tag::weighted_skewness>, int > acc2; + + // 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) + { + acc1(normal(), weight = rng()); + } + + // This check fails because epsilon is relative and not absolute + // BOOST_CHECK_CLOSE( weighted_skewness(acc1), 0., epsilon ); + + acc2(2, weight = 4); + acc2(7, weight = 1); + acc2(4, weight = 3); + acc2(9, weight = 1); + acc2(3, weight = 2); + + BOOST_CHECK_EQUAL( weighted_mean(acc2), 42./11. ); + BOOST_CHECK_EQUAL( accumulators::weighted_moment<2>(acc2), 212./11. ); + BOOST_CHECK_EQUAL( accumulators::weighted_moment<3>(acc2), 1350./11. ); + BOOST_CHECK_CLOSE( weighted_skewness(acc2), 1.30708406282, 1e-6 ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_skewness test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_sum.cpp b/src/boost/libs/accumulators/test/weighted_sum.cpp new file mode 100644 index 00000000..f0f79c81 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_sum.cpp @@ -0,0 +1,46 @@ +// (C) Copyright Eric Niebler, Olivier Gygi 2006. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_sum.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + accumulator_set<int, stats<tag::weighted_sum, tag::weighted_sum_of_variates<int, tag::covariate1> >, int> acc; + + acc(1, weight = 2, covariate1 = 3); + BOOST_CHECK_EQUAL(2, weighted_sum(acc)); + BOOST_CHECK_EQUAL(6, weighted_sum_of_variates(acc)); + + acc(2, weight = 3, covariate1 = 6); + BOOST_CHECK_EQUAL(8, weighted_sum(acc)); + BOOST_CHECK_EQUAL(24, weighted_sum_of_variates(acc)); + + acc(4, weight = 6, covariate1 = 9); + BOOST_CHECK_EQUAL(32, weighted_sum(acc)); + BOOST_CHECK_EQUAL(78, weighted_sum_of_variates(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_sum test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_sum_kahan.cpp b/src/boost/libs/accumulators/test/weighted_sum_kahan.cpp new file mode 100644 index 00000000..68cf87b8 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_sum_kahan.cpp @@ -0,0 +1,64 @@ +// (C) Copyright Simon West 2011. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_sum_kahan.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_weighted_sum_kahan +// +void test_weighted_sum_kahan() +{ + accumulator_set<float, stats<tag::weighted_sum_kahan>, float > acc; + + BOOST_CHECK_EQUAL(0.0f, weighted_sum_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(1, weight = 1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, weighted_sum_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// test_weighted_sum_of_variates_kahan +// +void test_weighted_sum_of_variates_kahan() +{ + accumulator_set< + float, + stats<tag::weighted_sum_of_variates_kahan<float, tag::covariate1> >, + float + > + acc; + + BOOST_CHECK_EQUAL(0.0f, weighted_sum_of_variates_kahan(acc)); + + for (size_t i = 0; i < 1e6; ++i) { + acc(0, weight = 1.0f, covariate1 = 1e-6f); + } + + BOOST_CHECK_EQUAL(1.0f, weighted_sum_of_variates_kahan(acc)); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted sum kahan tests"); + + test->add(BOOST_TEST_CASE(&test_weighted_sum_kahan)); + test->add(BOOST_TEST_CASE(&test_weighted_sum_of_variates_kahan)); + + return test; +} diff --git a/src/boost/libs/accumulators/test/weighted_tail_mean.cpp b/src/boost/libs/accumulators/test/weighted_tail_mean.cpp new file mode 100644 index 00000000..d7a6aa3e --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_tail_mean.cpp @@ -0,0 +1,77 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_tail_mean.hpp + +#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics.hpp> +#include <boost/accumulators/statistics/weighted_tail_mean.hpp> +#include <boost/accumulators/statistics/weighted_tail_quantile.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1; + + std::size_t n = 100000; // number of MC steps + std::size_t c = 25000; // cache size + + accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<right> >, double > + acc0( right_tail_cache_size = c ); + accumulator_set<double, stats<tag::non_coherent_weighted_tail_mean<left> >, double > + acc1( left_tail_cache_size = c ); + + // random number generators + boost::lagged_fibonacci607 rng; + + for (std::size_t i = 0; i < n; ++i) + { + double smpl = std::sqrt(rng()); + acc0(smpl, weight = 1./smpl); + } + + for (std::size_t i = 0; i < n; ++i) + { + double smpl = rng(); + acc1(smpl*smpl, weight = smpl); + } + + // check uniform distribution + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.95), 0.975, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.975), 0.9875, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.99), 0.995, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc0, quantile_probability = 0.999), 0.9995, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.05), 0.025, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.025), 0.0125, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.01), 0.005, epsilon ); + BOOST_CHECK_CLOSE( non_coherent_weighted_tail_mean(acc1, quantile_probability = 0.001), 0.0005, 5*epsilon ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_tail_mean test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_tail_quantile.cpp b/src/boost/libs/accumulators/test/weighted_tail_quantile.cpp new file mode 100644 index 00000000..33936661 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_tail_quantile.cpp @@ -0,0 +1,75 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_tail_quantile.hpp + +#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics.hpp> +#include <boost/accumulators/statistics/weighted_tail_quantile.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // tolerance in % + double epsilon = 1; + + std::size_t n = 100000; // number of MC steps + std::size_t c = 20000; // cache size + + double mu1 = 1.0; + double mu2 = -1.0; + boost::lagged_fibonacci607 rng; + boost::normal_distribution<> mean_sigma1(mu1,1); + boost::normal_distribution<> mean_sigma2(mu2,1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal1(rng, mean_sigma1); + boost::variate_generator<boost::lagged_fibonacci607&, boost::normal_distribution<> > normal2(rng, mean_sigma2); + + accumulator_set<double, stats<tag::weighted_tail_quantile<right> >, double> + acc1(right_tail_cache_size = c); + + accumulator_set<double, stats<tag::weighted_tail_quantile<left> >, double> + acc2(left_tail_cache_size = c); + + for (std::size_t i = 0; i < n; ++i) + { + double sample1 = normal1(); + double sample2 = normal2(); + acc1(sample1, weight = std::exp(-mu1 * (sample1 - 0.5 * mu1))); + acc2(sample2, weight = std::exp(-mu2 * (sample2 - 0.5 * mu2))); + } + + // check standard normal distribution + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.975), 1.959963, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc1, quantile_probability = 0.999), 3.090232, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.025), -1.959963, epsilon ); + BOOST_CHECK_CLOSE( quantile(acc2, quantile_probability = 0.001), -3.090232, epsilon ); + +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_tail_quantile test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_tail_variate_means.cpp b/src/boost/libs/accumulators/test/weighted_tail_variate_means.cpp new file mode 100644 index 00000000..e2248a25 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_tail_variate_means.cpp @@ -0,0 +1,131 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi. +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +// Test case for weighted_tail_variate_means.hpp + +#define BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT +#define BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT + +#include <boost/random.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/variates/covariate.hpp> +#include <boost/accumulators/statistics.hpp> +#include <boost/accumulators/statistics/weighted_tail_variate_means.hpp> + +using namespace boost; +using namespace unit_test; +using namespace boost::accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + std::size_t c = 5; // cache size + + typedef double variate_type; + typedef std::vector<variate_type> variate_set_type; + + accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(relative)>, double > + acc1( right_tail_cache_size = c ); + accumulator_set<double, stats<tag::weighted_tail_variate_means<right, variate_set_type, tag::covariate1>(absolute)>, double > + acc2( right_tail_cache_size = c ); + accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(relative)>, double > + acc3( left_tail_cache_size = c ); + accumulator_set<double, stats<tag::weighted_tail_variate_means<left, variate_set_type, tag::covariate1>(absolute)>, double > + acc4( left_tail_cache_size = c ); + + variate_set_type cov1, cov2, cov3, cov4, cov5; + double c1[] = { 10., 20., 30., 40. }; // 100 + double c2[] = { 26., 4., 17., 3. }; // 50 + double c3[] = { 46., 64., 40., 50. }; // 200 + double c4[] = { 1., 3., 70., 6. }; // 80 + double c5[] = { 2., 2., 2., 14. }; // 20 + cov1.assign(c1, c1 + sizeof(c1)/sizeof(variate_type)); + cov2.assign(c2, c2 + sizeof(c2)/sizeof(variate_type)); + cov3.assign(c3, c3 + sizeof(c3)/sizeof(variate_type)); + cov4.assign(c4, c4 + sizeof(c4)/sizeof(variate_type)); + cov5.assign(c5, c5 + sizeof(c5)/sizeof(variate_type)); + + acc1(100., weight = 0.8, covariate1 = cov1); + acc1( 50., weight = 0.9, covariate1 = cov2); + acc1(200., weight = 1.0, covariate1 = cov3); + acc1( 80., weight = 1.1, covariate1 = cov4); + acc1( 20., weight = 1.2, covariate1 = cov5); + + acc2(100., weight = 0.8, covariate1 = cov1); + acc2( 50., weight = 0.9, covariate1 = cov2); + acc2(200., weight = 1.0, covariate1 = cov3); + acc2( 80., weight = 1.1, covariate1 = cov4); + acc2( 20., weight = 1.2, covariate1 = cov5); + + acc3(100., weight = 0.8, covariate1 = cov1); + acc3( 50., weight = 0.9, covariate1 = cov2); + acc3(200., weight = 1.0, covariate1 = cov3); + acc3( 80., weight = 1.1, covariate1 = cov4); + acc3( 20., weight = 1.2, covariate1 = cov5); + + acc4(100., weight = 0.8, covariate1 = cov1); + acc4( 50., weight = 0.9, covariate1 = cov2); + acc4(200., weight = 1.0, covariate1 = cov3); + acc4( 80., weight = 1.1, covariate1 = cov4); + acc4( 20., weight = 1.2, covariate1 = cov5); + + // check relative risk contributions + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/(0.8*100 + 1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/(0.8*100 + 1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/(0.8*100 + 1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/(0.8*100 + 1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() ), (0.9*26 + 1.2*2)/(0.9*50 + 1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 1), (0.9*4 + 1.2*2)/(0.9*50 + 1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 2), (0.9*17 + 1.2*2)/(0.9*50 + 1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.3).begin() + 3), (0.9*3 + 1.2*14)/(0.9*50 + 1.2*20) ); + + // check absolute risk contributions + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() ), (0.8*10 + 1.0*46)/1.8 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 1), (0.8*20 + 1.0*64)/1.8 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 2), (0.8*30 + 1.0*40)/1.8 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.7).begin() + 3), (0.8*40 + 1.0*50)/1.8 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() ), (0.9*26 + 1.2*2)/2.1 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 1), (0.9*4 + 1.2*2)/2.1 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 2), (0.9*17 + 1.2*2)/2.1 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.3).begin() + 3), (0.9*3 + 1.2*14)/2.1 ); + + // check relative risk contributions + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() ), 1.0*46/(1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 1), 1.0*64/(1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 2), 1.0*40/(1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc1, quantile_probability = 0.9).begin() + 3), 1.0*50/(1.0*200) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() ), 1.2*2/(1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 1), 1.2*2/(1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 2), 1.2*2/(1.2*20) ); + BOOST_CHECK_EQUAL( *(relative_weighted_tail_variate_means(acc3, quantile_probability = 0.1).begin() + 3), 1.2*14/(1.2*20) ); + + // check absolute risk contributions + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() ), 1.0*46/1.0 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 1), 1.0*64/1.0 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 2), 1.0*40/1.0 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc2, quantile_probability = 0.9).begin() + 3), 1.0*50/1.0 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() ), 1.2*2/1.2 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 1), 1.2*2/1.2 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 2), 1.2*2/1.2 ); + BOOST_CHECK_EQUAL( *(weighted_tail_variate_means(acc4, quantile_probability = 0.1).begin() + 3), 1.2*14/1.2 ); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_tail_variate_means test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} + diff --git a/src/boost/libs/accumulators/test/weighted_variance.cpp b/src/boost/libs/accumulators/test/weighted_variance.cpp new file mode 100644 index 00000000..f4b81d47 --- /dev/null +++ b/src/boost/libs/accumulators/test/weighted_variance.cpp @@ -0,0 +1,81 @@ +// (C) Copyright 2006 Eric Niebler, Olivier Gygi +// Use, modification and distribution are subject to the +// Boost Software License, Version 1.0. (See accompanying file +// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include <boost/test/unit_test.hpp> +#include <boost/test/floating_point_comparison.hpp> +#include <boost/random.hpp> +#include <boost/accumulators/accumulators.hpp> +#include <boost/accumulators/statistics/stats.hpp> +#include <boost/accumulators/statistics/weighted_variance.hpp> + +using namespace boost; +using namespace unit_test; +using namespace accumulators; + +/////////////////////////////////////////////////////////////////////////////// +// test_stat +// +void test_stat() +{ + // lazy weighted_variance + accumulator_set<int, stats<tag::weighted_variance(lazy)>, int> acc1; + + acc1(1, weight = 2); // 2 + acc1(2, weight = 3); // 6 + acc1(3, weight = 1); // 3 + acc1(4, weight = 4); // 16 + acc1(5, weight = 1); // 5 + + // weighted_mean = (2+6+3+16+5) / (2+3+1+4+1) = 32 / 11 = 2.9090909090909090909090909090909 + + BOOST_CHECK_EQUAL(5u, count(acc1)); + BOOST_CHECK_CLOSE(2.9090909, weighted_mean(acc1), 1e-5); + BOOST_CHECK_CLOSE(10.1818182, accumulators::weighted_moment<2>(acc1), 1e-5); + BOOST_CHECK_CLOSE(1.7190083, weighted_variance(acc1), 1e-5); + + accumulator_set<int, stats<tag::weighted_variance>, int> acc2; + + acc2(1, weight = 2); + acc2(2, weight = 3); + acc2(3, weight = 1); + acc2(4, weight = 4); + acc2(5, weight = 1); + + BOOST_CHECK_EQUAL(5u, count(acc2)); + BOOST_CHECK_CLOSE(2.9090909, weighted_mean(acc2), 1e-5); + BOOST_CHECK_CLOSE(1.7190083, weighted_variance(acc2), 1e-5); + + // check lazy and immediate variance with random numbers + + // 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); + + accumulator_set<double, stats<tag::weighted_variance(lazy)>, double > acc_lazy; + accumulator_set<double, stats<tag::weighted_variance>, double > acc_immediate; + + for (std::size_t i=0; i<10000; ++i) + { + double value = normal(); + acc_lazy(value, weight = rng()); + acc_immediate(value, weight = rng()); + } + + BOOST_CHECK_CLOSE(1., weighted_variance(acc_lazy), 5.); + BOOST_CHECK_CLOSE(1., weighted_variance(acc_immediate), 5.); +} + +/////////////////////////////////////////////////////////////////////////////// +// init_unit_test_suite +// +test_suite* init_unit_test_suite( int argc, char* argv[] ) +{ + test_suite *test = BOOST_TEST_SUITE("weighted_variance test"); + + test->add(BOOST_TEST_CASE(&test_stat)); + + return test; +} |