Back to home page

EIC code displayed by LXR

 
 

    


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

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // tail_quantile.hpp
0003 //
0004 //  Copyright 2006 Daniel Egloff, Olivier Gygi. 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_TAIL_QUANTILE_HPP_DE_01_01_2006
0009 #define BOOST_ACCUMULATORS_STATISTICS_TAIL_QUANTILE_HPP_DE_01_01_2006
0010 
0011 #include <vector>
0012 #include <limits>
0013 #include <functional>
0014 #include <sstream>
0015 #include <stdexcept>
0016 #include <boost/config/no_tr1/cmath.hpp>             // For ceil
0017 #include <boost/throw_exception.hpp>
0018 #include <boost/parameter/keyword.hpp>
0019 #include <boost/mpl/placeholders.hpp>
0020 #include <boost/mpl/if.hpp>
0021 #include <boost/type_traits/is_same.hpp>
0022 #include <boost/accumulators/framework/depends_on.hpp>
0023 #include <boost/accumulators/framework/accumulator_base.hpp>
0024 #include <boost/accumulators/framework/extractor.hpp>
0025 #include <boost/accumulators/numeric/functional.hpp>
0026 #include <boost/accumulators/framework/parameters/sample.hpp>
0027 #include <boost/accumulators/statistics_fwd.hpp>
0028 #include <boost/accumulators/statistics/tail.hpp>
0029 #include <boost/accumulators/statistics/count.hpp>
0030 #include <boost/accumulators/statistics/parameters/quantile_probability.hpp>
0031 
0032 #ifdef _MSC_VER
0033 # pragma warning(push)
0034 # pragma warning(disable: 4127) // conditional expression is constant
0035 #endif
0036 
0037 namespace boost { namespace accumulators
0038 {
0039 
0040 namespace impl
0041 {
0042     ///////////////////////////////////////////////////////////////////////////////
0043     // tail_quantile_impl
0044     //  Tail quantile estimation based on order statistics
0045     /**
0046         @brief Tail quantile estimation based on order statistics (for both left and right tails)
0047 
0048         The estimation of a tail quantile \f$\hat{q}\f$ with level \f$\alpha\f$ based on order statistics requires the
0049         caching of at least the \f$\lceil n\alpha\rceil\f$ smallest or the \f$\lceil n(1-\alpha)\rceil\f$ largest samples,
0050         \f$n\f$ being the total number of samples. The largest of the \f$\lceil n\alpha\rceil\f$ smallest samples or the
0051         smallest of the \f$\lceil n(1-\alpha)\rceil\f$ largest samples provides an estimate for the quantile:
0052 
0053         \f[
0054             \hat{q}_{n,\alpha} = X_{\lceil \alpha n \rceil:n}
0055         \f]
0056 
0057         @param quantile_probability
0058     */
0059     template<typename Sample, typename LeftRight>
0060     struct tail_quantile_impl
0061       : accumulator_base
0062     {
0063         // for boost::result_of
0064         typedef Sample result_type;
0065 
0066         tail_quantile_impl(dont_care) {}
0067 
0068         template<typename Args>
0069         result_type result(Args const &args) const
0070         {
0071             std::size_t cnt = count(args);
0072 
0073             std::size_t n = static_cast<std::size_t>(
0074                 std::ceil(
0075                     cnt * ( ( is_same<LeftRight, left>::value ) ? args[quantile_probability] : 1. - args[quantile_probability] )
0076                 )
0077             );
0078 
0079             // If n is in a valid range, return result, otherwise return NaN or throw exception
0080             if ( n < static_cast<std::size_t>(tail(args).size()))
0081             {
0082                // Note that the cached samples of the left are sorted in ascending order,
0083                // whereas the samples of the right tail are sorted in descending order
0084                return *(boost::begin(tail(args)) + n - 1);
0085             }
0086             else
0087             {
0088                 if (std::numeric_limits<result_type>::has_quiet_NaN)
0089                 {
0090                     return std::numeric_limits<result_type>::quiet_NaN();
0091                 }
0092                 else
0093                 {
0094                     std::ostringstream msg;
0095                     msg << "index n = " << n << " is not in valid range [0, " << tail(args).size() << ")";
0096                     boost::throw_exception(std::runtime_error(msg.str()));
0097                     return Sample(0);
0098                 }
0099             }
0100         }
0101         
0102         // serialization is done by accumulators it depends on
0103         template<class Archive>
0104         void serialize(Archive & ar, const unsigned int file_version) {}
0105     };
0106 } // namespace impl
0107 
0108 ///////////////////////////////////////////////////////////////////////////////
0109 // tag::tail_quantile<>
0110 //
0111 namespace tag
0112 {
0113     template<typename LeftRight>
0114     struct tail_quantile
0115       : depends_on<count, tail<LeftRight> >
0116     {
0117         /// INTERNAL ONLY
0118         ///
0119         typedef accumulators::impl::tail_quantile_impl<mpl::_1, LeftRight> impl;
0120     };
0121 }
0122 
0123 ///////////////////////////////////////////////////////////////////////////////
0124 // extract::tail_quantile
0125 //
0126 namespace extract
0127 {
0128     extractor<tag::quantile> const tail_quantile = {};
0129 
0130     BOOST_ACCUMULATORS_IGNORE_GLOBAL(tail_quantile)
0131 }
0132 
0133 using extract::tail_quantile;
0134 
0135 // for the purposes of feature-based dependency resolution,
0136 // tail_quantile<LeftRight> provide the same feature as quantile
0137 template<typename LeftRight>
0138 struct feature_of<tag::tail_quantile<LeftRight> >
0139   : feature_of<tag::quantile>
0140 {
0141 };
0142 
0143 // So that tail_quantile can be automatically substituted with
0144 // weighted_tail_quantile when the weight parameter is non-void.
0145 template<typename LeftRight>
0146 struct as_weighted_feature<tag::tail_quantile<LeftRight> >
0147 {
0148     typedef tag::weighted_tail_quantile<LeftRight> type;
0149 };
0150 
0151 template<typename LeftRight>
0152 struct feature_of<tag::weighted_tail_quantile<LeftRight> >
0153   : feature_of<tag::tail_quantile<LeftRight> >
0154 {};
0155 
0156 }} // namespace boost::accumulators
0157 
0158 #ifdef _MSC_VER
0159 # pragma warning(pop)
0160 #endif
0161 
0162 #endif