weighted_kurtosis.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_kurtosis.hpp
  3. //
  4. // Copyright 2006 Olivier Gygi, Daniel Egloff. 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_KURTOSIS_HPP_EAN_28_10_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_KURTOSIS_HPP_EAN_28_10_2005
  9. #include <limits>
  10. #include <boost/mpl/placeholders.hpp>
  11. #include <boost/accumulators/framework/accumulator_base.hpp>
  12. #include <boost/accumulators/framework/extractor.hpp>
  13. #include <boost/accumulators/framework/parameters/sample.hpp>
  14. #include <boost/accumulators/numeric/functional.hpp>
  15. #include <boost/accumulators/framework/depends_on.hpp>
  16. #include <boost/accumulators/statistics_fwd.hpp>
  17. #include <boost/accumulators/statistics/weighted_moment.hpp>
  18. #include <boost/accumulators/statistics/weighted_mean.hpp>
  19. namespace boost { namespace accumulators
  20. {
  21. namespace impl
  22. {
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // weighted_kurtosis_impl
  25. /**
  26. @brief Kurtosis estimation for weighted samples
  27. The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central
  28. moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution
  29. has zero kurtosis. The kurtosis can also be expressed by the simple moments:
  30. \f[
  31. \hat{g}_2 =
  32. \frac
  33. {\widehat{m}_n^{(4)}-4\widehat{m}_n^{(3)}\hat{\mu}_n+6\widehat{m}_n^{(2)}\hat{\mu}_n^2-3\hat{\mu}_n^4}
  34. {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3,
  35. \f]
  36. where \f$ \widehat{m}_n^{(i)} \f$ are the \f$ i \f$-th moment and \f$ \hat{\mu}_n \f$ the mean (first moment) of the
  37. \f$ n \f$ samples.
  38. The kurtosis estimator for weighted samples is formally identical to the estimator for unweighted samples, except that
  39. the weighted counterparts of all measures it depends on are to be taken.
  40. */
  41. template<typename Sample, typename Weight>
  42. struct weighted_kurtosis_impl
  43. : accumulator_base
  44. {
  45. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  46. // for boost::result_of
  47. typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type;
  48. weighted_kurtosis_impl(dont_care)
  49. {
  50. }
  51. template<typename Args>
  52. result_type result(Args const &args) const
  53. {
  54. return numeric::fdiv(
  55. accumulators::weighted_moment<4>(args)
  56. - 4. * accumulators::weighted_moment<3>(args) * weighted_mean(args)
  57. + 6. * accumulators::weighted_moment<2>(args) * weighted_mean(args) * weighted_mean(args)
  58. - 3. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args) * weighted_mean(args)
  59. , ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
  60. * ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
  61. ) - 3.;
  62. }
  63. };
  64. } // namespace impl
  65. ///////////////////////////////////////////////////////////////////////////////
  66. // tag::weighted_kurtosis
  67. //
  68. namespace tag
  69. {
  70. struct weighted_kurtosis
  71. : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3>, weighted_moment<4> >
  72. {
  73. /// INTERNAL ONLY
  74. ///
  75. typedef accumulators::impl::weighted_kurtosis_impl<mpl::_1, mpl::_2> impl;
  76. };
  77. }
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // extract::weighted_kurtosis
  80. //
  81. namespace extract
  82. {
  83. extractor<tag::weighted_kurtosis> const weighted_kurtosis = {};
  84. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_kurtosis)
  85. }
  86. using extract::weighted_kurtosis;
  87. }} // namespace boost::accumulators
  88. #endif