Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineNormalizedMeanSquaredErrorCalculator.cs @ 17097

Last change on this file since 17097 was 17097, checked in by mkommend, 5 years ago

#2520: Merged 16565 - 16579 into stable.

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