weighted_skewness.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // weighted_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_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_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/weighted_moment.hpp>
  18. #include <boost/accumulators/statistics/weighted_mean.hpp>
  19. namespace boost { namespace accumulators
  20. {
  21. namespace impl
  22. {
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // weighted_skewness_impl
  25. /**
  26. @brief Skewness estimation for weighted samples
  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. 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. The skewness estimator for weighted samples is formally identical to the estimator for unweighted samples, except that
  38. the weighted counterparts of all measures it depends on are to be taken.
  39. */
  40. template<typename Sample, typename Weight>
  41. struct weighted_skewness_impl
  42. : accumulator_base
  43. {
  44. typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
  45. // for boost::result_of
  46. typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type;
  47. weighted_skewness_impl(dont_care) {}
  48. template<typename Args>
  49. result_type result(Args const &args) const
  50. {
  51. return numeric::fdiv(
  52. accumulators::weighted_moment<3>(args)
  53. - 3. * accumulators::weighted_moment<2>(args) * weighted_mean(args)
  54. + 2. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args)
  55. , ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
  56. * std::sqrt( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
  57. );
  58. }
  59. };
  60. } // namespace impl
  61. ///////////////////////////////////////////////////////////////////////////////
  62. // tag::weighted_skewness
  63. //
  64. namespace tag
  65. {
  66. struct weighted_skewness
  67. : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3> >
  68. {
  69. /// INTERNAL ONLY
  70. ///
  71. typedef accumulators::impl::weighted_skewness_impl<mpl::_1, mpl::_2> impl;
  72. };
  73. }
  74. ///////////////////////////////////////////////////////////////////////////////
  75. // extract::weighted_skewness
  76. //
  77. namespace extract
  78. {
  79. extractor<tag::weighted_skewness> const weighted_skewness = {};
  80. BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_skewness)
  81. }
  82. using extract::weighted_skewness;
  83. }} // namespace boost::accumulators
  84. #endif