123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
- #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_DENSITY_HPP_DE_01_01_2006
- #include <vector>
- #include <limits>
- #include <functional>
- #include <boost/range.hpp>
- #include <boost/parameter/keyword.hpp>
- #include <boost/mpl/placeholders.hpp>
- #include <boost/accumulators/framework/accumulator_base.hpp>
- #include <boost/accumulators/framework/extractor.hpp>
- #include <boost/accumulators/numeric/functional.hpp>
- #include <boost/accumulators/framework/parameters/sample.hpp>
- #include <boost/accumulators/statistics_fwd.hpp>
- #include <boost/accumulators/statistics/sum.hpp>
- #include <boost/accumulators/statistics/max.hpp>
- #include <boost/accumulators/statistics/min.hpp>
- #include <boost/accumulators/statistics/density.hpp> // for named parameters density_cache_size and density_num_bins
- #include <boost/serialization/vector.hpp>
- #include <boost/serialization/utility.hpp>
- namespace boost { namespace accumulators
- {
- namespace impl
- {
-
-
-
-
- template<typename Sample, typename Weight>
- struct weighted_density_impl
- : accumulator_base
- {
- typedef typename numeric::functional::fdiv<Weight, std::size_t>::result_type float_type;
- typedef std::vector<std::pair<float_type, float_type> > histogram_type;
- typedef std::vector<float_type> array_type;
-
- typedef iterator_range<typename histogram_type::iterator> result_type;
- template<typename Args>
- weighted_density_impl(Args const &args)
- : cache_size(args[density_cache_size])
- , cache(cache_size)
- , num_bins(args[density_num_bins])
- , samples_in_bin(num_bins + 2, 0.)
- , bin_positions(num_bins + 2)
- , histogram(
- num_bins + 2
- , std::make_pair(
- numeric::fdiv(args[sample | Sample()],(std::size_t)1)
- , numeric::fdiv(args[sample | Sample()],(std::size_t)1)
- )
- )
- , is_dirty(true)
- {
- }
- template<typename Args>
- void operator ()(Args const &args)
- {
- this->is_dirty = true;
- std::size_t cnt = count(args);
-
- if (cnt <= this->cache_size)
- {
- this->cache[cnt - 1] = std::make_pair(args[sample], args[weight]);
- }
-
-
-
- if (cnt == this->cache_size)
- {
- float_type minimum = numeric::fdiv((min)(args),(std::size_t)1);
- float_type maximum = numeric::fdiv((max)(args),(std::size_t)1);
- float_type bin_size = numeric::fdiv(maximum - minimum, this->num_bins);
-
- for (std::size_t i = 0; i < this->num_bins + 2; ++i)
- {
- this->bin_positions[i] = minimum + (i - 1.) * bin_size;
- }
- for (typename histogram_type::const_iterator iter = this->cache.begin(); iter != this->cache.end(); ++iter)
- {
- if (iter->first < this->bin_positions[1])
- {
- this->samples_in_bin[0] += iter->second;
- }
- else if (iter->first >= this->bin_positions[this->num_bins + 1])
- {
- this->samples_in_bin[this->num_bins + 1] += iter->second;
- }
- else
- {
- typename array_type::iterator it = std::upper_bound(
- this->bin_positions.begin()
- , this->bin_positions.end()
- , iter->first
- );
- std::size_t d = std::distance(this->bin_positions.begin(), it);
- this->samples_in_bin[d - 1] += iter->second;
- }
- }
- }
-
- else if (cnt > this->cache_size)
- {
- if (args[sample] < this->bin_positions[1])
- {
- this->samples_in_bin[0] += args[weight];
- }
- else if (args[sample] >= this->bin_positions[this->num_bins + 1])
- {
- this->samples_in_bin[this->num_bins + 1] += args[weight];
- }
- else
- {
- typename array_type::iterator it = std::upper_bound(
- this->bin_positions.begin()
- , this->bin_positions.end()
- , args[sample]
- );
- std::size_t d = std::distance(this->bin_positions.begin(), it);
- this->samples_in_bin[d - 1] += args[weight];
- }
- }
- }
- template<typename Args>
- result_type result(Args const &args) const
- {
- if (this->is_dirty)
- {
- this->is_dirty = false;
-
-
-
- for (std::size_t i = 0; i < this->num_bins + 2; ++i)
- {
- this->histogram[i] = std::make_pair(this->bin_positions[i], numeric::fdiv(this->samples_in_bin[i], sum_of_weights(args)));
- }
- }
-
- return make_iterator_range(this->histogram);
- }
-
-
- template<class Archive>
- 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;
- histogram_type cache;
- std::size_t num_bins;
- array_type samples_in_bin;
- array_type bin_positions;
- mutable histogram_type histogram;
- mutable bool is_dirty;
- };
- }
- namespace tag
- {
- struct weighted_density
- : depends_on<count, sum_of_weights, min, max>
- , density_cache_size
- , density_num_bins
- {
-
-
- typedef accumulators::impl::weighted_density_impl<mpl::_1, mpl::_2> impl;
- #ifdef BOOST_ACCUMULATORS_DOXYGEN_INVOKED
- static boost::parameter::keyword<density_cache_size> const cache_size;
- static boost::parameter::keyword<density_num_bins> const num_bins;
- #endif
- };
- }
- namespace extract
- {
- extractor<tag::density> const weighted_density = {};
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_density)
- }
- using extract::weighted_density;
- }}
- #endif
|