Median.java

  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. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  19. import org.apache.commons.math4.legacy.exception.NullArgumentException;
  20. import org.apache.commons.math4.legacy.stat.ranking.NaNStrategy;


  21. /**
  22.  * Returns the median of the available values.  This is the same as the 50th percentile.
  23.  * See {@link Percentile} for a description of the algorithm used.
  24.  * <p>
  25.  * <strong>Note that this implementation is not synchronized.</strong> If
  26.  * multiple threads access an instance of this class concurrently, and at least
  27.  * one of the threads invokes the <code>increment()</code> or
  28.  * <code>clear()</code> method, it must be synchronized externally.</p>
  29.  */
  30. public class Median extends Percentile {
  31.     /** Fixed quantile. */
  32.     private static final double FIXED_QUANTILE_50 = 50.0;

  33.     /**
  34.      * Default constructor.
  35.      */
  36.     public Median() {
  37.         // No try-catch or advertised exception - arg is valid
  38.         super(FIXED_QUANTILE_50);
  39.     }

  40.     /**
  41.      * Copy constructor, creates a new {@code Median} identical.
  42.      * to the {@code original}
  43.      *
  44.      * @param original the {@code Median} instance to copy
  45.      * @throws NullArgumentException if original is null
  46.      */
  47.     public Median(Median original) throws NullArgumentException {
  48.         super(original);
  49.     }

  50.     /**
  51.      * Constructs a Median with the specific {@link EstimationType}, {@link NaNStrategy} and {@link KthSelector}.
  52.      *
  53.      * @param estimationType one of the percentile {@link EstimationType  estimation types}
  54.      * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs
  55.      * @param kthSelector {@link KthSelector} to use for pivoting during search
  56.      * @throws MathIllegalArgumentException if p is not within (0,100]
  57.      * @throws NullArgumentException if type or NaNStrategy passed is null
  58.      */
  59.     private Median(final EstimationType estimationType, final NaNStrategy nanStrategy,
  60.                    final KthSelector kthSelector)
  61.         throws MathIllegalArgumentException {
  62.         super(FIXED_QUANTILE_50, estimationType, nanStrategy, kthSelector);
  63.     }

  64.     /** {@inheritDoc} */
  65.     @Override
  66.     public Median withEstimationType(final EstimationType newEstimationType) {
  67.         return new Median(newEstimationType, getNaNStrategy(), getKthSelector());
  68.     }

  69.     /** {@inheritDoc} */
  70.     @Override
  71.     public Median withNaNStrategy(final NaNStrategy newNaNStrategy) {
  72.         return new Median(getEstimationType(), newNaNStrategy, getKthSelector());
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public Median withKthSelector(final KthSelector newKthSelector) {
  77.         return new Median(getEstimationType(), getNaNStrategy(), newKthSelector);
  78.     }
  79. }