Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs @ 13397

Last change on this file since 13397 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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
22using System;
23using System.Collections.Generic;
24
25namespace 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}
Note: See TracBrowser for help on using the repository browser.