1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
26 | public class OnlineNormalizedMeanSquaredErrorEvaluator : IOnlineEvaluator {
|
---|
27 | private OnlineMeanAndVarianceCalculator meanSquaredErrorCalculator;
|
---|
28 | private OnlineMeanAndVarianceCalculator originalVarianceCalculator;
|
---|
29 |
|
---|
30 | public double NormalizedMeanSquaredError {
|
---|
31 | get {
|
---|
32 | double var = originalVarianceCalculator.Variance;
|
---|
33 | double m = meanSquaredErrorCalculator.Mean;
|
---|
34 | return var > 0 ? m / var : 0.0;
|
---|
35 | }
|
---|
36 | }
|
---|
37 |
|
---|
38 | public OnlineNormalizedMeanSquaredErrorEvaluator() {
|
---|
39 | meanSquaredErrorCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
40 | originalVarianceCalculator = new OnlineMeanAndVarianceCalculator();
|
---|
41 | Reset();
|
---|
42 | }
|
---|
43 |
|
---|
44 | #region IOnlineEvaluator Members
|
---|
45 | public double Value {
|
---|
46 | get { return NormalizedMeanSquaredError; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void Reset() {
|
---|
50 | meanSquaredErrorCalculator.Reset();
|
---|
51 | originalVarianceCalculator.Reset();
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Add(double original, double estimated) {
|
---|
55 | // no need to check for validity of values explicitly as it is checked in the meanAndVariance calculator anyway
|
---|
56 | double error = estimated - original;
|
---|
57 | meanSquaredErrorCalculator.Add(error * error);
|
---|
58 | originalVarianceCalculator.Add(original);
|
---|
59 | }
|
---|
60 | #endregion
|
---|
61 |
|
---|
62 | public static double Calculate(IEnumerable<double> first, IEnumerable<double> second) {
|
---|
63 | IEnumerator<double> firstEnumerator = first.GetEnumerator();
|
---|
64 | IEnumerator<double> secondEnumerator = second.GetEnumerator();
|
---|
65 | OnlineNormalizedMeanSquaredErrorEvaluator normalizedMSEEvaluator = new OnlineNormalizedMeanSquaredErrorEvaluator();
|
---|
66 |
|
---|
67 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
68 | while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) {
|
---|
69 | double estimated = secondEnumerator.Current;
|
---|
70 | double original = firstEnumerator.Current;
|
---|
71 | normalizedMSEEvaluator.Add(original, estimated);
|
---|
72 | }
|
---|
73 |
|
---|
74 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
75 | if (secondEnumerator.MoveNext() || firstEnumerator.MoveNext()) {
|
---|
76 | throw new ArgumentException("Number of elements in first and second enumeration doesn't match.");
|
---|
77 | } else {
|
---|
78 | return normalizedMSEEvaluator.NormalizedMeanSquaredError;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|