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