00001 /* 00002 * Copyright (C) 2009 Austin Robot Technology, Jack O'Quin 00003 * 00004 * License: Modified BSD Software License Agreement 00005 */ 00006 00022 #ifndef RUNNING_STATS_HH 00023 #define RUNNING_STATS_HH 00024 00025 #include <cmath> 00026 00027 class RunningStats { 00028 public: 00029 RunningStats() 00030 { 00031 reset(); 00032 } 00033 00034 // Add a new sample to the series. 00035 void inline addSample(double new_value) 00036 { 00037 ++N_; 00038 sum_ += new_value; 00039 sum2_ += new_value * new_value; 00040 } 00041 00042 // Reset the sample data. 00043 void reset(void) 00044 { 00045 N_ = 0; 00046 sum_ = 0.0; 00047 sum2_ = 0.0; 00048 } 00049 00050 // Return the sample mean. 00051 double inline getMean(void) 00052 { 00053 return (sum_ / N_); 00054 } 00055 00056 // Return the sample variance using Bessel's correction. 00057 double inline getVariance(void) 00058 { 00059 double mean = getMean(); 00060 if (N_ > 1) 00061 return (sum2_/(N_-1) - ((double)N_/(N_-1))*mean*mean); 00062 else 00063 return 0.0; 00064 } 00065 00066 // Return the sample standard deviation using Bessel's correction. 00067 double inline getStandardDeviation(void) 00068 { 00069 return sqrt(getVariance()); 00070 } 00071 00072 private: 00073 int N_; // number of samples seen 00074 double sum_; // sum of samples seen 00075 double sum2_; // sum of squares of samples seen 00076 }; 00077 00078 #endif // RUNNING_STATS_HH