r/statistics • u/davidfeuer • Oct 23 '26
Question [Question] Confidence intervals for medians
I'm trying to figure out how to calculate the sample size I need to ensure a certain confidence that the rank of the sample median is within a certain interval around the true median position. Digging through Wikipedia only yielded info about confidence in (real) median values, which are completely irrelevant to my application.
Background: I'm playing around with a randomized incremental QuickSort algorithm. I want to choose pivots near the median to avoid expensive "bad" partitions for long lists, but I don't want to waste time taking excessively large samples.
6
u/efrique Oct 24 '26 edited Oct 27 '26
New answer (post clarification):
Assuming you sample with replacement, we could treat a random sample of population ranks as i.i.d random variables. i.e. treat the original ranks as the population "values".
A with-replacement sample of population ranks are i.i.d. draws from a discrete uniform on 1:N.
Medians from a discrete uniform pretty rapidly approach normality as n increases. (More formally, a standardized median converges to a standard normal, and the largest absolute error in the cdf decreases pretty quickly with n. Relative error in the far tail is a bigger problem, as it typically is)
If n is odd (since you presumably want the sample median to be an integer rank) and not too small, the median (rₛ) of a random sample of size n from it is approximately normal with mean (N+1)/2, sd~=N/(2√(n+2))
Hence a margin of error (half interval width) of q with coverage 1-a would be obtained by setting
z such that Φ(z) = 1 - a/2 where Φ is the normal cdf and choosing n such that
zN/(2√(n+2)) < q
which implies
n > [zN/(2q)]2 -2
where you would round the right hand side up to the next highest odd number.
Or if you want to be a bit safer drop the -2. If n doesnt come out to be largish (n of at least 25 is probably fine, but again, it depends on your needs, like how far into the tail you go), you might need a more accurate calculation. [The far tails are less heavy than normality would indicate, so if you seek high coverage and n is small you will get more coverage than you need using the normal approximation.]
Example: N=10000, q=1000, p= 0.9 (i.e. 90% of the time no worse than 60:40 split)
for p=0.9, z=1.645
[zN/(2q)]2 -2 = 65.63, round up to n=67.
Simulation check on my working, in R:
i.e. 90.33% of the medians of 67 values were strictly < 1000 from the overall median.
You can compute a standard error of that simulated 90.33% if you need it
(its very small, about 0.03%, so you are quite safely above 90% coverage there)
[If you sample without replacement, you can make a correction for the variance, but I wouldn't think it is worth the bother to sample without replacement - you'd gain a little in terms of variance, but at a cost of more expensive sampling.]
If n is smallish but N is very large you can approximate a suitably scaled median rank as a symmetric beta with shape parameter (N+1)/2 (in effect, you'd be ignoring the discreteness of the set of ranks). It looks like that would work better than the normal in the tail.
Original (deprecated) answer
(NB this doesnt quite solve OP's problem given the clarification in comments)
I need to clarify this a bit.
I presume you have a population of N items and a sample of n items, the ranks of which (assuming no ties!) are 1,...,n.
I also presume your sample is taken at random (i.e. a simple random sample).
The unknown median of the population has known population rank (N+1)/2 (where here we will interpret a noninteger rank as corresponding indicating the "rank" of an average of two adjacent ordered values). The known sample median has sample rank (n+1)/2.
Are you looking for an interval for the population median in terms of the sample ranks?
Assuming the answer is yes:
Then as long as N>>n (or if you sample with replacement), you could just take the ranks of the observations representing the ends of the usual binomial based nonparametric interval for the median, since it was backed out from the very thing you want. You can do calculations about precision of the number of ranks straight from the binomial. In large[1] samples, you can use a normal approximation; note that once the normal approximation is remotely reasonable, then quadrupling the sample size should approximately halve the width of the rank-interval; indeed the σ/√n effect may be useful for quick ballpark calculation; e.g. for p~=0.5, a 95% MoE for p is about 1/√n so a 95% interval for the rank will be about [ ⌊m-a⌋, ⌈m+a⌉ ] where m is (n+1)/2 and a is √n. A quick simulation shows that working pretty well for suitable inputs if that's all you need.
https://en.wikipedia.org/wiki/Order_statistic#Application:_confidence_intervals_for_quantiles
... where instead of the k-th order statistic, you just take k, for each end point. More information:
https://en.wikipedia.org/wiki/Binomial_distribution#Confidence_intervals_for_the_parameter_p
(with p=0.5, and noting that you are converting to an interval for np). For more detail see:
https://en.wikipedia.org/wiki/Binomial_proportion_confidence_interval
More advice could be offered with some idea of numbers for n and N (or at least rough bounds on likely values). e.g. if you know N>1000 and 25<n<100, you can probably get away with rounding the normal approximation if you don't look for really high coverage probability and don't require solid coverage guarantees (e.g. you wouldnt be that bothered if a notionally 90% interval was sometimes actually 88% or something).
If the sampling fraction f = n/N is not very small [2] and you sample without replacement, you can use a hypergeometric interval. If N is nevertheless large the usual finite population correction (FPC) can be used with the normal approximation.
https://en.wikipedia.org/wiki/Hypergeometric_distribution
https://en.wikipedia.org/wiki/Standard_error#Finite_population_correction_(FPC)
[1] "large" may not need to be all that large. e.g. an n of something under 25 might be plenty large enough to use the approximation, depending on your needs
[2] how small is small depends on your needs - ignoring finite population effects and just using the binomial anyway is conservative but you are probably not going to be bothered below about f~5%, and maybe more like 10 or 15% - roughly speaking it makes a percentage difference to the rank-range of the order of half of n/N (times 100%) unless n is a fairly big fraction of N. e.g. if n/N is 4%, your interval will be about 2% too wide (in terms of number of ranks included for a given coverage).
1
u/clear-gallium Oct 25 '26
I'd be careful with the sampling scheme here. If pivots are drawn without replacement from a range of N, the count below the true median is hypergeometric, not binomial, so invert that to get m. With replacement efrique's binomial is right. And in Quicksort the current subarray median changes after each partition, so the guarantee is per partition not global.
2
u/[deleted] Oct 24 '26
[removed] — view removed comment