[12416] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[12416] | 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 | using HeuristicLab.Common;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16788] | 27 | public class OnlineWeightedClassificationMeanSquaredErrorCalculator : IOnlineCalculator {
|
---|
[12416] | 28 |
|
---|
| 29 | private double sse;
|
---|
| 30 | private int n;
|
---|
| 31 | public double WeightedResidualsMeanSquaredError {
|
---|
| 32 | get {
|
---|
| 33 | return n > 0 ? sse / n : 0.0;
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | public double PositiveClassValue { get; private set; }
|
---|
[12448] | 38 | public double ClassValuesMax { get; private set; }
|
---|
| 39 | public double ClassValuesMin { get; private set; }
|
---|
| 40 | public double DefiniteResidualsWeight { get; private set; }
|
---|
[12416] | 41 | public double PositiveClassResidualsWeight { get; private set; }
|
---|
[12448] | 42 | public double NegativeClassesResidualsWeight { get; private set; }
|
---|
[12416] | 43 |
|
---|
[16788] | 44 | public OnlineWeightedClassificationMeanSquaredErrorCalculator(double positiveClassValue, double classValuesMax, double classValuesMin,
|
---|
[12448] | 45 | double definiteResidualsWeight, double positiveClassResidualsWeight, double negativeClassesResidualsWeight) {
|
---|
[12416] | 46 | PositiveClassValue = positiveClassValue;
|
---|
[12448] | 47 | ClassValuesMax = classValuesMax;
|
---|
| 48 | ClassValuesMin = classValuesMin;
|
---|
| 49 | DefiniteResidualsWeight = definiteResidualsWeight;
|
---|
[12416] | 50 | PositiveClassResidualsWeight = positiveClassResidualsWeight;
|
---|
[12448] | 51 | NegativeClassesResidualsWeight = negativeClassesResidualsWeight;
|
---|
[12416] | 52 | Reset();
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | #region IOnlineCalculator Members
|
---|
| 56 | private OnlineCalculatorError errorState;
|
---|
| 57 | public OnlineCalculatorError ErrorState {
|
---|
| 58 | get { return errorState; }
|
---|
| 59 | }
|
---|
| 60 | public double Value {
|
---|
| 61 | get { return WeightedResidualsMeanSquaredError; }
|
---|
| 62 | }
|
---|
| 63 | public void Reset() {
|
---|
| 64 | n = 0;
|
---|
| 65 | sse = 0.0;
|
---|
| 66 | errorState = OnlineCalculatorError.InsufficientElementsAdded;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public void Add(double original, double estimated) {
|
---|
| 70 | if (double.IsNaN(estimated) || double.IsInfinity(estimated) ||
|
---|
| 71 | double.IsNaN(original) || double.IsInfinity(original) || (errorState & OnlineCalculatorError.InvalidValueAdded) > 0) {
|
---|
| 72 | errorState = errorState | OnlineCalculatorError.InvalidValueAdded;
|
---|
| 73 | } else {
|
---|
| 74 | double error = estimated - original;
|
---|
| 75 | double weight;
|
---|
| 76 | //apply weight
|
---|
[12448] | 77 | if (estimated > ClassValuesMax || estimated < ClassValuesMin) {
|
---|
| 78 | weight = DefiniteResidualsWeight;
|
---|
| 79 | } else if (original.IsAlmost(PositiveClassValue)) {
|
---|
[12416] | 80 | weight = PositiveClassResidualsWeight;
|
---|
[12448] | 81 | } else {
|
---|
| 82 | weight = NegativeClassesResidualsWeight;
|
---|
[12416] | 83 | }
|
---|
[12449] | 84 | sse += error * error * weight;
|
---|
[12416] | 85 | n++;
|
---|
| 86 | errorState = errorState & (~OnlineCalculatorError.InsufficientElementsAdded); // n >= 1
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | #endregion
|
---|
| 90 |
|
---|
[12448] | 91 | public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, double positiveClassValue, double classValuesMax, double classValuesMin,
|
---|
| 92 | double definiteResidualsWeight, double positiveClassResidualsWeight, double negativeClassesResidualsWeight, out OnlineCalculatorError errorState) {
|
---|
[12416] | 93 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 94 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
[16788] | 95 | OnlineWeightedClassificationMeanSquaredErrorCalculator calculator = new OnlineWeightedClassificationMeanSquaredErrorCalculator(positiveClassValue, classValuesMax, classValuesMin, definiteResidualsWeight, positiveClassResidualsWeight, negativeClassesResidualsWeight);
|
---|
[12416] | 96 |
|
---|
| 97 | // always move forward both enumerators (do not use short-circuit evaluation!)
|
---|
| 98 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 99 | double original = originalEnumerator.Current;
|
---|
| 100 | double estimated = estimatedEnumerator.Current;
|
---|
| 101 | calculator.Add(original, estimated);
|
---|
| 102 | if (calculator.ErrorState != OnlineCalculatorError.None) break;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | // check if both enumerators are at the end to make sure both enumerations have the same length
|
---|
| 106 | if (calculator.ErrorState == OnlineCalculatorError.None &&
|
---|
| 107 | (estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
|
---|
| 108 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
| 109 | } else {
|
---|
| 110 | errorState = calculator.ErrorState;
|
---|
| 111 | return calculator.WeightedResidualsMeanSquaredError;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|