View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
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   * Test cases for the {@link Median} class.
45   */
46  public class MedianTest extends UnivariateStatisticAbstractTest{
47  
48      /**
49       * {@link Percentile.EstimationType type} to be used while calling
50       * {@link #getUnivariateStatistic()}
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       * Simple test assertion utility method
90       *
91       * @param d input data
92       * @param map of expected result against a {@link EstimationType}
93       * @param tolerance the tolerance of difference allowed
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 }