Chi-Squared Distribution Shift Test

~15 mincode completion

Implement chi2_stat(observed, expected) that returns the chi-squared statistic as a float.

Examples

3-category shift: chi2 = 100/60 + 100/40 + 0 = 4.1667

Input
chi2_stat([50, 50, 50], [60, 40, 50])
Output
4.16667

Identical distributions: chi2 = 0.0

Input
chi2_stat([30, 20, 50], [30, 20, 50])
Output
0

Binary case: 2-category deviation

Input
chi2_stat([40, 60], [50, 50])
Output
4

Hints

Hint 1

Sum with , and check which axis you are summing over.

Hint 2

Watch for this: divided by observed not expected.

Requirements

  • observed: 1D array of observed counts

  • expected: 1D array of expected counts

  • Return Chi-squared statistic as a float.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~15 min

8 employers weight this skill

3 big tech firms, 2 enterprise vendors, 1 defense company, 1 AI product company, 1 health and bio company. Top match scores 87.

Python
import numpy as np

def chi2_stat(observed: np.ndarray, expected: np.ndarray) -> float:
    """
    Compute the chi-squared statistic between observed and expected count arrays.
    Skip bins where expected count is zero.

    Args:
        observed: 1D array of observed counts
        expected: 1D array of expected counts

    Returns:
        Chi-squared statistic as a float.
    """
    # 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.