#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Collections;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis {
[StorableClass]
[Item("MedianThresholdCalculator", "")]
public class MedianThresholdCalculator : DiscriminantClassificationWeightCalculator {
public MedianThresholdCalculator()
: base() {
}
[StorableConstructor]
protected MedianThresholdCalculator(bool deserializing) : base(deserializing) { }
protected MedianThresholdCalculator(MedianThresholdCalculator original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new MedianThresholdCalculator(this, cloner);
}
protected double[] threshold;
protected double[] classValues;
protected override IEnumerable DiscriminantCalculateWeights(IEnumerable discriminantSolutions) {
classValues = discriminantSolutions.First().Model.ClassValues.ToArray();
var modelThresholds = discriminantSolutions.Select(x => x.Model.Thresholds.ToArray());
threshold = new double[modelThresholds.First().GetLength(0)];
for (int i = 0; i < modelThresholds.First().GetLength(0); i++) {
threshold[i] = GetMedian(modelThresholds.Select(x => x[i]).ToList());
}
return Enumerable.Repeat(1, discriminantSolutions.Count());
}
protected override double GetDiscriminantConfidence(IEnumerable solutions, int index, double estimatedClassValue, CheckPoint handler) {
Dataset dataset = solutions.First().ProblemData.Dataset;
IList values = solutions.Where(s => handler(s.ProblemData, index)).Select(s => s.Model.GetEstimatedValues(dataset, Enumerable.Repeat(index, 1)).First()).ToList();
if (values.Count <= 0)
return double.NaN;
double median = GetMedian(values);
return GetMedianConfidence(median, estimatedClassValue);
}
public override IEnumerable GetDiscriminantConfidence(IEnumerable solutions, IEnumerable indices, IEnumerable estimatedClassValue, CheckPoint handler) {
Dataset dataset = solutions.First().ProblemData.Dataset;
List indicesList = indices.ToList();
var solValues = solutions.ToDictionary(x => x, x => x.Model.GetEstimatedValues(dataset, indicesList).ToArray());
double[] confidences = new double[indices.Count()];
double[] estimatedClassValueArr = estimatedClassValue.ToArray();
for (int i = 0; i < indicesList.Count; i++) {
var values = solValues.Where(x => handler(x.Key.ProblemData, indicesList[i])).Select(x => x.Value[i]).ToList();
if (values.Count <= 0) {
confidences[i] = double.NaN;
} else {
double median = GetMedian(values);
confidences[i] = GetMedianConfidence(median, estimatedClassValueArr[i]);
}
}
return confidences;
}
protected double GetMedianConfidence(double median, double estimatedClassValue) {
for (int i = 0; i < classValues.Length; i++) {
if (estimatedClassValue.Equals(classValues[i])) {
//special case: median is higher than value of highest class
if (i == classValues.Length - 1 && median >= estimatedClassValue) {
return 1;
}
//special case: median is lower than value of lowest class
if (i == 0 && median < estimatedClassValue) {
return 1;
}
//special case: median is not between threshold of estimated class value
if ((i < classValues.Length - 1 && median >= threshold[i + 1]) || median <= threshold[i]) {
return 0;
}
double thresholdToClassDistance, thresholdToMedianValueDistance;
if (median >= classValues[i]) {
thresholdToClassDistance = threshold[i + 1] - classValues[i];
thresholdToMedianValueDistance = threshold[i + 1] - median;
} else {
thresholdToClassDistance = classValues[i] - threshold[i];
thresholdToMedianValueDistance = median - threshold[i];
}
return (1 / thresholdToClassDistance) * thresholdToMedianValueDistance;
}
}
return double.NaN;
}
protected double GetMedian(IList estimatedValues) {
int count = estimatedValues.Count;
if (count % 2 == 0)
return 0.5 * (estimatedValues[count / 2 - 1] + estimatedValues[count / 2]);
else
return estimatedValues[count / 2];
}
}
}