libStatGen Software
1
|
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 m_newM = x; 00048 m_newS = 0.0; 00049 } 00050 else 00051 { 00052 m_newM = m_oldM + (x - m_oldM)/m_n; 00053 m_newS = m_oldS + (x - m_oldM)*(x - m_newM); 00054 00055 // set up for next iteration 00056 m_oldM = m_newM; 00057 m_oldS = m_newS; 00058 } 00059 } 00060 00061 int NumDataValues() const 00062 { 00063 return m_n; 00064 } 00065 00066 double Mean() const 00067 { 00068 return (m_n > 0) ? m_newM : 0.0; 00069 } 00070 00071 double Variance() const 00072 { 00073 return ((m_n > 1) ? m_newS/(m_n - 1) : 0.0); 00074 } 00075 00076 double StandardDeviation() const 00077 { 00078 return sqrt(Variance()); 00079 } 00080 00081 private: 00082 uint64_t m_n; 00083 double m_oldM, m_newM, m_oldS, m_newS; 00084 }; 00085 00086 // 00087 // helpers for Tabulate template 00088 // 00089 inline bool operator == (RunningStat &r, int i) 00090 { 00091 return r.NumDataValues() == i; 00092 } 00093 00094 inline std::ostream &operator << (std::ostream &stream, RunningStat &s) 00095 { 00096 stream << "N: " << s.NumDataValues() 00097 << " Mean: " << s.Mean() 00098 << " Standard Deviation: " << s.StandardDeviation(); 00099 00100 return stream; 00101 } 00102 00103 00104 #endif