Back to home page

EIC code displayed by LXR

 
 

    


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

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // mean.hpp
0003 //
0004 //  Copyright 2005 Eric Niebler. 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_MEAN_HPP_EAN_28_10_2005
0009 #define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
0010 
0011 #include <boost/mpl/placeholders.hpp>
0012 #include <boost/accumulators/framework/accumulator_base.hpp>
0013 #include <boost/accumulators/framework/extractor.hpp>
0014 #include <boost/accumulators/numeric/functional.hpp>
0015 #include <boost/accumulators/framework/parameters/sample.hpp>
0016 #include <boost/accumulators/framework/depends_on.hpp>
0017 #include <boost/accumulators/statistics_fwd.hpp>
0018 #include <boost/accumulators/statistics/count.hpp>
0019 #include <boost/accumulators/statistics/sum.hpp>
0020 
0021 namespace boost { namespace accumulators
0022 {
0023 
0024 namespace impl
0025 {
0026     ///////////////////////////////////////////////////////////////////////////////
0027     // mean_impl
0028     //      lazy, by default
0029     template<typename Sample, typename SumFeature>
0030     struct mean_impl
0031       : accumulator_base
0032     {
0033         // for boost::result_of
0034         typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
0035 
0036         mean_impl(dont_care) {}
0037 
0038         template<typename Args>
0039         result_type result(Args const &args) const
0040         {
0041             extractor<SumFeature> sum;
0042             return numeric::fdiv(sum(args), count(args));
0043         }
0044 
0045         // serialization is done by accumulators it depends on
0046         template<class Archive>
0047         void serialize(Archive & /* ar */, const unsigned int /* file_version */) {}
0048     };
0049 
0050     template<typename Sample, typename Tag>
0051     struct immediate_mean_impl
0052       : accumulator_base
0053     {
0054         // for boost::result_of
0055         typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
0056 
0057         template<typename Args>
0058         immediate_mean_impl(Args const &args)
0059           : mean(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
0060         {
0061         }
0062 
0063         template<typename Args>
0064         void operator ()(Args const &args)
0065         {
0066             std::size_t cnt = count(args);
0067             this->mean = numeric::fdiv(
0068                 (this->mean * (cnt - 1)) + args[parameter::keyword<Tag>::get()]
0069               , cnt
0070             );
0071         }
0072 
0073         result_type result(dont_care) const
0074         {
0075             return this->mean;
0076         }
0077 
0078         template<class Archive>
0079         void serialize(Archive & ar, const unsigned int /* file_version */)
0080         {
0081             ar & mean;
0082         }
0083 
0084     private:
0085         result_type mean;
0086     };
0087 
0088 } // namespace impl
0089 
0090 ///////////////////////////////////////////////////////////////////////////////
0091 // tag::mean
0092 // tag::immediate_mean
0093 // tag::mean_of_weights
0094 // tag::immediate_mean_of_weights
0095 // tag::mean_of_variates
0096 // tag::immediate_mean_of_variates
0097 //
0098 namespace tag
0099 {
0100     struct mean
0101       : depends_on<count, sum>
0102     {
0103         /// INTERNAL ONLY
0104         ///
0105         typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
0106     };
0107     struct immediate_mean
0108       : depends_on<count>
0109     {
0110         /// INTERNAL ONLY
0111         ///
0112         typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl;
0113     };
0114     struct mean_of_weights
0115       : depends_on<count, sum_of_weights>
0116     {
0117         typedef mpl::true_ is_weight_accumulator;
0118         /// INTERNAL ONLY
0119         ///
0120         typedef accumulators::impl::mean_impl<mpl::_2, sum_of_weights> impl;
0121     };
0122     struct immediate_mean_of_weights
0123       : depends_on<count>
0124     {
0125         typedef mpl::true_ is_weight_accumulator;
0126         /// INTERNAL ONLY
0127         ///
0128         typedef accumulators::impl::immediate_mean_impl<mpl::_2, tag::weight> impl;
0129     };
0130     template<typename VariateType, typename VariateTag>
0131     struct mean_of_variates
0132       : depends_on<count, sum_of_variates<VariateType, VariateTag> >
0133     {
0134         /// INTERNAL ONLY
0135         ///
0136         typedef mpl::always<accumulators::impl::mean_impl<VariateType, sum_of_variates<VariateType, VariateTag> > > impl;
0137     };
0138     template<typename VariateType, typename VariateTag>
0139     struct immediate_mean_of_variates
0140       : depends_on<count>
0141     {
0142         /// INTERNAL ONLY
0143         ///
0144         typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl;
0145     };
0146 }
0147 
0148 ///////////////////////////////////////////////////////////////////////////////
0149 // extract::mean
0150 // extract::mean_of_weights
0151 // extract::mean_of_variates
0152 //
0153 namespace extract
0154 {
0155     extractor<tag::mean> const mean = {};
0156     extractor<tag::mean_of_weights> const mean_of_weights = {};
0157     BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, mean_of_variates, (typename)(typename))
0158 
0159     BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean)
0160     BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean_of_weights)
0161 }
0162 
0163 using extract::mean;
0164 using extract::mean_of_weights;
0165 using extract::mean_of_variates;
0166 
0167 // mean(lazy) -> mean
0168 template<>
0169 struct as_feature<tag::mean(lazy)>
0170 {
0171     typedef tag::mean type;
0172 };
0173 
0174 // mean(immediate) -> immediate_mean
0175 template<>
0176 struct as_feature<tag::mean(immediate)>
0177 {
0178     typedef tag::immediate_mean type;
0179 };
0180 
0181 // mean_of_weights(lazy) -> mean_of_weights
0182 template<>
0183 struct as_feature<tag::mean_of_weights(lazy)>
0184 {
0185     typedef tag::mean_of_weights type;
0186 };
0187 
0188 // mean_of_weights(immediate) -> immediate_mean_of_weights
0189 template<>
0190 struct as_feature<tag::mean_of_weights(immediate)>
0191 {
0192     typedef tag::immediate_mean_of_weights type;
0193 };
0194 
0195 // mean_of_variates<VariateType, VariateTag>(lazy) -> mean_of_variates<VariateType, VariateTag>
0196 template<typename VariateType, typename VariateTag>
0197 struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(lazy)>
0198 {
0199     typedef tag::mean_of_variates<VariateType, VariateTag> type;
0200 };
0201 
0202 // mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_mean_of_variates<VariateType, VariateTag>
0203 template<typename VariateType, typename VariateTag>
0204 struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(immediate)>
0205 {
0206     typedef tag::immediate_mean_of_variates<VariateType, VariateTag> type;
0207 };
0208 
0209 // for the purposes of feature-based dependency resolution,
0210 // immediate_mean provides the same feature as mean
0211 template<>
0212 struct feature_of<tag::immediate_mean>
0213   : feature_of<tag::mean>
0214 {
0215 };
0216 
0217 // for the purposes of feature-based dependency resolution,
0218 // immediate_mean provides the same feature as mean
0219 template<>
0220 struct feature_of<tag::immediate_mean_of_weights>
0221   : feature_of<tag::mean_of_weights>
0222 {
0223 };
0224 
0225 // for the purposes of feature-based dependency resolution,
0226 // immediate_mean provides the same feature as mean
0227 template<typename VariateType, typename VariateTag>
0228 struct feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
0229   : feature_of<tag::mean_of_variates<VariateType, VariateTag> >
0230 {
0231 };
0232 
0233 // So that mean can be automatically substituted with
0234 // weighted_mean when the weight parameter is non-void.
0235 template<>
0236 struct as_weighted_feature<tag::mean>
0237 {
0238     typedef tag::weighted_mean type;
0239 };
0240 
0241 template<>
0242 struct feature_of<tag::weighted_mean>
0243   : feature_of<tag::mean>
0244 {};
0245 
0246 // So that immediate_mean can be automatically substituted with
0247 // immediate_weighted_mean when the weight parameter is non-void.
0248 template<>
0249 struct as_weighted_feature<tag::immediate_mean>
0250 {
0251     typedef tag::immediate_weighted_mean type;
0252 };
0253 
0254 template<>
0255 struct feature_of<tag::immediate_weighted_mean>
0256   : feature_of<tag::immediate_mean>
0257 {};
0258 
0259 // So that mean_of_weights<> can be automatically substituted with
0260 // weighted_mean_of_variates<> when the weight parameter is non-void.
0261 template<typename VariateType, typename VariateTag>
0262 struct as_weighted_feature<tag::mean_of_variates<VariateType, VariateTag> >
0263 {
0264     typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
0265 };
0266 
0267 template<typename VariateType, typename VariateTag>
0268 struct feature_of<tag::weighted_mean_of_variates<VariateType, VariateTag> >
0269   : feature_of<tag::mean_of_variates<VariateType, VariateTag> >
0270 {
0271 };
0272 
0273 // So that immediate_mean_of_weights<> can be automatically substituted with
0274 // immediate_weighted_mean_of_variates<> when the weight parameter is non-void.
0275 template<typename VariateType, typename VariateTag>
0276 struct as_weighted_feature<tag::immediate_mean_of_variates<VariateType, VariateTag> >
0277 {
0278     typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
0279 };
0280 
0281 template<typename VariateType, typename VariateTag>
0282 struct feature_of<tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> >
0283   : feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
0284 {
0285 };
0286 
0287 ////////////////////////////////////////////////////////////////////////////
0288 //// droppable_accumulator<mean_impl>
0289 ////  need to specialize droppable lazy mean to cache the result at the
0290 ////  point the accumulator is dropped.
0291 ///// INTERNAL ONLY
0292 /////
0293 //template<typename Sample, typename SumFeature>
0294 //struct droppable_accumulator<impl::mean_impl<Sample, SumFeature> >
0295 //  : droppable_accumulator_base<
0296 //        with_cached_result<impl::mean_impl<Sample, SumFeature> >
0297 //    >
0298 //{
0299 //    template<typename Args>
0300 //    droppable_accumulator(Args const &args)
0301 //      : droppable_accumulator::base(args)
0302 //    {
0303 //    }
0304 //};
0305 
0306 }} // namespace boost::accumulators
0307 
0308 #endif