|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import policyengine_uk.scenarios.uc_reform as uc_reform |
| 5 | +from policyengine_uk import Simulation |
| 6 | + |
| 7 | +YEARS = range(2025, 2030) |
| 8 | + |
| 9 | + |
| 10 | +def _uc_claimant(age_2025: int) -> dict: |
| 11 | + return { |
| 12 | + "people": { |
| 13 | + "person": { |
| 14 | + "age": {year: age_2025 + year - 2025 for year in YEARS}, |
| 15 | + "employment_income": {year: 0 for year in YEARS}, |
| 16 | + "uc_limited_capability_for_WRA": {year: True for year in YEARS}, |
| 17 | + } |
| 18 | + }, |
| 19 | + "benunits": {"benunit": {"members": ["person"]}}, |
| 20 | + "households": {"household": {"members": ["person"]}}, |
| 21 | + } |
| 22 | + |
| 23 | + |
| 24 | +class _FixedRng: |
| 25 | + def __init__(self, values): |
| 26 | + self.values = np.array(values, dtype=float) |
| 27 | + |
| 28 | + def random(self, size): |
| 29 | + assert size == len(self.values) |
| 30 | + return self.values |
| 31 | + |
| 32 | + |
| 33 | +def _force_uc_seed(monkeypatch, values): |
| 34 | + monkeypatch.setattr( |
| 35 | + uc_reform.np.random, "default_rng", lambda seed: _FixedRng(values) |
| 36 | + ) |
| 37 | + |
| 38 | + |
| 39 | +def _benefit_uprating_factor(sim: Simulation, year: int) -> float: |
| 40 | + parameters = sim.tax_benefit_system.parameters |
| 41 | + current_index = float(parameters(str(year)).gov.benefit_uprating_cpi) |
| 42 | + baseline_index = float(parameters("2025").gov.benefit_uprating_cpi) |
| 43 | + return current_index / baseline_index |
| 44 | + |
| 45 | + |
| 46 | +def _rebalanced_standard_allowance_monthly( |
| 47 | + sim: Simulation, year: int, claimant_type: str |
| 48 | +) -> float: |
| 49 | + current = sim.tax_benefit_system.parameters(str(year)) |
| 50 | + standard_allowance = float( |
| 51 | + current.gov.dwp.universal_credit.standard_allowance.amount[claimant_type] |
| 52 | + ) |
| 53 | + uplift = float( |
| 54 | + current.gov.dwp.universal_credit.rebalancing.standard_allowance_uplift |
| 55 | + ) |
| 56 | + return standard_allowance * (1 + uplift) |
| 57 | + |
| 58 | + |
| 59 | +def _cpi_protected_uc_award_monthly( |
| 60 | + sim: Simulation, year: int, claimant_type: str |
| 61 | +) -> float: |
| 62 | + baseline = sim.tax_benefit_system.parameters("2025").gov.dwp.universal_credit |
| 63 | + baseline_standard_allowance = float( |
| 64 | + baseline.standard_allowance.amount[claimant_type] |
| 65 | + ) |
| 66 | + baseline_health_element = float(baseline.elements.disabled.amount) |
| 67 | + return _benefit_uprating_factor(sim, year) * ( |
| 68 | + baseline_standard_allowance + baseline_health_element |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize("age_2025", [20, 30]) |
| 73 | +def test_existing_claimants_keep_combined_award_cpi_protected(monkeypatch, age_2025): |
| 74 | + _force_uc_seed(monkeypatch, [0.99]) |
| 75 | + sim = Simulation(situation=_uc_claimant(age_2025)) |
| 76 | + claimant_type = "SINGLE_YOUNG" if age_2025 < 25 else "SINGLE_OLD" |
| 77 | + |
| 78 | + for year in range(2026, 2030): |
| 79 | + standard_allowance = sim.calculate("uc_standard_allowance", year)[0] / 12 |
| 80 | + health_element = sim.calculate("uc_LCWRA_element", year)[0] / 12 |
| 81 | + new_claimant_health_element = float( |
| 82 | + sim.tax_benefit_system.parameters( |
| 83 | + str(year) |
| 84 | + ).gov.dwp.universal_credit.rebalancing.new_claimant_health_element |
| 85 | + ) |
| 86 | + |
| 87 | + assert health_element > new_claimant_health_element |
| 88 | + assert standard_allowance + health_element == pytest.approx( |
| 89 | + _cpi_protected_uc_award_monthly(sim, year, claimant_type) |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +def test_new_claimants_use_fixed_health_element(monkeypatch): |
| 94 | + _force_uc_seed(monkeypatch, [0.0]) |
| 95 | + sim = Simulation(situation=_uc_claimant(30)) |
| 96 | + |
| 97 | + for year in range(2026, 2030): |
| 98 | + health_element = sim.calculate("uc_LCWRA_element", year)[0] / 12 |
| 99 | + expected_health = float( |
| 100 | + sim.tax_benefit_system.parameters( |
| 101 | + str(year) |
| 102 | + ).gov.dwp.universal_credit.rebalancing.new_claimant_health_element |
| 103 | + ) |
| 104 | + |
| 105 | + assert health_element == pytest.approx(expected_health) |
| 106 | + |
| 107 | + |
| 108 | +def test_standard_allowance_reforms_still_change_standard_allowance(monkeypatch): |
| 109 | + _force_uc_seed(monkeypatch, [0.99]) |
| 110 | + baseline = Simulation(situation=_uc_claimant(30)) |
| 111 | + reformed = Simulation( |
| 112 | + situation=_uc_claimant(30), |
| 113 | + reform={ |
| 114 | + "gov.dwp.universal_credit.standard_allowance.amount.SINGLE_OLD": { |
| 115 | + "2025-01-01.2100-12-31": 800 |
| 116 | + } |
| 117 | + }, |
| 118 | + ) |
| 119 | + |
| 120 | + baseline_standard_allowance = baseline.calculate("uc_standard_allowance", 2026)[0] |
| 121 | + reformed_standard_allowance = reformed.calculate("uc_standard_allowance", 2026)[0] |
| 122 | + |
| 123 | + assert reformed_standard_allowance / baseline_standard_allowance > 1.5 |
0 commit comments