Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.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: 5.5 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;
23using System.Collections.Generic;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator, IDeepCloneable {
28    private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
29    private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
30
31    public double NormalizedMeanSquaredError {
32      get {
33        double var = originalVarianceCalculator.PopulationVariance;
34        double m = meanSquaredErrorCalculator.Mean;
35        return var > 0 ? m / var : 0.0;
36      }
37    }
38
39    public OnlineNormalizedMeanSquaredErrorCalculator() {
40      meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
41      originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
42      Reset();
43    }
44
45    // private constructor used internally by the Clone() method
46    public OnlineNormalizedMeanSquaredErrorCalculator(OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator, OnlineMeanAndVarianceCalculator originalVarianceCalculator) {
47      this.meanSquaredErrorCalculator = meanSquaredErrorCalculator;
48      this.originalVarianceCalculator = originalVarianceCalculator;
49    }
50
51    #region IOnlineCalculator Members
52    public OnlineCalculatorError ErrorState {
53      get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; }
54    }
55    public double Value {
56      get { return NormalizedMeanSquaredError; }
57    }
58
59    public void Reset() {
60      meanSquaredErrorCalculator.Reset();
61      originalVarianceCalculator.Reset();
62    }
63
64    public void Add(double original, double estimated) {
65      // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
66      double error = estimated - original;
67      meanSquaredErrorCalculator.Add(error * error);
68      originalVarianceCalculator.Add(original);
69    }
70    #endregion
71
72    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
73      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
74      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
75      OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
76
77      //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded
78      if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
79        double original = originalEnumerator.Current;
80        double estimated = estimatedEnumerator.Current;
81        normalizedMSECalculator.Add(original, estimated);
82      }
83
84      // always move forward both enumerators (do not use short-circuit evaluation!)
85      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
86        double original = originalEnumerator.Current;
87        double estimated = estimatedEnumerator.Current;
88        normalizedMSECalculator.Add(original, estimated);
89        if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
90      }
91
92      // check if both enumerators are at the end to make sure both enumerations have the same length
93      if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
94           (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
95        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
96      } else {
97        errorState = normalizedMSECalculator.ErrorState;
98        return normalizedMSECalculator.NormalizedMeanSquaredError;
99      }
100    }
101
102    // IDeepCloneable members
103    public object Clone() {
104      var meanSquaredErrorCalculatorClone = (OnlineMeanAndVarianceCalculator)meanSquaredErrorCalculator.Clone();
105      var originalVarianceCalculatorClone = (OnlineMeanAndVarianceCalculator)originalVarianceCalculator.Clone();
106      return new OnlineNormalizedMeanSquaredErrorCalculator(meanSquaredErrorCalculatorClone, originalVarianceCalculatorClone);
107    }
108
109    public IDeepCloneable Clone(Cloner cloner) {
110      var clone = cloner.GetClone(this);
111      if (clone == null) {
112        var meanSquaredErrorCalculatorClone = (OnlineMeanAndVarianceCalculator)meanSquaredErrorCalculator.Clone(cloner);
113        var originalVarianceCalculatorClone = (OnlineMeanAndVarianceCalculator)originalVarianceCalculator.Clone(cloner);
114        clone = new OnlineNormalizedMeanSquaredErrorCalculator(meanSquaredErrorCalculatorClone, originalVarianceCalculatorClone);
115        cloner.RegisterClonedObject(this, clone);
116      }
117      return clone;
118    }
119  }
120}
Note: See TracBrowser for help on using the repository browser.