Skip to content

Instantly share code, notes, and snippets.

@musically-ut
Created December 20, 2011 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musically-ut/1502030 to your computer and use it in GitHub Desktop.
Save musically-ut/1502030 to your computer and use it in GitHub Desktop.
Naive standard deviation calculation
class StdDevCalc {
private:
long long m_count;
double m_sum_sq, m_sum, m_var;
public:
StdDevCalc() {
m_sum_sq = m_sum = m_var = m_count = 0;
}
void append(double d) {
m_count++;
m_sum_sq += d * d;
m_sum += d;
if (m_count > 1)
m_var = (m_sum_sq - m_sum * m_sum / m_count) / (m_count - 1);
}
double get_std_dev() {
return sqrt(m_var);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment