Skip to content

Commit cf2ed5d

Browse files
MaxGhenisclaude
authored andcommitted
Add tests for stochastic variable generation
Tests verify: - Take-up rate parameters load correctly (EITC, SNAP, Medicaid, etc.) - Seeded RNG produces deterministic results - Take-up proportions match expected rates 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 25d7f2c commit cf2ed5d

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""Tests for stochastic variable generation in the data package.
2+
3+
These tests verify that:
4+
1. Take-up rate parameters load correctly
5+
2. Seeded RNG produces deterministic results
6+
3. Take-up rates produce plausible proportions
7+
"""
8+
9+
import pytest
10+
import numpy as np
11+
from policyengine_us_data.parameters import load_take_up_rate
12+
13+
14+
class TestTakeUpRateParameters:
15+
"""Test that take-up rate parameters load correctly."""
16+
17+
def test_eitc_rate_loads(self):
18+
"""EITC take-up rates should load and be plausible."""
19+
rates = load_take_up_rate("eitc", 2022)
20+
# EITC rates are by number of children: 0, 1, 2, 3+
21+
assert isinstance(rates, dict) or isinstance(rates, float)
22+
if isinstance(rates, dict):
23+
for key, rate in rates.items():
24+
assert 0 < rate <= 1
25+
26+
def test_snap_rate_loads(self):
27+
"""SNAP take-up rate should load and be plausible."""
28+
rate = load_take_up_rate("snap", 2022)
29+
assert 0 < rate <= 1
30+
31+
def test_medicaid_rate_loads(self):
32+
"""Medicaid take-up rate should load and be plausible."""
33+
rate = load_take_up_rate("medicaid", 2022)
34+
assert 0 < rate <= 1
35+
36+
def test_aca_rate_loads(self):
37+
"""ACA take-up rate should load and be plausible."""
38+
rate = load_take_up_rate("aca", 2022)
39+
assert 0 < rate <= 1
40+
41+
def test_head_start_rate_loads(self):
42+
"""Head Start take-up rate should load and be plausible."""
43+
rate = load_take_up_rate("head_start", 2022)
44+
assert 0 < rate <= 1
45+
46+
def test_early_head_start_rate_loads(self):
47+
"""Early Head Start take-up rate should load and be plausible."""
48+
rate = load_take_up_rate("early_head_start", 2022)
49+
assert 0 < rate <= 1
50+
51+
def test_dc_ptc_rate_loads(self):
52+
"""DC PTC take-up rate should load and be plausible."""
53+
rate = load_take_up_rate("dc_ptc", 2022)
54+
assert 0 < rate <= 1
55+
56+
57+
class TestSeededRandomness:
58+
"""Test that stochastic generation is deterministic."""
59+
60+
def test_same_seed_produces_same_results(self):
61+
"""Using the same seed should produce identical results."""
62+
seed = 0
63+
n = 1_000
64+
65+
generator1 = np.random.default_rng(seed=seed)
66+
result1 = generator1.random(n)
67+
68+
generator2 = np.random.default_rng(seed=seed)
69+
result2 = generator2.random(n)
70+
71+
np.testing.assert_array_equal(result1, result2)
72+
73+
def test_different_seeds_produce_different_results(self):
74+
"""Different seeds should produce different results."""
75+
n = 1_000
76+
77+
generator1 = np.random.default_rng(seed=0)
78+
result1 = generator1.random(n)
79+
80+
generator2 = np.random.default_rng(seed=1)
81+
result2 = generator2.random(n)
82+
83+
assert not np.array_equal(result1, result2)
84+
85+
86+
class TestTakeUpProportions:
87+
"""Test that take-up rates produce plausible proportions."""
88+
89+
def test_take_up_produces_expected_proportion(self):
90+
"""Simulated take-up should match the rate approximately."""
91+
rate = 0.7
92+
n = 10_000
93+
generator = np.random.default_rng(seed=42)
94+
95+
take_up = generator.random(n) < rate
96+
actual_proportion = take_up.mean()
97+
98+
# Should be within 5 percentage points of the rate
99+
assert abs(actual_proportion - rate) < 0.05
100+
101+
def test_boolean_generation(self):
102+
"""Take-up decisions should be boolean."""
103+
rate = 0.5
104+
n = 100
105+
generator = np.random.default_rng(seed=42)
106+
107+
take_up = generator.random(n) < rate
108+
109+
assert take_up.dtype == bool
110+
assert set(take_up).issubset({True, False})

0 commit comments

Comments
 (0)