|
| 1 | +""" |
| 2 | +Salary sacrifice imputation for pension contributions. |
| 3 | +
|
| 4 | +This module imputes salary sacrifice pension amounts using QRF trained on |
| 5 | +FRS respondents who were asked the SALSAC question. The model predicts |
| 6 | +the continuous amount (pension_contributions_via_salary_sacrifice), with |
| 7 | +non-participants naturally having 0. |
| 8 | +
|
| 9 | +Training data (FRS 2023-24): |
| 10 | +- SALSAC='1' (Yes): ~224 jobs with reported SPNAMT amounts |
| 11 | +- SALSAC='2' (No): ~3,803 jobs with SPNAMT=0 |
| 12 | +
|
| 13 | +Imputation candidates: |
| 14 | +- SALSAC=' ' (skip/not asked): ~13,265 jobs |
| 15 | +
|
| 16 | +Targeting to HMRC totals (~24bn SS contributions) happens via weight |
| 17 | +calibration, not in this imputation step. |
| 18 | +""" |
| 19 | + |
| 20 | +import pandas as pd |
| 21 | +import numpy as np |
| 22 | +from policyengine_uk_data.storage import STORAGE_FOLDER |
| 23 | +from policyengine_uk.data import UKSingleYearDataset |
| 24 | +from policyengine_uk import Microsimulation |
| 25 | + |
| 26 | + |
| 27 | +PREDICTORS = [ |
| 28 | + "age", |
| 29 | + "employment_income", |
| 30 | +] |
| 31 | + |
| 32 | +IMPUTATIONS = [ |
| 33 | + "pension_contributions_via_salary_sacrifice", |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +def save_salary_sacrifice_model(): |
| 38 | + """ |
| 39 | + Train and save salary sacrifice imputation model using FRS data. |
| 40 | +
|
| 41 | + Uses FRS respondents who were asked about salary sacrifice (SALSAC field) |
| 42 | + as training data. The model learns to predict the SS pension amount |
| 43 | + directly - non-participants have 0, participants have their reported |
| 44 | + SPNAMT value. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + Trained QRF model for salary sacrifice imputation. |
| 48 | + """ |
| 49 | + from policyengine_uk_data.utils import QRF |
| 50 | + |
| 51 | + # Load the base FRS dataset |
| 52 | + frs_path = STORAGE_FOLDER / "frs_2023_24.h5" |
| 53 | + if not frs_path.exists(): |
| 54 | + raise FileNotFoundError( |
| 55 | + f"FRS dataset not found at {frs_path}. " |
| 56 | + "Run create_frs() first to generate the base dataset." |
| 57 | + ) |
| 58 | + |
| 59 | + dataset = UKSingleYearDataset(frs_path) |
| 60 | + sim = Microsimulation(dataset=dataset) |
| 61 | + |
| 62 | + # Get predictor variables |
| 63 | + age = sim.calculate("age").values |
| 64 | + employment_income = sim.calculate("employment_income").values |
| 65 | + |
| 66 | + # Get SS amounts and indicator for who was asked |
| 67 | + ss_amount = ( |
| 68 | + dataset.person.pension_contributions_via_salary_sacrifice.values |
| 69 | + ) |
| 70 | + if "salary_sacrifice_asked" not in dataset.person.columns: |
| 71 | + raise ValueError( |
| 72 | + "Dataset missing salary_sacrifice_asked field. " |
| 73 | + "Ensure frs.py extracts SALSAC before numeric conversion." |
| 74 | + ) |
| 75 | + ss_asked = dataset.person.salary_sacrifice_asked.values |
| 76 | + |
| 77 | + # Build training DataFrame with only those who were asked |
| 78 | + # This includes both participants (with amounts) and non-participants (0) |
| 79 | + training_mask = ss_asked == 1 |
| 80 | + |
| 81 | + if training_mask.sum() == 0: |
| 82 | + raise ValueError( |
| 83 | + "No training data found - no respondents were asked SALSAC." |
| 84 | + ) |
| 85 | + |
| 86 | + train_df = pd.DataFrame( |
| 87 | + { |
| 88 | + "age": age[training_mask], |
| 89 | + "employment_income": employment_income[training_mask], |
| 90 | + "pension_contributions_via_salary_sacrifice": ss_amount[ |
| 91 | + training_mask |
| 92 | + ], |
| 93 | + } |
| 94 | + ) |
| 95 | + |
| 96 | + n_participants = ( |
| 97 | + train_df["pension_contributions_via_salary_sacrifice"] > 0 |
| 98 | + ).sum() |
| 99 | + print(f"Training salary sacrifice model on {len(train_df)} observations") |
| 100 | + print( |
| 101 | + f" With SS contributions: {n_participants} " |
| 102 | + f"({n_participants / len(train_df):.1%})" |
| 103 | + ) |
| 104 | + mean_amount = train_df.loc[ |
| 105 | + train_df["pension_contributions_via_salary_sacrifice"] > 0, |
| 106 | + "pension_contributions_via_salary_sacrifice", |
| 107 | + ].mean() |
| 108 | + print(f" Mean SS amount (participants): £{mean_amount:,.0f}") |
| 109 | + |
| 110 | + # Train QRF model |
| 111 | + model = QRF() |
| 112 | + model.fit(train_df[PREDICTORS], train_df[IMPUTATIONS]) |
| 113 | + model.save(STORAGE_FOLDER / "salary_sacrifice.pkl") |
| 114 | + |
| 115 | + return model |
| 116 | + |
| 117 | + |
| 118 | +def create_salary_sacrifice_model(overwrite_existing: bool = False): |
| 119 | + """ |
| 120 | + Create or load salary sacrifice imputation model. |
| 121 | +
|
| 122 | + Args: |
| 123 | + overwrite_existing: Whether to retrain model if it exists. |
| 124 | +
|
| 125 | + Returns: |
| 126 | + Trained QRF model for salary sacrifice imputation. |
| 127 | + """ |
| 128 | + from policyengine_uk_data.utils.qrf import QRF |
| 129 | + |
| 130 | + model_path = STORAGE_FOLDER / "salary_sacrifice.pkl" |
| 131 | + if model_path.exists() and not overwrite_existing: |
| 132 | + return QRF(file_path=model_path) |
| 133 | + return save_salary_sacrifice_model() |
| 134 | + |
| 135 | + |
| 136 | +def impute_salary_sacrifice( |
| 137 | + dataset: UKSingleYearDataset, |
| 138 | +) -> UKSingleYearDataset: |
| 139 | + """ |
| 140 | + Impute salary sacrifice pension amounts for FRS non-respondents. |
| 141 | +
|
| 142 | + For respondents not asked about salary sacrifice (SALSAC=' '), uses |
| 143 | + a QRF model trained on those who were asked to predict the SS pension |
| 144 | + contribution amount directly. The model naturally predicts 0 for |
| 145 | + non-participants and positive amounts for likely participants. |
| 146 | +
|
| 147 | + Note: This imputation does NOT target any specific total. Targeting |
| 148 | + to HMRC figures happens via weight calibration in a subsequent step. |
| 149 | +
|
| 150 | + Args: |
| 151 | + dataset: PolicyEngine UK dataset with salary_sacrifice_asked |
| 152 | + field from FRS processing. |
| 153 | +
|
| 154 | + Returns: |
| 155 | + Dataset with imputed salary sacrifice amounts. |
| 156 | + """ |
| 157 | + dataset = dataset.copy() |
| 158 | + sim = Microsimulation(dataset=dataset) |
| 159 | + |
| 160 | + # Get variables needed for imputation |
| 161 | + age = sim.calculate("age").values |
| 162 | + employment_income = sim.calculate("employment_income").values |
| 163 | + current_ss = ( |
| 164 | + dataset.person.pension_contributions_via_salary_sacrifice.values |
| 165 | + ) |
| 166 | + |
| 167 | + # Get indicator for who was asked |
| 168 | + if "salary_sacrifice_asked" not in dataset.person.columns: |
| 169 | + print( |
| 170 | + "Warning: salary_sacrifice_asked not in dataset, " |
| 171 | + "skipping imputation" |
| 172 | + ) |
| 173 | + return dataset |
| 174 | + |
| 175 | + ss_asked = dataset.person.salary_sacrifice_asked.values |
| 176 | + |
| 177 | + # Identify imputation candidates: those not asked about SS |
| 178 | + not_asked = ss_asked == 0 |
| 179 | + |
| 180 | + # Create prediction DataFrame for all records |
| 181 | + pred_df = pd.DataFrame( |
| 182 | + { |
| 183 | + "age": age, |
| 184 | + "employment_income": employment_income, |
| 185 | + } |
| 186 | + ) |
| 187 | + |
| 188 | + # Get or train model and predict |
| 189 | + model = create_salary_sacrifice_model() |
| 190 | + predictions = model.predict(pred_df) |
| 191 | + |
| 192 | + # Get imputed amounts (QRF predicts continuous values) |
| 193 | + imputed_ss = predictions[ |
| 194 | + "pension_contributions_via_salary_sacrifice" |
| 195 | + ].values |
| 196 | + |
| 197 | + # Ensure non-negative |
| 198 | + imputed_ss = np.maximum(0, imputed_ss) |
| 199 | + |
| 200 | + # For those who were asked, keep their reported values |
| 201 | + # For those not asked, use the imputed values |
| 202 | + final_ss = np.where( |
| 203 | + ss_asked == 1, |
| 204 | + current_ss, # Keep reported values exactly |
| 205 | + imputed_ss, # Use imputed for non-respondents |
| 206 | + ) |
| 207 | + |
| 208 | + # Update dataset |
| 209 | + dataset.person["pension_contributions_via_salary_sacrifice"] = final_ss |
| 210 | + |
| 211 | + # Report results (no targeting - just descriptive) |
| 212 | + weights = sim.calculate("person_weight").values |
| 213 | + is_employee = employment_income > 0 |
| 214 | + total_ss = (final_ss * weights).sum() |
| 215 | + participation_rate = ((final_ss > 0) * weights * is_employee).sum() / ( |
| 216 | + weights * is_employee |
| 217 | + ).sum() |
| 218 | + |
| 219 | + print("Salary sacrifice imputation results (pre-calibration):") |
| 220 | + print(f" Total SS contributions: £{total_ss / 1e9:.1f}bn") |
| 221 | + print(f" Employee participation rate: {participation_rate:.1%}") |
| 222 | + print(" (Final totals depend on subsequent weight calibration)") |
| 223 | + |
| 224 | + return dataset |
0 commit comments