/// <summary> /// Calculates decremental running average and variance. /// </summary> /// <param name="data">Sample data.</param> /// <param name="onCalculated"> /// Action callback which fires on each element removal. /// <para>Parameters are: (index, decremental average, decremental variance).</para> /// </param> public static void RunningVarianceDecremental(this IList <double> data, Action <int, double, double> onCalculated) { double avg = data.Average(), variance = data.Variance(); for (int i = 0; i < data.Count; i++) { var item = data[i]; var postAvg = avg; avg -= RunningAverage.UpdateAverageDecremental(avg, data.Count - i, item); variance -= UpdateVarianceDecremental(postAvg, avg, variance, data.Count - i, item); onCalculated(i, avg, variance); } }
/// <summary> /// Calculates decremental running average and variance. /// </summary> /// <param name="data">Sample data.</param> /// <param name="onCalculated"> /// Action callback which fires on each element removal. /// <para>Parameters are: (index, decremental average, decremental variance).</para> /// </param> public static void RunningVarianceDecremental(this IList <double[]> data, Action <int, double[], double> onCalculated) { double[] avg = data.Average(); double varianceSum = data.Variance().Sum(); for (int i = 0; i < data.Count; i++) { var item = data[i]; var postAvg = avg; avg = avg.Subtract(RunningAverage.UpdateAverageDecremental(avg, data.Count - i, item)); varianceSum -= UpdateVarianceDecremental(postAvg, avg, varianceSum, data.Count - i, item); onCalculated(i, avg, varianceSum); } }
/// <summary> /// Calculates incremental and decremental running variance. /// </summary> /// <param name="data">Sample data.</param> /// <param name="onCalculated"> /// Action callback which fires on each element addition-removal. /// <para>Parameters are: (index, incremental average, incremental variance, decremental average, decremental variance).</para> /// </param> public static void RunningVarianceIncDec(this IList <double> data, Action <int, double, double, double, double> onCalculated) { double avgInc = 0d, varInc = 0d; double avgDec = data.Average(), varDec = data.Variance(); for (int i = 0; i < data.Count; i++) { var item = data[i]; var prevAvg = avgInc; avgInc += RunningAverage.UpdateAverageIncremental(avgInc, i, item); var postAvg = avgDec; avgDec -= RunningAverage.UpdateAverageDecremental(avgDec, data.Count - i, item); varInc += UpdateVarianceIncremental(prevAvg, avgInc, varInc, i, item); varDec -= UpdateVarianceDecremental(postAvg, avgDec, varDec, data.Count - i, item); onCalculated(i, avgInc, varInc, avgDec, varDec); } }
/// <summary> /// Calculates incremental and decremental running variance. /// </summary> /// <param name="data">Sample data.</param> /// <param name="onCalculated"> /// Action callback which fires on each element addition-removal. /// <para>Parameters are: (index, incremental (average, incremental variance, decremental average, decremental variance).</para> /// </param> public static void RunningVarianceIncDec(this IList <double[]> data, Action <int, double[], double, double[], double> onCalculated) { var dim = data.First().Length; double[] avgInc = new double[dim], avgDec = data.Average(); double varInc = 0d, varDec = data.Variance().Sum(); for (int i = 0; i < data.Count; i++) { var item = data[i]; var prevAvg = avgInc; avgInc = avgInc.Add(RunningAverage.UpdateAverageIncremental(avgInc, i, item)); var postAvg = avgDec; avgDec = avgDec.Subtract(RunningAverage.UpdateAverageDecremental(avgDec, data.Count - i, item)); varInc += UpdateVarianceIncremental(prevAvg, avgInc, varInc, i, item); varDec -= UpdateVarianceDecremental(postAvg, avgDec, varDec, data.Count - i, item); onCalculated(i, avgInc, varInc, avgDec, varDec); } }