001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.stat.descriptive.rank; 018 019import java.io.Serializable; 020 021import org.apache.commons.math3.exception.MathIllegalArgumentException; 022import org.apache.commons.math3.exception.NullArgumentException; 023import org.apache.commons.math3.stat.ranking.NaNStrategy; 024import org.apache.commons.math3.util.KthSelector; 025 026 027/** 028 * Returns the median of the available values. This is the same as the 50th percentile. 029 * See {@link Percentile} for a description of the algorithm used. 030 * <p> 031 * <strong>Note that this implementation is not synchronized.</strong> If 032 * multiple threads access an instance of this class concurrently, and at least 033 * one of the threads invokes the <code>increment()</code> or 034 * <code>clear()</code> method, it must be synchronized externally.</p> 035 * 036 */ 037public class Median extends Percentile implements Serializable { 038 039 /** Serializable version identifier */ 040 private static final long serialVersionUID = -3961477041290915687L; 041 042 /** Fixed quantile. */ 043 private static final double FIXED_QUANTILE_50 = 50.0; 044 045 /** 046 * Default constructor. 047 */ 048 public Median() { 049 // No try-catch or advertised exception - arg is valid 050 super(FIXED_QUANTILE_50); 051 } 052 053 /** 054 * Copy constructor, creates a new {@code Median} identical 055 * to the {@code original} 056 * 057 * @param original the {@code Median} instance to copy 058 * @throws NullArgumentException if original is null 059 */ 060 public Median(Median original) throws NullArgumentException { 061 super(original); 062 } 063 064 /** 065 * Constructs a Median with the specific {@link EstimationType}, {@link NaNStrategy} and {@link PivotingStrategy}. 066 * 067 * @param estimationType one of the percentile {@link EstimationType estimation types} 068 * @param nanStrategy one of {@link NaNStrategy} to handle with NaNs 069 * @param kthSelector {@link KthSelector} to use for pivoting during search 070 * @throws MathIllegalArgumentException if p is not within (0,100] 071 * @throws NullArgumentException if type or NaNStrategy passed is null 072 */ 073 private Median(final EstimationType estimationType, final NaNStrategy nanStrategy, 074 final KthSelector kthSelector) 075 throws MathIllegalArgumentException { 076 super(FIXED_QUANTILE_50, estimationType, nanStrategy, kthSelector); 077 } 078 079 /** {@inheritDoc} */ 080 @Override 081 public Median withEstimationType(final EstimationType newEstimationType) { 082 return new Median(newEstimationType, getNaNStrategy(), getKthSelector()); 083 } 084 085 /** {@inheritDoc} */ 086 @Override 087 public Median withNaNStrategy(final NaNStrategy newNaNStrategy) { 088 return new Median(getEstimationType(), newNaNStrategy, getKthSelector()); 089 } 090 091 /** {@inheritDoc} */ 092 @Override 093 public Median withKthSelector(final KthSelector newKthSelector) { 094 return new Median(getEstimationType(), getNaNStrategy(), newKthSelector); 095 } 096 097}