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