File size: 3,051 Bytes
b1b3bae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | /// ------------------------------------------------------
/// 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
{
/// <summary>
/// Compute statistical measures in accumulating manner, incl.
/// mean, variance, std.deviation, min, max.
/// </summary>
public class StatisticsAccumulator
{
#region Constructors.
/// <summary>
/// Construct the object.
/// </summary>
public StatisticsAccumulator()
{
Clear();
}
#endregion
#region Private fields.
/// <summary>
/// Accumulator variable.
/// </summary>
double Q;
#endregion
#region Public fields.
/// <summary>
/// Number of accumulations performed so far.
/// </summary>
public double Count
{
get;
private set;
}
/// <summary>
/// Mean.
/// </summary>
public double Mean
{
get;
private set;
}
/// <summary>
/// Min.
/// </summary>
public double Min
{
get;
private set;
}
/// <summary>
/// Max.
/// </summary>
public double Max
{
get;
private set;
}
/// <summary>
/// Variance.
/// </summary>
public double Variance
{
get { return Q / Count; }
}
/// <summary>
/// Standard deviation.
/// </summary>
public double StandardDeviation
{
get { return System.Math.Sqrt(Variance); }
}
#endregion
#region Public methods.
/// <summary>
/// Input data and accumulate variance.
/// </summary>
/// <param name="x">Data to input.</param>
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
}
/// <summary>
/// Clear the accumulated variance.
/// </summary>
public void Clear()
{
Mean = 0;
Q = 0;
Count = 0;
Min = System.Double.MaxValue;
Max = System.Double.MinValue;
}
#endregion
}
}
|