Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12492 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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;
24
25namespace HeuristicLab.Problems.DataAnalysis {
26  public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator {
27    private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
28    private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
29
30    public double NormalizedMeanSquaredError {
31      get {
32        double var = originalVarianceCalculator.Variance;
33        double m = meanSquaredErrorCalculator.Mean;
34        return var > 0 ? m / var : 0.0;
35      }
36    }
37
38    public OnlineNormalizedMeanSquaredErrorCalculator() {
39      meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
40      originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
41      Reset();
42    }
43
44    #region IOnlineCalculator Members
45    public OnlineCalculatorError ErrorState {
46      get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.VarianceErrorState; }
47    }
48    public double Value {
49      get { return NormalizedMeanSquaredError; }
50    }
51
52    public void Reset() {
53      meanSquaredErrorCalculator.Reset();
54      originalVarianceCalculator.Reset();
55    }
56
57    public void Add(double original, double estimated) {
58      // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
59      double error = estimated - original;
60      meanSquaredErrorCalculator.Add(error * error);
61      originalVarianceCalculator.Add(original);
62    }
63    #endregion
64
65    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
66      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
67      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
68      OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
69
70      //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded
71      if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
72        double original = originalEnumerator.Current;
73        double estimated = estimatedEnumerator.Current;
74        normalizedMSECalculator.Add(original, estimated);
75      }
76
77      // always move forward both enumerators (do not use short-circuit evaluation!)
78      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
79        double original = originalEnumerator.Current;
80        double estimated = estimatedEnumerator.Current;
81        normalizedMSECalculator.Add(original, estimated);
82        if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
83      }
84
85      // check if both enumerators are at the end to make sure both enumerations have the same length
86      if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
87           (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
88        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
89      } else {
90        errorState = normalizedMSECalculator.ErrorState;
91        return normalizedMSECalculator.NormalizedMeanSquaredError;
92      }
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.