mean.hpp 9.1 KB

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