[7272] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[7272] | 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 |
|
---|
| 25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 26 | public class OnlineMeanErrorCalculator : IOnlineCalculator {
|
---|
| 27 |
|
---|
| 28 | private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
|
---|
| 29 | public double MeanError {
|
---|
| 30 | get { return meanAndVarianceCalculator.Mean; }
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | public OnlineMeanErrorCalculator() {
|
---|
| 34 | meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
| 35 | Reset();
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | #region IOnlineCalculator Members
|
---|
| 39 | public OnlineCalculatorError ErrorState {
|
---|
| 40 | get { return meanAndVarianceCalculator.MeanErrorState; }
|
---|
| 41 | }
|
---|
| 42 | public double Value {
|
---|
| 43 | get { return MeanError; }
|
---|
| 44 | }
|
---|
| 45 | public void Reset() {
|
---|
| 46 | meanAndVarianceCalculator.Reset();
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | public void Add(double original, double estimated) {
|
---|
| 50 | meanAndVarianceCalculator.Add(estimated - original);
|
---|
| 51 | }
|
---|
| 52 | #endregion
|
---|
| 53 |
|
---|
| 54 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
| 55 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 56 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
| 57 | OnlineMeanErrorCalculator meCalculator = new OnlineMeanErrorCalculator();
|
---|
| 58 |
|
---|
| 59 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
| 60 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 61 | double original = originalEnumerator.Current;
|
---|
| 62 | double estimated = estimatedEnumerator.Current;
|
---|
| 63 | meCalculator.Add(original, estimated);
|
---|
| 64 | if (meCalculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 68 | if (meCalculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 69 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
| 70 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
| 71 | } else {
|
---|
| 72 | errorState = meCalculator.ErrorState;
|
---|
| 73 | return meCalculator.MeanError;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 | }
|
---|