mean.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // mean.hpp
  3. //
  4. // Copyright 2005 Eric Niebler. 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. #ifndef BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
  8. #define BOOST_ACCUMULATORS_STATISTICS_MEAN_HPP_EAN_28_10_2005
  9. #include <boost/mpl/placeholders.hpp>
  10. #include <boost/accumulators/framework/accumulator_base.hpp>
  11. #include <boost/accumulators/framework/extractor.hpp>
  12. #include <boost/accumulators/numeric/functional.hpp>
  13. #include <boost/accumulators/framework/parameters/sample.hpp>
  14. #include <boost/accumulators/framework/depends_on.hpp>
  15. #include <boost/accumulators/statistics_fwd.hpp>
  16. #include <boost/accumulators/statistics/count.hpp>
  17. #include <boost/accumulators/statistics/sum.hpp>
  18. namespace boost { namespace accumulators
  19. {
  20. namespace impl
  21. {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // mean_impl
  24. // lazy, by default
  25. template<typename Sample, typename SumFeature>
  26. struct mean_impl
  27. : accumulator_base
  28. {
  29. // for boost::result_of
  30. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
  31. mean_impl(dont_care) {}
  32. template<typename Args>
  33. result_type result(Args const &args) const
  34. {
  35. extractor<SumFeature> sum;
  36. return numeric::fdiv(sum(args), count(args));
  37. }
  38. };
  39. template<typename Sample, typename Tag>
  40. struct immediate_mean_impl
  41. : accumulator_base
  42. {
  43. // for boost::result_of
  44. typedef typename numeric::functional::fdiv<Sample, std::size_t>::result_type result_type;
  45. template<typename Args>
  46. immediate_mean_impl(Args const &args)
  47. : mean(numeric::fdiv(args[sample | Sample()], numeric::one<std::size_t>::value))
  48. {
  49. }
  50. template<typename Args>
  51. void operator ()(Args const &args)
  52. {
  53. std::size_t cnt = count(args);
  54. this->mean = numeric::fdiv(
  55. (this->mean * (cnt - 1)) + args[parameter::keyword<Tag>::get()]
  56. , cnt
  57. );
  58. }
  59. result_type result(dont_care) const
  60. {
  61. return this->mean;
  62. }
  63. private:
  64. result_type mean;
  65. };
  66. } // namespace impl
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // tag::mean
  69. // tag::immediate_mean
  70. // tag::mean_of_weights
  71. // tag::immediate_mean_of_weights
  72. // tag::mean_of_variates
  73. // tag::immediate_mean_of_variates
  74. //
  75. namespace tag
  76. {
  77. struct mean
  78. : depends_on<count, sum>
  79. {
  80. /// INTERNAL ONLY
  81. ///
  82. typedef accumulators::impl::mean_impl<mpl::_1, sum> impl;
  83. };
  84. struct immediate_mean
  85. : depends_on<count>
  86. {
  87. /// INTERNAL ONLY
  88. ///
  89. typedef accumulators::impl::immediate_mean_impl<mpl::_1, tag::sample> impl;
  90. };
  91. struct mean_of_weights
  92. : depends_on<count, sum_of_weights>
  93. {
  94. typedef mpl::true_ is_weight_accumulator;
  95. /// INTERNAL ONLY
  96. ///
  97. typedef accumulators::impl::mean_impl<mpl::_2, sum_of_weights> impl;
  98. };
  99. struct immediate_mean_of_weights
  100. : depends_on<count>
  101. {
  102. typedef mpl::true_ is_weight_accumulator;
  103. /// INTERNAL ONLY
  104. ///
  105. typedef accumulators::impl::immediate_mean_impl<mpl::_2, tag::weight> impl;
  106. };
  107. template<typename VariateType, typename VariateTag>
  108. struct mean_of_variates
  109. : depends_on<count, sum_of_variates<VariateType, VariateTag> >
  110. {
  111. /// INTERNAL ONLY
  112. ///
  113. typedef mpl::always<accumulators::impl::mean_impl<VariateType, sum_of_variates<VariateType, VariateTag> > > impl;
  114. };
  115. template<typename VariateType, typename VariateTag>
  116. struct immediate_mean_of_variates
  117. : depends_on<count>
  118. {
  119. /// INTERNAL ONLY
  120. ///
  121. typedef mpl::always<accumulators::impl::immediate_mean_impl<VariateType, VariateTag> > impl;
  122. };
  123. }
  124. ///////////////////////////////////////////////////////////////////////////////
  125. // extract::mean
  126. // extract::mean_of_weights
  127. // extract::mean_of_variates
  128. //
  129. namespace extract
  130. {
  131. extractor<tag::mean> const mean = {};
  132. extractor<tag::mean_of_weights> const mean_of_weights = {};
  133. BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, mean_of_variates, (typename)(typename))
  134. BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean)
  135. BOOST_ACCUMULATORS_IGNORE_GLOBAL(mean_of_weights)
  136. }
  137. using extract::mean;
  138. using extract::mean_of_weights;
  139. using extract::mean_of_variates;
  140. // mean(lazy) -> mean
  141. template<>
  142. struct as_feature<tag::mean(lazy)>
  143. {
  144. typedef tag::mean type;
  145. };
  146. // mean(immediate) -> immediate_mean
  147. template<>
  148. struct as_feature<tag::mean(immediate)>
  149. {
  150. typedef tag::immediate_mean type;
  151. };
  152. // mean_of_weights(lazy) -> mean_of_weights
  153. template<>
  154. struct as_feature<tag::mean_of_weights(lazy)>
  155. {
  156. typedef tag::mean_of_weights type;
  157. };
  158. // mean_of_weights(immediate) -> immediate_mean_of_weights
  159. template<>
  160. struct as_feature<tag::mean_of_weights(immediate)>
  161. {
  162. typedef tag::immediate_mean_of_weights type;
  163. };
  164. // mean_of_variates<VariateType, VariateTag>(lazy) -> mean_of_variates<VariateType, VariateTag>
  165. template<typename VariateType, typename VariateTag>
  166. struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(lazy)>
  167. {
  168. typedef tag::mean_of_variates<VariateType, VariateTag> type;
  169. };
  170. // mean_of_variates<VariateType, VariateTag>(immediate) -> immediate_mean_of_variates<VariateType, VariateTag>
  171. template<typename VariateType, typename VariateTag>
  172. struct as_feature<tag::mean_of_variates<VariateType, VariateTag>(immediate)>
  173. {
  174. typedef tag::immediate_mean_of_variates<VariateType, VariateTag> type;
  175. };
  176. // for the purposes of feature-based dependency resolution,
  177. // immediate_mean provides the same feature as mean
  178. template<>
  179. struct feature_of<tag::immediate_mean>
  180. : feature_of<tag::mean>
  181. {
  182. };
  183. // for the purposes of feature-based dependency resolution,
  184. // immediate_mean provides the same feature as mean
  185. template<>
  186. struct feature_of<tag::immediate_mean_of_weights>
  187. : feature_of<tag::mean_of_weights>
  188. {
  189. };
  190. // for the purposes of feature-based dependency resolution,
  191. // immediate_mean provides the same feature as mean
  192. template<typename VariateType, typename VariateTag>
  193. struct feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
  194. : feature_of<tag::mean_of_variates<VariateType, VariateTag> >
  195. {
  196. };
  197. // So that mean can be automatically substituted with
  198. // weighted_mean when the weight parameter is non-void.
  199. template<>
  200. struct as_weighted_feature<tag::mean>
  201. {
  202. typedef tag::weighted_mean type;
  203. };
  204. template<>
  205. struct feature_of<tag::weighted_mean>
  206. : feature_of<tag::mean>
  207. {};
  208. // So that immediate_mean can be automatically substituted with
  209. // immediate_weighted_mean when the weight parameter is non-void.
  210. template<>
  211. struct as_weighted_feature<tag::immediate_mean>
  212. {
  213. typedef tag::immediate_weighted_mean type;
  214. };
  215. template<>
  216. struct feature_of<tag::immediate_weighted_mean>
  217. : feature_of<tag::immediate_mean>
  218. {};
  219. // So that mean_of_weights<> can be automatically substituted with
  220. // weighted_mean_of_variates<> when the weight parameter is non-void.
  221. template<typename VariateType, typename VariateTag>
  222. struct as_weighted_feature<tag::mean_of_variates<VariateType, VariateTag> >
  223. {
  224. typedef tag::weighted_mean_of_variates<VariateType, VariateTag> type;
  225. };
  226. template<typename VariateType, typename VariateTag>
  227. struct feature_of<tag::weighted_mean_of_variates<VariateType, VariateTag> >
  228. : feature_of<tag::mean_of_variates<VariateType, VariateTag> >
  229. {
  230. };
  231. // So that immediate_mean_of_weights<> can be automatically substituted with
  232. // immediate_weighted_mean_of_variates<> when the weight parameter is non-void.
  233. template<typename VariateType, typename VariateTag>
  234. struct as_weighted_feature<tag::immediate_mean_of_variates<VariateType, VariateTag> >
  235. {
  236. typedef tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> type;
  237. };
  238. template<typename VariateType, typename VariateTag>
  239. struct feature_of<tag::immediate_weighted_mean_of_variates<VariateType, VariateTag> >
  240. : feature_of<tag::immediate_mean_of_variates<VariateType, VariateTag> >
  241. {
  242. };
  243. ////////////////////////////////////////////////////////////////////////////
  244. //// droppable_accumulator<mean_impl>
  245. //// need to specialize droppable lazy mean to cache the result at the
  246. //// point the accumulator is dropped.
  247. ///// INTERNAL ONLY
  248. /////
  249. //template<typename Sample, typename SumFeature>
  250. //struct droppable_accumulator<impl::mean_impl<Sample, SumFeature> >
  251. // : droppable_accumulator_base<
  252. // with_cached_result<impl::mean_impl<Sample, SumFeature> >
  253. // >
  254. //{
  255. // template<typename Args>
  256. // droppable_accumulator(Args const &args)
  257. // : droppable_accumulator::base(args)
  258. // {
  259. // }
  260. //};
  261. }} // namespace boost::accumulators
  262. #endif