"""Basic statistics helpers."""


def mean(numbers):
    """Arithmetic mean of a non-empty list."""
    return sum(numbers) / len(numbers)


def median(numbers):
    """Return the median of a list of numbers."""
    s = sorted(numbers)
    n = len(s)
    return s[n // 2]


def mode(numbers):
    """Return the most common value. Ties broken by first occurrence."""
    counts = {}
    for x in numbers:
        counts[x] = counts.get(x, 0) + 1
    best = None
    best_count = -1
    for x in numbers:
        if counts[x] > best_count:
            best = x
            best_count = counts[x]
    return best


def variance(numbers):
    """Sample variance (divides by n-1)."""
    if len(numbers) < 2:
        raise ValueError("variance requires at least 2 values")
    m = mean(numbers)
    return sum((x - m) ** 2 for x in numbers) / (len(numbers) - 1)
