kurtosis.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // 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_KURTOSIS_HPP_EAN_28_10_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_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/mean.hpp>
  17. #include <boost/accumulators/statistics/moment.hpp>
  18. namespace boost { namespace accumulators
  19. {
  20. namespace impl
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // kurtosis_impl
  24. /**
  25. @brief Kurtosis estimation
  26. The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central
  27. moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution
  28. has zero kurtosis. The kurtosis can also be expressed by the simple moments:
  29. \f[
  30. \hat{g}_2 =
  31. \frac
  32. {\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}
  33. {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3,
  34. \f]
  35. 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
  36. \f$ n \f$ samples.
  37. */
  38. template<typename Sample>
  39. struct kurtosis_impl
  40. : accumulator_base
  41. {
  42. // for boost::result_of
  43. typedef typename numeric::functional::fdiv<Sample, Sample>::result_type result_type;
  44. kurtosis_impl(dont_care) {}
  45. template<typename Args>
  46. result_type result(Args const &args) const
  47. {
  48. return numeric::fdiv(
  49. accumulators::moment<4>(args)
  50. - 4. * accumulators::moment<3>(args) * mean(args)
  51. + 6. * accumulators::moment<2>(args) * mean(args) * mean(args)
  52. - 3. * mean(args) * mean(args) * mean(args) * mean(args)
  53. , ( accumulators::moment<2>(args) - mean(args) * mean(args) )
  54. * ( accumulators::moment<2>(args) - mean(args) * mean(args) )
  55. ) - 3.;
  56. }
  57. };
  58. } // namespace impl
  59. ///////////////////////////////////////////////////////////////////////////////
  60. // tag::kurtosis
  61. //
  62. namespace tag
  63. {
  64. struct kurtosis
  65. : depends_on<mean, moment<2>, moment<3>, moment<4> >
  66. {
  67. /// INTERNAL ONLY
  68. ///
  69. typedef accumulators::impl::kurtosis_impl<mpl::_1> impl;
  70. };
  71. }
  72. ///////////////////////////////////////////////////////////////////////////////
  73. // extract::kurtosis
  74. //
  75. namespace extract
  76. {
  77. extractor<tag::kurtosis> const kurtosis = {};
  78. BOOST_ACCUMULATORS_IGNORE_GLOBAL(kurtosis)
  79. }
  80. using extract::kurtosis;
  81. // So that kurtosis can be automatically substituted with
  82. // weighted_kurtosis when the weight parameter is non-void
  83. template<>
  84. struct as_weighted_feature<tag::kurtosis>
  85. {
  86. typedef tag::weighted_kurtosis type;
  87. };
  88. template<>
  89. struct feature_of<tag::weighted_kurtosis>
  90. : feature_of<tag::kurtosis>
  91. {
  92. };
  93. }} // namespace boost::accumulators
  94. #endif