kurtosis.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // serialization is done by accumulators it depends on
  58. template<class Archive>
  59. void serialize(Archive & ar, const unsigned int file_version) {}
  60. };
  61. } // namespace impl
  62. ///////////////////////////////////////////////////////////////////////////////
  63. // tag::kurtosis
  64. //
  65. namespace tag
  66. {
  67. struct kurtosis
  68. : depends_on<mean, moment<2>, moment<3>, moment<4> >
  69. {
  70. /// INTERNAL ONLY
  71. ///
  72. typedef accumulators::impl::kurtosis_impl<mpl::_1> impl;
  73. };
  74. }
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // extract::kurtosis
  77. //
  78. namespace extract
  79. {
  80. extractor<tag::kurtosis> const kurtosis = {};
  81. BOOST_ACCUMULATORS_IGNORE_GLOBAL(kurtosis)
  82. }
  83. using extract::kurtosis;
  84. // So that kurtosis can be automatically substituted with
  85. // weighted_kurtosis when the weight parameter is non-void
  86. template<>
  87. struct as_weighted_feature<tag::kurtosis>
  88. {
  89. typedef tag::weighted_kurtosis type;
  90. };
  91. template<>
  92. struct feature_of<tag::weighted_kurtosis>
  93. : feature_of<tag::kurtosis>
  94. {
  95. };
  96. }} // namespace boost::accumulators
  97. #endif