source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs @ 14295

Last change on this file since 14295 was 14295, checked in by bburlacu, 3 years ago

#2672: Fix typo bug in cloning constructor.

File size: 5.0 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    protected OnlineNormalizedMeanSquaredErrorCalculator(OnlineNormalizedMeanSquaredErrorCalculator other, Cloner cloner) {
46      meanSquaredErrorCalculator = (OnlineMeanAndVarianceCalculator)other.meanSquaredErrorCalculator.Clone(cloner);
47      originalVarianceCalculator = (OnlineMeanAndVarianceCalculator)other.originalVarianceCalculator.Clone(cloner);
48    }
49
50    #region IOnlineCalculator Members
51    public OnlineCalculatorError ErrorState {
52      get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; }
53    }
54    public double Value {
55      get { return NormalizedMeanSquaredError; }
56    }
57
58    public void Reset() {
59      meanSquaredErrorCalculator.Reset();
60      originalVarianceCalculator.Reset();
61    }
62
63    public void Add(double original, double estimated) {
64      // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
65      double error = estimated - original;
66      meanSquaredErrorCalculator.Add(error * error);
67      originalVarianceCalculator.Add(original);
68    }
69    #endregion
70
71    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
72      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
73      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
74      OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
75
76      //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded
77      if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
78        double original = originalEnumerator.Current;
79        double estimated = estimatedEnumerator.Current;
80        normalizedMSECalculator.Add(original, estimated);
81      }
82
83      // always move forward both enumerators (do not use short-circuit evaluation!)
84      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
85        double original = originalEnumerator.Current;
86        double estimated = estimatedEnumerator.Current;
87        normalizedMSECalculator.Add(original, estimated);
88        if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
89      }
90
91      // check if both enumerators are at the end to make sure both enumerations have the same length
92      if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
93           (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
94        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
95      } else {
96        errorState = normalizedMSECalculator.ErrorState;
97        return normalizedMSECalculator.NormalizedMeanSquaredError;
98      }
99    }
100
101    // IDeepCloneable members
102    public object Clone() {
103      var cloner = new Cloner();
104      return new OnlineNormalizedMeanSquaredErrorCalculator(this, cloner);
105    }
106
107    public IDeepCloneable Clone(Cloner cloner) {
108      var clone = cloner.GetClone(this);
109      if (clone == null) {
110        clone = new OnlineNormalizedMeanSquaredErrorCalculator(this, cloner);
111        cloner.RegisterClonedObject(this, clone);
112      }
113      return clone;
114    }
115  }
116}
Note: See TracBrowser for help on using the repository browser.