EasyProbability and Statistics

Percentiles / Quantiles

Probability and Statistics

Easy

Problem

Compute requested percentiles with linear interpolation. After sorting n values, convert percentile q to a zero-based position:

r = \frac{q}{100}(n-1)

Let l=\lfloor r\rfloor, u=\lceil r\rceil, and w=r-l. Interpolate between the sorted values:

P_q = (1-w)x_l + wx_u

Apply this calculation to every value in q and return a NumPy array in the same order as the requested percentiles.

Theory

A percentile indicates the relative standing of a value within a dataset. The p percentile is a value below which p percent of the data falls.

Example: If your test score is at the 90th percentile, you scored higher than 90% of test-takers.

Percentiles are used to understand distributions, identify outliers, and compare values across different scales.


Formal Definition

The p percentile (where 0 \leq p \leq 100) is a value x_p such that:

Note: Different methods exist for computing percentiles, especially when the percentile falls between data points.


Special Percentiles

Quartiles divide data into four parts:

Deciles divide data into ten parts:

The median is the 50th percentile:


Computing Percentiles: Basic Method

Step 1: Sort the data in ascending order

Step 2: Calculate the rank position:

L = \frac{p}{100} \times (n + 1)

where p is the percentile and n is the sample size.

Step 3:


Worked Example: Computing Percentiles

Data: [15, 20, 35, 40, 50] (n = 5, already sorted)

Find the 25th percentile (Q1):

L = \frac{25}{100} \times (5 + 1) = 0.25 \times 6 = 1.5

Position 1.5 means: interpolate between positions 1 and 2

P_{25} = x_1 + 0.5 \times (x_2 - x_1) = 15 + 0.5 \times (20 - 15) = 15 + 2.5 = 17.5

Find the 50th percentile (Median):

L = \frac{50}{100} \times 6 = 3

Position 3 is exactly the 3rd value.

P_{50} = 35

Find the 75th percentile (Q3):

L = \frac{75}{100} \times 6 = 4.5

P_{75} = x_4 + 0.5 \times (x_5 - x_4) = 40 + 0.5 \times (50 - 40) = 45


Alternative Calculation Methods

There are multiple conventions for computing percentiles. Common methods include:

Method 1: Linear interpolation (most common)

Used in the example above. Interpolates between adjacent data points.

Method 2: Nearest rank

Round L to the nearest integer and take that value. No interpolation.

Method 3: Exclusive method

L = \frac{p}{100} \times (n + 1)

Method 4: Inclusive method

L = \frac{p}{100} \times (n - 1) + 1

Different software uses different methods. Results may differ slightly for small samples.


The Interquartile Range (IQR)

The IQR measures the spread of the middle 50% of data:

\text{IQR} = Q3 - Q1 = P_{75} - P_{25}

Properties:

Example: If Q1 = 17.5 and Q3 = 45:

\text{IQR} = 45 - 17.5 = 27.5


Using IQR to Detect Outliers

A common rule defines outliers as values outside:

Lower fence: Q1 - 1.5 \times \text{IQR}

Upper fence: Q3 + 1.5 \times \text{IQR}

Example: With Q1 = 17.5, Q3 = 45, \text{IQR} = 27.5:

Lower fence = 17.5 - 1.5 \times 27.5 = 17.5 - 41.25 = -23.75

Upper fence = 45 + 1.5 \times 27.5 = 45 + 41.25 = 86.25

Values below -23.75 or above 86.25 would be flagged as outliers.


Five-Number Summary

The five-number summary consists of:

  1. Minimum
  2. Q1 (25th percentile)
  3. Median (50th percentile)
  4. Q3 (75th percentile)
  5. Maximum

This summary captures the distribution's shape and spread and is the basis for box plots.

Example: For data [15, 20, 35, 40, 50]:


Box Plots (Box-and-Whisker Plots)

Box plots visualize the five-number summary:

Box plots allow quick comparison of distributions across groups.


Percentile Rank

The percentile rank of a value x tells what percentage of data falls at or below x:

\text{Percentile Rank}(x) = \frac{\text{number of values} \leq x}{n} \times 100

Example: In data [15, 20, 35, 40, 50], what is the percentile rank of 35?

3 values are \leq 35 (15, 20, 35)

Percentile rank = \frac{3}{5} \times 100 = 60\%

The value 35 is at the 60th percentile.


Percentiles vs Quantiles

Percentiles: Divide data into 100 parts (0th to 100th)

Quartiles: Divide data into 4 parts (Q1, Q2, Q3)

Deciles: Divide data into 10 parts

Quantiles: General term for any division


Percentiles of Common Distributions

Normal distribution:

For N(\mu, \sigma^2):

These correspond to standard scores (z-scores).


Z-Scores and Percentiles

For a Normal distribution, the z-score tells how many standard deviations from the mean:

z = \frac{x - \mu}{\sigma}

Common z-scores and percentiles:


Applications of Percentiles

Standardized testing:

Income and wealth:

Growth charts:

Website performance:


Percentiles in Machine Learning

Feature scaling:

Quantile regression:

Anomaly detection:

Model evaluation:


Computing Percentiles Efficiently

For small datasets:

For single percentile:

For streaming data:


Percentiles vs Mean and Standard Deviation

Mean and SD:

Percentiles:

For skewed distributions, reporting Q1, median, Q3 is often more informative than mean and SD.

Examples

Example 1

Input
x = [1, 2, 3, 4], q = [25, 50, 75]
Output
[1.75, 2.5, 3.25]
Explanation
The three percentile positions fall between adjacent sorted values and are linearly interpolated.

Example 2

Input
x = [1, 2, 3, 4, 5], q = [50]
Output
[3.0]

Example 3

Input
x = [4, 1, 3, 2], q = [25, 75]
Output
[1.75, 3.25]

Hints

  1. Use positions = q / 100.0 * (x.size - 1).
  2. Use np.floor and np.ceil to locate the interpolation neighbors.

Requirements

Constraints

Starter Code

import numpy as np

def percentiles(x: list, q: list) -> np.ndarray:
    """
    Returns a NumPy array of percentiles.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basic quartilespublic
Single percentile (median)public
Unsorted 2-percentileExample 3public