Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Classification-Extensions/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs @ 11603

Last change on this file since 11603 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 OnlineBoundedMeanSquaredErrorCalculator : IOnlineCalculator {
27
28    private double errorSum;
29    private int n;
30    public double BoundedMeanSquaredError {
31      get {
32        return n > 0 ? errorSum / n : 0.0;
33      }
34    }
35
36    public double LowerBound { get; private set; }
37    public double UpperBound { get; private set; }
38
39
40    public OnlineBoundedMeanSquaredErrorCalculator(double lowerBound, double upperbound) {
41      LowerBound = lowerBound;
42      UpperBound = upperbound;
43      Reset();
44    }
45
46    #region IOnlineCalculator Members
47    private OnlineCalculatorError errorState;
48    public OnlineCalculatorError ErrorState {
49      get { return errorState; }
50    }
51    public double Value {
52      get { return BoundedMeanSquaredError; }
53    }
54    public void Reset() {
55      n = 0;
56      errorSum = 0.0;
57      errorState = OnlineCalculatorError.InsufficientElementsAdded;
58    }
59
60    public void Add(double original, double estimated) {
61      if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
62          double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
63        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
64      } else {
65        double error = estimated - original;
66        if (estimated < LowerBound || estimated > UpperBound)
67          errorSum += Math.Abs(error);
68        else
69          errorSum += error * error;
70        n++;
71        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
72      }
73    }
74    #endregion
75
76    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, double lowerBound, double upperBound, out OnlineCalculatorError errorState) {
77      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
78      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
79      OnlineBoundedMeanSquaredErrorCalculator boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowerBound, upperBound);
80
81      // always move forward both enumerators (do not use short-circuit evaluation!)
82      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
83        double original = originalEnumerator.Current;
84        double estimated = estimatedEnumerator.Current;
85        boundedMseCalculator.Add(original, estimated);
86        if (boundedMseCalculator.ErrorState != OnlineCalculatorError.None) break;
87      }
88
89      // check if both enumerators are at the end to make sure both enumerations have the same length
90      if (boundedMseCalculator.ErrorState == OnlineCalculatorError.None &&
91         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
92        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
93      } else {
94        errorState = boundedMseCalculator.ErrorState;
95        return boundedMseCalculator.BoundedMeanSquaredError;
96      }
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.