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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
27 | public class OnlineBoundedMeanSquaredErrorCalculator : DeepCloneable, IOnlineCalculator {
|
---|
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)
|
---|
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 |
|
---|
59 | #region IOnlineCalculator Members
|
---|
60 | private OnlineCalculatorError errorState;
|
---|
61 | public OnlineCalculatorError ErrorState {
|
---|
62 | get { return errorState; }
|
---|
63 | }
|
---|
64 | public double Value {
|
---|
65 | get { return BoundedMeanSquaredError; }
|
---|
66 | }
|
---|
67 | public void Reset() {
|
---|
68 | n = 0;
|
---|
69 | errorSum = 0.0;
|
---|
70 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void Add(double original, double estimated) {
|
---|
74 | if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
|
---|
75 | double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
76 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
77 | } else {
|
---|
78 | double error = estimated - original;
|
---|
79 | if (estimated < LowerBound || estimated > UpperBound)
|
---|
80 | errorSum += Math.Abs(error);
|
---|
81 | else
|
---|
82 | errorSum += error * error;
|
---|
83 | n++;
|
---|
84 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
85 | }
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, double lowerBound, double upperBound, out OnlineCalculatorError errorState) {
|
---|
90 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
91 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
92 | OnlineBoundedMeanSquaredErrorCalculator boundedMseCalculator = new OnlineBoundedMeanSquaredErrorCalculator(lowerBound, upperBound);
|
---|
93 |
|
---|
94 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
95 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
96 | double original = originalEnumerator.Current;
|
---|
97 | double estimated = estimatedEnumerator.Current;
|
---|
98 | boundedMseCalculator.Add(original, estimated);
|
---|
99 | if (boundedMseCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
100 | }
|
---|
101 |
|
---|
102 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
103 | if (boundedMseCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
104 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
105 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
106 | } else {
|
---|
107 | errorState = boundedMseCalculator.ErrorState;
|
---|
108 | return boundedMseCalculator.BoundedMeanSquaredError;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|