|
| 1 | +"""Dataset-side disability benefit category mapping. |
| 2 | +
|
| 3 | +PolicyEngine UK models PIP, DLA, and Attendance Allowance from category |
| 4 | +inputs. The FRS observes reported amounts, so the data pipeline keeps those |
| 5 | +amounts as internal build intermediates and converts them to model inputs |
| 6 | +before datasets are published. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +from functools import lru_cache |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import pandas as pd |
| 15 | +from policyengine_uk import CountryTaxBenefitSystem |
| 16 | +from policyengine_uk.data import UKSingleYearDataset |
| 17 | +from policyengine_uk.model_api import WEEKS_IN_YEAR as MODEL_WEEKS_IN_YEAR |
| 18 | + |
| 19 | + |
| 20 | +DISABILITY_REPORTED_AMOUNT_COLUMNS = ( |
| 21 | + "attendance_allowance_reported", |
| 22 | + "dla_sc_reported", |
| 23 | + "dla_m_reported", |
| 24 | + "pip_m_reported", |
| 25 | + "pip_dl_reported", |
| 26 | +) |
| 27 | + |
| 28 | +DISABILITY_CATEGORY_COLUMNS = ( |
| 29 | + "aa_category", |
| 30 | + "dla_sc_category", |
| 31 | + "dla_m_category", |
| 32 | + "pip_m_category", |
| 33 | + "pip_dl_category", |
| 34 | +) |
| 35 | + |
| 36 | +SAFETY_MARGIN = 0.1 |
| 37 | +SURVEY_REPORTED_AMOUNT_WEEKS_IN_YEAR = 365.25 / 7 |
| 38 | + |
| 39 | + |
| 40 | +@lru_cache(maxsize=None) |
| 41 | +def _dwp_category_threshold_parameters(year: int): |
| 42 | + # Match the category formulas removed from policyengine-uk. Those formulas |
| 43 | + # thresholded reported amounts against the baseline DWP rates. |
| 44 | + return CountryTaxBenefitSystem().parameters(year).baseline.gov.dwp |
| 45 | + |
| 46 | + |
| 47 | +@lru_cache(maxsize=None) |
| 48 | +def _dwp_flag_parameters(year: int): |
| 49 | + # Match the FRS disability flag derivation that already lived in uk-data. |
| 50 | + return CountryTaxBenefitSystem().parameters(year).gov.dwp |
| 51 | + |
| 52 | + |
| 53 | +def _reported_amount(person: pd.DataFrame, column: str) -> pd.Series: |
| 54 | + if column not in person.columns: |
| 55 | + return pd.Series(0.0, index=person.index) |
| 56 | + return pd.to_numeric(person[column], errors="coerce").fillna(0.0) |
| 57 | + |
| 58 | + |
| 59 | +def _category_from_reported_amount( |
| 60 | + reported_amount: pd.Series, |
| 61 | + thresholds: tuple[tuple[str, float], ...], |
| 62 | +) -> np.ndarray: |
| 63 | + weekly_amount = pd.to_numeric(reported_amount, errors="coerce").fillna(0) |
| 64 | + weekly_amount = weekly_amount.to_numpy(dtype=float) / MODEL_WEEKS_IN_YEAR |
| 65 | + category = np.full(len(weekly_amount), "NONE", dtype=object) |
| 66 | + for category_name, weekly_rate in thresholds: |
| 67 | + category[weekly_amount >= float(weekly_rate) * (1 - SAFETY_MARGIN)] = ( |
| 68 | + category_name |
| 69 | + ) |
| 70 | + return category |
| 71 | + |
| 72 | + |
| 73 | +def add_disability_benefit_categories_from_reported_amounts( |
| 74 | + person: pd.DataFrame, |
| 75 | + year: int, |
| 76 | + *, |
| 77 | + inplace: bool = False, |
| 78 | +) -> pd.DataFrame: |
| 79 | + """Convert reported disability benefit amounts into category inputs.""" |
| 80 | + |
| 81 | + if not inplace: |
| 82 | + person = person.copy() |
| 83 | + |
| 84 | + dwp = _dwp_category_threshold_parameters(int(year)) |
| 85 | + mappings = ( |
| 86 | + ( |
| 87 | + "attendance_allowance_reported", |
| 88 | + "aa_category", |
| 89 | + ( |
| 90 | + ("LOWER", dwp.attendance_allowance.lower), |
| 91 | + ("HIGHER", dwp.attendance_allowance.higher), |
| 92 | + ), |
| 93 | + ), |
| 94 | + ( |
| 95 | + "dla_sc_reported", |
| 96 | + "dla_sc_category", |
| 97 | + ( |
| 98 | + ("LOWER", dwp.dla.self_care.lower), |
| 99 | + ("MIDDLE", dwp.dla.self_care.middle), |
| 100 | + ("HIGHER", dwp.dla.self_care.higher), |
| 101 | + ), |
| 102 | + ), |
| 103 | + ( |
| 104 | + "dla_m_reported", |
| 105 | + "dla_m_category", |
| 106 | + ( |
| 107 | + ("LOWER", dwp.dla.mobility.lower), |
| 108 | + ("HIGHER", dwp.dla.mobility.higher), |
| 109 | + ), |
| 110 | + ), |
| 111 | + ( |
| 112 | + "pip_m_reported", |
| 113 | + "pip_m_category", |
| 114 | + ( |
| 115 | + ("STANDARD", dwp.pip.mobility.standard), |
| 116 | + ("ENHANCED", dwp.pip.mobility.enhanced), |
| 117 | + ), |
| 118 | + ), |
| 119 | + ( |
| 120 | + "pip_dl_reported", |
| 121 | + "pip_dl_category", |
| 122 | + ( |
| 123 | + ("STANDARD", dwp.pip.daily_living.standard), |
| 124 | + ("ENHANCED", dwp.pip.daily_living.enhanced), |
| 125 | + ), |
| 126 | + ), |
| 127 | + ) |
| 128 | + |
| 129 | + for reported_column, category_column, thresholds in mappings: |
| 130 | + if reported_column in person.columns: |
| 131 | + person[category_column] = _category_from_reported_amount( |
| 132 | + person[reported_column], |
| 133 | + thresholds, |
| 134 | + ) |
| 135 | + |
| 136 | + return person |
| 137 | + |
| 138 | + |
| 139 | +def add_disability_benefit_flags_from_reported_amounts( |
| 140 | + person: pd.DataFrame, |
| 141 | + year: int, |
| 142 | + *, |
| 143 | + inplace: bool = False, |
| 144 | +) -> pd.DataFrame: |
| 145 | + """Recompute disability flags derived from reported benefit amounts.""" |
| 146 | + |
| 147 | + if not inplace: |
| 148 | + person = person.copy() |
| 149 | + |
| 150 | + dwp = _dwp_flag_parameters(int(year)) |
| 151 | + dla_sc = _reported_amount(person, "dla_sc_reported") |
| 152 | + dla_m = _reported_amount(person, "dla_m_reported") |
| 153 | + pip_m = _reported_amount(person, "pip_m_reported") |
| 154 | + pip_dl = _reported_amount(person, "pip_dl_reported") |
| 155 | + afcs = _reported_amount(person, "afcs_reported") |
| 156 | + |
| 157 | + person["is_disabled_for_benefits"] = (dla_sc + dla_m + pip_m + pip_dl) > 0 |
| 158 | + |
| 159 | + threshold_safety_gap = 1 * SURVEY_REPORTED_AMOUNT_WEEKS_IN_YEAR |
| 160 | + dla_sc_higher = ( |
| 161 | + dwp.dla.self_care.higher * SURVEY_REPORTED_AMOUNT_WEEKS_IN_YEAR |
| 162 | + - threshold_safety_gap |
| 163 | + ) |
| 164 | + pip_dl_enhanced = ( |
| 165 | + dwp.pip.daily_living.enhanced * SURVEY_REPORTED_AMOUNT_WEEKS_IN_YEAR |
| 166 | + - threshold_safety_gap |
| 167 | + ) |
| 168 | + |
| 169 | + person["is_enhanced_disabled_for_benefits"] = dla_sc > dla_sc_higher |
| 170 | + person["is_severely_disabled_for_benefits"] = ( |
| 171 | + (dla_sc >= dla_sc_higher) | (pip_dl >= pip_dl_enhanced) | (afcs > 0) |
| 172 | + ) |
| 173 | + |
| 174 | + return person |
| 175 | + |
| 176 | + |
| 177 | +def drop_internal_disability_reported_amounts( |
| 178 | + person: pd.DataFrame, |
| 179 | + *, |
| 180 | + inplace: bool = False, |
| 181 | +) -> pd.DataFrame: |
| 182 | + """Drop disability amount intermediates that are not PE-UK inputs.""" |
| 183 | + |
| 184 | + if inplace: |
| 185 | + person.drop( |
| 186 | + columns=list(DISABILITY_REPORTED_AMOUNT_COLUMNS), |
| 187 | + errors="ignore", |
| 188 | + inplace=True, |
| 189 | + ) |
| 190 | + return person |
| 191 | + return person.drop( |
| 192 | + columns=list(DISABILITY_REPORTED_AMOUNT_COLUMNS), |
| 193 | + errors="ignore", |
| 194 | + ) |
| 195 | + |
| 196 | + |
| 197 | +def strip_internal_disability_reported_amounts( |
| 198 | + dataset: UKSingleYearDataset, |
| 199 | +) -> UKSingleYearDataset: |
| 200 | + """Return ``dataset`` without internal disability amount intermediates.""" |
| 201 | + |
| 202 | + dataset = dataset.copy() |
| 203 | + dataset.person = drop_internal_disability_reported_amounts(dataset.person) |
| 204 | + return dataset |
0 commit comments