Welcome, guest | Sign In | My Account | Store | Cart

This function find the percentile of a list of values. Note that the list must be sorted already.

Python, 26 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import math
import functools

def percentile(N, percent, key=lambda x:x):
    """
    Find the percentile of a list of values.

    @parameter N - is a list of values. Note N MUST BE already sorted.
    @parameter percent - a float value from 0.0 to 1.0.
    @parameter key - optional key function to compute value from each element of N.

    @return - the percentile of the values
    """
    if not N:
        return None
    k = (len(N)-1) * percent
    f = math.floor(k)
    c = math.ceil(k)
    if f == c:
        return key(N[int(k)])
    d0 = key(N[int(f)]) * (c-k)
    d1 = key(N[int(c)]) * (k-f)
    return d0+d1

# median is 50th percentile.
median = functools.partial(percentile, percent=0.5)

e.g.

>>> percentile(range(10),0.25)
2.25
>>> percentile(range(10),0.75)
6.75
>>> median(range(10))
4.5
>>> median(range(11))
5

2 comments

Matthew Shaer 16 years, 10 months ago  # | flag

Correction. That does the interpolation in the wrong direction (ie the higher the percent the closer to the lower value it went). Change to: d0 = key(N[int(f)]) * (c-k) and d1 = key(N[int(c)]) * (k-f)

Wai Yip Tung (author) 12 years, 11 months ago  # | flag

Thank you for your correction. I takes me 4 years to find this comment and correct it??