Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineBoundedMeanSquaredErrorCalculator.cs @ 14294

Last change on this file since 14294 was 14294, checked in by bburlacu, 8 years ago

#2672: Add cloning constructors.

File size: 4.7 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 OnlineBoundedMeanSquaredErrorCalculator : IOnlineCalculator, IDeepCloneable {
28
29    private double errorSum;
30    private int n;
31    public double BoundedMeanSquaredError {
32      get {
33        return n > 0 ? errorSum / n : 0.0;
34      }
35    }
36
37    public double LowerBound { get; private set; }
38    public double UpperBound { get; private set; }
39
40
41    public OnlineBoundedMeanSquaredErrorCalculator(double lowerBound, double upperBound) {
42      LowerBound = lowerBound;
43      UpperBound = upperBound;
44      Reset();
45    }
46
47    protected OnlineBoundedMeanSquaredErrorCalculator(OnlineBoundedMeanSquaredErrorCalculator original, Cloner cloner = null) {
48      LowerBound = original.LowerBound;
49      UpperBound = original.UpperBound;
50      n = original.n;
51      errorSum = original.errorSum;
52      errorState = original.ErrorState;
53    }
54
55    #region IOnlineCalculator Members
56    private OnlineCalculatorError errorState;
57    public OnlineCalculatorError ErrorState {
58      get { return errorState; }
59    }
60    public double Value {
61      get { return BoundedMeanSquaredError; }
62    }
63    public void Reset() {
64      n = 0;
65      errorSum = 0.0;
66      errorState = OnlineCalculatorError.InsufficientElementsAdded;
67    }
68
69    public void Add(double original, double estimated) {
70      if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
71          double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
72        errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
73      } else {
74        double error = estimated - original;
75        if (estimated < LowerBound || estimated > UpperBound)
76          errorSum += Math.Abs(error);
77        else
78          errorSum += error * error;
79        n++;
80        errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded);        // n >= 1
81      }
82    }
83    #endregion
84
85    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, double lowerBound, double upperBound, out OnlineCalculatorError errorState) {
86      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
87      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
88      OnlineBoundedMeanSquaredErrorCalculator boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowerBound, upperBound);
89
90      // always move forward both enumerators (do not use short-circuit evaluation!)
91      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
92        double original = originalEnumerator.Current;
93        double estimated = estimatedEnumerator.Current;
94        boundedMseCalculator.Add(original, estimated);
95        if (boundedMseCalculator.ErrorState != OnlineCalculatorError.None) break;
96      }
97
98      // check if both enumerators are at the end to make sure both enumerations have the same length
99      if (boundedMseCalculator.ErrorState == OnlineCalculatorError.None &&
100         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
101        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
102      } else {
103        errorState = boundedMseCalculator.ErrorState;
104        return boundedMseCalculator.BoundedMeanSquaredError;
105      }
106    }
107
108    // IDeepCloneable interface members
109    public object Clone() {
110      return new OnlineBoundedMeanSquaredErrorCalculator(this);
111    }
112
113    public IDeepCloneable Clone(Cloner cloner) {
114      var clone = cloner.GetClone(this);
115      if (clone == null) {
116        clone = new OnlineBoundedMeanSquaredErrorCalculator(this);
117        cloner.RegisterClonedObject(this, clone);
118      }
119      return clone;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.