EasyProbability and Statistics

Mean, Median, Mode

Probability and Statistics

Easy

Problem

Compute three measures of a nonempty one-dimensional numeric dataset. The mean is:

\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i

The median is the middle sorted value, or the average of the two middle values when n is even. The mode is the most frequent value. If several values share the highest frequency, choose the smallest. Return mean, median, and mode in a dictionary of Python floats.

Theory

Central tendency describes where the "center" of a dataset lies. The three most common measures are:

Each measure has different properties and is appropriate in different situations.


The Mean (Arithmetic Average)

The mean is the sum of all values divided by the count:

\bar{x} = \frac{1}{n} \sum_{i=1}^{n} x_i = \frac{x_1 + x_2 + ... + x_n}{n}

Properties:


Computing the Mean: Example

Data: [4, 8, 6, 5, 3, 9, 7]

Step 1: Sum the values

4 + 8 + 6 + 5 + 3 + 9 + 7 = 42

Step 2: Count the values

n = 7

Step 3: Divide

\bar{x} = \frac{42}{7} = 6

The mean is 6.


The Median

The median is the middle value when data is sorted in order:

For odd n: The median is the value at position \frac{n+1}{2}.

For even n: The median is the average of values at positions \frac{n}{2} and \frac{n}{2} + 1.

Properties:


Computing the Median: Odd n

Data: [4, 8, 6, 5, 3, 9, 7]

Step 1: Sort the data

[3, 4, 5, 6, 7, 8, 9]

Step 2: Find the middle position

\text{position} = \frac{n+1}{2} = \frac{7+1}{2} = 4

Step 3: The median is the 4th value

\text{median} = 6


Computing the Median: Even n

Data: [4, 8, 6, 5, 3, 9]

Step 1: Sort the data

[3, 4, 5, 6, 8, 9]

Step 2: Find the two middle positions

\text{positions} = \frac{n}{2} \text{ and } \frac{n}{2} + 1 = 3 \text{ and } 4

Step 3: Average the 3rd and 4th values

\text{median} = \frac{5 + 6}{2} = 5.5


The Mode

The mode is the value that appears most frequently:

Properties:


Computing the Mode: Example 1

Data: [4, 8, 6, 5, 3, 6, 7, 6, 9]

Count occurrences:

Mode = 6 (appears most frequently)


Computing the Mode: No Mode

Data: [4, 8, 6, 5, 3, 9, 7]

Each value appears exactly once. There is no mode (or we say all values are modes).


Computing the Mode: Multiple Modes

Data: [4, 8, 6, 5, 3, 6, 8, 9]

Count occurrences:

Modes = 6 and 8 (bimodal distribution)


Comparison: Mean vs Median

Symmetric distributions:

Right-skewed distributions (long right tail):

Left-skewed distributions (long left tail):

With outliers:


Effect of Outliers: Example

Data without outlier: [10, 12, 11, 13, 12, 11, 14]

Mean = (10+12+11+13+12+11+14)/7 = 83/7 = 11.86

Median = 12 (middle of sorted [10, 11, 11, 12, 12, 13, 14])

Data with outlier: [10, 12, 11, 13, 12, 11, 100]

Mean = (10+12+11+13+12+11+100)/7 = 169/7 = 24.14

Median = 12 (middle of sorted [10, 11, 11, 12, 12, 13, 100])

The outlier dramatically affects the mean but not the median.


When to Use Each Measure

Use Mean when:

Use Median when:

Use Mode when:


Relationship in Different Distributions

Symmetric distribution:

\text{Mean} = \text{Median} = \text{Mode}

Right-skewed distribution:

\text{Mode} < \text{Median} < \text{Mean}

Left-skewed distribution:

\text{Mean} < \text{Median} < \text{Mode}

This relationship is approximate and does not always hold exactly.


Weighted Mean

When data points have different importances (weights):

\bar{x}_w = \frac{\sum_{i=1}^{n} w_i x_i}{\sum_{i=1}^{n} w_i}

Example: Calculate GPA where courses have different credits.

Grades: A(4), B(3), A(4), C(2) Credits: 3, 4, 2, 3

\text{GPA} = \frac{4(3) + 3(4) + 4(2) + 2(3)}{3 + 4 + 2 + 3} = \frac{12 + 12 + 8 + 6}{12} = \frac{38}{12} = 3.17


Trimmed Mean

The trimmed mean removes a percentage of extreme values before computing the mean:

Process:

  1. Sort the data
  2. Remove the lowest k\% and highest k\%
  3. Compute mean of remaining values

Example: 10% trimmed mean of 20 values removes the 2 smallest and 2 largest.

This provides robustness to outliers while still using most data.


Geometric Mean

For positive values, the geometric mean is:

\bar{x}_g = \sqrt[n]{x_1 \cdot x_2 \cdot ... \cdot x_n} = \left(\prod_{i=1}^{n} x_i\right)^{1/n}

Use cases:

Example: Growth rates of 10%, 20%, 30%

Geometric mean = \sqrt[3]{1.1 \times 1.2 \times 1.3} = \sqrt[3]{1.716} = 1.197

Average growth rate \approx 19.7\%


Harmonic Mean

The harmonic mean is:

\bar{x}_h = \frac{n}{\sum_{i=1}^{n} \frac{1}{x_i}}

Use cases:

Example: Drive 60 mph one way, 40 mph return

Harmonic mean = \frac{2}{\frac{1}{60} + \frac{1}{40}} = \frac{2}{\frac{2+3}{120}} = \frac{2 \times 120}{5} = 48 mph

The average speed is 48 mph, not 50 mph.


Relationship Between Means

For positive values:

\text{Harmonic Mean} \leq \text{Geometric Mean} \leq \text{Arithmetic Mean}

Equality holds only when all values are equal.


Central Tendency in Machine Learning

Imputation:

Loss functions:

Baseline models:

Feature engineering:


Population vs Sample Notation

Population mean: \mu = \frac{1}{N}\sum_{i=1}^{N} x_i

Sample mean: \bar{x} = \frac{1}{n}\sum_{i=1}^{n} x_i

The sample mean \bar{x} estimates the population mean \mu.

Key property: E[\bar{X}] = \mu (unbiased estimator)


Computational Considerations

Numerical stability for mean:

For very large datasets, use running mean:

\bar{x}_n = \bar{x}_{n-1} + \frac{x_n - \bar{x}_{n-1}}{n}

Efficiency for median:

Mode:

Examples

Example 1

Input
x = [1, 2, 3, 4, 5]
Output
{"mean": 3.0, "median": 3.0, "mode": 1.0}
Explanation
Every value appears once, so the smallest value wins the mode tie.

Example 2

Input
x = [1, 2, 2, 3, 4]
Output
{"mean": 2.4, "median": 2.0, "mode": 2.0}

Example 3

Input
x = [1, 2, 3, 4]
Output
{"mean": 2.5, "median": 2.5, "mode": 1.0}

Hints

  1. Use np.mean and np.median for the first two values.
  2. Count values with Counter, find the highest frequency, then take the smallest matching key.

Requirements

Constraints

Starter Code

from collections import Counter
import numpy as np

def mean_median_mode(x: list) -> dict:
    """
    Returns a dictionary with mean, median, and mode.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Basic Odd Lengthpublic
With Duplicatespublic
Even Lengthpublic