Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14226 was 14226, checked in by gkronber, 8 years ago

#2649 adapted OnlineSharpeRatioCalculator and OnlineNormalizedMeanSquaredErrorCalculator to use PopulationVariance instead of Variance (minor difference for vectors with only a few elements)

File size: 4.1 KB
RevLine 
[4022]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 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
22using System;
[5507]23using System.Collections.Generic;
[4022]24
[5491]25namespace HeuristicLab.Problems.DataAnalysis {
[5942]26  public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator {
[4022]27    private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
28    private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
29
30    public double NormalizedMeanSquaredError {
31      get {
[14226]32        double var = originalVarianceCalculator.PopulationVariance;
[5746]33        double m = meanSquaredErrorCalculator.Mean;
34        return var > 0 ? m / var : 0.0;
[4022]35      }
36    }
37
[5942]38    public OnlineNormalizedMeanSquaredErrorCalculator() {
[4022]39      meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
40      originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
41      Reset();
42    }
43
[5942]44    #region IOnlineCalculator Members
45    public OnlineCalculatorError ErrorState {
[14226]46      get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.PopulationVarianceErrorState; }
[5894]47    }
[4022]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) {
[5746]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);
[4022]62    }
63    #endregion
[5507]64
[6961]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();
[5942]68      OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
[5507]69
[5962]70      //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded
[6961]71      if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
72        double original = originalEnumerator.Current;
73        double estimated = estimatedEnumerator.Current;
[5962]74        normalizedMSECalculator.Add(original, estimated);
75      }
76
[5564]77      // always move forward both enumerators (do not use short-circuit evaluation!)
[6961]78      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
[6964]79        double original = originalEnumerator.Current;
[6961]80        double estimated = estimatedEnumerator.Current;
[5942]81        normalizedMSECalculator.Add(original, estimated);
[5945]82        if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
[5507]83      }
84
[5564]85      // check if both enumerators are at the end to make sure both enumerations have the same length
[5945]86      if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
[6961]87           (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
[6964]88        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
[5507]89      } else {
[5942]90        errorState = normalizedMSECalculator.ErrorState;
91        return normalizedMSECalculator.NormalizedMeanSquaredError;
[5507]92      }
93    }
[4022]94  }
95}
Note: See TracBrowser for help on using the repository browser.