#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.Common;
namespace HeuristicLab.Problems.DataAnalysis {
public class QualityCalculator //: IOnlineCalculator
{
public QualityCalculator(double positiveClassValue) {
this.positiveClassValue = positiveClassValue;
Reset();
}
private double positiveClassValue;
private int truePositive, falsePositive, trueNegative, falseNegative;
public double TruePositiveRate {
get {
double divisor = truePositive + falseNegative;
return divisor.IsAlmost(0) ? double.NaN : truePositive / divisor;
}
}
public double TrueNegativeRate {
get {
double divisor = falsePositive + trueNegative;
return divisor.IsAlmost(0) ? double.NaN : trueNegative / divisor;
}
}
public double PositivePredictiveValue {
get {
double divisor = truePositive + falsePositive;
return divisor.IsAlmost(0) ? double.NaN : truePositive / divisor;
}
}
public double NegativePredictiveValue {
get {
double divisor = trueNegative + falseNegative;
return divisor.IsAlmost(0) ? double.NaN : trueNegative / divisor;
}
}
public double FalsePositiveRate {
get {
double divisor = falsePositive + trueNegative;
return divisor.IsAlmost(0) ? double.NaN : falsePositive / divisor;
}
}
public double FalseDiscoveryRate {
get {
double divisor = falsePositive + truePositive;
return divisor.IsAlmost(0) ? double.NaN : falsePositive / divisor;
}
}
//#region IOnlineCalculator Members
private OnlineCalculatorError errorState;
public OnlineCalculatorError ErrorState {
get { return errorState; }
}
/*
public double Value
{
get { return FalsePositiveRate; }
}*/
public void Reset() {
truePositive = 0;
falseNegative = 0;
trueNegative = 0;
falseNegative = 0;
errorState = OnlineCalculatorError.InsufficientElementsAdded;
}
public void Add(double original, double estimated) {
// ignore cases where original is NaN completely
if (!double.IsNaN(original)) {
if (original.IsAlmost(positiveClassValue) || estimated.IsAlmost(positiveClassValue)) //positive class/positive estimation
{
if (estimated.IsAlmost(original)) {
truePositive++;
} else {
if (estimated.IsAlmost(positiveClassValue))
falsePositive++;
else
falseNegative++;
}
} else { //negative class/negative estimation
trueNegative++;
}
errorState = OnlineCalculatorError.None; // number of (non-NaN) samples >= 1
}
}
//#endregion
public void Calculate(IEnumerable originalValues, IEnumerable estimatedValues, out OnlineCalculatorError errorState) {
IEnumerator originalEnumerator = originalValues.GetEnumerator();
IEnumerator estimatedEnumerator = estimatedValues.GetEnumerator();
// always move forward both enumerators (do not use short-circuit evaluation!)
while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
double original = originalEnumerator.Current;
double estimated = estimatedEnumerator.Current;
Add(original, estimated);
if (ErrorState != OnlineCalculatorError.None) break;
}
// check if both enumerators are at the end to make sure both enumerations have the same length
if (ErrorState == OnlineCalculatorError.None &&
(estimatedEnumerator.MoveNext() || originalEnumerator.MoveNext())) {
throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
} else {
errorState = ErrorState;
}
}
}
}