[4022] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4022] | 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 |
|
---|
[5530] | 22 | using System.Collections.Generic;
|
---|
[14801] | 23 | using HeuristicLab.Common;
|
---|
[4022] | 24 |
|
---|
[5491] | 25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[14801] | 26 | public class OnlineMeanAndVarianceCalculator : DeepCloneable {
|
---|
[4022] | 27 |
|
---|
| 28 | private double m_oldM, m_newM, m_oldS, m_newS;
|
---|
| 29 | private int n;
|
---|
| 30 |
|
---|
[5942] | 31 | private OnlineCalculatorError varianceErrorState;
|
---|
| 32 | public OnlineCalculatorError VarianceErrorState {
|
---|
[5894] | 33 | get { return varianceErrorState; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[4022] | 36 | public double Variance {
|
---|
| 37 | get {
|
---|
| 38 | return (n > 1) ? m_newS / (n - 1) : 0.0;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[5942] | 42 | private OnlineCalculatorError errorState;
|
---|
| 43 | public OnlineCalculatorError PopulationVarianceErrorState {
|
---|
[5894] | 44 | get { return errorState; }
|
---|
| 45 | }
|
---|
[4122] | 46 | public double PopulationVariance {
|
---|
| 47 | get {
|
---|
| 48 | return (n > 0) ? m_newS / n : 0.0;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[5942] | 52 | public OnlineCalculatorError MeanErrorState {
|
---|
[5894] | 53 | get { return errorState; }
|
---|
| 54 | }
|
---|
[4022] | 55 | public double Mean {
|
---|
| 56 | get {
|
---|
| 57 | return (n > 0) ? m_newM : 0.0;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[4044] | 61 | public int Count {
|
---|
| 62 | get { return n; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[4022] | 65 | public OnlineMeanAndVarianceCalculator() {
|
---|
| 66 | Reset();
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[14801] | 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 |
|
---|
[4022] | 83 | public void Reset() {
|
---|
| 84 | n = 0;
|
---|
[5942] | 85 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 86 | varianceErrorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
[4022] | 87 | }
|
---|
| 88 |
|
---|
| 89 | public void Add(double x) {
|
---|
[8966] | 90 | if (double.IsNaN(x) || double.IsInfinity(x) || x > 1E13 || x < -1E13 || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
[5942] | 91 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[14303] | 92 | varianceErrorState = varianceErrorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[5904] | 93 | } else {
|
---|
[4022] | 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;
|
---|
[5942] | 99 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
[4022] | 100 | } else {
|
---|
[6095] | 101 |
|
---|
[5942] | 102 | varianceErrorState = varianceErrorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 2
|
---|
[4022] | 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 | }
|
---|
[5530] | 112 |
|
---|
[5942] | 113 | public static void Calculate(IEnumerable<double> x, out double mean, out double variance, out OnlineCalculatorError meanErrorState, out OnlineCalculatorError varianceErrorState) {
|
---|
[5530] | 114 | OnlineMeanAndVarianceCalculator meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 115 | foreach (double xi in x) {
|
---|
| 116 | meanAndVarianceCalculator.Add(xi);
|
---|
| 117 | }
|
---|
| 118 | mean = meanAndVarianceCalculator.Mean;
|
---|
| 119 | variance = meanAndVarianceCalculator.Variance;
|
---|
[5894] | 120 | meanErrorState = meanAndVarianceCalculator.MeanErrorState;
|
---|
| 121 | varianceErrorState = meanAndVarianceCalculator.VarianceErrorState;
|
---|
[5530] | 122 | }
|
---|
[4022] | 123 | }
|
---|
| 124 | }
|
---|