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