Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2672: Implement online calculator cloning.

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