weighted_moment.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_moment.hpp
  3. //
  4. // Copyright 2006, Eric Niebler, Olivier Gygi. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_MOMENT_HPP_EAN_15_11_2005
  9. #include <boost/config/no_tr1/cmath.hpp>
  10. #include <boost/mpl/int.hpp>
  11. #include <boost/mpl/assert.hpp>
  12. #include <boost/mpl/placeholders.hpp>
  13. #include <boost/preprocessor/arithmetic/inc.hpp>
  14. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  15. #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
  16. #include <boost/preprocessor/repetition/enum_trailing_binary_params.hpp>
  17. #include <boost/accumulators/framework/accumulator_base.hpp>
  18. #include <boost/accumulators/framework/extractor.hpp>
  19. #include <boost/accumulators/numeric/functional.hpp>
  20. #include <boost/accumulators/framework/parameters/sample.hpp>
  21. #include <boost/accumulators/framework/depends_on.hpp>
  22. #include <boost/accumulators/statistics_fwd.hpp>
  23. #include <boost/accumulators/statistics/count.hpp>
  24. #include <boost/accumulators/statistics/moment.hpp> // for pow()
  25. #include <boost/accumulators/statistics/sum.hpp>
  26. namespace boost { namespace accumulators
  27. {
  28. namespace impl
  29. {
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // weighted_moment_impl
  32. template<typename N, typename Sample, typename Weight>
  33. struct weighted_moment_impl
  34. : accumulator_base // TODO: also depends_on sum of powers
  35. {
  36. BOOST_MPL_ASSERT_RELATION(N::value, >, 0);
  37. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  38. // for boost::result_of
  39. typedef typename numeric::functional::fdiv<weighted_sample, Weight>::result_type result_type;
  40. template<typename Args>
  41. weighted_moment_impl(Args const &args)
  42. : sum(args[sample | Sample()] * numeric::one<Weight>::value)
  43. {
  44. }
  45. template<typename Args>
  46. void operator ()(Args const &args)
  47. {
  48. this->sum += args[weight] * numeric::pow(args[sample], N());
  49. }
  50. template<typename Args>
  51. result_type result(Args const &args) const
  52. {
  53. return numeric::fdiv(this->sum, sum_of_weights(args));
  54. }
  55. // make this accumulator serializeable
  56. template<class Archive>
  57. void serialize(Archive & ar, const unsigned int file_version)
  58. {
  59. ar & sum;
  60. }
  61. private:
  62. weighted_sample sum;
  63. };
  64. } // namespace impl
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // tag::weighted_moment
  67. //
  68. namespace tag
  69. {
  70. template<int N>
  71. struct weighted_moment
  72. : depends_on<count, sum_of_weights>
  73. {
  74. /// INTERNAL ONLY
  75. ///
  76. typedef accumulators::impl::weighted_moment_impl<mpl::int_<N>, mpl::_1, mpl::_2> impl;
  77. };
  78. }
  79. ///////////////////////////////////////////////////////////////////////////////
  80. // extract::weighted_moment
  81. //
  82. namespace extract
  83. {
  84. BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, weighted_moment, (int))
  85. }
  86. using extract::weighted_moment;
  87. }} // namespace boost::accumulators
  88. #endif