Go to the documentation of this file.00001
00004
00005
00006
00007
00008 #ifndef ecl_statistics_CUMULATIVE_STATISTICS_HPP_
00009 #define ecl_statistics_CUMULATIVE_STATISTICS_HPP_
00010
00011
00012
00013
00014
00015 #include <ecl/mpl.hpp>
00016 #include <ecl/type_traits.hpp>
00017
00018
00019
00020
00021
00022 namespace ecl {
00023
00024
00025
00026
00027
00034 template <typename T, typename Enable = void>
00035 class CumulativeStatistics : public ecl::FailedObject {};
00036
00040 template <typename T>
00041 class CumulativeStatistics<T, typename ecl::enable_if< ecl::is_float<T> >::type> {
00042 public:
00043 CumulativeStatistics(): number_of_data(0.0) {}
00044
00045 void clear() { number_of_data = 0.0; }
00046
00055 void push_back(const T & x)
00056 {
00057 number_of_data++;
00058 if(number_of_data == 1.0)
00059 {
00060 old_mean = new_mean = x;
00061 old_variance = 0.0;
00062 }
00063 else
00064 {
00065 new_mean = old_mean + static_cast<T>(x - old_mean) / number_of_data;
00066 new_variance = old_variance + static_cast<T>(x - old_mean) * static_cast<T>(x - new_mean);
00067
00068 old_mean = new_mean;
00069 old_variance = new_variance;
00070 }
00071 }
00072
00077 T size() const { return number_of_data; }
00078
00083 T mean() const { return (number_of_data > 0) ? new_mean : 0.0; }
00084
00089 T variance() const { return ((number_of_data > 1) ? new_variance / (number_of_data - 1) : 0.0); }
00090
00091 private:
00092 T number_of_data;
00093 T old_mean, old_variance;
00094 T new_mean, new_variance;
00095 };
00096
00097
00098
00099
00100
00101 }
00102
00103 #endif