Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:22

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // weighted_skewness.hpp
0003 //
0004 //  Copyright 2006 Olivier Gygi, Daniel Egloff. Distributed under the Boost
0005 //  Software License, Version 1.0. (See accompanying file
0006 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 
0008 #ifndef BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005
0009 #define BOOST_ACCUMULATORS_STATISTICS_WEIGHTED_SKEWNESS_HPP_EAN_28_10_2005
0010 
0011 #include <limits>
0012 #include <boost/mpl/placeholders.hpp>
0013 #include <boost/accumulators/framework/accumulator_base.hpp>
0014 #include <boost/accumulators/framework/extractor.hpp>
0015 #include <boost/accumulators/framework/parameters/sample.hpp>
0016 #include <boost/accumulators/numeric/functional.hpp>
0017 #include <boost/accumulators/framework/depends_on.hpp>
0018 #include <boost/accumulators/statistics_fwd.hpp>
0019 #include <boost/accumulators/statistics/weighted_moment.hpp>
0020 #include <boost/accumulators/statistics/weighted_mean.hpp>
0021 
0022 namespace boost { namespace accumulators
0023 {
0024 
0025 namespace impl
0026 {
0027     ///////////////////////////////////////////////////////////////////////////////
0028     // weighted_skewness_impl
0029     /**
0030         @brief Skewness estimation for weighted samples
0031 
0032         The skewness of a sample distribution is defined as the ratio of the 3rd central moment and the \f$ 3/2 \f$-th power $
0033         of the 2nd central moment (the variance) of the samples. The skewness can also be expressed by the simple moments:
0034 
0035         \f[
0036             \hat{g}_1 =
0037                 \frac
0038                 {\widehat{m}_n^{(3)}-3\widehat{m}_n^{(2)}\hat{\mu}_n+2\hat{\mu}_n^3}
0039                 {\left(\widehat{m}_n^{(2)} - \hat{\mu}_n^{2}\right)^{3/2}}
0040         \f]
0041 
0042         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
0043         \f$ n \f$ samples.
0044 
0045         The skewness estimator for weighted samples is formally identical to the estimator for unweighted samples, except that
0046         the weighted counterparts of all measures it depends on are to be taken.
0047     */
0048     template<typename Sample, typename Weight>
0049     struct weighted_skewness_impl
0050       : accumulator_base
0051     {
0052         typedef typename numeric::functional::multiplies<Sample, Weight>::result_type weighted_sample;
0053         // for boost::result_of
0054         typedef typename numeric::functional::fdiv<weighted_sample, weighted_sample>::result_type result_type;
0055 
0056         weighted_skewness_impl(dont_care) {}
0057 
0058         template<typename Args>
0059         result_type result(Args const &args) const
0060         {
0061             return numeric::fdiv(
0062                         accumulators::weighted_moment<3>(args)
0063                         - 3. * accumulators::weighted_moment<2>(args) * weighted_mean(args)
0064                         + 2. * weighted_mean(args) * weighted_mean(args) * weighted_mean(args)
0065                       , ( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
0066                       * std::sqrt( accumulators::weighted_moment<2>(args) - weighted_mean(args) * weighted_mean(args) )
0067                    );
0068         }
0069     };
0070 
0071 } // namespace impl
0072 
0073 ///////////////////////////////////////////////////////////////////////////////
0074 // tag::weighted_skewness
0075 //
0076 namespace tag
0077 {
0078     struct weighted_skewness
0079       : depends_on<weighted_mean, weighted_moment<2>, weighted_moment<3> >
0080     {
0081         /// INTERNAL ONLY
0082         ///
0083         typedef accumulators::impl::weighted_skewness_impl<mpl::_1, mpl::_2> impl;
0084     };
0085 }
0086 
0087 ///////////////////////////////////////////////////////////////////////////////
0088 // extract::weighted_skewness
0089 //
0090 namespace extract
0091 {
0092     extractor<tag::weighted_skewness> const weighted_skewness = {};
0093 
0094     BOOST_ACCUMULATORS_IGNORE_GLOBAL(weighted_skewness)
0095 }
0096 
0097 using extract::weighted_skewness;
0098 
0099 }} // namespace boost::accumulators
0100 
0101 #endif