File indexing completed on 2025-01-18 09:28:20
0001
0002
0003
0004
0005
0006
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
0028
0029 template<typename Sample, typename SumFeature>
0030 struct mean_impl
0031 : accumulator_base
0032 {
0033
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
0046 template<class Archive>
0047 void serialize(Archive & , const unsigned int ) {}
0048 };
0049
0050 template<typename Sample, typename Tag>
0051 struct immediate_mean_impl
0052 : accumulator_base
0053 {
0054
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 )
0080 {
0081 ar & mean;
0082 }
0083
0084 private:
0085 result_type mean;
0086 };
0087
0088 }
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 namespace tag
0099 {
0100 struct mean
0101 : depends_on<count, sum>
0102 {
0103
0104
0105 typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
0106 };
0107 struct immediate_mean
0108 : depends_on<count>
0109 {
0110
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
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
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
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
0143
0144 typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl;
0145 };
0146 }
0147
0148
0149
0150
0151
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
0168 template<>
0169 struct as_feature<tag::mean(lazy)>
0170 {
0171 typedef tag::mean type;
0172 };
0173
0174
0175 template<>
0176 struct as_feature<tag::mean(immediate)>
0177 {
0178 typedef tag::immediate_mean type;
0179 };
0180
0181
0182 template<>
0183 struct as_feature<tag::mean_of_weights(lazy)>
0184 {
0185 typedef tag::mean_of_weights type;
0186 };
0187
0188
0189 template<>
0190 struct as_feature<tag::mean_of_weights(immediate)>
0191 {
0192 typedef tag::immediate_mean_of_weights type;
0193 };
0194
0195
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
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
0210
0211 template<>
0212 struct feature_of<tag::immediate_mean>
0213 : feature_of<tag::mean>
0214 {
0215 };
0216
0217
0218
0219 template<>
0220 struct feature_of<tag::immediate_mean_of_weights>
0221 : feature_of<tag::mean_of_weights>
0222 {
0223 };
0224
0225
0226
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
0234
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
0247
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
0260
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
0274
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
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306 }}
0307
0308 #endif