1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.stat.descriptive.rank;
23
24 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.LEGACY;
25 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_1;
26 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_2;
27 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_3;
28 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_4;
29 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_5;
30 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_6;
31 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_7;
32 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_8;
33 import static org.hipparchus.stat.descriptive.rank.Percentile.EstimationType.R_9;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertTrue;
36
37 import org.hipparchus.stat.descriptive.UnivariateStatistic;
38 import org.hipparchus.stat.descriptive.UnivariateStatisticAbstractTest;
39 import org.hipparchus.stat.descriptive.rank.Percentile.EstimationType;
40 import org.hipparchus.stat.ranking.NaNStrategy;
41 import org.junit.Test;
42
43
44
45
46 public class MedianTest extends UnivariateStatisticAbstractTest{
47
48
49
50
51
52 private final EstimationType estimationType = LEGACY;
53
54 @Override
55 public Median getUnivariateStatistic() {
56 return new Median().withEstimationType(estimationType);
57 }
58
59 private Median getTestMedian(EstimationType type) {
60 NaNStrategy strategy = (type == LEGACY) ? NaNStrategy.FIXED : NaNStrategy.REMOVED;
61 return new Median().withEstimationType(type).withNaNStrategy(strategy);
62 }
63
64 @Override
65 public double expectedValue() {
66 return this.median;
67 }
68
69 @Test
70 public void testAllTechniquesSingleton() {
71 double[] singletonArray = new double[] { 1d };
72 for (EstimationType e : EstimationType.values()) {
73 UnivariateStatistic percentile = getTestMedian(e);
74 assertEquals(1d, percentile.evaluate(singletonArray), 0);
75 assertEquals(1d, percentile.evaluate(singletonArray, 0, 1), 0);
76 assertTrue(Double.isNaN(percentile.evaluate(singletonArray, 0, 0)));
77 }
78 }
79
80 @Test
81 public void testAllTechniquesMedian() {
82 double[] d = new double[] { 1, 3, 2, 4 };
83 testAssertMappedValues(d, new Object[][] { { LEGACY, 2.5d },
84 { R_1, 2d }, { R_2, 2.5d }, { R_3, 2d }, { R_4, 2d }, { R_5, 2.5 },
85 { R_6, 2.5 },{ R_7, 2.5 },{ R_8, 2.5 }, { R_9 , 2.5 } }, 1.0e-05);
86 }
87
88
89
90
91
92
93
94
95 protected void testAssertMappedValues(double[] d, Object[][] map, Double tolerance) {
96 for (Object[] o : map) {
97 EstimationType e = (EstimationType) o[0];
98 double expected = (Double) o[1];
99 double result = getTestMedian(e).evaluate(d);
100 assertEquals("expected[" + e + "] = " + expected +
101 " but was = " + result, expected, result, tolerance);
102 }
103 }
104 }