1 /////////////////////////////////////////////////////////////////////////////// 2 // 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_KURTOSIS_HPP_EAN_28_10_2005 9 #define BOOST_ACCUMULATORS_STATISTICS_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/mean.hpp> 19 #include <boost/accumulators/statistics/moment.hpp> 20 21 namespace boost { namespace accumulators 22 { 23 24 namespace impl 25 { 26 /////////////////////////////////////////////////////////////////////////////// 27 // kurtosis_impl 28 /** 29 @brief Kurtosis estimation 30 31 The kurtosis of a sample distribution is defined as the ratio of the 4th central moment and the square of the 2nd central 32 moment (the variance) of the samples, minus 3. The term \f$ -3 \f$ is added in order to ensure that the normal distribution 33 has zero kurtosis. The kurtosis can also be expressed by the simple moments: 34 35 \f[ 36 \hat{g}_2 = 37 \frac 38 {\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} 39 {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^2} - 3, 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 template<typename Sample> 46 struct kurtosis_impl 47 : accumulator_base 48 { 49 // for boost::result_of 50 typedef typename numeric::functional::fdiv<Sample, Sample>::result_type result_type; 51 kurtosis_implboost::accumulators::impl::kurtosis_impl52 kurtosis_impl(dont_care) {} 53 54 template<typename Args> resultboost::accumulators::impl::kurtosis_impl55 result_type result(Args const &args) const 56 { 57 return numeric::fdiv( 58 accumulators::moment<4>(args) 59 - 4. * accumulators::moment<3>(args) * mean(args) 60 + 6. * accumulators::moment<2>(args) * mean(args) * mean(args) 61 - 3. * mean(args) * mean(args) * mean(args) * mean(args) 62 , ( accumulators::moment<2>(args) - mean(args) * mean(args) ) 63 * ( accumulators::moment<2>(args) - mean(args) * mean(args) ) 64 ) - 3.; 65 } 66 67 // serialization is done by accumulators it depends on 68 template<class Archive> serializeboost::accumulators::impl::kurtosis_impl69 void serialize(Archive & ar, const unsigned int file_version) {} 70 }; 71 72 } // namespace impl 73 74 /////////////////////////////////////////////////////////////////////////////// 75 // tag::kurtosis 76 // 77 namespace tag 78 { 79 struct kurtosis 80 : depends_on<mean, moment<2>, moment<3>, moment<4> > 81 { 82 /// INTERNAL ONLY 83 /// 84 typedef accumulators::impl::kurtosis_impl<mpl::_1> impl; 85 }; 86 } 87 88 /////////////////////////////////////////////////////////////////////////////// 89 // extract::kurtosis 90 // 91 namespace extract 92 { 93 extractor<tag::kurtosis> const kurtosis = {}; 94 95 BOOST_ACCUMULATORS_IGNORE_GLOBAL(kurtosis) 96 } 97 98 using extract::kurtosis; 99 100 // So that kurtosis can be automatically substituted with 101 // weighted_kurtosis when the weight parameter is non-void 102 template<> 103 struct as_weighted_feature<tag::kurtosis> 104 { 105 typedef tag::weighted_kurtosis type; 106 }; 107 108 template<> 109 struct feature_of<tag::weighted_kurtosis> 110 : feature_of<tag::kurtosis> 111 { 112 }; 113 114 }} // namespace boost::accumulators 115 116 #endif 117