1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
26 | public class OnlineMeanAndVarianceCalculator : DeepCloneable {
|
---|
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 | protected OnlineMeanAndVarianceCalculator(OnlineMeanAndVarianceCalculator original, Cloner cloner = null)
|
---|
70 | : base(original, cloner) {
|
---|
71 | m_oldS = original.m_oldS;
|
---|
72 | m_oldM = original.m_oldM;
|
---|
73 | m_newS = original.m_newS;
|
---|
74 | m_newM = original.m_newM;
|
---|
75 | n = original.n;
|
---|
76 | errorState = original.errorState;
|
---|
77 | varianceErrorState = original.varianceErrorState;
|
---|
78 | }
|
---|
79 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
80 | return new OnlineMeanAndVarianceCalculator(this, cloner);
|
---|
81 | }
|
---|
82 |
|
---|
83 | public void Reset() {
|
---|
84 | n = 0;
|
---|
85 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
86 | varianceErrorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void Add(double x) {
|
---|
90 | if (double.IsNaN(x) || double.IsInfinity(x) || x > 1E13 || x < -1E13 || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
91 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
92 | varianceErrorState = varianceErrorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
93 | } else {
|
---|
94 | n++;
|
---|
95 | // See Knuth TAOCP vol 2, 3rd edition, page 232
|
---|
96 | if (n == 1) {
|
---|
97 | m_oldM = m_newM = x;
|
---|
98 | m_oldS = 0.0;
|
---|
99 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
100 | } else {
|
---|
101 |
|
---|
102 | varianceErrorState = varianceErrorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 2
|
---|
103 | m_newM = m_oldM + (x - m_oldM) / n;
|
---|
104 | m_newS = m_oldS + (x - m_oldM) * (x - m_newM);
|
---|
105 |
|
---|
106 | // set up for next iteration
|
---|
107 | m_oldM = m_newM;
|
---|
108 | m_oldS = m_newS;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static void Calculate(IEnumerable<double> x, out double mean, out double variance, out OnlineCalculatorError meanErrorState, out OnlineCalculatorError varianceErrorState) {
|
---|
114 | OnlineMeanAndVarianceCalculator meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
115 | foreach (double xi in x) {
|
---|
116 | meanAndVarianceCalculator.Add(xi);
|
---|
117 | }
|
---|
118 | mean = meanAndVarianceCalculator.Mean;
|
---|
119 | variance = meanAndVarianceCalculator.Variance;
|
---|
120 | meanErrorState = meanAndVarianceCalculator.MeanErrorState;
|
---|
121 | varianceErrorState = meanAndVarianceCalculator.VarianceErrorState;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|