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