skewness.hpp 3.4 KB

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