public static void CalculateThresholds(IClassificationProblemData problemData, IEnumerable<double> estimatedValues, IEnumerable<double> targetClassValues, out double[] classValues, out double[] thresholds) {
      var estimatedTargetValues = Enumerable.Zip(estimatedValues, targetClassValues, (e, t) => new { EstimatedValue = e, TargetValue = t }).ToList();
      double estimatedValuesRange = estimatedValues.Range();

      Dictionary<double, double> classMean = new Dictionary<double, double>();
      Dictionary<double, double> classStdDev = new Dictionary<double, double>();
      // calculate moments per class
      foreach (var group in estimatedTargetValues.GroupBy(p => p.TargetValue)) {
        IEnumerable<double> estimatedClassValues = group.Select(x => x.EstimatedValue);
        double classValue = group.Key;
        double mean, variance;
        OnlineCalculatorError meanErrorState, varianceErrorState;
        OnlineMeanAndVarianceCalculator.Calculate(estimatedClassValues, out mean, out variance, out meanErrorState, out varianceErrorState);

        if (meanErrorState == OnlineCalculatorError.None && varianceErrorState == OnlineCalculatorError.None) {
          classMean[classValue] = mean;
          classStdDev[classValue] = Math.Sqrt(variance);
        }
      }

      double[] originalClasses = classMean.Keys.OrderBy(x => x).ToArray();
      int nClasses = originalClasses.Length;
      List<double> thresholdList = new List<double>();
      for (int i = 0; i < nClasses - 1; i++) {
        for (int j = i + 1; j < nClasses; j++) {
          double x1, x2;
          double class0 = originalClasses[i];
          double class1 = originalClasses[j];
          // calculate all thresholds
          CalculateCutPoints(classMean[class0], classStdDev[class0], classMean[class1], classStdDev[class1], out x1, out x2);

          // if the two cut points are too close (for instance because the stdDev=0)
          // then move them by 0.1% of the range of estimated values
          if (x1.IsAlmost(x2)) {
            x1 -= 0.001 * estimatedValuesRange;
            x2 += 0.001 * estimatedValuesRange;
          }
          if (!double.IsInfinity(x1) && !thresholdList.Any(x => x.IsAlmost(x1))) thresholdList.Add(x1);
          if (!double.IsInfinity(x2) && !thresholdList.Any(x => x.IsAlmost(x2))) thresholdList.Add(x2);
        }
      }
      thresholdList.Sort();

      // add small value and large value for the calculation of most influential class in each thresholded section
      thresholdList.Insert(0, double.NegativeInfinity);
      thresholdList.Add(double.PositiveInfinity);


      // find the most likely class for the points between thresholds m
      List<double> filteredThresholds = new List<double>();
      List<double> filteredClassValues = new List<double>();
      for (int i = 0; i < thresholdList.Count - 1; i++) {
        // determine class with maximal density mass between the thresholds
        double maxDensity = DensityMass(thresholdList[i], thresholdList[i + 1], classMean[originalClasses[0]], classStdDev[originalClasses[0]]);
        double maxDensityClassValue = originalClasses[0];
        foreach (var classValue in originalClasses.Skip(1)) {
          double density = DensityMass(thresholdList[i], thresholdList[i + 1], classMean[classValue], classStdDev[classValue]);
          if (density > maxDensity) {
            maxDensity = density;
            maxDensityClassValue = classValue;
          }
        }
        if (maxDensity > double.NegativeInfinity &&
          (filteredClassValues.Count == 0 || !maxDensityClassValue.IsAlmost(filteredClassValues.Last()))) {
          filteredThresholds.Add(thresholdList[i]);
          filteredClassValues.Add(maxDensityClassValue);
        }
      }

      if (filteredThresholds.Count == 0 || !double.IsNegativeInfinity(filteredThresholds.First())) {
        // this happens if there are no thresholds (distributions for all classes are exactly the same)
        // or when the CDF up to the first threshold is zero
        // -> all samples should be classified as the class with the most observations
        // group observations by target class and select the class with largest count 
        double mostFrequentClass = targetClassValues.GroupBy(c => c)
                              .OrderBy(g => g.Count())
                              .Last().Key;
        filteredThresholds.Insert(0, double.NegativeInfinity);
        filteredClassValues.Insert(0, mostFrequentClass);
      }

      thresholds = filteredThresholds.ToArray();
      classValues = filteredClassValues.ToArray();
    }
Example #2
0
 protected override object ProcessSubgroup(IEnumerable<double> data)
 {
     return data != null && data.Any() ? data.Range() : 0;
 }