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 org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
20  import org.apache.commons.math4.legacy.exception.NullArgumentException;
21  import org.apache.commons.math4.legacy.stat.ranking.NaNStrategy;
22  
23  
24  /**
25   * Returns the median of the available values.  This is the same as the 50th percentile.
26   * See {@link Percentile} for a description of the algorithm used.
27   * <p>
28   * <strong>Note that this implementation is not synchronized.</strong> If
29   * multiple threads access an instance of this class concurrently, and at least
30   * one of the threads invokes the <code>increment()</code> or
31   * <code>clear()</code> method, it must be synchronized externally.</p>
32   */
33  public class Median extends Percentile {
34      /** Fixed quantile. */
35      private static final double FIXED_QUANTILE_50 = 50.0;
36  
37      /**
38       * Default constructor.
39       */
40      public Median() {
41          // No try-catch or advertised exception - arg is valid
42          super(FIXED_QUANTILE_50);
43      }
44  
45      /**
46       * Copy constructor, creates a new {@code Median} identical.
47       * to the {@code original}
48       *
49       * @param original the {@code Median} instance to copy
50       * @throws NullArgumentException if original is null
51       */
52      public Median(Median original) throws NullArgumentException {
53          super(original);
54      }
55  
56      /**
57       * Constructs a Median with the specific {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
58       *
59       * @param estimationType one of the percentile {@link EstimationType  estimation types}
60       * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
61       * @param kthSelector {@link KthSelector} to use for pivoting during search
62       * @throws MathIllegalArgumentException if p is not within (0,100]
63       * @throws NullArgumentException if type or NaNStrategy passed is null
64       */
65      private Median(final EstimationType estimationType, final NaNStrategy nanStrategy,
66                     final KthSelector kthSelector)
67          throws MathIllegalArgumentException {
68          super(FIXED_QUANTILE_50, estimationType, nanStrategy, kthSelector);
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public Median withEstimationType(final EstimationType newEstimationType) {
74          return new Median(newEstimationType, getNaNStrategy(), getKthSelector());
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public Median withNaNStrategy(final NaNStrategy newNaNStrategy) {
80          return new Median(getEstimationType(), newNaNStrategy, getKthSelector());
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public Median withKthSelector(final KthSelector newKthSelector) {
86          return new Median(getEstimationType(), getNaNStrategy(), newKthSelector);
87      }
88  }