The Two-Sample t-Statistic

~14 mincode completion

Implement t_statistic(a, b) returning as a float. Use the unbiased variance (ddof=1) for each group.

Examples

Two samples two units apart, with matching spread

Input
t_statistic([1, 2, 3, 4], [3, 4, 5, 6])
Output
-2.19089

Identical samples give exactly zero

Input
t_statistic([1, 2, 3], [1, 2, 3])
Output
0

The same gap, but far noisier, is much less convincing

Input
t_statistic([1, 5, 9, 13], [3, 7, 11, 15])
Output
-0.54772

Hints

Hint 1

Take the square root at the end, not inside the sum.

Hint 2

A common slip here: pools the variances instead of dividing each by its own n.

Requirements

  • a: first sample, shape (n_a,)

  • b: second sample, shape (n_b,)

  • Return float: (mean_a - mean_b) / sqrt(var_a/n_a + var_b/n_b)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~14 min

8 employers weight this skill

4 quant funds, 2 health and bio companies, 2 big tech firms. Top match scores 92.

Python
import numpy as np


def t_statistic(a, b):
    """
    Welch's two-sample t-statistic.

    Args:
        a: first sample, shape (n_a,)
        b: second sample, shape (n_b,)

    Returns:
        float: (mean_a - mean_b) / sqrt(var_a/n_a + var_b/n_b)
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.