Here is some code to calculate Std Dev & RSquared in an incremental fashion. Does anyone know how to do something similar for Skew & Kurtosis?
Code:
protected void Add( double x, double y ) {
//Console.WriteLine( "add,{0},{1:#.00},{2:#.000}", dsSlope.Name, val, x );
SumXX += x * x;
SumX += x;
SumXY += x * y;
SumY += y;
SumYY += y * y;
Xcnt++;
}
protected void Remove( double x, double y ) {
//Console.WriteLine( "rem,{0},{1:#.00},{2:#.000}", dsSlope.Name, val, x );
SumXX -= x * x;
SumX -= x;
SumXY -= x * y;
SumY -= y;
SumYY -= y * y;
Xcnt--;
CanCalcSlope = true;
}
protected virtual void CalcStats() {
double oldb1 = b1;
Sxx = SumXX - SumX * SumX / Xcnt;
Sxy = SumXY - SumX * SumY / Xcnt;
Syy = SumYY - SumY * SumY / Xcnt;
SST = Syy;
SSR = Sxy * Sxy / Sxx;
SSE = SST - SSR;
RR = SSR / SST;
R = Sxy / Math.Sqrt(Sxx * Syy);
SD = Math.Sqrt( Syy / ( Xcnt - 1 ) );
meanY = SumY / Xcnt;
b1 = CanCalcSlope ? Sxy / Sxx : 0;
b0 = ( 1 / Xcnt ) * ( SumY - b1 * SumX );
b2 = b1 - oldb1; // *** do this differently
}