diff --git a/example/Jamfile.v2 b/example/Jamfile.v2 index ad050d3..eff105b 100644 --- a/example/Jamfile.v2 +++ b/example/Jamfile.v2 @@ -8,4 +8,6 @@ exe example : ../../.. $(BOOST_ROOT) + /boost/serialization + "-Wno-deprecated-declarations" ; diff --git a/example/main.cpp b/example/main.cpp index 8541dba..2c525b2 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -6,6 +6,7 @@ // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include +#include // needed for de/serialization example (4) #include #include #include @@ -13,6 +14,9 @@ #include #include #include +// needed for de/serialization example (4) +#include +#include using namespace boost; using namespace boost::accumulators; @@ -159,6 +163,85 @@ void example3() } } +/////////////////////////////////////////////////////////////////////////////// +// example4 +// +// Show how accumulators could be persisted into a file, and then continued +// from where they were left of +// +void example4() +{ + accumulator_set< + double + , stats, tag::p_square_quantile, tag::kurtosis > + > acc(quantile_probability = 0.9); + + { + // accumulate values from array + boost::array data = {-10., -8., -7., -6., -5., -4., -3., -2., -1., 0.}; + + acc = std::for_each(data.begin(), data.end(), acc); + } + + std::cout << " min = " << (min)(acc) << std::endl; + std::cout << " mean = " << mean(acc) << std::endl; + std::cout << " count = " << count(acc) << std::endl; + std::cout << " sum = " << sum(acc) << std::endl; + std::cout << " moment<2> = " << accumulators::moment<2>(acc) << std::endl; + std::cout << " p_square_quantile = " << accumulators::p_square_quantile(acc) << std::endl; + std::cout << " kurtosis = " << accumulators::kurtosis(acc) << std::endl; + + // save accumulator list into a file called "saved-stats" + const unsigned ACC_VER = 0; + const char* file_name = "saved-stats"; + { + std::ofstream ofs(file_name); + boost::archive::text_oarchive oa(ofs); + acc.serialize(oa, ACC_VER); + } + + // create a new accumulator set and initialize from data saved into the file + accumulator_set< + double + , stats, tag::p_square_quantile, tag::kurtosis > + > restored_acc(quantile_probability = 0.9); + + { + std::ifstream ifs(file_name); + boost::archive::text_iarchive ia(ifs); + restored_acc.serialize(ia, ACC_VER); + } + + // continue accumulating into both sets + { + // accumulate values from array + boost::array data = {10., 8., 7., 6., 5., 4., 3., 2., 1., 0.}; + + acc = std::for_each(data.begin(), data.end(), acc); + restored_acc = std::for_each(data.begin(), data.end(), restored_acc); + } + + // validate that both return th same values + std::cout << std::endl << "Values in original set:" << std::endl; + std::cout << " min""(acc) = " << (min)(acc) << std::endl; + std::cout << " mean(acc) = " << mean(acc) << std::endl; + std::cout << " count(acc) = " << count(acc) << std::endl; + std::cout << " sum(acc) = " << sum(acc) << std::endl; + std::cout << " moment<2>(acc) = " << accumulators::moment<2>(acc) << std::endl; + std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(acc) << std::endl; + std::cout << " kurtosis(acc) = " << accumulators::kurtosis(acc) << std::endl; + + std::cout << std::endl << "Values in restored set:" << std::endl; + std::cout << " min""(acc) = " << (min)(restored_acc) << std::endl; + std::cout << " mean(acc) = " << mean(restored_acc) << std::endl; + std::cout << " count(acc) = " << count(restored_acc) << std::endl; + std::cout << " sum(acc) = " << sum(restored_acc) << std::endl; + std::cout << " moment<2>(acc) = " << accumulators::moment<2>(restored_acc) << std::endl; + std::cout << " p_square_quantile(acc) = " << accumulators::p_square_quantile(restored_acc) << std::endl; + std::cout << " kurtosis(acc) = " << accumulators::kurtosis(restored_acc) << std::endl; + +} + /////////////////////////////////////////////////////////////////////////////// // main int main() @@ -172,5 +255,8 @@ int main() std::cout << "\nExample 3:\n"; example3(); + std::cout << "\nExample 4:\n"; + example4(); + return 0; } diff --git a/include/boost/accumulators/framework/accumulator_set.hpp b/include/boost/accumulators/framework/accumulator_set.hpp index ed1ceb1..5b68e3f 100644 --- a/include/boost/accumulators/framework/accumulator_set.hpp +++ b/include/boost/accumulators/framework/accumulator_set.hpp @@ -86,6 +86,25 @@ namespace detail { }; + // function object that serialize an accumulator + template + struct serialize_accumulator + { + serialize_accumulator(Archive & _ar, const unsigned int _file_version) : + ar(_ar), file_version(_file_version) + {} + + template + void operator ()(Accumulator &accumulator) + { + accumulator.serialize(ar, file_version); + } + + private: + Archive& ar; + const unsigned int file_version; + }; + } // namespace detail #ifdef _MSC_VER @@ -338,6 +357,14 @@ struct accumulator_set ); } + // make the accumulator set serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + detail::serialize_accumulator serializer(ar, file_version); + fusion::for_each(this->accumulators, serializer); + } + private: accumulators_type accumulators; diff --git a/include/boost/accumulators/statistics/count.hpp b/include/boost/accumulators/statistics/count.hpp index 6d30b41..b83435a 100644 --- a/include/boost/accumulators/statistics/count.hpp +++ b/include/boost/accumulators/statistics/count.hpp @@ -43,6 +43,13 @@ namespace impl return this->cnt; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & cnt; + } + private: std::size_t cnt; }; diff --git a/include/boost/accumulators/statistics/covariance.hpp b/include/boost/accumulators/statistics/covariance.hpp index b3030b9..d80a85e 100644 --- a/include/boost/accumulators/statistics/covariance.hpp +++ b/include/boost/accumulators/statistics/covariance.hpp @@ -152,6 +152,13 @@ namespace impl return this->cov_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & cov_; + } + private: result_type cov_; }; diff --git a/include/boost/accumulators/statistics/density.hpp b/include/boost/accumulators/statistics/density.hpp index 88ca17d..32b4806 100644 --- a/include/boost/accumulators/statistics/density.hpp +++ b/include/boost/accumulators/statistics/density.hpp @@ -25,6 +25,8 @@ #include #include #include +#include +#include namespace boost { namespace accumulators { @@ -184,6 +186,20 @@ namespace impl return make_iterator_range(this->histogram); } + // make this accumulator serializeable + // TODO split to save/load and check on parameters provided in ctor + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & cache_size; + ar & cache; + ar & num_bins; + ar & samples_in_bin; + ar & bin_positions; + ar & histogram; + ar & is_dirty; + } + private: std::size_t cache_size; // number of cached samples array_type cache; // cache to store the first cache_size samples diff --git a/include/boost/accumulators/statistics/extended_p_square.hpp b/include/boost/accumulators/statistics/extended_p_square.hpp index e6cc8dc..6990050 100644 --- a/include/boost/accumulators/statistics/extended_p_square.hpp +++ b/include/boost/accumulators/statistics/extended_p_square.hpp @@ -26,6 +26,7 @@ #include #include #include +#include namespace boost { namespace accumulators { @@ -236,6 +237,19 @@ namespace impl ); } + public: + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that the parameters did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & probabilities; + ar & heights; + ar & actual_positions; + ar & desired_positions; + ar & positions_increments; + } + private: array_type probabilities; // the quantile probabilities array_type heights; // q_i diff --git a/include/boost/accumulators/statistics/extended_p_square_quantile.hpp b/include/boost/accumulators/statistics/extended_p_square_quantile.hpp index a17843d..f57304c 100644 --- a/include/boost/accumulators/statistics/extended_p_square_quantile.hpp +++ b/include/boost/accumulators/statistics/extended_p_square_quantile.hpp @@ -185,6 +185,17 @@ namespace impl } } + + public: + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that the parameters did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & probabilities; + ar & probability; + } + private: array_type probabilities; diff --git a/include/boost/accumulators/statistics/kurtosis.hpp b/include/boost/accumulators/statistics/kurtosis.hpp index 76c93d3..9ff9ecd 100644 --- a/include/boost/accumulators/statistics/kurtosis.hpp +++ b/include/boost/accumulators/statistics/kurtosis.hpp @@ -63,6 +63,10 @@ namespace impl * ( accumulators::moment<2>(args) - mean(args) * mean(args) ) ) - 3.; } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/max.hpp b/include/boost/accumulators/statistics/max.hpp index 820f659..15033e6 100644 --- a/include/boost/accumulators/statistics/max.hpp +++ b/include/boost/accumulators/statistics/max.hpp @@ -48,6 +48,13 @@ namespace impl return this->max_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & max_; + } + private: Sample max_; }; diff --git a/include/boost/accumulators/statistics/mean.hpp b/include/boost/accumulators/statistics/mean.hpp index 4788837..c6df176 100644 --- a/include/boost/accumulators/statistics/mean.hpp +++ b/include/boost/accumulators/statistics/mean.hpp @@ -41,6 +41,10 @@ namespace impl extractor sum; return numeric::fdiv(sum(args), count(args)); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; template @@ -71,6 +75,12 @@ namespace impl return this->mean; } + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & mean; + } + private: result_type mean; }; diff --git a/include/boost/accumulators/statistics/median.hpp b/include/boost/accumulators/statistics/median.hpp index d361c6d..4d4118d 100644 --- a/include/boost/accumulators/statistics/median.hpp +++ b/include/boost/accumulators/statistics/median.hpp @@ -48,6 +48,10 @@ namespace impl { return p_square_quantile_for_median(args); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; /////////////////////////////////////////////////////////////////////////////// // with_density_median_impl @@ -105,6 +109,16 @@ namespace impl return this->median; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + ar & is_dirty; + ar & median; + } + + private: mutable float_type sum; mutable bool is_dirty; @@ -160,6 +174,15 @@ namespace impl return this->median; } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & is_dirty; + ar & median; + } + private: mutable bool is_dirty; diff --git a/include/boost/accumulators/statistics/min.hpp b/include/boost/accumulators/statistics/min.hpp index 83943bd..bf30b79 100644 --- a/include/boost/accumulators/statistics/min.hpp +++ b/include/boost/accumulators/statistics/min.hpp @@ -48,6 +48,13 @@ namespace impl return this->min_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & min_; + } + private: Sample min_; }; diff --git a/include/boost/accumulators/statistics/moment.hpp b/include/boost/accumulators/statistics/moment.hpp index 3dabd47..9ba1e4c 100644 --- a/include/boost/accumulators/statistics/moment.hpp +++ b/include/boost/accumulators/statistics/moment.hpp @@ -75,6 +75,13 @@ namespace impl return numeric::fdiv(this->sum, count(args)); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + } + private: Sample sum; }; diff --git a/include/boost/accumulators/statistics/p_square_cumul_dist.hpp b/include/boost/accumulators/statistics/p_square_cumul_dist.hpp index 5069283..daa66f9 100644 --- a/include/boost/accumulators/statistics/p_square_cumul_dist.hpp +++ b/include/boost/accumulators/statistics/p_square_cumul_dist.hpp @@ -20,6 +20,8 @@ #include #include #include +#include +#include namespace boost { namespace accumulators { @@ -204,6 +206,20 @@ namespace impl //return histogram; return make_iterator_range(this->histogram); } + + // make this accumulator serializeable + // TODO split to save/load and check on parameters provided in ctor + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & num_cells; + ar & heights; + ar & actual_positions; + ar & desired_positions; + ar & positions_increments; + ar & histogram; + ar & is_dirty; + } private: std::size_t num_cells; // number of cells b diff --git a/include/boost/accumulators/statistics/p_square_quantile.hpp b/include/boost/accumulators/statistics/p_square_quantile.hpp index 3678594..d87c1b3 100644 --- a/include/boost/accumulators/statistics/p_square_quantile.hpp +++ b/include/boost/accumulators/statistics/p_square_quantile.hpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace boost { namespace accumulators { @@ -192,6 +193,18 @@ namespace impl return this->heights[2]; } + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that P did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & p; + ar & heights; + ar & actual_positions; + ar & desired_positions; + ar & positions_increments; + } + private: float_type p; // the quantile probability p array_type heights; // q_i diff --git a/include/boost/accumulators/statistics/peaks_over_threshold.hpp b/include/boost/accumulators/statistics/peaks_over_threshold.hpp index f04f743..de73c65 100644 --- a/include/boost/accumulators/statistics/peaks_over_threshold.hpp +++ b/include/boost/accumulators/statistics/peaks_over_threshold.hpp @@ -181,6 +181,21 @@ namespace impl return this->fit_parameters_; } + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that threshold did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & Nu_; + ar & mu_; + ar & sigma2_; + ar & threshold_; + ar & get<0>(fit_parameters_); + ar & get<1>(fit_parameters_); + ar & get<2>(fit_parameters_); + ar & is_dirty_; + } + private: std::size_t Nu_; // number of samples larger than threshold mutable float_type mu_; // mean of Nu_ largest samples @@ -291,6 +306,20 @@ namespace impl return this->fit_parameters_; } + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that threshold did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & mu_; + ar & sigma2_; + ar & threshold_probability_; + ar & get<0>(fit_parameters_); + ar & get<1>(fit_parameters_); + ar & get<2>(fit_parameters_); + ar & is_dirty_; + } + private: mutable float_type mu_; // mean of samples above threshold u mutable float_type sigma2_; // variance of samples above threshold u diff --git a/include/boost/accumulators/statistics/pot_quantile.hpp b/include/boost/accumulators/statistics/pot_quantile.hpp index 470bdba..ecd49f2 100644 --- a/include/boost/accumulators/statistics/pot_quantile.hpp +++ b/include/boost/accumulators/statistics/pot_quantile.hpp @@ -80,6 +80,13 @@ namespace impl , -xi_hat ) - 1.)); } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sign_; + } private: short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result diff --git a/include/boost/accumulators/statistics/pot_tail_mean.hpp b/include/boost/accumulators/statistics/pot_tail_mean.hpp index a78043f..96f4dc1 100644 --- a/include/boost/accumulators/statistics/pot_tail_mean.hpp +++ b/include/boost/accumulators/statistics/pot_tail_mean.hpp @@ -90,6 +90,14 @@ namespace impl is_same::value ? args[quantile_probability] : 1. - args[quantile_probability] , -xi_hat); } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sign_; + } + private: short sign_; // if the fit parameters from the mirrored left tail extreme values are used, mirror back the result }; diff --git a/include/boost/accumulators/statistics/rolling_count.hpp b/include/boost/accumulators/statistics/rolling_count.hpp index 1e34f76..803f1b7 100644 --- a/include/boost/accumulators/statistics/rolling_count.hpp +++ b/include/boost/accumulators/statistics/rolling_count.hpp @@ -40,6 +40,10 @@ namespace impl { return static_cast(rolling_window_plus1(args).size()) - is_rolling_window_plus1_full(args); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/rolling_mean.hpp b/include/boost/accumulators/statistics/rolling_mean.hpp index 5ae10b2..bd5494c 100644 --- a/include/boost/accumulators/statistics/rolling_mean.hpp +++ b/include/boost/accumulators/statistics/rolling_mean.hpp @@ -43,6 +43,10 @@ namespace boost { namespace accumulators { return numeric::fdiv(rolling_sum(args), rolling_count(args)); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; /////////////////////////////////////////////////////////////////////////////// @@ -87,6 +91,13 @@ namespace boost { namespace accumulators { return mean_; } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & mean_; + } private: @@ -182,4 +193,4 @@ namespace boost { namespace accumulators }; }} // namespace boost::accumulators -#endif \ No newline at end of file +#endif diff --git a/include/boost/accumulators/statistics/rolling_moment.hpp b/include/boost/accumulators/statistics/rolling_moment.hpp index f172cee..28ea5b5 100644 --- a/include/boost/accumulators/statistics/rolling_moment.hpp +++ b/include/boost/accumulators/statistics/rolling_moment.hpp @@ -58,6 +58,13 @@ namespace impl return numeric::fdiv(this->sum_, rolling_count(args)); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum_; + } + private: result_type sum_; }; diff --git a/include/boost/accumulators/statistics/rolling_sum.hpp b/include/boost/accumulators/statistics/rolling_sum.hpp index bbb7a8e..c511460 100644 --- a/include/boost/accumulators/statistics/rolling_sum.hpp +++ b/include/boost/accumulators/statistics/rolling_sum.hpp @@ -51,6 +51,13 @@ namespace impl return this->sum_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum_; + } + private: Sample sum_; }; diff --git a/include/boost/accumulators/statistics/rolling_variance.hpp b/include/boost/accumulators/statistics/rolling_variance.hpp index 33b3922..249d5c1 100644 --- a/include/boost/accumulators/statistics/rolling_variance.hpp +++ b/include/boost/accumulators/statistics/rolling_variance.hpp @@ -62,6 +62,10 @@ namespace impl if (nr_samples < 2) return result_type(); return nr_samples*(rolling_moment<2>(args) - mean*mean)/(nr_samples-1); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; //! Iterative calculation of the rolling variance. @@ -138,6 +142,14 @@ namespace impl if (nr_samples < 2) return result_type(); return numeric::fdiv(sum_of_squares_,(nr_samples-1)); } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & previous_mean_; + ar & sum_of_squares_; + } private: diff --git a/include/boost/accumulators/statistics/rolling_window.hpp b/include/boost/accumulators/statistics/rolling_window.hpp index 2e0a334..a43ecdf 100644 --- a/include/boost/accumulators/statistics/rolling_window.hpp +++ b/include/boost/accumulators/statistics/rolling_window.hpp @@ -21,6 +21,45 @@ #include #include #include +#include + +namespace boost { namespace serialization { + +// implement serialization for boost::circular_buffer +template +void save(Archive& ar, const circular_buffer& b, const unsigned int /* version */) +{ + typename circular_buffer::size_type size = b.size(); + ar << b.capacity(); + ar << size; + const typename circular_buffer::const_array_range one = b.array_one(); + const typename circular_buffer::const_array_range two = b.array_two(); + ar.save_binary(one.first, one.second*sizeof(T)); + ar.save_binary(two.first, two.second*sizeof(T)); +} + +template +void load(Archive& ar, circular_buffer& b, const unsigned int /* version */) +{ + typename circular_buffer::capacity_type capacity; + typename circular_buffer::size_type size; + ar >> capacity; + b.set_capacity(capacity); + ar >> size; + b.clear(); + const typename circular_buffer::pointer buff = new T[size*sizeof(T)]; + ar.load_binary(buff, size*sizeof(T)); + b.insert(b.begin(), buff, buff+size); + delete[] buff; +} + +template +inline void serialize(Archive & ar, circular_buffer& b, const unsigned int version) +{ + split_free(ar, b, version); +} + +} } // end namespace boost::serialization namespace boost { namespace accumulators { @@ -83,6 +122,12 @@ namespace impl return result_type(this->buffer_.begin(), this->buffer_.end()); } + template + void serialize(Archive & ar, const unsigned int version) + { + ar & buffer_; + } + private: circular_buffer buffer_; }; @@ -112,6 +157,10 @@ namespace impl { return rolling_window_plus1(args).advance_begin(is_rolling_window_plus1_full(args)); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/skewness.hpp b/include/boost/accumulators/statistics/skewness.hpp index c383ec4..9c9c376 100644 --- a/include/boost/accumulators/statistics/skewness.hpp +++ b/include/boost/accumulators/statistics/skewness.hpp @@ -65,6 +65,10 @@ namespace impl * std::sqrt( accumulators::moment<2>(args) - mean(args) * mean(args) ) ); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/sum.hpp b/include/boost/accumulators/statistics/sum.hpp index 126ce24..7e507cc 100644 --- a/include/boost/accumulators/statistics/sum.hpp +++ b/include/boost/accumulators/statistics/sum.hpp @@ -51,8 +51,13 @@ namespace impl return this->sum; } - private: + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + } + private: Sample sum; }; diff --git a/include/boost/accumulators/statistics/sum_kahan.hpp b/include/boost/accumulators/statistics/sum_kahan.hpp index 97ade18..b5e9092 100644 --- a/include/boost/accumulators/statistics/sum_kahan.hpp +++ b/include/boost/accumulators/statistics/sum_kahan.hpp @@ -66,6 +66,14 @@ struct sum_kahan_impl return this->sum; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + ar & compensation; + } + private: Sample sum; Sample compensation; diff --git a/include/boost/accumulators/statistics/tail.hpp b/include/boost/accumulators/statistics/tail.hpp index be6cdee..154a30c 100644 --- a/include/boost/accumulators/statistics/tail.hpp +++ b/include/boost/accumulators/statistics/tail.hpp @@ -268,6 +268,17 @@ namespace impl std::vector const &samples; }; + public: + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & is_sorted; + ar & indices; + ar & samples; + } + + private: mutable bool is_sorted; mutable std::vector indices; std::vector samples; diff --git a/include/boost/accumulators/statistics/tail_mean.hpp b/include/boost/accumulators/statistics/tail_mean.hpp index 67dae37..f56606d 100644 --- a/include/boost/accumulators/statistics/tail_mean.hpp +++ b/include/boost/accumulators/statistics/tail_mean.hpp @@ -88,6 +88,10 @@ namespace impl - numeric::fdiv(n, count(args)) ); } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; /////////////////////////////////////////////////////////////////////////////// @@ -159,6 +163,10 @@ namespace impl } } } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/tail_quantile.hpp b/include/boost/accumulators/statistics/tail_quantile.hpp index 9ff56b5..ed1f6e6 100644 --- a/include/boost/accumulators/statistics/tail_quantile.hpp +++ b/include/boost/accumulators/statistics/tail_quantile.hpp @@ -98,6 +98,10 @@ namespace impl } } } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; } // namespace impl diff --git a/include/boost/accumulators/statistics/tail_variate.hpp b/include/boost/accumulators/statistics/tail_variate.hpp index a9fc7d2..6885ece 100644 --- a/include/boost/accumulators/statistics/tail_variate.hpp +++ b/include/boost/accumulators/statistics/tail_variate.hpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace boost { namespace accumulators { @@ -69,6 +70,14 @@ namespace impl ); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & variates; + } + + private: std::vector variates; }; diff --git a/include/boost/accumulators/statistics/tail_variate_means.hpp b/include/boost/accumulators/statistics/tail_variate_means.hpp index a97eab5..efe9f7c 100644 --- a/include/boost/accumulators/statistics/tail_variate_means.hpp +++ b/include/boost/accumulators/statistics/tail_variate_means.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #ifdef _MSC_VER # pragma warning(push) @@ -145,6 +146,13 @@ namespace impl return make_iterator_range(this->tail_means_); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & tail_means_; + } + private: mutable array_type tail_means_; diff --git a/include/boost/accumulators/statistics/variance.hpp b/include/boost/accumulators/statistics/variance.hpp index baac556..9db858d 100644 --- a/include/boost/accumulators/statistics/variance.hpp +++ b/include/boost/accumulators/statistics/variance.hpp @@ -53,6 +53,10 @@ namespace impl result_type tmp = mean(args); return accumulators::moment<2>(args) - tmp * tmp; } + + // serialization is done by accumulators it depends on + template + void serialize(Archive & ar, const unsigned int file_version) {} }; //! Iterative calculation of variance. @@ -113,6 +117,13 @@ namespace impl return this->variance; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & variance; + } + private: result_type variance; }; diff --git a/include/boost/accumulators/statistics/weighted_covariance.hpp b/include/boost/accumulators/statistics/weighted_covariance.hpp index 25d613c..e423a22 100644 --- a/include/boost/accumulators/statistics/weighted_covariance.hpp +++ b/include/boost/accumulators/statistics/weighted_covariance.hpp @@ -97,6 +97,13 @@ namespace impl return this->cov_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & cov_; + } + private: result_type cov_; }; diff --git a/include/boost/accumulators/statistics/weighted_density.hpp b/include/boost/accumulators/statistics/weighted_density.hpp index 1407368..6b5a0ee 100644 --- a/include/boost/accumulators/statistics/weighted_density.hpp +++ b/include/boost/accumulators/statistics/weighted_density.hpp @@ -23,6 +23,8 @@ #include #include #include // for named parameters density_cache_size and density_num_bins +#include +#include namespace boost { namespace accumulators { @@ -171,6 +173,20 @@ namespace impl return make_iterator_range(this->histogram); } + // make this accumulator serializeable + // TODO split to save/load and check on parameters provided in ctor + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & cache_size; + ar & cache; + ar & num_bins; + ar & samples_in_bin; + ar & bin_positions; + ar & histogram; + ar & is_dirty; + } + private: std::size_t cache_size; // number of cached samples histogram_type cache; // cache to store the first cache_size samples with their weights as std::pair diff --git a/include/boost/accumulators/statistics/weighted_extended_p_square.hpp b/include/boost/accumulators/statistics/weighted_extended_p_square.hpp index ac857e0..f88e9b9 100644 --- a/include/boost/accumulators/statistics/weighted_extended_p_square.hpp +++ b/include/boost/accumulators/statistics/weighted_extended_p_square.hpp @@ -28,6 +28,7 @@ #include #include #include +#include namespace boost { namespace accumulators { @@ -251,6 +252,17 @@ namespace impl ); } + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that the parameters did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & probabilities; + ar & heights; + ar & actual_positions; + ar & desired_positions; + } + private: array_type probabilities; // the quantile probabilities array_type heights; // q_i diff --git a/include/boost/accumulators/statistics/weighted_mean.hpp b/include/boost/accumulators/statistics/weighted_mean.hpp index a80ef09..1ddce8d 100644 --- a/include/boost/accumulators/statistics/weighted_mean.hpp +++ b/include/boost/accumulators/statistics/weighted_mean.hpp @@ -97,6 +97,13 @@ namespace impl return this->mean; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & mean; + } + private: result_type mean; }; diff --git a/include/boost/accumulators/statistics/weighted_median.hpp b/include/boost/accumulators/statistics/weighted_median.hpp index ed7cadb..22224a8 100644 --- a/include/boost/accumulators/statistics/weighted_median.hpp +++ b/include/boost/accumulators/statistics/weighted_median.hpp @@ -106,6 +106,15 @@ namespace impl return this->median; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + ar & is_dirty; + ar & median; + } + private: mutable float_type sum; mutable bool is_dirty; @@ -162,6 +171,15 @@ namespace impl return this->median; } + + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & is_dirty; + ar & median; + } + private: mutable bool is_dirty; mutable float_type median; diff --git a/include/boost/accumulators/statistics/weighted_moment.hpp b/include/boost/accumulators/statistics/weighted_moment.hpp index 011701c..5b61290 100644 --- a/include/boost/accumulators/statistics/weighted_moment.hpp +++ b/include/boost/accumulators/statistics/weighted_moment.hpp @@ -60,6 +60,13 @@ namespace impl return numeric::fdiv(this->sum, sum_of_weights(args)); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sum; + } + private: weighted_sample sum; }; diff --git a/include/boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp b/include/boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp index ce750ed..f8a44d6 100644 --- a/include/boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp +++ b/include/boost/accumulators/statistics/weighted_p_square_cumul_dist.hpp @@ -221,6 +221,19 @@ namespace impl return make_iterator_range(this->histogram); } + // make this accumulator serializeable + // TODO split to save/load and check on parameters provided in ctor + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & num_cells; + ar & heights; + ar & actual_positions; + ar & desired_positions; + ar & histogram; + ar & is_dirty; + } + private: std::size_t num_cells; // number of cells b array_type heights; // q_i diff --git a/include/boost/accumulators/statistics/weighted_p_square_quantile.hpp b/include/boost/accumulators/statistics/weighted_p_square_quantile.hpp index 2ebc7b1..fecd993 100644 --- a/include/boost/accumulators/statistics/weighted_p_square_quantile.hpp +++ b/include/boost/accumulators/statistics/weighted_p_square_quantile.hpp @@ -208,6 +208,17 @@ namespace impl { return this->heights[2]; } + // make this accumulator serializeable + // TODO split to save/load and check on parameters provided in ctor + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & p; + ar & heights; + ar & actual_positions; + ar & desired_positions; + } + private: float_type p; // the quantile probability p array_type heights; // q_i diff --git a/include/boost/accumulators/statistics/weighted_peaks_over_threshold.hpp b/include/boost/accumulators/statistics/weighted_peaks_over_threshold.hpp index 418b38c..f3dc327 100644 --- a/include/boost/accumulators/statistics/weighted_peaks_over_threshold.hpp +++ b/include/boost/accumulators/statistics/weighted_peaks_over_threshold.hpp @@ -109,6 +109,19 @@ namespace impl return this->fit_parameters_; } + // make this accumulator serializeable + // TODO: do we need to split to load/save and verify that threshold did not change? + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & sign_; + ar & mu_; + ar & sigma2_; + ar & threshold_; + ar & fit_parameters_; + ar & is_dirty_; + } + private: short sign_; // for left tail fitting, mirror the extreme values mutable float_type mu_; // mean of samples above threshold diff --git a/include/boost/accumulators/statistics/weighted_sum.hpp b/include/boost/accumulators/statistics/weighted_sum.hpp index 2715390..41091f8 100644 --- a/include/boost/accumulators/statistics/weighted_sum.hpp +++ b/include/boost/accumulators/statistics/weighted_sum.hpp @@ -55,6 +55,13 @@ namespace impl return this->weighted_sum_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & weighted_sum_; + } + private: weighted_sample weighted_sum_; diff --git a/include/boost/accumulators/statistics/weighted_sum_kahan.hpp b/include/boost/accumulators/statistics/weighted_sum_kahan.hpp index fbb0303..5ee5250 100644 --- a/include/boost/accumulators/statistics/weighted_sum_kahan.hpp +++ b/include/boost/accumulators/statistics/weighted_sum_kahan.hpp @@ -68,6 +68,14 @@ namespace impl return this->weighted_sum_; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & weighted_sum_; + ar & compensation; + } + private: weighted_sample weighted_sum_; weighted_sample compensation; diff --git a/include/boost/accumulators/statistics/weighted_tail_variate_means.hpp b/include/boost/accumulators/statistics/weighted_tail_variate_means.hpp index b113310..f096a93 100644 --- a/include/boost/accumulators/statistics/weighted_tail_variate_means.hpp +++ b/include/boost/accumulators/statistics/weighted_tail_variate_means.hpp @@ -179,6 +179,13 @@ namespace impl return make_iterator_range(this->tail_means_); } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & tail_means_; + } + private: mutable array_type tail_means_; diff --git a/include/boost/accumulators/statistics/weighted_variance.hpp b/include/boost/accumulators/statistics/weighted_variance.hpp index bc199af..f7d2b6e 100644 --- a/include/boost/accumulators/statistics/weighted_variance.hpp +++ b/include/boost/accumulators/statistics/weighted_variance.hpp @@ -103,6 +103,13 @@ namespace impl return this->weighted_variance; } + // make this accumulator serializeable + template + void serialize(Archive & ar, const unsigned int file_version) + { + ar & weighted_variance; + } + private: result_type weighted_variance; }; diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 56cab5d..c6ecb32 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -24,6 +24,8 @@ project # MSVC's iterator debugging causes some tests to run forever. msvc:off intel-win:off + "-Wno-deprecated-declarations" + /boost/serialization ; test-suite "accumulators" @@ -48,6 +50,8 @@ test-suite "accumulators" [ 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 ] diff --git a/test/count.cpp b/test/count.cpp index 01d48a5..e2f4436 100644 --- a/test/count.cpp +++ b/test/count.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -35,6 +38,31 @@ void test_stat() BOOST_CHECK_EQUAL(5u, count(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + { + accumulator_set > 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 > 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 // @@ -43,6 +71,7 @@ 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/test/covariance.cpp b/test/covariance.cpp index 6a7fbec..abdbc9e 100644 --- a/test/covariance.cpp +++ b/test/covariance.cpp @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -88,6 +91,36 @@ void test_stat() 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 > > 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 > > 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 // @@ -96,6 +129,7 @@ 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/test/extended_p_square.cpp b/test/extended_p_square.cpp index f3a6320..09f50cf 100644 --- a/test/extended_p_square.cpp +++ b/test/extended_p_square.cpp @@ -15,17 +15,21 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; using namespace boost::accumulators; +typedef accumulator_set > accumulator_t; + /////////////////////////////////////////////////////////////////////////////// // test_stat // void test_stat() { - typedef accumulator_set > accumulator_t; // tolerance double epsilon = 3; @@ -61,6 +65,36 @@ void test_stat() } } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + // tolerance + double epsilon = 3.; + std::vector 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 // @@ -69,6 +103,7 @@ 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/test/extended_p_square_quantile.cpp b/test/extended_p_square_quantile.cpp index e56223b..7d60201 100644 --- a/test/extended_p_square_quantile.cpp +++ b/test/extended_p_square_quantile.cpp @@ -15,21 +15,24 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; using namespace boost::accumulators; +typedef accumulator_set > accumulator_t; +typedef accumulator_set, double > accumulator_t_weighted; +typedef accumulator_set > accumulator_t_quadratic; +typedef accumulator_set, double > accumulator_t_weighted_quadratic; + /////////////////////////////////////////////////////////////////////////////// // test_stat // void test_stat() { - typedef accumulator_set > accumulator_t; - typedef accumulator_set, double > accumulator_t_weighted; - typedef accumulator_set > accumulator_t_quadratic; - typedef accumulator_set, double > accumulator_t_weighted_quadratic; - // tolerance double epsilon = 1; @@ -88,6 +91,74 @@ void test_stat() } } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + // "persistent" storage + std::stringstream ss; + + // tolerance + double epsilon = 1.; + + // a random number generator + boost::lagged_fibonacci607 rng; + + std::vector 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 // @@ -96,6 +167,7 @@ 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/test/kurtosis.cpp b/test/kurtosis.cpp index 8283375..92c5cfe 100644 --- a/test/kurtosis.cpp +++ b/test/kurtosis.cpp @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -56,6 +59,34 @@ void test_stat() 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 > 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 > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE( kurtosis(acc), kurtosis_value, epsilon); + +} /////////////////////////////////////////////////////////////////////////////// // init_unit_test_suite // @@ -64,6 +95,7 @@ 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/test/max.cpp b/test/max.cpp index 6a6b96c..00a9793 100644 --- a/test/max.cpp +++ b/test/max.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -29,6 +32,27 @@ void test_stat() BOOST_CHECK_EQUAL(2, (max)(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set > 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 > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(2u, max(acc)); +} + /////////////////////////////////////////////////////////////////////////////// // init_unit_test_suite // @@ -37,6 +61,7 @@ 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/test/mean.cpp b/test/mean.cpp index b08a2d7..29d4877 100644 --- a/test/mean.cpp +++ b/test/mean.cpp @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -20,18 +23,18 @@ void assert_is_double(T const &) BOOST_MPL_ASSERT((is_same)); } +typedef accumulator_set > > mean_t; + +typedef accumulator_set(immediate) > > immediate_mean_t; + /////////////////////////////////////////////////////////////////////////////// // test_stat // void test_stat() { - accumulator_set< - int - , stats< - tag::mean - , tag::mean_of_variates - > - > acc, test_acc(sample = 0); + mean_t acc, test_acc(sample = 0); acc(1, covariate1 = 3); BOOST_CHECK_CLOSE(1., mean(acc), 1e-5); @@ -53,13 +56,7 @@ void test_stat() assert_is_double(mean(acc)); - accumulator_set< - int - , stats< - tag::mean(immediate) - , tag::mean_of_variates(immediate) - > - > acc2, test_acc2(sample = 0); + immediate_mean_t acc2, test_acc2(sample = 0); acc2(1, covariate1 = 3); BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); @@ -79,6 +76,39 @@ void test_stat() 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(acc1)), 1e-5); + BOOST_CHECK_CLOSE(1., mean(acc2), 1e-5); + BOOST_CHECK_CLOSE(5., (accumulators::mean_of_variates(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(acc2)), 1e-5); +} + /////////////////////////////////////////////////////////////////////////////// // init_unit_test_suite // @@ -87,6 +117,7 @@ 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/test/median.cpp b/test/median.cpp index 697c252..e348393 100644 --- a/test/median.cpp +++ b/test/median.cpp @@ -9,11 +9,19 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; using namespace accumulators; + +typedef accumulator_set > p_square_median_t; +typedef accumulator_set > dist_median_t; +typedef accumulator_set > p_square_dist_median_t; + /////////////////////////////////////////////////////////////////////////////// // test_stat // @@ -25,11 +33,9 @@ void test_stat() boost::normal_distribution<> mean_sigma(mu,1); boost::variate_generator > normal(rng, mean_sigma); - accumulator_set > acc; - accumulator_set > - acc_dens( density_cache_size = 10000, density_num_bins = 1000 ); - accumulator_set > - acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); + 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) { @@ -44,6 +50,52 @@ void test_stat() 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 > 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 // @@ -52,6 +104,7 @@ 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/test/min.cpp b/test/min.cpp index 52ca6ac..614fab4 100644 --- a/test/min.cpp +++ b/test/min.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -29,6 +32,27 @@ void test_stat() BOOST_CHECK_EQUAL(0, (min)(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set > 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 > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_EQUAL(0, (min)(acc)); +} + /////////////////////////////////////////////////////////////////////////////// // init_unit_test_suite // @@ -37,6 +61,7 @@ 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/test/moment.cpp b/test/moment.cpp index b91d0af..44fa2a5 100644 --- a/test/moment.cpp +++ b/test/moment.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -38,6 +41,29 @@ void test_stat() BOOST_CHECK_CLOSE(1106., accumulators::moment<5>(acc2), 1e-5); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-5; + std::stringstream ss; + { + accumulator_set > > 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 > > acc; + boost::archive::text_iarchive ia(ss); + acc.serialize(ia, 0); + BOOST_CHECK_CLOSE(15., accumulators::moment<2>(acc), epsilon); +} + /////////////////////////////////////////////////////////////////////////////// // init_unit_test_suite // @@ -46,6 +72,7 @@ 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/test/p_square_cumul_dist.cpp b/test/p_square_cumul_dist.cpp index 4bc014d..33e8109 100644 --- a/test/p_square_cumul_dist.cpp +++ b/test/p_square_cumul_dist.cpp @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -34,6 +37,9 @@ double my_erf(double const& x, int const& n = 1000) return sum * 2. / std::sqrt(3.141592653); } +typedef accumulator_set > accumulator_t; +typedef iterator_range >::iterator > histogram_type; + /////////////////////////////////////////////////////////////////////////////// // test_stat // @@ -42,8 +48,6 @@ void test_stat() // tolerance in % double epsilon = 3; - typedef accumulator_set > accumulator_t; - accumulator_t acc(p_square_cumulative_distribution_num_cells = 100); // two random number generators @@ -51,12 +55,11 @@ void test_stat() boost::normal_distribution<> mean_sigma(0,1); boost::variate_generator > normal(rng, mean_sigma); - for (std::size_t i=0; i<100000; ++i) + for (std::size_t i=0; i<1000000; ++i) { acc(normal()); } - typedef iterator_range >::iterator > histogram_type; histogram_type histogram = p_square_cumulative_distribution(acc); for (std::size_t i = 0; i < histogram.size(); ++i) @@ -67,6 +70,45 @@ void test_stat() } } +/////////////////////////////////////////////////////////////////////////////// +// 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 > 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 // @@ -75,6 +117,7 @@ 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/test/p_square_quantile.cpp b/test/p_square_quantile.cpp index 3f21281..ba67823 100644 --- a/test/p_square_quantile.cpp +++ b/test/p_square_quantile.cpp @@ -14,18 +14,21 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; using namespace boost::accumulators; +typedef accumulator_set > accumulator_t; + /////////////////////////////////////////////////////////////////////////////// // test_stat // void test_stat() { - typedef accumulator_set > accumulator_t; - // tolerance in % double epsilon = 1; @@ -67,6 +70,43 @@ void test_stat() 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 // @@ -75,6 +115,7 @@ 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/test/p_square_quantile_extended.cpp b/test/p_square_quantile_extended.cpp index 97d3480..debe33c 100644 --- a/test/p_square_quantile_extended.cpp +++ b/test/p_square_quantile_extended.cpp @@ -15,18 +15,21 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; using namespace boost::accumulators; +typedef accumulator_set > accumulator_t; + /////////////////////////////////////////////////////////////////////////////// // test_stat // void test_stat() { - typedef accumulator_set > accumulator_t; - // tolerance double epsilon = 1e-6; @@ -56,6 +59,49 @@ void test_stat() } } +/////////////////////////////////////////////////////////////////////////////// +// 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 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; iadd(BOOST_TEST_CASE(&test_stat)); + test->add(BOOST_TEST_CASE(&test_persistency)); return test; } diff --git a/test/pot_quantile.cpp b/test/pot_quantile.cpp index 4457071..4e12ad6 100644 --- a/test/pot_quantile.cpp +++ b/test/pot_quantile.cpp @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -83,6 +86,36 @@ void test_stat() 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 > normal(rng, mean_sigma); + + accumulator_set(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(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 // @@ -91,6 +124,7 @@ 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/test/rolling_count.cpp b/test/rolling_count.cpp index fef132c..a1b6b6f 100644 --- a/test/rolling_count.cpp +++ b/test/rolling_count.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -37,6 +40,30 @@ void test_stat() BOOST_CHECK_EQUAL(3u, rolling_count(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set > 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 > 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 // @@ -45,6 +72,7 @@ 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/test/rolling_mean.cpp b/test/rolling_mean.cpp index a375198..f8037b4 100644 --- a/test/rolling_mean.cpp +++ b/test/rolling_mean.cpp @@ -11,6 +11,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -82,6 +85,39 @@ test_rolling_mean_unsigned_test_impl(accumulator_set_type& acc) assert_is_double(rolling_mean(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency_impl +// +template +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() @@ -135,6 +171,40 @@ void test_rolling_mean() test_rolling_mean_unsigned_test_impl(acc_immediate_rolling_mean5); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +void test_persistency() +{ + accumulator_set > + 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 > + acc_immediate_rolling_mean3(tag::immediate_rolling_mean::window_size = window_size); + + accumulator_set > + 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 > + acc_lazy_rolling_mean3(tag::lazy_rolling_mean::window_size = window_size); + + accumulator_set > + 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 // @@ -143,6 +213,7 @@ 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/test/rolling_moment.cpp b/test/rolling_moment.cpp index 9a25920..30d0c88 100644 --- a/test/rolling_moment.cpp +++ b/test/rolling_moment.cpp @@ -10,6 +10,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -71,6 +74,36 @@ void test_rolling_fifth_moment() assert_is_double(rolling_moment<5>(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set > > acc2(tag::rolling_moment<2>::window_size = 3); + accumulator_set > > 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 > > acc2(tag::rolling_moment<2>::window_size = 3); + accumulator_set > > 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 // @@ -80,6 +113,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] ) 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; -} \ No newline at end of file +} diff --git a/test/rolling_sum.cpp b/test/rolling_sum.cpp index 6e20d47..374514c 100644 --- a/test/rolling_sum.cpp +++ b/test/rolling_sum.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -37,6 +40,29 @@ void test_stat() BOOST_CHECK_EQUAL(12, rolling_sum(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set > 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 > 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 // @@ -45,6 +71,7 @@ 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/test/rolling_variance.cpp b/test/rolling_variance.cpp index df41fef..d7f0075 100644 --- a/test/rolling_variance.cpp +++ b/test/rolling_variance.cpp @@ -9,7 +9,9 @@ #include #include #include - +#include +#include +#include #include using namespace boost; @@ -132,6 +134,66 @@ void test_rolling_variance() BOOST_CHECK (sizeof(acc_lazy_rolling_variance) == sizeof(acc_lazy_rolling_variance2)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency_impl +// +template +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 > + acc_immediate_rolling_variance(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set > + acc_immediate_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set > + acc_immediate_rolling_variance3(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set > + acc_lazy_rolling_variance(tag::lazy_rolling_variance::window_size = window_size); + + accumulator_set > + acc_lazy_rolling_variance2(tag::immediate_rolling_variance::window_size = window_size); + + accumulator_set > + 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 // @@ -140,6 +202,7 @@ 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; -} \ No newline at end of file +} diff --git a/test/skewness.cpp b/test/skewness.cpp index 7aaea90..8a82468 100644 --- a/test/skewness.cpp +++ b/test/skewness.cpp @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -55,6 +58,33 @@ void test_stat() BOOST_CHECK_CLOSE( skewness(acc2), 0.406040288214, 1e-6 ); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-6; + std::stringstream ss; + { + accumulator_set > 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 > 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 // @@ -63,6 +93,7 @@ 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/test/sum.cpp b/test/sum.cpp index 52eeae6..c0c8f8c 100644 --- a/test/sum.cpp +++ b/test/sum.cpp @@ -9,6 +9,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -37,6 +40,32 @@ void test_stat() BOOST_CHECK_EQUAL(18, sum_of_variates(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set >, 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> 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 // @@ -45,6 +74,7 @@ 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/test/sum_kahan.cpp b/test/sum_kahan.cpp index fcfd9fb..85edb11 100644 --- a/test/sum_kahan.cpp +++ b/test/sum_kahan.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -66,6 +69,41 @@ void test_sum_of_variates_kahan() BOOST_CHECK_EQUAL(1.0f, sum_of_variates_kahan(acc)); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + std::stringstream ss; + { + accumulator_set< + float, + stats >, + 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 >, + 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 // @@ -76,6 +114,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] ) 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/test/variance.cpp b/test/variance.cpp index 710fa14..8c48713 100644 --- a/test/variance.cpp +++ b/test/variance.cpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include using namespace boost; using namespace unit_test; @@ -55,6 +58,41 @@ void test_stat() BOOST_CHECK_CLOSE(2., variance(acc2), 1e-5); } +/////////////////////////////////////////////////////////////////////////////// +// test_persistency +// +void test_persistency() +{ + double epsilon = 1e-5; + std::stringstream ss; + { + accumulator_set > acc1; + accumulator_set > 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 > acc1; + accumulator_set > 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 // @@ -63,6 +101,7 @@ 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/test/weighted_median.cpp b/test/weighted_median.cpp index eed3ad9..d7d274e 100644 --- a/test/weighted_median.cpp +++ b/test/weighted_median.cpp @@ -38,7 +38,7 @@ void test_stat() acc_cdist( p_square_cumulative_distribution_num_cells = 100 ); - for (std::size_t i=0; i<100000; ++i) + for (std::size_t i=0; i<1000000; ++i) { double sample = normal_narrow(); acc(sample, weight = std::exp(0.5 * (sample - mu) * (sample - mu) * ( 1./sigma_narrow/sigma_narrow - 1./sigma/sigma )));