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 8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005 9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005 10 11 #include <limits> 12 #include <boost/mpl/placeholders.hpp> 13 #include <boost/accumulators/framework/accumulator_base.hpp> 14 #include <boost/accumulators/framework/extractor.hpp> 15 #include <boost/accumulators/framework/parameters/sample.hpp> 16 #include <boost/accumulators/numeric/functional.hpp> 17 #include <boost/accumulators/framework/depends_on.hpp> 18 #include <boost/accumulators/statistics_fwd.hpp> 19 #include <boost/accumulators/statistics/weighted_moment.hpp> 20 #include <boost/accumulators/statistics/weighted_mean.hpp> 21 22 namespace boost { namespace accumulators 23 { 24 25 namespace impl 26 { 27 /////////////////////////////////////////////////////////////////////////////// 28 // weighted_skewness_impl 29 /** 30 @brief Skewness estimation for weighted samples 31 32 The skewness of a sample distribution is defined as the ratio of the 3rd central moment and the \f$ 3/2 \f$-th power $ 33 of the 2nd central moment (the variance) of the samples. The skewness can also be expressed by the simple moments: 34 35 \f[ 36 \hat{g}_1 = 37 \frac 38 {\widehat{m}_n^{(3)}-3\widehat{m}_n^{(2)}\hat{\mu}_n+2\hat{\mu}_n^3} 39 {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^{3/2}} 40 \f] 41 42 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 43 \f$ n \f$ samples. 44 45 The skewness estimator for weighted samples is formally identical to the estimator for unweighted samples, except that 46 the weighted counterparts of all measures it depends on are to be taken. 47 */ 48 template<typename Sample, typename Weight> 49 struct weighted_skewness_impl 50 : accumulator_base 51 { 52 typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample; 53 // for boost::result_of 54 typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type; 55 weighted_skewness_implboost::accumulators::impl::weighted_skewness_impl56 weighted_skewness_impl(dont_care) {} 57 58 template<typename Args> resultboost::accumulators::impl::weighted_skewness_impl59 result_type result(Args const &args) const 60 { 61 return numeric::fdiv( 62 accumulators::weighted_moment<3>(args) 63 - 3. * accumulators::weighted_moment<2>(args) * weighted_mean(args) 64 + 2. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args) 65 , ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) ) 66 * std::sqrt( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) ) 67 ); 68 } 69 }; 70 71 } // namespace impl 72 73 /////////////////////////////////////////////////////////////////////////////// 74 // tag::weighted_skewness 75 // 76 namespace tag 77 { 78 struct weighted_skewness 79 : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3> > 80 { 81 /// INTERNAL ONLY 82 /// 83 typedef accumulators::impl::weighted_skewness_impl<mpl::_1, mpl::_2> impl; 84 }; 85 } 86 87 /////////////////////////////////////////////////////////////////////////////// 88 // extract::weighted_skewness 89 // 90 namespace extract 91 { 92 extractor<tag::weighted_skewness> const weighted_skewness = {}; 93 94 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_skewness) 95 } 96 97 using extract::weighted_skewness; 98 99 }} // namespace boost::accumulators 100 101 #endif 102