Created
June 7, 2016 21:01
-
-
Save glamp/a62847718622eb8e259c7f4d4a296841 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Greg's Program | |
gcc gregs-program.cc -o main && ./main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* sqrt example */ | |
#include <stdio.h> /* printf */ | |
#include <math.h> /* sqrt */ | |
class RunningStat | |
{ | |
public: | |
RunningStat() : m_n(0) {} | |
void Clear() | |
{ | |
m_n = 0; | |
} | |
void Push(double x) | |
{ | |
m_n++; | |
// See Knuth TAOCP vol 2, 3rd edition, page 232 | |
if (m_n == 1) | |
{ | |
m_oldM = m_newM = x; | |
m_oldS = 0.0; | |
} | |
else | |
{ | |
m_newM = m_oldM + (x - m_oldM)/m_n; | |
m_newS = m_oldS + (x - m_oldM)*(x - m_newM); | |
// set up for next iteration | |
m_oldM = m_newM; | |
m_oldS = m_newS; | |
} | |
} | |
int NumDataValues() const | |
{ | |
return m_n; | |
} | |
double Mean() const | |
{ | |
return (m_n > 0) ? m_newM : 0.0; | |
} | |
double Variance() const | |
{ | |
return ( (m_n > 1) ? m_newS/(m_n - 1) : 0.0 ); | |
} | |
double StandardDeviation() const | |
{ | |
return sqrt( Variance() ); | |
} | |
private: | |
int m_n; | |
double m_oldM, m_newM, m_oldS, m_newS; | |
}; | |
int main() | |
{ | |
RunningStat rs; | |
rs.Push(17.0); | |
rs.Push(19.0); | |
rs.Push(24.0); | |
rs.Push(24.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
rs.Push(1.0); | |
double mean = rs.Mean(); | |
double variance = rs.Variance(); | |
double stdev = rs.StandardDeviation(); | |
printf ("mean = %f\nvariance = %f\nstdev = %f\n", mean, variance, stdev); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment