Skip to content

Instantly share code, notes, and snippets.

@neerajnagi
Last active October 1, 2016 05:37
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 neerajnagi/39ab78a8eacddb8ce2130113decfc712 to your computer and use it in GitHub Desktop.
Save neerajnagi/39ab78a8eacddb8ce2130113decfc712 to your computer and use it in GitHub Desktop.
online moment algorithm

WINDOW is # of batches to be used

#define WINDOW = 24 //for 4 hours double n,m1,m2,m3,m4 //mN is Nth moment

function reset(){ n=0 m1=0 m2=0 m3=0 m4=0 }

function add(double x){ if(n>=WINDOW) reset()

double n1 , delta , delta_n, delta_n2, term1;

n1=n; n=n+1; delta = x-m1; delta_n = delta/n; delta_n2 = delta_n * delta_n; term1 = delta * delta_n * n1; m1 += delta_n; m4 += term1 * delta_n2 * (nn - 3n + 3) + 6 * delta_n2 * m2 - 4 * delta_n * m3; m3 += term1 * delta_n * (n - 2) - 3 * delta_n * m2; m2 += term1; }

function mean(){ return m1 }

function variance(){ return m2/n-1 }

function skewness(){ return Math.sqrt(n)*m3/Math.pow(m2,1.5) }

function kurtosis(){ return (nm4/(m2m2)) - 3 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment