Skip to content

Percentile

Concept

\(R = \text{Ceiling}\left(\frac{k}{100} \cdot N\right)\)

  • \(R\): Rank
  • \(k\): Desired percentile
  • \(N\): Total number of points
  • \(Ceiling(x)\): The smallest integer greater or equal to \(x\)
1
2
3
import math

from IPython.display import Markdown, display

The sample data

data = [40, 5, 5, 20, 25, 25, 30, 45, 45, 50, 55]
data
[40, 5, 5, 20, 25, 25, 30, 45, 45, 50, 55]

Sort the data

sorted_data = sorted(data)
sorted_data
[5, 5, 20, 25, 25, 30, 40, 45, 45, 50, 55]

Define the desired percentile

N = len(sorted_data)
k = 75

Calculate the rank R

1
2
3
rank_float = (k / 100) * N
R = math.ceil(rank_float)
R
9

Get the value at rank R (0-based index is R-1)

1
2
3
4
5
6
7
for index, number in enumerate(sorted_data):
    print(f"{index:5d} | {index + 1:4d}th | {number:5d}")

percentile_value = sorted_data[int(R) - 1]

print("\nResult:")
print(f"{k}th Percentile (Value at Rank {R}): {percentile_value}")
    0 |    1th |     5

    1 |    2th |     5

    2 |    3th |    20

    3 |    4th |    25

    4 |    5th |    25

    5 |    6th |    30

    6 |    7th |    40

    7 |    8th |    45

    8 |    9th |    45

    9 |   10th |    50

   10 |   11th |    55



Result:

75th Percentile (Value at Rank 9): 45

The result means that 75% of the entire distribution is less than 45

Quartile

Quartile means a quarter

  • \(1^{st}\): 25%
  • \(2^{nd}\): 50%
  • \(3^{rd}\): 75%