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