1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_skewness.hpp
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)
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005
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>
22 namespace boost { namespace accumulators
27 ///////////////////////////////////////////////////////////////////////////////
28 // weighted_skewness_impl
30 @brief Skewness estimation for weighted samples
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:
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}}
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
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.
48 template<typename Sample, typename Weight>
49 struct weighted_skewness_impl
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;
56 weighted_skewness_impl(dont_care) {}
58 template<typename Args>
59 result_type result(Args const &args) const
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) )
73 ///////////////////////////////////////////////////////////////////////////////
74 // tag::weighted_skewness
78 struct weighted_skewness
79 : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3> >
83 typedef accumulators::impl::weighted_skewness_impl<mpl::_1, mpl::_2> impl;
87 ///////////////////////////////////////////////////////////////////////////////
88 // extract::weighted_skewness
92 extractor<tag::weighted_skewness> const weighted_skewness = {};
94 BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_skewness)
97 using extract::weighted_skewness;
99 }} // namespace boost::accumulators