Created
June 26, 2018 23:55
-
-
Save vsmelov/4486656ff21de79fa42d16f7574a7444 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
private long mN = 0L; | |
private double mM = 0.0; | |
private double mS = 0.0; | |
public void handle(double x) { | |
++mN; | |
double nextM = mM + (x – mM) / mN; | |
mS += (x – mM) * (x – nextM); | |
mM = nextM; | |
} | |
public void unHandle(double x) { | |
if (mN == 0) { | |
throw new IllegalStateException(); | |
} else if (mN == 1) { | |
mN = 0; mM = 0.0; mS = 0.0; | |
} else { | |
double mMOld = (mN * mM - x)/(mN-1); | |
mS -= (x - mM) * (x - mMOld); | |
mM = mMOld; | |
--mN; | |
} | |
} | |
public double mean() { | |
return mM; | |
} | |
public double varianceUnbiased() { | |
return mN > 1 ? mS/(mN-1) : 0.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment