Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanAndVarianceCalculator.cs @ 14293

Last change on this file since 14293 was 14293, checked in by bburlacu, 8 years ago

#2672: Implement online calculator cloning.

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using HeuristicLab.Common;
24
25namespace HeuristicLab.Problems.DataAnalysis {
26  public class OnlineMeanAndVarianceCalculator : IDeepCloneable {
27
28    private double m_oldM, m_newM, m_oldS, m_newS;
29    private int n;
30
31    private OnlineCalculatorError varianceErrorState;
32    public OnlineCalculatorError VarianceErrorState {
33      get { return varianceErrorState; }
34    }
35
36    public double Variance {
37      get {
38        return (n > 1) ? m_newS / (n - 1) : 0.0;
39      }
40    }
41
42    private OnlineCalculatorError errorState;
43    public OnlineCalculatorError PopulationVarianceErrorState {
44      get { return errorState; }
45    }
46    public double PopulationVariance {
47      get {
48        return (n > 0) ? m_newS / n : 0.0;
49      }
50    }
51
52    public OnlineCalculatorError MeanErrorState {
53      get { return errorState; }
54    }
55    public double Mean {
56      get {
57        return (n > 0) ? m_newM : 0.0;
58      }
59    }
60
61    public int Count {
62      get { return n; }
63    }
64
65    public OnlineMeanAndVarianceCalculator() {
66      Reset();
67    }
68
69    private OnlineMeanAndVarianceCalculator(OnlineMeanAndVarianceCalculator other) : this() {
70      m_oldS = other.m_oldS;
71      m_oldM = other.m_oldM;
72      m_newS = other.m_newS;
73      m_newM = other.m_newM;
74      n = other.n;
75      errorState = other.errorState;
76      varianceErrorState = other.varianceErrorState;
77    }
78
79    public void Reset() {
80      n = 0;
81      errorState = OnlineCalculatorError.InsufficientElementsAdded;
82      varianceErrorState = OnlineCalculatorError.InsufficientElementsAdded;
83    }
84
85    public void Add(double x) {
86      if (double.IsNaN(x) || double.IsInfinity(x) || x > 1E13 || x < -1E13 || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
87        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
88        varianceErrorState = varianceErrorState | OnlineCalculatorError.InvalidValueAdded;
89      } else {
90        n++;
91        // See Knuth TAOCP vol 2, 3rd edition, page 232
92        if (n == 1) {
93          m_oldM = m_newM = x;
94          m_oldS = 0.0;
95          errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
96        } else {
97
98          varianceErrorState = varianceErrorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 2
99          m_newM = m_oldM + (x - m_oldM) / n;
100          m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
101
102          // set up for next iteration
103          m_oldM = m_newM;
104          m_oldS = m_newS;
105        }
106      }
107    }
108
109    public static void Calculate(IEnumerable<double> x, out double mean, out double variance, out OnlineCalculatorError meanErrorState, out OnlineCalculatorError varianceErrorState) {
110      OnlineMeanAndVarianceCalculator meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
111      foreach (double xi in x) {
112        meanAndVarianceCalculator.Add(xi);
113      }
114      mean = meanAndVarianceCalculator.Mean;
115      variance = meanAndVarianceCalculator.Variance;
116      meanErrorState = meanAndVarianceCalculator.MeanErrorState;
117      varianceErrorState = meanAndVarianceCalculator.VarianceErrorState;
118    }
119
120    // IDeepCloneable members
121    public object Clone() {
122      return new OnlineMeanAndVarianceCalculator(this);
123    }
124
125    public IDeepCloneable Clone(Cloner cloner) {
126      var clone = cloner.GetClone(this);
127      if (clone == null) {
128        clone = (IDeepCloneable)this.Clone();
129        cloner.RegisterClonedObject(this, clone);
130      }
131      return clone;
132    }
133  }
134}
Note: See TracBrowser for help on using the repository browser.