[8638] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8638] | 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 Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[9785] | 23 |
|
---|
[9764] | 24 | namespace HeuristicLab.Problems.DataAnalysis.Tests {
|
---|
[8638] | 25 |
|
---|
| 26 | [TestClass()]
|
---|
| 27 | public class ThresholdCalculatorsTest {
|
---|
| 28 | [TestMethod]
|
---|
[9785] | 29 | [TestCategory("Problems.DataAnalysis")]
|
---|
| 30 | [TestProperty("Time", "short")]
|
---|
[8638] | 31 | public void NormalDistributionCutPointsThresholdCalculatorTest() {
|
---|
| 32 |
|
---|
| 33 | {
|
---|
| 34 | // simple two-class case
|
---|
| 35 | double[] estimatedValues = new double[] { 1.0, 0.99, 1.01, 2.0, 1.99, 2.01 };
|
---|
| 36 | double[] targetClassValues = new double[] { 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
|
---|
| 37 | double[] classValues;
|
---|
| 38 | double[] thresholds;
|
---|
| 39 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 40 | out classValues, out thresholds);
|
---|
| 41 |
|
---|
| 42 | var expectedClassValues = new double[] { 0.0, 1.0 };
|
---|
| 43 | var expectedTresholds = new double[] { double.NegativeInfinity, 1.5 };
|
---|
| 44 |
|
---|
| 45 | AssertEqual(expectedClassValues, classValues);
|
---|
| 46 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | {
|
---|
| 50 | // switched classes two-class case
|
---|
| 51 | double[] estimatedValues = new double[] { 1.0, 0.99, 1.01, 2.0, 1.99, 2.01 };
|
---|
| 52 | double[] targetClassValues = new double[] { 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 };
|
---|
| 53 | double[] classValues;
|
---|
| 54 | double[] thresholds;
|
---|
| 55 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 56 | out classValues, out thresholds);
|
---|
| 57 |
|
---|
| 58 | var expectedClassValues = new double[] { 1.0, 0.0 };
|
---|
| 59 | var expectedTresholds = new double[] { double.NegativeInfinity, 1.5 };
|
---|
| 60 |
|
---|
| 61 | AssertEqual(expectedClassValues, classValues);
|
---|
| 62 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | {
|
---|
| 66 | // three-class case with permutated estimated values
|
---|
| 67 | double[] estimatedValues = new double[] { 1.0, 0.99, 1.01, 2.0, 1.99, 2.01, -1.0, -0.99, -1.01 };
|
---|
| 68 | double[] targetClassValues = new double[] { 2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
|
---|
| 69 | double[] classValues;
|
---|
| 70 | double[] thresholds;
|
---|
| 71 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 72 | out classValues, out thresholds);
|
---|
| 73 |
|
---|
| 74 | var expectedClassValues = new double[] { 1.0, 2.0, 0.0 };
|
---|
| 75 | var expectedTresholds = new double[] { double.NegativeInfinity, 0.0, 1.5 };
|
---|
| 76 |
|
---|
| 77 | AssertEqual(expectedClassValues, classValues);
|
---|
| 78 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | {
|
---|
| 82 | // constant output values for all classes
|
---|
[8917] | 83 | // most frequent class is 0
|
---|
| 84 | double[] estimatedValues = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 };
|
---|
| 85 | double[] targetClassValues = new double[] { 2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
|
---|
[8638] | 86 | double[] classValues;
|
---|
| 87 | double[] thresholds;
|
---|
| 88 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 89 | out classValues, out thresholds);
|
---|
| 90 |
|
---|
| 91 | var expectedClassValues = new double[] { 0.0 };
|
---|
| 92 | var expectedTresholds = new double[] { double.NegativeInfinity };
|
---|
| 93 |
|
---|
| 94 | AssertEqual(expectedClassValues, classValues);
|
---|
| 95 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | {
|
---|
| 99 | // constant output values for two of three classes
|
---|
| 100 | double[] estimatedValues = new double[] { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0, -0.99, -1.01 };
|
---|
| 101 | double[] targetClassValues = new double[] { 2.0, 2.0, 2.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
|
---|
| 102 | double[] classValues;
|
---|
| 103 | double[] thresholds;
|
---|
| 104 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 105 | out classValues, out thresholds);
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | var expectedClassValues = new double[] { 1.0, 0.0, 1.0 };
|
---|
| 109 | double range = 1.0 + 1.01;
|
---|
| 110 | var expectedTresholds = new double[] { double.NegativeInfinity, 1.0 - 0.001 * range, 1.0 + 0.001 * range };
|
---|
| 111 |
|
---|
| 112 | AssertEqual(expectedClassValues, classValues);
|
---|
| 113 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[8658] | 116 |
|
---|
| 117 | {
|
---|
| 118 | // normal operation
|
---|
| 119 | double[] estimatedValues = new double[]
|
---|
| 120 | {
|
---|
| 121 | 2.9937,
|
---|
| 122 | 2.9861,
|
---|
| 123 | 1.0202,
|
---|
| 124 | 0.9844,
|
---|
| 125 | 1.9912,
|
---|
| 126 | 1.9970,
|
---|
| 127 | 0.9776,
|
---|
| 128 | 0.9611,
|
---|
| 129 | 1.9882,
|
---|
| 130 | 1.9953,
|
---|
| 131 | 2.0147,
|
---|
| 132 | 2.0106,
|
---|
| 133 | 2.9949,
|
---|
| 134 | 0.9925,
|
---|
| 135 | 3.0050,
|
---|
| 136 | 1.9987,
|
---|
| 137 | 2.9973,
|
---|
| 138 | 1.0110,
|
---|
| 139 | 2.0160,
|
---|
| 140 | 2.9559,
|
---|
| 141 | 1.9943,
|
---|
| 142 | 2.9477,
|
---|
| 143 | 2.0158,
|
---|
| 144 | 2.0026,
|
---|
| 145 | 1.9837,
|
---|
| 146 | 3.0185,
|
---|
| 147 | };
|
---|
| 148 | double[] targetClassValues = new double[]
|
---|
| 149 | {
|
---|
| 150 | 3,
|
---|
| 151 | 3,
|
---|
| 152 | 1,
|
---|
| 153 | 1,
|
---|
| 154 | 2,
|
---|
| 155 | 2,
|
---|
| 156 | 1,
|
---|
| 157 | 1,
|
---|
| 158 | 2,
|
---|
| 159 | 2,
|
---|
| 160 | 2,
|
---|
| 161 | 2,
|
---|
| 162 | 3,
|
---|
| 163 | 1,
|
---|
| 164 | 3,
|
---|
| 165 | 2,
|
---|
| 166 | 3,
|
---|
| 167 | 1,
|
---|
| 168 | 2,
|
---|
| 169 | 3,
|
---|
| 170 | 2,
|
---|
| 171 | 3,
|
---|
| 172 | 2,
|
---|
| 173 | 2,
|
---|
| 174 | 2,
|
---|
| 175 | 3,
|
---|
| 176 | };
|
---|
| 177 |
|
---|
| 178 | double[] classValues;
|
---|
| 179 | double[] thresholds;
|
---|
| 180 | NormalDistributionCutPointsThresholdCalculator.CalculateThresholds(null, estimatedValues, targetClassValues,
|
---|
| 181 | out classValues, out thresholds);
|
---|
| 182 |
|
---|
| 183 |
|
---|
[8917] | 184 | var expectedClassValues = new double[] { 3.0, 1.0, 2.0, 3.0 };
|
---|
| 185 | var expectedTresholds = new double[] { double.NegativeInfinity, -18.36483129043598, 1.6574168546810319, 2.3148463106026012 };
|
---|
[8658] | 186 |
|
---|
| 187 | AssertEqual(expectedClassValues, classValues);
|
---|
| 188 | AssertEqual(expectedTresholds, thresholds);
|
---|
| 189 | }
|
---|
[8638] | 190 | }
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 | private static void AssertEqual(double[] expected, double[] actual) {
|
---|
| 194 | Assert.AreEqual(expected.Length, actual.Length);
|
---|
| 195 | for (int i = 0; i < expected.Length; i++)
|
---|
| 196 | Assert.AreEqual(expected[i], actual[i]);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|