SimpleStats.h

00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef _SIMPLESTATS_H_
00019 #define _SIMPLESTATS_H_
00020 
00021 #include <math.h>   // for sqrt
00022 #include <iostream>
00023 
00024 //
00025 // see http://www.johndcook.com/standard_deviation.html
00026 // or Donald Knuth's Art of Computer Programming, Vol 2, page 232, 3rd edition
00027 //
00028 class RunningStat
00029 {
00030 public:
00031     RunningStat() : m_n(0), m_oldM(0), m_newM(0), m_oldS(0), m_newS(0) {}
00032 
00033     void Clear()
00034     {
00035         m_n = 0;
00036     }
00037 
00038     void Push(double x)
00039     {
00040         m_n++;
00041 
00042         // See Knuth TAOCP vol 2, 3rd edition, page 232
00043         if (m_n == 1)
00044         {
00045             m_oldM = x;
00046             m_oldS = 0.0;
00047         }
00048         else
00049         {
00050             m_newM = m_oldM + (x - m_oldM)/m_n;
00051             m_newS = m_oldS + (x - m_oldM)*(x - m_newM);
00052 
00053             // set up for next iteration
00054             m_oldM = m_newM;
00055             m_oldS = m_newS;
00056         }
00057     }
00058 
00059     int NumDataValues() const
00060     {
00061         return m_n;
00062     }
00063 
00064     double Mean() const
00065     {
00066         return (m_n > 0) ? m_newM : 0.0;
00067     }
00068 
00069     double Variance() const
00070     {
00071         return ((m_n > 1) ? m_newS/(m_n - 1) : 0.0);
00072     }
00073 
00074     double StandardDeviation() const
00075     {
00076         return sqrt(Variance());
00077     }
00078 
00079 private:
00080     uint64_t m_n;
00081     double m_oldM, m_newM, m_oldS, m_newS;
00082 };
00083 
00084 //
00085 // helpers for Tabulate template
00086 //
00087 inline bool operator == (RunningStat &r, int i)
00088 {
00089     return r.NumDataValues() == i;
00090 }
00091 
00092 inline std::ostream &operator << (std::ostream &stream, RunningStat &s)
00093 {
00094     stream  << "N: " << s.NumDataValues()
00095     << " Mean: " << s.Mean()
00096     << " Standard Deviation: " << s.StandardDeviation();
00097 
00098     return stream;
00099 }
00100 
00101 
00102 #endif
Generated on Tue Sep 6 17:52:01 2011 for libStatGen Software by  doxygen 1.6.3