code-switch-mtp / tests /test_uncertainty.py
Anupam-Rawat-IITB's picture
Add code, result files and documentation
3bbd5dd verified
Raw History Blame Contribute Delete
1.9 kB
"""Tests for per-sentence dispersion and paired-difference statistics."""
import math
from src.measure.uncertainty import mean_estimate, paired_difference
def test_mean_and_stderr_on_known_values():
values = [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0]
est = mean_estimate(values, resamples=200)
assert math.isclose(est.mean, 5.0)
# sample stdev of this textbook set is exactly sqrt(32/7)
assert math.isclose(est.stderr, math.sqrt(32.0 / 7.0) / math.sqrt(8), rel_tol=1e-9)
assert est.n == 8
def test_nan_values_are_dropped_not_propagated():
est = mean_estimate([1.0, float("nan"), 3.0], resamples=100)
assert est.n == 2
assert math.isclose(est.mean, 2.0)
def test_constant_values_give_zero_spread():
est = mean_estimate([3.0] * 20, resamples=100)
assert math.isclose(est.mean, 3.0)
assert math.isclose(est.stderr, 0.0)
assert math.isclose(est.ci_low, 3.0) and math.isclose(est.ci_high, 3.0)
def test_bootstrap_ci_brackets_the_mean():
values = [float(i) for i in range(50)]
est = mean_estimate(values, resamples=500, seed=0)
assert est.ci_low < est.mean < est.ci_high
def test_paired_difference_uses_positions_not_pooled_means():
"""A constant per-sentence offset must show up as that offset with ~no spread,
even when the underlying values vary wildly -- the property that makes the
paired form more sensitive than differencing two independent means.
"""
a = [1.0, 10.0, 100.0, 1000.0]
b = [0.5, 9.5, 99.5, 999.5]
est = paired_difference(a, b, resamples=200)
assert math.isclose(est.mean, 0.5, rel_tol=1e-9)
assert math.isclose(est.stderr, 0.0, abs_tol=1e-12)
def test_paired_difference_drops_pairs_with_a_missing_side():
a = [1.0, float("nan"), 3.0]
b = [0.0, 5.0, 1.0]
est = paired_difference(a, b, resamples=100)
assert est.n == 2
assert math.isclose(est.mean, 1.5)