/// ------------------------------------------------------ /// SwarmOps - Numeric and heuristic optimization for C# /// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen. /// Please see the file license.txt for license details. /// SwarmOps on the internet: http://www.Hvass-Labs.org/ /// ------------------------------------------------------ using System.Diagnostics; namespace SwarmOps { /// /// Compute statistical measures in accumulating manner, incl. /// mean, variance, std.deviation, min, max. /// public class StatisticsAccumulator { #region Constructors. /// /// Construct the object. /// public StatisticsAccumulator() { Clear(); } #endregion #region Private fields. /// /// Accumulator variable. /// double Q; #endregion #region Public fields. /// /// Number of accumulations performed so far. /// public double Count { get; private set; } /// /// Mean. /// public double Mean { get; private set; } /// /// Min. /// public double Min { get; private set; } /// /// Max. /// public double Max { get; private set; } /// /// Variance. /// public double Variance { get { return Q / Count; } } /// /// Standard deviation. /// public double StandardDeviation { get { return System.Math.Sqrt(Variance); } } #endregion #region Public methods. /// /// Input data and accumulate variance. /// /// Data to input. public void Accumulate(double x) { #region Mean and variance update. double meanOld = Mean; Mean = Mean + (x - Mean) / (Count + 1); Q = Q + (x - meanOld) * (x - Mean); Count++; #endregion #region Min and Max update. Min = System.Math.Min(Min, x); Max = System.Math.Max(Max, x); #endregion } /// /// Clear the accumulated variance. /// public void Clear() { Mean = 0; Q = 0; Count = 0; Min = System.Double.MaxValue; Max = System.Double.MinValue; } #endregion } }