123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- #ifndef BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
- #define BOOST_ACCUMULATORS_STATISTICS_P_SQUARE_QUANTILE_HPP_DE_01_01_2006
- #include <cmath>
- #include <functional>
- #include <boost/array.hpp>
- #include <boost/mpl/placeholders.hpp>
- #include <boost/type_traits/is_same.hpp>
- #include <boost/parameter/keyword.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/framework/depends_on.hpp>
- #include <boost/accumulators/statistics_fwd.hpp>
- #include <boost/accumulators/statistics/count.hpp>
- #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
- #include <boost/serialization/boost_array.hpp>
- namespace boost { namespace accumulators
- {
- namespace impl
- {
-
-
-
-
- template<typename Sample, typename Impl>
- struct p_square_quantile_impl
- : accumulator_base
- {
- typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type float_type;
- typedef array<float_type, 5> array_type;
-
- typedef float_type result_type;
- template<typename Args>
- p_square_quantile_impl(Args const &args)
- : p(is_same<Impl, for_median>::value ? float_type(0.5) : args[quantile_probability | float_type(0.5)])
- , heights()
- , actual_positions()
- , desired_positions()
- , positions_increments()
- {
- for(std::size_t i = 0; i < 5; ++i)
- {
- this->actual_positions[i] = i + float_type(1.);
- }
- this->desired_positions[0] = float_type(1.);
- this->desired_positions[1] = float_type(1.) + float_type(2.) * this->p;
- this->desired_positions[2] = float_type(1.) + float_type(4.) * this->p;
- this->desired_positions[3] = float_type(3.) + float_type(2.) * this->p;
- this->desired_positions[4] = float_type(5.);
- this->positions_increments[0] = float_type(0.);
- this->positions_increments[1] = this->p / float_type(2.);
- this->positions_increments[2] = this->p;
- this->positions_increments[3] = (float_type(1.) + this->p) / float_type(2.);
- this->positions_increments[4] = float_type(1.);
- }
- template<typename Args>
- void operator ()(Args const &args)
- {
- std::size_t cnt = count(args);
-
- if(cnt <= 5)
- {
- this->heights[cnt - 1] = args[sample];
-
- if(cnt == 5)
- {
- std::sort(this->heights.begin(), this->heights.end());
- }
- }
- else
- {
- std::size_t sample_cell = 1;
-
- if (args[sample] < this->heights[0])
- {
- this->heights[0] = args[sample];
- sample_cell = 1;
- }
- else if (this->heights[4] <= args[sample])
- {
- this->heights[4] = args[sample];
- sample_cell = 4;
- }
- else
- {
- typedef typename array_type::iterator iterator;
- iterator it = std::upper_bound(
- this->heights.begin()
- , this->heights.end()
- , args[sample]
- );
- sample_cell = std::distance(this->heights.begin(), it);
- }
-
- for(std::size_t i = sample_cell; i < 5; ++i)
- {
- ++this->actual_positions[i];
- }
-
- for(std::size_t i = 0; i < 5; ++i)
- {
- this->desired_positions[i] += this->positions_increments[i];
- }
-
- for(std::size_t i = 1; i <= 3; ++i)
- {
-
- float_type d = this->desired_positions[i] - this->actual_positions[i];
-
- float_type dp = this->actual_positions[i + 1] - this->actual_positions[i];
-
- float_type dm = this->actual_positions[i - 1] - this->actual_positions[i];
-
- float_type hp = (this->heights[i + 1] - this->heights[i]) / dp;
- float_type hm = (this->heights[i - 1] - this->heights[i]) / dm;
- if((d >= float_type(1.) && dp > float_type(1.)) || (d <= float_type(-1.) && dm < float_type(-1.)))
- {
- short sign_d = static_cast<short>(d / std::abs(d));
-
- float_type h = this->heights[i] + sign_d / (dp - dm) * ((sign_d - dm) * hp
- + (dp - sign_d) * hm);
- if(this->heights[i - 1] < h && h < this->heights[i + 1])
- {
- this->heights[i] = h;
- }
- else
- {
-
- if(d > float_type(0))
- {
- this->heights[i] += hp;
- }
- if(d < float_type(0))
- {
- this->heights[i] -= hm;
- }
- }
- this->actual_positions[i] += sign_d;
- }
- }
- }
- }
- result_type result(dont_care) const
- {
- return this->heights[2];
- }
-
-
- template<class Archive>
- 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;
- array_type heights;
- array_type actual_positions;
- array_type desired_positions;
- array_type positions_increments;
- };
- }
- namespace tag
- {
- struct p_square_quantile
- : depends_on<count>
- {
-
-
- typedef accumulators::impl::p_square_quantile_impl<mpl::_1, regular> impl;
- };
- struct p_square_quantile_for_median
- : depends_on<count>
- {
-
-
- typedef accumulators::impl::p_square_quantile_impl<mpl::_1, for_median> impl;
- };
- }
- namespace extract
- {
- extractor<tag::p_square_quantile> const p_square_quantile = {};
- extractor<tag::p_square_quantile_for_median> const p_square_quantile_for_median = {};
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile)
- BOOST_ACCUMULATORS_IGNORE_GLOBAL(p_square_quantile_for_median)
- }
- using extract::p_square_quantile;
- using extract::p_square_quantile_for_median;
- template<>
- struct as_weighted_feature<tag::p_square_quantile>
- {
- typedef tag::weighted_p_square_quantile type;
- };
- template<>
- struct feature_of<tag::weighted_p_square_quantile>
- : feature_of<tag::p_square_quantile>
- {
- };
- }}
- #endif
|