Statistics Percentiles
A percentile tells you what percentage of the data falls below a particular value, letting you see where one observation stands relative to the whole group.
What a Percentile Really Means
The Pth percentile is the value below which P percent of the observations in a data set fall. If a quiz score sits at the 80th percentile, that means roughly 80% of the class scored at or below it, and about 20% scored higher. Percentiles do not tell you how many points someone scored -- they tell you how that score compares to everyone else's.
- The 50th percentile is the median -- half the data lies below it.
- The 25th and 75th percentiles are usually called the first and third quartiles (Q1 and Q3).
- Percentiles that split data into tenths (10th, 20th, 30th, ...) are called deciles.
The Data Set We'll Use
Here are quiz scores (out of 50) for 12 students, already sorted from lowest to highest: 32, 35, 38, 40, 41, 43, 44, 45, 47, 48, 49, 50. We'll compute several percentiles from this same list so you can see how the position of a percentile shifts as P changes.
Calculating a Percentile Step by Step
- Sort the data from smallest to largest.
- Compute a rank: rank = (P / 100) x (n - 1), where n is the number of values.
- If the rank is a whole number, that position's value is the percentile.
- If the rank falls between two positions, interpolate: take the lower value, then add the fractional part of the rank multiplied by the gap to the next value.
Example: Computing Several Percentiles
scores = [32, 35, 38, 40, 41, 43, 44, 45, 47, 48, 49, 50]
scores.sort()
n = len(scores)
def percentile(data, p):
rank = (p / 100) * (n - 1)
lower = int(rank) # index just below the rank
upper = lower + 1 if lower + 1 < n else lower
fraction = rank - lower
return data[lower] + fraction * (data[upper] - data[lower])
p25 = percentile(scores, 25)
p50 = percentile(scores, 50)
p75 = percentile(scores, 75)
p90 = percentile(scores, 90)
print(f"25th percentile (Q1): {p25}")
print(f"50th percentile (median): {p50}")
print(f"75th percentile (Q3): {p75}")
print(f"90th percentile: {p90}")
# Output:
# 25th percentile (Q1): 39.5
# 50th percentile (median): 43.5
# 75th percentile (Q3): 47.25
# 90th percentile: 48.9Example: Finding the Percentile Rank of a Score
scores = [32, 35, 38, 40, 41, 43, 44, 45, 47, 48, 49, 50]
def percentile_rank(data, value):
below = sum(1 for x in data if x < value)
equal = sum(1 for x in data if x == value)
n = len(data)
return (below + 0.5 * equal) / n * 100
rank_of_44 = percentile_rank(scores, 44)
rank_of_40 = percentile_rank(scores, 40)
print(f"A score of 44 is at the {rank_of_44:.1f}th percentile")
print(f"A score of 40 is at the {rank_of_40:.1f}th percentile")
# Output:
# A score of 44 is at the 54.2th percentile
# A score of 40 is at the 29.2th percentile