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    *      http://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  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   * Test cases for the {@link UnivariateStatistic} class.
40   */
41  public class MedianTest extends UnivariateStatisticAbstractTest{
42  
43      protected Median stat;
44  
45      /**
46       * {@link  org.apache.commons.math4.legacy.stat.descriptive.rank.Percentile.EstimationType type}
47       *  to be used while calling
48       * {@link #getUnivariateStatistic()}
49       */
50      protected EstimationType estimationType = LEGACY;
51  
52      /**
53       * {@inheritDoc}
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       * {@inheritDoc}
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      * Simple test assertion utility method
101      *
102      * @param d input data
103      * @param map of expected result against a {@link EstimationType}
104      * @param tolerance the tolerance of difference allowed
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 }