Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/OnlineMeanErrorCalculator.cs @ 16752

Last change on this file since 16752 was 15584, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers on stable

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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;
24using HeuristicLab.Common;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public class OnlineMeanErrorCalculator : DeepCloneable, IOnlineCalculator {
28
29    private readonly OnlineMeanAndVarianceCalculator meanAndVarianceCalculator;
30    public double MeanError {
31      get { return meanAndVarianceCalculator.Mean; }
32    }
33
34    public OnlineMeanErrorCalculator() {
35      meanAndVarianceCalculator = new OnlineMeanAndVarianceCalculator();
36      Reset();
37    }
38
39    protected OnlineMeanErrorCalculator(OnlineMeanErrorCalculator original, Cloner cloner)
40      : base(original, cloner) {
41      meanAndVarianceCalculator = cloner.Clone(original.meanAndVarianceCalculator);
42    }
43    public override IDeepCloneable Clone(Cloner cloner) {
44      return new OnlineMeanErrorCalculator(this, cloner);
45    }
46
47    #region IOnlineCalculator Members
48    public OnlineCalculatorError ErrorState {
49      get { return meanAndVarianceCalculator.MeanErrorState; }
50    }
51    public double Value {
52      get { return MeanError; }
53    }
54    public void Reset() {
55      meanAndVarianceCalculator.Reset();
56    }
57
58    public void Add(double original, double estimated) {
59      meanAndVarianceCalculator.Add(estimated - original);
60    }
61    #endregion
62
63    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
64      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
65      IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
66      OnlineMeanErrorCalculator meCalculator = new OnlineMeanErrorCalculator();
67
68      // always move forward both enumerators (do not use short-circuit evaluation!)
69      while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
70        double original = originalEnumerator.Current;
71        double estimated = estimatedEnumerator.Current;
72        meCalculator.Add(original, estimated);
73        if (meCalculator.ErrorState != OnlineCalculatorError.None) break;
74      }
75
76      // check if both enumerators are at the end to make sure both enumerations have the same length
77      if (meCalculator.ErrorState == OnlineCalculatorError.None &&
78         (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
79        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
80      } else {
81        errorState = meCalculator.ErrorState;
82        return meCalculator.MeanError;
83      }
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.