-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfrs.py
More file actions
1537 lines (1351 loc) · 53.5 KB
/
Copy pathfrs.py
File metadata and controls
1537 lines (1351 loc) · 53.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Family Resources Survey (FRS) dataset processing for PolicyEngine UK.
This module processes raw FRS survey data into PolicyEngine UK dataset format,
handling household demographics, income, benefits, and other survey variables.
The FRS is the primary source of UK household survey data used for tax-benefit
modelling and policy analysis.
"""
from functools import lru_cache
from pathlib import Path
import numpy as np
import pandas as pd
from policyengine_uk import CountryTaxBenefitSystem
from policyengine_uk.data import UKSingleYearDataset
from policyengine_uk.variables.household.income.employment_status import (
EmploymentStatus,
)
from policyengine_uk_data.utils.datasets import (
sum_to_entity,
categorical,
sum_from_positive_fields,
sum_positive_variables,
fill_with_mean,
STORAGE_FOLDER,
)
from policyengine_uk_data.parameters import load_take_up_rate, load_parameter
# Canonical weeks-per-year conversion factor for annualising weekly survey
# variables. 365.25 / 7 ≈ 52.1786 accounts for leap years; using the rounded
# integer 52 would under-count by ~0.34%. Exposed at module level so sibling
# loaders (e.g. LCFS/ETB in `datasets/imputations/consumption.py`) can import
# the same value rather than re-defining `* 52` locally and drifting.
WEEKS_IN_YEAR = 365.25 / 7
LEGACY_JOBSEEKER_MIN_AGE = 18
HOURS_WORKED_WEEKS_PER_YEAR = 52
ESA_MIN_AGE = 16
ESA_HEALTH_EMPLOYMENT_STATUSES = (
EmploymentStatus.LONG_TERM_DISABLED.name,
EmploymentStatus.SHORT_TERM_DISABLED.name,
)
FORMULA_MODELED_EDUCATION_GRANT_VARIABLES = (
"childcare_grant",
"parents_learning_allowance",
"adult_dependants_grant",
)
DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT = (
"disabled_students_allowance_eligible_expenses"
)
DISABLED_STUDENTS_ALLOWANCE_FIRST_MODELED_YEAR = 2025
DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES = (
"maintenance_loan_in_england_system",
"disabled_students_allowance_course_eligible",
"disabled_students_allowance_has_qualifying_condition",
)
PIP_CATEGORY_SAFETY_MARGIN = 0.1
def _category_from_reported(
reported,
thresholds: tuple[tuple[str, float], ...],
) -> np.ndarray:
"""Convert annual reported amounts to PE-UK category inputs."""
reported_weekly = pd.Series(reported).fillna(0).astype(float) / WEEKS_IN_YEAR
return np.select(
[
reported_weekly >= rate * (1 - PIP_CATEGORY_SAFETY_MARGIN)
for _, rate in thresholds
],
[category for category, _ in thresholds],
default="NONE",
)
BENEFITS_IN_OWN_RIGHT_REPORTED_COLUMNS = (
"universal_credit_reported",
"jsa_contrib_reported",
"jsa_income_reported",
"esa_contrib_reported",
"esa_income_reported",
)
NON_ADVANCED_EDUCATION_LEVELS = (
"PRE_PRIMARY",
"PRIMARY",
"LOWER_SECONDARY",
"UPPER_SECONDARY",
"POST_SECONDARY",
)
# FRS government-training question variants use 10 or 13 for "None of these".
FRS_APPROVED_TRAINING_CODES = tuple(range(1, 10))
UNKNOWN_QUALIFYING_EDUCATION_OR_TRAINING_ENTRY_AGE = 1000
@lru_cache(maxsize=None)
def load_legacy_jobseeker_max_annual_hours(year: int) -> int:
"""Read the JSA single-claimant hours rule from policyengine-uk."""
system = CountryTaxBenefitSystem()
max_weekly_hours = int(system.parameters.gov.dwp.JSA.hours.single(str(year)))
return max_weekly_hours * HOURS_WORKED_WEEKS_PER_YEAR
def derive_legacy_jobseeker_proxy(
age,
employment_status,
hours_worked,
current_education,
employment_status_reported,
state_pension_age,
max_annual_hours,
) -> np.ndarray:
"""Approximate legacy JSA claimant-state from observed survey data.
This is intentionally a proxy, not a legislative determination. It
identifies person-level working-age adults who report being unemployed
and working less than the legacy JSA 16-hour weekly limit. The
``hours_worked`` input is the annualised FRS-derived measure used in the
dataset, so the threshold is converted to annual hours here.
"""
age = np.asarray(age)
employment_status = np.asarray(employment_status)
hours_worked = np.asarray(hours_worked)
current_education = np.asarray(current_education)
employment_status_reported = np.asarray(employment_status_reported)
state_pension_age = np.asarray(state_pension_age)
return (
employment_status_reported
& (age >= LEGACY_JOBSEEKER_MIN_AGE)
& (age < state_pension_age)
& (employment_status == "UNEMPLOYED")
& (hours_worked < max_annual_hours)
& (current_education == "NOT_IN_EDUCATION")
)
def derive_esa_health_condition_proxy(
age,
employment_status,
employment_status_reported,
state_pension_age,
) -> np.ndarray:
"""Approximate working-age ESA health-related claimant-state.
This proxy relies only on person-level labour market status, not on
current disability or incapacity benefit receipt. It is a dataset-side
approximation for future modelling, not a direct observation of ESA
legal entitlement or LCW/LCWRA status.
"""
age = np.asarray(age)
employment_status = np.asarray(employment_status)
employment_status_reported = np.asarray(employment_status_reported)
state_pension_age = np.asarray(state_pension_age)
disability_labour_market_state = np.isin(
employment_status, ESA_HEALTH_EMPLOYMENT_STATUSES
)
return (
employment_status_reported
& (age >= ESA_MIN_AGE)
& (age < state_pension_age)
& disability_labour_market_state
)
def derive_esa_support_group_proxy(
age,
employment_status,
hours_worked,
esa_health_condition_proxy,
employment_status_reported,
state_pension_age,
) -> np.ndarray:
"""Approximate a severe-health ESA subgroup akin to support group.
This is a stricter subset of ``esa_health_condition_proxy`` intended
for future legacy ESA approximation work. It uses only non-receipt
labour market signals already available in the survey.
"""
age = np.asarray(age)
employment_status = np.asarray(employment_status)
hours_worked = np.asarray(hours_worked)
esa_health_condition_proxy = np.asarray(esa_health_condition_proxy)
employment_status_reported = np.asarray(employment_status_reported)
state_pension_age = np.asarray(state_pension_age)
severe_health_evidence = (employment_status == "LONG_TERM_DISABLED") & (
hours_worked <= 0
)
return (
employment_status_reported
& (age >= ESA_MIN_AGE)
& (age < state_pension_age)
& esa_health_condition_proxy
& severe_health_evidence
)
def derive_receives_benefits_in_own_right(pe_person: pd.DataFrame) -> pd.Series:
"""Identify people reporting adult benefits that end QYP status."""
return (
pe_person[list(BENEFITS_IN_OWN_RIGHT_REPORTED_COLUMNS)].fillna(0).sum(axis=1)
> 0
)
def derive_is_in_non_advanced_education(
current_education,
is_apprentice=None,
) -> np.ndarray:
"""Identify current non-advanced education from PolicyEngine education states."""
current_education = np.asarray(current_education)
if is_apprentice is None:
is_apprentice = np.zeros(len(current_education), dtype=bool)
else:
is_apprentice = np.asarray(is_apprentice)
return np.isin(current_education, NON_ADVANCED_EDUCATION_LEVELS) & ~is_apprentice
def derive_is_in_approved_training_from_frs_person(
person: pd.DataFrame,
) -> pd.Series:
"""Identify reported government training scheme participation in FRS."""
if "train" not in person.columns:
return pd.Series(False, index=person.index)
train = pd.to_numeric(person.train, errors="coerce").fillna(0)
return train.isin(FRS_APPROVED_TRAINING_CODES)
def derive_age_started_or_accepted_current_education_or_training(
age,
is_in_non_advanced_education,
is_in_approved_training,
) -> np.ndarray:
"""Approximate the entry age for current QYP education or training.
FRS observes current education/training status but not the age at which the
current course or programme was started, enrolled on, or accepted. For
people currently in qualifying education/training, cap the imputed entry
age at 18 so observed 19-year-olds remain eligible for rules requiring
entry before age 19.
"""
age = np.asarray(age)
in_qualifying_education_or_training = np.asarray(
is_in_non_advanced_education
) | np.asarray(is_in_approved_training)
return np.where(
in_qualifying_education_or_training,
np.minimum(age, 18),
UNKNOWN_QUALIFYING_EDUCATION_OR_TRAINING_ENTRY_AGE,
)
def derive_is_before_universal_credit_qualifying_young_person_terminal_date(
age,
is_in_non_advanced_education,
is_in_approved_training,
) -> np.ndarray:
"""Approximate the UC terminal-date condition for observed 19-year-olds.
FRS does not expose the date-of-birth and assessment-period detail needed
to identify the exact 1 September terminal date. Use current qualifying
education/training status as the microdata proxy for age-19 records.
"""
age = np.asarray(age)
in_qualifying_education_or_training = np.asarray(
is_in_non_advanced_education
) | np.asarray(is_in_approved_training)
return (age == 19) & in_qualifying_education_or_training
def add_legacy_benefit_proxies(
pe_person: pd.DataFrame,
employment_status_reported,
state_pension_age,
legacy_jobseeker_max_annual_hours,
) -> pd.DataFrame:
"""Populate person-scoped ESA/JSA proxy columns on the person frame.
These remain person-level by design because the claimant-state inputs
they approximate attach to individuals. Downstream benunit-level legacy
benefit models should aggregate them explicitly rather than assuming the
raw survey contains a benunit claimant-state field.
"""
pe_person["legacy_jobseeker_proxy"] = derive_legacy_jobseeker_proxy(
age=pe_person.age,
employment_status=pe_person.employment_status,
hours_worked=pe_person.hours_worked,
current_education=pe_person.current_education,
employment_status_reported=employment_status_reported,
state_pension_age=state_pension_age,
max_annual_hours=legacy_jobseeker_max_annual_hours,
)
pe_person["esa_health_condition_proxy"] = derive_esa_health_condition_proxy(
age=pe_person.age,
employment_status=pe_person.employment_status,
employment_status_reported=employment_status_reported,
state_pension_age=state_pension_age,
)
pe_person["esa_support_group_proxy"] = derive_esa_support_group_proxy(
age=pe_person.age,
employment_status=pe_person.employment_status,
hours_worked=pe_person.hours_worked,
esa_health_condition_proxy=pe_person.esa_health_condition_proxy,
employment_status_reported=employment_status_reported,
state_pension_age=state_pension_age,
)
return pe_person
def apply_legacy_benefit_proxies(
pe_person: pd.DataFrame, sim, year: int, employment_status_reported
) -> pd.DataFrame:
"""Attach legacy ESA/JSA proxies using post-build simulation context."""
state_pension_age = sim.calculate("state_pension_age", year).values
legacy_jobseeker_max_annual_hours = load_legacy_jobseeker_max_annual_hours(year)
return add_legacy_benefit_proxies(
pe_person,
employment_status_reported=employment_status_reported,
state_pension_age=state_pension_age,
legacy_jobseeker_max_annual_hours=legacy_jobseeker_max_annual_hours,
)
def attach_legacy_benefit_proxies_from_frs_person(
pe_person: pd.DataFrame, person: pd.DataFrame, sim, year: int
) -> pd.DataFrame:
"""Bridge raw FRS person fields into the proxy derivation hook."""
employment_status_reported = person.empstati.fillna(0).to_numpy() > 0
return apply_legacy_benefit_proxies(
pe_person,
sim,
year,
employment_status_reported=employment_status_reported,
)
def derive_is_parent_from_frs_microdata(
person_ids,
person_benunit_ids,
adult_person_ids,
benunit_ids,
dependent_children,
) -> np.ndarray:
"""Identify FRS adults in benefit units with dependent children.
FRS benefit units contain either one adult or a couple plus any dependent
children. Using the raw adult table and benefit-unit dependent-child count
avoids ranking adults across the whole household when multiple benefit
units share a household.
"""
dependent_children_by_benunit = pd.Series(
np.asarray(dependent_children, dtype=float),
index=np.asarray(benunit_ids),
)
has_dependent_children = (
pd.Series(np.asarray(person_benunit_ids))
.map(dependent_children_by_benunit)
.fillna(0)
.to_numpy()
> 0
)
is_adult_record = np.isin(np.asarray(person_ids), np.asarray(adult_person_ids))
return is_adult_record & has_dependent_children
def _as_non_negative_array(values) -> np.ndarray:
values = np.asarray(values, dtype=float)
return np.maximum(np.nan_to_num(values, nan=0.0), 0.0)
def allocate_reported_education_grants(
reported_grants, grant_capacities: dict[str, np.ndarray]
) -> dict[str, np.ndarray]:
"""Split aggregate FRS education grants across modelled grant capacity.
The FRS reports several direct education grants in one aggregate field. When
several modelled grants are plausible for the same person, allocate the
reported amount proportionally to each grant's modelled capacity and keep any
excess in the generic ``education_grants`` residual.
"""
reported_grants = _as_non_negative_array(reported_grants)
capacities = {
variable: _as_non_negative_array(capacity)
for variable, capacity in grant_capacities.items()
}
total_capacity = np.zeros_like(reported_grants, dtype=float)
for variable, capacity in capacities.items():
if capacity.shape != reported_grants.shape:
raise ValueError(
f"{variable} capacity has shape {capacity.shape}, "
f"expected {reported_grants.shape}."
)
total_capacity += capacity
allocation_fraction = np.divide(
reported_grants,
total_capacity,
out=np.zeros_like(reported_grants, dtype=float),
where=total_capacity > 0,
)
allocation_fraction = np.minimum(allocation_fraction, 1)
allocations = {}
allocated_total = np.zeros_like(reported_grants, dtype=float)
for variable, capacity in capacities.items():
allocation = capacity * allocation_fraction
allocations[variable] = allocation
allocated_total += allocation
allocations["education_grants"] = np.maximum(reported_grants - allocated_total, 0)
return allocations
def calculate_disabled_students_allowance_reported_grant_capacity(
sim, year: int, maximum: float
) -> np.ndarray:
if year < DISABLED_STUDENTS_ALLOWANCE_FIRST_MODELED_YEAR:
return np.zeros_like(
np.asarray(
sim.calculate(
DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES[0], year
)
),
dtype=float,
)
eligible = None
for variable in DISABLED_STUDENTS_ALLOWANCE_ELIGIBILITY_VARIABLES:
variable_eligible = np.asarray(sim.calculate(variable, year), dtype=bool)
eligible = (
variable_eligible if eligible is None else eligible & variable_eligible
)
equivalent_support = np.asarray(
sim.calculate("disabled_students_allowance_receives_equivalent_support", year),
dtype=bool,
)
return np.where(eligible & ~equivalent_support, float(maximum), 0.0)
def split_reported_education_grants(
pe_person: pd.DataFrame, sim, year: int, dsa_maximum: float
) -> pd.DataFrame:
"""Move specific modelled grants out of the generic education-grant residual.
PLA, ADG, and Childcare Grant remain formula-driven because they are
calibration targets. Their modelled capacity is only used to avoid also
counting the same reported FRS grant amount in the generic residual.
DSA lacks a modelled amount signal, so its allocation seeds eligible
expenses directly where the DSA parameter is available.
"""
grant_capacities = {
variable: sim.calculate(variable, year)
for variable in FORMULA_MODELED_EDUCATION_GRANT_VARIABLES
}
grant_capacities[DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT] = (
calculate_disabled_students_allowance_reported_grant_capacity(
sim, year, dsa_maximum
)
)
allocations = allocate_reported_education_grants(
pe_person["education_grants"], grant_capacities
)
pe_person["education_grants"] = allocations["education_grants"]
pe_person[DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT] = allocations[
DISABLED_STUDENTS_ALLOWANCE_EXPENSE_INPUT
]
return pe_person
def create_frs(
raw_frs_folder: str,
year: int,
) -> UKSingleYearDataset:
"""
Process raw FRS data into PolicyEngine UK dataset format.
Transforms the Family Resources Survey microdata from raw tab-delimited
files into a structured PolicyEngine UK dataset with person, benefit unit,
and household-level variables mapped to the appropriate tax-benefit system
variables.
Args:
raw_frs_folder: Path to folder containing raw FRS .tab files.
year: Survey year for the dataset.
Returns:
UKSingleYearDataset with processed FRS data ready for policy simulation.
"""
raw_folder = Path(raw_frs_folder)
if not raw_folder.exists():
raise FileNotFoundError(f"Raw folder {raw_folder} does not exist.")
frs = {}
# Store SALSAC values before numeric conversion (for salary sacrifice
# imputation)
job_salsac_raw = None
for file in raw_folder.glob("*.tab"):
table_name = file.stem
# Read raw data first
df_raw = pd.read_csv(file, sep="\t")
df_raw.columns = df_raw.columns.str.lower()
# Preserve SALSAC column from job table before numeric conversion
# SALSAC indicates salary sacrifice participation:
# '1' = Yes, '2' = No, ' ' or blank = skip/not asked
if table_name == "job" and "salsac" in df_raw.columns:
job_salsac_raw = df_raw["salsac"].copy()
# Make numeric where possible
df = df_raw.apply(pd.to_numeric, errors="coerce")
# Standardise column names to lower case (already done above)
# df.columns = df.columns.str.lower()
# Edit ID variables for simplicity
if "sernum" in df.columns:
df.rename(columns={"sernum": "household_id"}, inplace=True)
if "benunit" in df.columns:
# In the tables, benunit is the index of the benefit unit *within* the household.
df.rename(columns={"benunit": "benunit_id"}, inplace=True)
df["benunit_id"] = (df["household_id"] * 1e2 + df["benunit_id"]).astype(int)
if "person" in df.columns:
df.rename(columns={"person": "person_id"}, inplace=True)
df["person_id"] = (df["household_id"] * 1e3 + df["person_id"]).astype(int)
frs[table_name] = df
# Combine adult and child tables for convenience
frs["person"] = pd.concat([frs["adult"], frs["child"]]).sort_index().fillna(0)
person = frs["person"]
benunit = frs["benunit"]
household = frs["househol"]
household = household.set_index("household_id")
pension = frs["pension"]
oddjob = frs["oddjob"]
account = frs["accounts"]
job = frs["job"]
# Add raw SALSAC column to job table for salary sacrifice imputation
# SALSAC values: '1' = Yes (participates), '2' = No, ' '/blank = not asked
if job_salsac_raw is not None:
job["salsac_raw"] = job_salsac_raw.values
benefits = frs["benefits"]
maintenance = frs["maint"]
pen_prov = frs["penprov"]
childcare = frs["chldcare"]
extchild = frs["extchild"]
mortgage = frs["mortgage"]
pe_person = pd.DataFrame()
pe_benunit = pd.DataFrame()
pe_household = pd.DataFrame()
# Add primary and foreign keys
pe_person["person_id"] = person.person_id
pe_person["person_benunit_id"] = person.benunit_id
pe_person["person_household_id"] = person.household_id
pe_benunit["benunit_id"] = benunit.benunit_id
pe_household["household_id"] = person.household_id.sort_values().unique()
# Add grossing weights
pe_household["household_weight"] = household.gross4.values
# Add basic personal variables
age = person.age80 + person.age
pe_person["age"] = age
# birth_year should be calculated from age and period in the model,
# not stored as static data (see PolicyEngine/policyengine-uk#1352)
# Age fields are AGE80 (top-coded) and AGE in the adult and child tables, respectively.
pe_person["gender"] = np.where(person.sex == 1, "MALE", "FEMALE")
pe_person["hours_worked"] = np.maximum(person.tothours, 0) * 52
pe_person["is_household_head"] = person.hrpid == 1
pe_person["is_benunit_head"] = person.uperson == 1
dependent_children = (
benunit.depchldb
if "depchldb" in benunit
else frs["child"]
.groupby("benunit_id")
.size()
.reindex(benunit.benunit_id)
.fillna(0)
.to_numpy()
)
pe_person["is_parent"] = derive_is_parent_from_frs_microdata(
person_ids=pe_person.person_id,
person_benunit_ids=pe_person.person_benunit_id,
adult_person_ids=frs["adult"].person_id,
benunit_ids=pe_benunit.benunit_id,
dependent_children=dependent_children,
)
MARITAL = [
"MARRIED",
"SINGLE",
"SINGLE",
"WIDOWED",
"SEPARATED",
"DIVORCED",
]
pe_person["marital_status"] = categorical(
person.marital, 2, range(1, 7), MARITAL
).fillna("SINGLE")
# Add education levels
if "fted" in person.columns:
fted = person.fted
else:
fted = person.educft # Renamed in FRS 2022-23
typeed2 = person.typeed2
def determine_education_level(fted_val, typeed2_val, age_val):
# By default, not in education
if fted_val in (2, -1, 0):
return "NOT_IN_EDUCATION"
# In pre-primary
elif typeed2_val == 1:
return "PRE_PRIMARY"
# In primary education
elif (
typeed2_val in (2, 4)
or (typeed2_val in (3, 8) and age_val < 11)
or (typeed2_val == 0 and fted_val == 1 and age_val > 5 and age_val < 11)
):
return "PRIMARY"
# In lower secondary
elif (
typeed2_val in (5, 6)
or (typeed2_val in (3, 8) and age_val >= 11 and age_val <= 16)
or (typeed2_val == 0 and fted_val == 1 and age_val <= 16)
):
return "LOWER_SECONDARY"
# In upper secondary
elif (
typeed2_val == 7
or (typeed2_val in (3, 8) and age_val > 16)
or (typeed2_val == 0 and fted_val == 1 and age_val > 16)
):
return "UPPER_SECONDARY"
# In post-secondary
elif typeed2_val in (7, 8) and age_val >= 19:
return "POST_SECONDARY"
# In tertiary
elif typeed2_val == 9 or (typeed2_val == 0 and fted_val == 1 and age_val >= 19):
return "TERTIARY"
else:
return "NOT_IN_EDUCATION"
# Apply the function to determine education level
pe_person["current_education"] = pd.Series(
[determine_education_level(f, t, a) for f, t, a in zip(fted, typeed2, age)],
index=pe_person.index,
)
pe_person["is_in_non_advanced_education"] = derive_is_in_non_advanced_education(
pe_person.current_education
)
pe_person["is_in_approved_training"] = (
derive_is_in_approved_training_from_frs_person(person)
)
pe_person["age_started_or_accepted_current_education_or_training"] = (
derive_age_started_or_accepted_current_education_or_training(
age,
pe_person.is_in_non_advanced_education,
pe_person.is_in_approved_training,
)
)
pe_person["is_before_universal_credit_qualifying_young_person_terminal_date"] = (
derive_is_before_universal_credit_qualifying_young_person_terminal_date(
age,
pe_person.is_in_non_advanced_education,
pe_person.is_in_approved_training,
)
)
# Add highest education from EDUCQUAL (highest qualification achieved)
# Codes from FRS ADT_324X classification; unmapped codes default to UPPER_SECONDARY
EDUCQUAL_MAP = {
1: "NOT_COMPLETED_PRIMARY",
2: "LOWER_SECONDARY", # GCSE D-G / CSE 2-5
3: "LOWER_SECONDARY", # GCSE A-C / O-level A-C
4: "UPPER_SECONDARY", # AS-level
5: "UPPER_SECONDARY", # A-level (1 subject)
6: "UPPER_SECONDARY", # A-level (2 subjects)
7: "UPPER_SECONDARY", # A-level (3+ subjects)
8: "LOWER_SECONDARY", # Scottish Standard/Ordinary Grade
9: "UPPER_SECONDARY", # Scottish Higher Grade
10: "UPPER_SECONDARY", # Scottish 6th Year Studies
11: "POST_SECONDARY", # HNC/HND
12: "POST_SECONDARY", # City & Guilds advanced / BTEC National
13: "UPPER_SECONDARY", # City & Guilds craft / BTEC General
14: "POST_SECONDARY", # ONC/OND / BTEC National (lower)
15: "UPPER_SECONDARY", # City & Guilds foundation
16: "POST_SECONDARY", # RSA advanced
17: "TERTIARY", # First/foundation degree
18: "TERTIARY", # Second degree
19: "TERTIARY", # Higher degree (Masters/PhD)
20: "TERTIARY", # PGCE / teaching qualification
21: "TERTIARY", # Nursing/paramedical qualification
66: "UPPER_SECONDARY", # NVQ/SVQ Level 1
67: "UPPER_SECONDARY", # NVQ/SVQ Level 2
68: "UPPER_SECONDARY", # NVQ/SVQ Level 3
69: "POST_SECONDARY", # NVQ/SVQ Level 4
70: "TERTIARY", # NVQ/SVQ Level 5
}
# Codes 22-65 and 71-85 are further vocational/professional qualifications;
# treat as POST_SECONDARY. Codes 86-87 are catch-alls; treat as UPPER_SECONDARY.
for code in range(22, 66):
EDUCQUAL_MAP[code] = "POST_SECONDARY"
for code in range(71, 86):
EDUCQUAL_MAP[code] = "POST_SECONDARY"
EDUCQUAL_MAP[86] = "UPPER_SECONDARY"
EDUCQUAL_MAP[87] = "UPPER_SECONDARY"
educqual = pd.to_numeric(person.educqual, errors="coerce")
pe_person["highest_education"] = educqual.map(EDUCQUAL_MAP).fillna(
"UPPER_SECONDARY"
)
# Add employment status
EMPLOYMENTS = [
"CHILD",
"FT_EMPLOYED",
"PT_EMPLOYED",
"FT_SELF_EMPLOYED",
"PT_SELF_EMPLOYED",
"UNEMPLOYED",
"RETIRED",
"STUDENT",
"CARER",
"LONG_TERM_DISABLED",
"SHORT_TERM_DISABLED",
]
pe_person["employment_status"] = categorical(
person.empstati, 1, range(12), EMPLOYMENTS
).fillna("LONG_TERM_DISABLED")
REGIONS = [
"NORTH_EAST",
"NORTH_WEST",
"YORKSHIRE",
"EAST_MIDLANDS",
"WEST_MIDLANDS",
"EAST_OF_ENGLAND",
"LONDON",
"SOUTH_EAST",
"SOUTH_WEST",
"WALES",
"SCOTLAND",
"NORTHERN_IRELAND",
"UNKNOWN",
]
pe_household["region"] = categorical(
household.gvtregno, 14, [1, 2] + list(range(4, 15)), REGIONS
).values
TENURES = [
"RENT_FROM_COUNCIL",
"RENT_FROM_HA",
"RENT_PRIVATELY",
"RENT_PRIVATELY",
"OWNED_OUTRIGHT",
"OWNED_WITH_MORTGAGE",
]
pe_household["tenure_type"] = categorical(
household.ptentyp2, 3, range(1, 7), TENURES
).values
frs["num_bedrooms"] = household.bedroom6
ACCOMMODATIONS = [
"HOUSE_DETACHED",
"HOUSE_SEMI_DETACHED",
"HOUSE_TERRACED",
"FLAT",
"CONVERTED_HOUSE",
"MOBILE",
"OTHER",
]
pe_household["accommodation_type"] = categorical(
household.typeacc, 1, range(1, 8), ACCOMMODATIONS
).values
# Impute Council Tax
# Only ~25% of household report Council Tax bills - use
# these to build a model to impute missing values
CT_valid = household.ctannual > 0
# Find the mean reported Council Tax bill for a given
# (region, CT band, is-single-person-household) triplet
region = household.gvtregno[CT_valid]
band = household.ctband[CT_valid]
single_person = (household.adulth == 1)[CT_valid]
ctannual = household.ctannual[CT_valid]
# Build the table
ct_mean = ctannual.groupby([region, band, single_person], dropna=False).mean()
ct_mean = ct_mean.replace(-1, ct_mean.mean())
# For every household consult the table to find the imputed
# Council Tax bill
pairs = household.set_index(
[household.gvtregno, household.ctband, (household.adulth == 1)]
)
hh_CT_mean = pd.Series(index=pairs.index)
has_mean = pairs.index.isin(ct_mean.index)
hh_CT_mean[has_mean] = ct_mean[pairs.index[has_mean]].values
hh_CT_mean[~has_mean] = 0
ct_imputed = hh_CT_mean
# For households which originally reported Council Tax,
# use the reported value. Otherwise, use the imputed value
council_tax = pd.Series(
np.where(
# 2018 FRS uses blanks for missing values, 2019 FRS
# uses -1 for missing values
(household.ctannual < 0) | household.ctannual.isna(),
np.maximum(ct_imputed, 0).values,
household.ctannual,
)
)
pe_household["council_tax"] = council_tax.fillna(0)
BANDS = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]
# Band 1 is the most common
pe_household["council_tax_band"] = (
categorical(household.ctband, 1, range(1, 10), BANDS).fillna("D").values
)
# Domestic rates variables are all weeklyised, unlike Council Tax variables (despite the variable name suggesting otherwise)
if year < 2021:
DOMESTIC_RATES_VARIABLE = "rtannual"
else:
DOMESTIC_RATES_VARIABLE = "niratlia"
pe_household["domestic_rates"] = (
np.select(
[
household[DOMESTIC_RATES_VARIABLE] >= 0,
household.rt2rebam >= 0,
True,
],
[
household[DOMESTIC_RATES_VARIABLE],
household.rt2rebam,
0,
],
)
* 52
).astype(float)
WEEKS_IN_YEAR = 365.25 / 7
pe_person["employment_income"] = np.maximum(0, person.inearns) * WEEKS_IN_YEAR
pension_payment = sum_to_entity(
pension.penpay * (pension.penpay > 0),
pension.person_id,
person.person_id,
)
pension_tax_paid = sum_to_entity(
(pension.ptamt * ((pension.ptinc == 2) & (pension.ptamt > 0))),
pension.person_id,
person.person_id,
)
pension_deductions_removed = sum_to_entity(
pension.poamt
* (((pension.poinc == 2) | (pension.penoth == 1)) & (pension.poamt > 0)),
pension.person_id,
person.person_id,
)
pe_person["private_pension_income"] = (
pension_payment + pension_tax_paid + pension_deductions_removed
) * WEEKS_IN_YEAR
pe_person["self_employment_income"] = np.maximum(0, person.seincam2) * WEEKS_IN_YEAR
INVERTED_BASIC_RATE = 1.25
pe_person["tax_free_savings_income"] = np.maximum(
0,
sum_to_entity(
account.accint * (account.account == 21),
account.person_id,
person.person_id,
)
* WEEKS_IN_YEAR,
)
taxable_savings_interest = (
sum_to_entity(
(account.accint * np.where(account.acctax == 1, INVERTED_BASIC_RATE, 1))
* (account.account.isin((1, 3, 5, 27, 28))),
account.person_id,
person.person_id,
)
* WEEKS_IN_YEAR
)
pe_person["savings_interest_income"] = np.maximum(
0,
taxable_savings_interest + pe_person["tax_free_savings_income"].values,
)
pe_person["dividend_income"] = np.maximum(
0,
sum_to_entity(
(account.accint * np.where(account.invtax == 1, INVERTED_BASIC_RATE, 1))
* (
((account.account == 6) & (account.invtax == 1)) # GGES
| account.account.isin((7, 8)) # Stocks/shares/UITs
),
account.person_id,
person.index,
)
* 52,
)
is_head = person.hrpid == 1
household_property_income = (
household.tentyp2.isin((5, 6)) * household.subrent
) # Owned and subletting
persons_household_property_income = (
pd.Series(
household_property_income[person.household_id].values,
index=person.person_id,
)
.fillna(0)
.values
)
pe_person["property_income"] = (
np.maximum(
0,
is_head * persons_household_property_income + person.cvpay + person.royyr1,
)
* WEEKS_IN_YEAR
)
maintenance_to_self = np.maximum(
pd.Series(np.where(person.mntus1 == 2, person.mntusam1, person.mntamt1)).fillna(
0
),
0,
)
maintenance_from_dwp = person.mntamt2
pe_person["maintenance_income"] = (
sum_positive_variables([maintenance_to_self, maintenance_from_dwp])
* WEEKS_IN_YEAR
)
odd_job_income = sum_to_entity(
oddjob.ojamt * (oddjob.ojnow == 1), oddjob.person_id, person.person_id
)
MISC_INCOME_FIELDS = [
"allpay2",
"royyr2",
"royyr3",
"royyr4",
"chamtern",
"chamttst",
]
pe_person["miscellaneous_income"] = (
odd_job_income + sum_from_positive_fields(person, MISC_INCOME_FIELDS)
) * WEEKS_IN_YEAR
PRIVATE_TRANSFER_INCOME_FIELDS = [
"apamt",
"apdamt",
"pareamt",
"allpay2",
"allpay3",
"allpay4",
]
pe_person["private_transfer_income"] = (
sum_from_positive_fields(person, PRIVATE_TRANSFER_INCOME_FIELDS) * WEEKS_IN_YEAR
)
pe_person["lump_sum_income"] = person.redamt
pe_person["student_loan_repayments"] = person.slrepamt * WEEKS_IN_YEAR