skewness.hpp 3.6 KB

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