1 ///////////////////////////////////////////////////////////////////////////////
2 // weighted_kurtosis.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_KURTOSIS_HPP_EAN_28_10_2005
9 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_KURTOSIS_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_kurtosis_impl
29     /**
30         @brief Kurtosis estimation for weighted samples
31 
32         The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central
33         moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution
34         has zero kurtosis. The kurtosis can also be expressed by the simple moments:
35 
36         \f[
37             \hat{g}_2 =
38                 \frac
39                 {\widehat{m}_n^{(4)}-4\widehat{m}_n^{(3)}\hat{\mu}_n+6\widehat{m}_n^{(2)}\hat{\mu}_n^2-3\hat{\mu}_n^4}
40                 {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3,
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         The kurtosis estimator for weighted samples is formally identical to the estimator for unweighted samples, except that
47         the weighted counterparts of all measures it depends on are to be taken.
48     */
49     template<typename Sample, typename Weight>
50     struct weighted_kurtosis_impl
51       : accumulator_base
52     {
53         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
54         // for boost::result_of
55         typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type;
56 
weighted_kurtosis_implboost::accumulators::impl::weighted_kurtosis_impl57         weighted_kurtosis_impl(dont_care)
58         {
59         }
60 
61         template<typename Args>
resultboost::accumulators::impl::weighted_kurtosis_impl62         result_type result(Args const &args) const
63         {
64             return numeric::fdiv(
65                         accumulators::weighted_moment<4>(args)
66                         - 4. * accumulators::weighted_moment<3>(args) * weighted_mean(args)
67                         + 6. * accumulators::weighted_moment<2>(args) * weighted_mean(args) * weighted_mean(args)
68                         - 3. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args) * weighted_mean(args)
69                       , ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
70                         * ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
71                    ) - 3.;
72         }
73     };
74 
75 } // namespace impl
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 // tag::weighted_kurtosis
79 //
80 namespace tag
81 {
82     struct weighted_kurtosis
83       : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3>, weighted_moment<4> >
84     {
85         /// INTERNAL ONLY
86         ///
87         typedef accumulators::impl::weighted_kurtosis_impl<mpl::_1, mpl::_2> impl;
88     };
89 }
90 
91 ///////////////////////////////////////////////////////////////////////////////
92 // extract::weighted_kurtosis
93 //
94 namespace extract
95 {
96     extractor<tag::weighted_kurtosis> const weighted_kurtosis = {};
97 
98     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_kurtosis)
99 }
100 
101 using extract::weighted_kurtosis;
102 
103 }} // namespace boost::accumulators
104 
105 #endif
106