[3996] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 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 |
|
---|
| 22 | using System;
|
---|
[5500] | 23 | using System.Collections.Generic;
|
---|
[14801] | 24 | using HeuristicLab.Common;
|
---|
[3996] | 25 |
|
---|
[5491] | 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[14801] | 27 | public class OnlineBoundedMeanSquaredErrorCalculator : DeepCloneable, IOnlineCalculator {
|
---|
[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 |
|
---|
[14801] | 41 | public OnlineBoundedMeanSquaredErrorCalculator(double lowerBound, double upperBound) {
|
---|
[8664] | 42 | LowerBound = lowerBound;
|
---|
[14801] | 43 | UpperBound = upperBound;
|
---|
[3996] | 44 | Reset();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[14801] | 47 | protected OnlineBoundedMeanSquaredErrorCalculator(OnlineBoundedMeanSquaredErrorCalculator original, Cloner cloner)
|
---|
| 48 | : base(original, cloner) {
|
---|
| 49 | LowerBound = original.LowerBound;
|
---|
| 50 | UpperBound = original.UpperBound;
|
---|
| 51 | n = original.n;
|
---|
| 52 | errorSum = original.errorSum;
|
---|
| 53 | errorState = original.ErrorState;
|
---|
| 54 | }
|
---|
| 55 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 56 | return new OnlineBoundedMeanSquaredErrorCalculator(this, cloner);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[5942] | 59 | #region IOnlineCalculator Members
|
---|
| 60 | private OnlineCalculatorError errorState;
|
---|
| 61 | public OnlineCalculatorError ErrorState {
|
---|
[5894] | 62 | get { return errorState; }
|
---|
| 63 | }
|
---|
[4022] | 64 | public double Value {
|
---|
[8664] | 65 | get { return BoundedMeanSquaredError; }
|
---|
[4022] | 66 | }
|
---|
[3996] | 67 | public void Reset() {
|
---|
| 68 | n = 0;
|
---|
[8664] | 69 | errorSum = 0.0;
|
---|
[5942] | 70 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
[3996] | 71 | }
|
---|
| 72 |
|
---|
| 73 | public void Add(double original, double estimated) {
|
---|
| 74 | if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
|
---|
[5942] | 75 | double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
| 76 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
[5904] | 77 | } else {
|
---|
[3996] | 78 | double error = estimated - original;
|
---|
[8664] | 79 | if (estimated < LowerBound || estimated > UpperBound)
|
---|
| 80 | errorSum += Math.Abs(error);
|
---|
| 81 | else
|
---|
| 82 | errorSum += error * error;
|
---|
[3996] | 83 | n++;
|
---|
[5942] | 84 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
[3996] | 85 | }
|
---|
| 86 | }
|
---|
| 87 | #endregion
|
---|
[5500] | 88 |
|
---|
[8664] | 89 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, double lowerBound, double upperBound, out OnlineCalculatorError errorState) {
|
---|
[6961] | 90 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 91 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
[8664] | 92 | OnlineBoundedMeanSquaredErrorCalculator boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowerBound, upperBound);
|
---|
[5500] | 93 |
|
---|
[5564] | 94 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
[6961] | 95 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 96 | double original = originalEnumerator.Current;
|
---|
| 97 | double estimated = estimatedEnumerator.Current;
|
---|
[8664] | 98 | boundedMseCalculator.Add(original, estimated);
|
---|
| 99 | if (boundedMseCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[5500] | 100 | }
|
---|
| 101 |
|
---|
[5564] | 102 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
[8664] | 103 | if (boundedMseCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[6961] | 104 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
[6964] | 105 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
[5500] | 106 | } else {
|
---|
[8664] | 107 | errorState = boundedMseCalculator.ErrorState;
|
---|
| 108 | return boundedMseCalculator.BoundedMeanSquaredError;
|
---|
[5500] | 109 | }
|
---|
| 110 | }
|
---|
[3996] | 111 | }
|
---|
| 112 | }
|
---|