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 
8 #ifndef BOOST_ACCUMULATORS_STATISTICS_SKEWNESS_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_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/moment.hpp>
20 #include <boost/accumulators/statistics/mean.hpp>
21 
22 
23 namespace boost { namespace accumulators
24 {
25 
26 namespace impl
27 {
28     ///////////////////////////////////////////////////////////////////////////////
29     // skewness_impl
30     /**
31         @brief Skewness estimation
32 
33         The skewness of a sample distribution is defined as the ratio of the 3rd central moment and the \f$ 3/2 \f$-th power
34         of the 2nd central moment (the variance) of the samples 3. The skewness can also be expressed by the simple moments:
35 
36         \f[
37             \hat{g}_1 =
38                 \frac
39                 {\widehat{m}_n^{(3)}-3\widehat{m}_n^{(2)}\hat{\mu}_n+2\hat{\mu}_n^3}
40                 {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^{3/2}}
41         \f]
42 
43         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
44         \f$ n \f$ samples.
45     */
46     template<typename Sample>
47     struct skewness_impl
48       : accumulator_base
49     {
50         // for boost::result_of
51         typedef typename numeric::functional::fdiv<Sample, Sample>::result_type result_type;
52 
skewness_implboost::accumulators::impl::skewness_impl53         skewness_impl(dont_care)
54         {
55         }
56 
57         template<typename Args>
resultboost::accumulators::impl::skewness_impl58         result_type result(Args const &args) const
59         {
60             return numeric::fdiv(
61                         accumulators::moment<3>(args)
62                         - 3. * accumulators::moment<2>(args) * mean(args)
63                         + 2. * mean(args) * mean(args) * mean(args)
64                       , ( accumulators::moment<2>(args) - mean(args) * mean(args) )
65                         * std::sqrt( accumulators::moment<2>(args) - mean(args) * mean(args) )
66                    );
67         }
68 
69         // serialization is done by accumulators it depends on
70         template<class Archive>
serializeboost::accumulators::impl::skewness_impl71         void serialize(Archive & ar, const unsigned int file_version) {}
72     };
73 
74 } // namespace impl
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 // tag::skewness
78 //
79 namespace tag
80 {
81     struct skewness
82       : depends_on<mean, moment<2>, moment<3> >
83     {
84         /// INTERNAL ONLY
85         ///
86         typedef accumulators::impl::skewness_impl<mpl::_1> impl;
87     };
88 }
89 
90 ///////////////////////////////////////////////////////////////////////////////
91 // extract::skewness
92 //
93 namespace extract
94 {
95     extractor<tag::skewness> const skewness = {};
96 
97     BOOST_ACCUMULATORS_IGNORE_GLOBAL(skewness)
98 }
99 
100 using extract::skewness;
101 
102 // So that skewness can be automatically substituted with
103 // weighted_skewness when the weight parameter is non-void
104 template<>
105 struct as_weighted_feature<tag::skewness>
106 {
107     typedef tag::weighted_skewness type;
108 };
109 
110 template<>
111 struct feature_of<tag::weighted_skewness>
112   : feature_of<tag::skewness>
113 {
114 };
115 
116 }} // namespace boost::accumulators
117 
118 #endif
119