|
| 1 | +import pandas as pd |
| 2 | +from microdf import MicroDataFrame |
| 3 | +import numpy as np |
| 4 | +from policyengine_us import Microsimulation |
| 5 | +from microimpute.models import QRF |
| 6 | +from policyengine_us_data.storage import STORAGE_FOLDER |
| 7 | +import pickle |
| 8 | +from huggingface_hub import hf_hub_download |
| 9 | + |
| 10 | + |
| 11 | +def train_tip_model(): |
| 12 | + DOWNLOAD_FULL_SIPP = False |
| 13 | + |
| 14 | + if DOWNLOAD_FULL_SIPP: |
| 15 | + hf_hub_download( |
| 16 | + repo_id="PolicyEngine/policyengine-us-data", |
| 17 | + filename="pu2023.csv", |
| 18 | + repo_type="model", |
| 19 | + local_dir=STORAGE_FOLDER, |
| 20 | + ) |
| 21 | + cols = [ |
| 22 | + "SSUID", |
| 23 | + "PNUM", |
| 24 | + "MONTHCODE", |
| 25 | + "ERESIDENCEID", |
| 26 | + "ERELRPE", |
| 27 | + "SPANEL", |
| 28 | + "SWAVE", |
| 29 | + "WPFINWGT", |
| 30 | + "ESEX", |
| 31 | + "TAGE", |
| 32 | + "TAGE_EHC", |
| 33 | + "ERACE", |
| 34 | + "EORIGIN", |
| 35 | + "EEDUC", |
| 36 | + "EDEPCLM", |
| 37 | + "EMS", |
| 38 | + "EFSTATUS", |
| 39 | + "TJB1_TXAMT", |
| 40 | + "TJB1_MSUM", |
| 41 | + "TJB1_OCC", |
| 42 | + "TJB1_IND", |
| 43 | + "AJB1_TXAMT", |
| 44 | + "TPTOTINC", |
| 45 | + ] |
| 46 | + |
| 47 | + for col in cols: |
| 48 | + if "JB1" in col: |
| 49 | + for i in range(2, 8): |
| 50 | + cols.append(col.replace("JB1", f"JB{i}")) |
| 51 | + |
| 52 | + df = pd.read_csv( |
| 53 | + STORAGE_FOLDER / "pu2023.csv", |
| 54 | + delimiter="|", |
| 55 | + usecols=cols, |
| 56 | + ) |
| 57 | + |
| 58 | + else: |
| 59 | + hf_hub_download( |
| 60 | + repo_id="PolicyEngine/policyengine-us-data", |
| 61 | + filename="pu2023_slim.csv", |
| 62 | + repo_type="model", |
| 63 | + local_dir=STORAGE_FOLDER, |
| 64 | + ) |
| 65 | + df = pd.read_csv( |
| 66 | + STORAGE_FOLDER / "pu2023_slim.csv", |
| 67 | + ) |
| 68 | + # Sum tip columns (AJB*_TXAMT + TJB*_TXAMT) across all jobs. |
| 69 | + df["tip_income"] = ( |
| 70 | + df[df.columns[df.columns.str.contains("TXAMT")]].fillna(0).sum(axis=1) |
| 71 | + * 12 |
| 72 | + ) |
| 73 | + df["employment_income"] = df.TPTOTINC * 12 |
| 74 | + df["is_under_18"] = (df.TAGE < 18) & (df.MONTHCODE == 12) |
| 75 | + df["is_under_6"] = (df.TAGE < 6) & (df.MONTHCODE == 12) |
| 76 | + df["count_under_18"] = ( |
| 77 | + df.groupby("SSUID")["is_under_18"].sum().loc[df.SSUID.values].values |
| 78 | + ) |
| 79 | + df["count_under_6"] = ( |
| 80 | + df.groupby("SSUID")["is_under_6"].sum().loc[df.SSUID.values].values |
| 81 | + ) |
| 82 | + df["household_weight"] = df.WPFINWGT |
| 83 | + df["household_id"] = df.SSUID |
| 84 | + df["age"] = df.TAGE |
| 85 | + |
| 86 | + sipp = df[ |
| 87 | + [ |
| 88 | + "household_id", |
| 89 | + "employment_income", |
| 90 | + "tip_income", |
| 91 | + "count_under_18", |
| 92 | + "count_under_6", |
| 93 | + "age", |
| 94 | + "household_weight", |
| 95 | + ] |
| 96 | + ] |
| 97 | + |
| 98 | + sipp = sipp[~sipp.isna().any(axis=1)] |
| 99 | + |
| 100 | + sipp = sipp.loc[ |
| 101 | + np.random.choice( |
| 102 | + sipp.index, |
| 103 | + size=100_000, |
| 104 | + replace=True, |
| 105 | + p=sipp.household_weight / sipp.household_weight.sum(), |
| 106 | + ) |
| 107 | + ] |
| 108 | + |
| 109 | + model = QRF() |
| 110 | + |
| 111 | + model = model.fit( |
| 112 | + X_train=sipp, |
| 113 | + predictors=[ |
| 114 | + "employment_income", |
| 115 | + "age", |
| 116 | + "count_under_18", |
| 117 | + "count_under_6", |
| 118 | + ], |
| 119 | + imputed_variables=["tip_income"], |
| 120 | + ) |
| 121 | + |
| 122 | + return model |
| 123 | + |
| 124 | + |
| 125 | +def get_tip_model() -> QRF: |
| 126 | + model_path = STORAGE_FOLDER / "tips.pkl" |
| 127 | + |
| 128 | + if not model_path.exists(): |
| 129 | + model = train_tip_model() |
| 130 | + |
| 131 | + with open(model_path, "wb") as f: |
| 132 | + pickle.dump(model, f) |
| 133 | + else: |
| 134 | + with open(model_path, "rb") as f: |
| 135 | + model = pickle.load(f) |
| 136 | + |
| 137 | + return model |
0 commit comments