|
35 | 35 | EmploymentStatus.LONG_TERM_DISABLED.name, |
36 | 36 | EmploymentStatus.SHORT_TERM_DISABLED.name, |
37 | 37 | ) |
| 38 | +FORMULA_MODELED_EDUCATION_GRANT_VARIABLES = ( |
| 39 | + "childcare_grant", |
| 40 | + "parents_learning_allowance", |
| 41 | + "adult_dependants_grant", |
| 42 | +) |
| 43 | +DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT = ( |
| 44 | + "disabled_students_allowance_eligible_expenses" |
| 45 | +) |
| 46 | +DISABLED_STUDENTS_ALLOWANCE_FIRST_MODELED_YEAR = 2025 |
| 47 | +DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES = ( |
| 48 | + "maintenance_loan_in_england_system", |
| 49 | + "disabled_students_allowance_course_eligible", |
| 50 | + "disabled_students_allowance_has_qualifying_condition", |
| 51 | +) |
38 | 52 |
|
39 | 53 |
|
40 | 54 | @lru_cache(maxsize=None) |
@@ -214,6 +228,114 @@ def attach_legacy_benefit_proxies_from_frs_person( |
214 | 228 | ) |
215 | 229 |
|
216 | 230 |
|
| 231 | +def _as_non_negative_array(values) -> np.ndarray: |
| 232 | + values = np.asarray(values, dtype=float) |
| 233 | + return np.maximum(np.nan_to_num(values, nan=0.0), 0.0) |
| 234 | + |
| 235 | + |
| 236 | +def allocate_reported_education_grants( |
| 237 | + reported_grants, grant_capacities: dict[str, np.ndarray] |
| 238 | +) -> dict[str, np.ndarray]: |
| 239 | + """Split aggregate FRS education grants across modelled grant capacity. |
| 240 | +
|
| 241 | + The FRS reports several direct education grants in one aggregate field. When |
| 242 | + several modelled grants are plausible for the same person, allocate the |
| 243 | + reported amount proportionally to each grant's modelled capacity and keep any |
| 244 | + excess in the generic ``education_grants`` residual. |
| 245 | + """ |
| 246 | + |
| 247 | + reported_grants = _as_non_negative_array(reported_grants) |
| 248 | + capacities = { |
| 249 | + variable: _as_non_negative_array(capacity) |
| 250 | + for variable, capacity in grant_capacities.items() |
| 251 | + } |
| 252 | + total_capacity = np.zeros_like(reported_grants, dtype=float) |
| 253 | + for variable, capacity in capacities.items(): |
| 254 | + if capacity.shape != reported_grants.shape: |
| 255 | + raise ValueError( |
| 256 | + f"{variable} capacity has shape {capacity.shape}, " |
| 257 | + f"expected {reported_grants.shape}." |
| 258 | + ) |
| 259 | + total_capacity += capacity |
| 260 | + |
| 261 | + allocation_fraction = np.divide( |
| 262 | + reported_grants, |
| 263 | + total_capacity, |
| 264 | + out=np.zeros_like(reported_grants, dtype=float), |
| 265 | + where=total_capacity > 0, |
| 266 | + ) |
| 267 | + allocation_fraction = np.minimum(allocation_fraction, 1) |
| 268 | + |
| 269 | + allocations = {} |
| 270 | + allocated_total = np.zeros_like(reported_grants, dtype=float) |
| 271 | + for variable, capacity in capacities.items(): |
| 272 | + allocation = capacity * allocation_fraction |
| 273 | + allocations[variable] = allocation |
| 274 | + allocated_total += allocation |
| 275 | + |
| 276 | + allocations["education_grants"] = np.maximum(reported_grants - allocated_total, 0) |
| 277 | + return allocations |
| 278 | + |
| 279 | + |
| 280 | +def calculate_disabled_students_allowance_reported_grant_capacity( |
| 281 | + sim, year: int, maximum: float |
| 282 | +) -> np.ndarray: |
| 283 | + if year < DISABLED_STUDENTS_ALLOWANCE_FIRST_MODELED_YEAR: |
| 284 | + return np.zeros_like( |
| 285 | + np.asarray( |
| 286 | + sim.calculate( |
| 287 | + DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES[0], year |
| 288 | + ) |
| 289 | + ), |
| 290 | + dtype=float, |
| 291 | + ) |
| 292 | + |
| 293 | + eligible = None |
| 294 | + for variable in DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES: |
| 295 | + variable_eligible = np.asarray(sim.calculate(variable, year), dtype=bool) |
| 296 | + eligible = ( |
| 297 | + variable_eligible if eligible is None else eligible & variable_eligible |
| 298 | + ) |
| 299 | + equivalent_support = np.asarray( |
| 300 | + sim.calculate("disabled_students_allowance_receives_equivalent_support", year), |
| 301 | + dtype=bool, |
| 302 | + ) |
| 303 | + return np.where(eligible & ~equivalent_support, float(maximum), 0.0) |
| 304 | + |
| 305 | + |
| 306 | +def split_reported_education_grants( |
| 307 | + pe_person: pd.DataFrame, sim, year: int, dsa_maximum: float |
| 308 | +) -> pd.DataFrame: |
| 309 | + """Move specific modelled grants out of the generic education-grant residual. |
| 310 | +
|
| 311 | + PLA, ADG, and Childcare Grant remain formula-driven because they are |
| 312 | + calibration targets. Their modelled capacity is only used to avoid also |
| 313 | + counting the same reported FRS grant amount in the generic residual. |
| 314 | + DSA lacks a modelled amount signal, so its allocation seeds eligible |
| 315 | + expenses directly where the DSA parameter is available. |
| 316 | + """ |
| 317 | + |
| 318 | + grant_capacities = { |
| 319 | + variable: sim.calculate(variable, year) |
| 320 | + for variable in FORMULA_MODELED_EDUCATION_GRANT_VARIABLES |
| 321 | + } |
| 322 | + grant_capacities[DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT] = ( |
| 323 | + calculate_disabled_students_allowance_reported_grant_capacity( |
| 324 | + sim, year, dsa_maximum |
| 325 | + ) |
| 326 | + ) |
| 327 | + allocations = allocate_reported_education_grants( |
| 328 | + pe_person["education_grants"], grant_capacities |
| 329 | + ) |
| 330 | + |
| 331 | + pe_person["education_grants"] = allocations["education_grants"] |
| 332 | + pe_person[DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT] = allocations[ |
| 333 | + DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT |
| 334 | + ] |
| 335 | + |
| 336 | + return pe_person |
| 337 | + |
| 338 | + |
217 | 339 | def create_frs( |
218 | 340 | raw_frs_folder: str, |
219 | 341 | year: int, |
@@ -1006,6 +1128,21 @@ def determine_education_level(fted_val, typeed2_val, age_val): |
1006 | 1128 | pe_person, person, sim, year |
1007 | 1129 | ) |
1008 | 1130 |
|
| 1131 | + if (pe_person["education_grants"] > 0).any(): |
| 1132 | + student_support_dataset = UKSingleYearDataset( |
| 1133 | + person=pe_person, |
| 1134 | + benunit=pe_benunit, |
| 1135 | + household=pe_household, |
| 1136 | + fiscal_year=year, |
| 1137 | + ) |
| 1138 | + student_support_sim = Microsimulation(dataset=student_support_dataset) |
| 1139 | + dsa_maximum = student_support_sim.tax_benefit_system.parameters( |
| 1140 | + year |
| 1141 | + ).gov.dfe.disabled_students_allowance.maximum |
| 1142 | + pe_person = split_reported_education_grants( |
| 1143 | + pe_person, student_support_sim, year, dsa_maximum |
| 1144 | + ) |
| 1145 | + |
1009 | 1146 | # Generate stochastic take-up decisions |
1010 | 1147 | # All randomness is generated here in the data package using take-up rates |
1011 | 1148 | # stored in YAML parameter files. This keeps the country package purely |
|
0 commit comments