← back

Descriptive Statistics Calculator

#78 · Statistics · Easy

⊣ Solve on deep-ml.com

Problem

Build a descriptive statistics calculator that computes the mean, median, mode, variance, and standard deviation of a given dataset.

Solution

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
27
28
29
30
31
32
33
def descriptive_stats(data):
    n = len(data)
    if n == 0:
        return {}

    # Mean
    mean = sum(data) / n

    # Median
    sorted_data = sorted(data)
    if n % 2 == 1:
        median = sorted_data[n // 2]
    else:
        median = (sorted_data[n // 2 - 1] + sorted_data[n // 2]) / 2

    # Mode
    counts = {}
    for val in data:
        counts[val] = counts.get(val, 0) + 1
    max_count = max(counts.values())
    mode = min(val for val, cnt in counts.items() if cnt == max_count)

    # Variance and Standard Deviation (population)
    variance = sum((x - mean) ** 2 for x in data) / n
    std_dev = variance ** 0.5

    return {
        "mean": round(mean, 4),
        "median": round(median, 4),
        "mode": mode,
        "variance": round(variance, 4),
        "std_dev": round(std_dev, 4)
    }

Explanation

  1. Mean: sum of all values divided by count.
  2. Median: middle value when sorted; average of two middle values if even count.
  3. Mode: most frequently occurring value (smallest if tied).
  4. Variance: average of squared deviations from the mean (population variance).
  5. Standard Deviation: square root of variance, in the same units as the data.

Complexity

  • Time: O(n log n) due to sorting for the median
  • Space: O(n) for the sorted copy and frequency counts