/// <summary>
        /// Calculates incremental running average and variance.
        /// </summary>
        /// <param name="data">Sample data.</param>
        /// <param name="onCalculated">
        /// Action callback which fires on each element addition.
        /// <para>Parameters are: (index, incremental average, incremental variance).</para>
        /// </param>
        public static void RunningVarianceIncremental(this IList <double[]> data, Action <int, double[], double> onCalculated)
        {
            var dim = data.First().Length;

            double[] avg         = new double[dim];
            double   varianceSum = 0d;

            for (int i = 0; i < data.Count; i++)
            {
                var item = data[i];

                var prevAvg = avg;
                avg          = avg.Add(RunningAverage.UpdateAverageIncremental(avg, i, item));
                varianceSum += UpdateVarianceIncremental(prevAvg, avg, varianceSum, i, item);
                onCalculated(i, avg, varianceSum);
            }
        }
Exemplo n.º 2
0
        /// <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);
            }
        }
Exemplo n.º 3
0
        /// <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);
            }
        }