[4022] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[5445] | 3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4022] | 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;
|
---|
[5507] | 23 | using System.Collections.Generic;
|
---|
[4022] | 24 |
|
---|
[5491] | 25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[5942] | 26 | public class OnlineNormalizedMeanSquaredErrorCalculator : IOnlineCalculator {
|
---|
[4022] | 27 | private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
|
---|
| 28 | private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
|
---|
| 29 |
|
---|
| 30 | public double NormalizedMeanSquaredError {
|
---|
| 31 | get {
|
---|
[5746] | 32 | double var = originalVarianceCalculator.Variance;
|
---|
| 33 | double m = meanSquaredErrorCalculator.Mean;
|
---|
| 34 | return var > 0 ? m / var : 0.0;
|
---|
[4022] | 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[5942] | 38 | public OnlineNormalizedMeanSquaredErrorCalculator() {
|
---|
[4022] | 39 | meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 40 | originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 41 | Reset();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[5942] | 44 | #region IOnlineCalculator Members
|
---|
| 45 | public OnlineCalculatorError ErrorState {
|
---|
[5894] | 46 | get { return meanSquaredErrorCalculator.MeanErrorState | originalVarianceCalculator.VarianceErrorState; }
|
---|
| 47 | }
|
---|
[4022] | 48 | public double Value {
|
---|
| 49 | get { return NormalizedMeanSquaredError; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public void Reset() {
|
---|
| 53 | meanSquaredErrorCalculator.Reset();
|
---|
| 54 | originalVarianceCalculator.Reset();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public void Add(double original, double estimated) {
|
---|
[5746] | 58 | // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
|
---|
| 59 | double error = estimated - original;
|
---|
| 60 | meanSquaredErrorCalculator.Add(error * error);
|
---|
| 61 | originalVarianceCalculator.Add(original);
|
---|
[4022] | 62 | }
|
---|
| 63 | #endregion
|
---|
[5507] | 64 |
|
---|
[7085] | 65 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
| 66 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 67 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
[5942] | 68 | OnlineNormalizedMeanSquaredErrorCalculator normalizedMSECalculator = new OnlineNormalizedMeanSquaredErrorCalculator();
|
---|
[5507] | 69 |
|
---|
[5962] | 70 | //needed because otherwise the normalizedMSECalculator is in ErrorState.InsufficientValuesAdded
|
---|
[7085] | 71 | if (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 72 | double original = originalEnumerator.Current;
|
---|
| 73 | double estimated = estimatedEnumerator.Current;
|
---|
[5962] | 74 | normalizedMSECalculator.Add(original, estimated);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[5564] | 77 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
[7085] | 78 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 79 | double original = originalEnumerator.Current;
|
---|
| 80 | double estimated = estimatedEnumerator.Current;
|
---|
[5942] | 81 | normalizedMSECalculator.Add(original, estimated);
|
---|
[5945] | 82 | if (normalizedMSECalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
[5507] | 83 | }
|
---|
| 84 |
|
---|
[5564] | 85 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
[5945] | 86 | if (normalizedMSECalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
[7085] | 87 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
| 88 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumeration doesn't match.");
|
---|
[5507] | 89 | } else {
|
---|
[5942] | 90 | errorState = normalizedMSECalculator.ErrorState;
|
---|
| 91 | return normalizedMSECalculator.NormalizedMeanSquaredError;
|
---|
[5507] | 92 | }
|
---|
| 93 | }
|
---|
[4022] | 94 | }
|
---|
| 95 | }
|
---|