-
Notifications
You must be signed in to change notification settings - Fork 10
Impute tips #220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Impute tips #220
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a1f68d6
Add initial notebook
nikhilwoodruff 6af5bf9
Add update to notebook
nikhilwoodruff e7b1382
Add to data creation
nikhilwoodruff 2dffbb5
Add tips
nikhilwoodruff 74c8096
Update python version
nikhilwoodruff 93fe974
Use hf file
nikhilwoodruff 3f659d3
Don't retrain
nikhilwoodruff 17518d5
Model, not dataset
nikhilwoodruff 2948c89
Don't download the full CSV
nikhilwoodruff 215866e
Add missing import
nikhilwoodruff 7a0458b
Add test
nikhilwoodruff 3581146
Remove notebook
nikhilwoodruff 4c13d73
Add tip income
nikhilwoodruff a747d1d
Add calibration of tip income
nikhilwoodruff 1d5b8d0
Format
nikhilwoodruff 16046dd
Address comments
nikhilwoodruff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ | |
| !spm_threshold_agi.csv | ||
| **/_build | ||
| !population_by_state.csv | ||
| **/*.pkl | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| - bump: minor | ||
| changes: | ||
| added: | ||
| - Tip income. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from .sipp import train_tip_model, get_tip_model |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import pandas as pd | ||
| from microdf import MicroDataFrame | ||
| import numpy as np | ||
| from policyengine_us import Microsimulation | ||
| from microimpute.models import QRF | ||
| from policyengine_us_data.storage import STORAGE_FOLDER | ||
| import pickle | ||
| from huggingface_hub import hf_hub_download | ||
|
|
||
|
|
||
| def train_tip_model(): | ||
| DOWNLOAD_FULL_SIPP = False | ||
|
|
||
| if DOWNLOAD_FULL_SIPP: | ||
| hf_hub_download( | ||
| repo_id="PolicyEngine/policyengine-us-data", | ||
| filename="pu2023.csv", | ||
| repo_type="model", | ||
| local_dir=STORAGE_FOLDER, | ||
| ) | ||
| cols = [ | ||
| "SSUID", | ||
| "PNUM", | ||
| "MONTHCODE", | ||
| "ERESIDENCEID", | ||
| "ERELRPE", | ||
| "SPANEL", | ||
| "SWAVE", | ||
| "WPFINWGT", | ||
| "ESEX", | ||
| "TAGE", | ||
| "TAGE_EHC", | ||
| "ERACE", | ||
| "EORIGIN", | ||
| "EEDUC", | ||
| "EDEPCLM", | ||
| "EMS", | ||
| "EFSTATUS", | ||
| "TJB1_TXAMT", | ||
| "TJB1_MSUM", | ||
| "TJB1_OCC", | ||
| "TJB1_IND", | ||
| "AJB1_TXAMT", | ||
| "TPTOTINC", | ||
| ] | ||
|
|
||
| for col in cols: | ||
| if "JB1" in col: | ||
| for i in range(2, 8): | ||
| cols.append(col.replace("JB1", f"JB{i}")) | ||
|
|
||
| df = pd.read_csv( | ||
| STORAGE_FOLDER / "pu2023.csv", | ||
| delimiter="|", | ||
| usecols=cols, | ||
| ) | ||
|
|
||
| else: | ||
| hf_hub_download( | ||
| repo_id="PolicyEngine/policyengine-us-data", | ||
| filename="pu2023_slim.csv", | ||
| repo_type="model", | ||
| local_dir=STORAGE_FOLDER, | ||
| ) | ||
| df = pd.read_csv( | ||
| STORAGE_FOLDER / "pu2023_slim.csv", | ||
| ) | ||
| # Sum tip columns (AJB*_TXAMT + TJB*_TXAMT) across all jobs. | ||
| df["tip_income"] = ( | ||
| df[df.columns[df.columns.str.contains("TXAMT")]].fillna(0).sum(axis=1) | ||
| * 12 | ||
| ) | ||
| df["employment_income"] = df.TPTOTINC * 12 | ||
| df["is_under_18"] = (df.TAGE < 18) & (df.MONTHCODE == 12) | ||
| df["is_under_6"] = (df.TAGE < 6) & (df.MONTHCODE == 12) | ||
| df["count_under_18"] = ( | ||
| df.groupby("SSUID")["is_under_18"].sum().loc[df.SSUID.values].values | ||
| ) | ||
| df["count_under_6"] = ( | ||
| df.groupby("SSUID")["is_under_6"].sum().loc[df.SSUID.values].values | ||
| ) | ||
| df["household_weight"] = df.WPFINWGT | ||
| df["household_id"] = df.SSUID | ||
| df["age"] = df.TAGE | ||
|
|
||
| sipp = df[ | ||
| [ | ||
| "household_id", | ||
| "employment_income", | ||
| "tip_income", | ||
| "count_under_18", | ||
| "count_under_6", | ||
| "age", | ||
| "household_weight", | ||
| ] | ||
| ] | ||
|
|
||
| sipp = sipp[~sipp.isna().any(axis=1)] | ||
|
|
||
| sipp = sipp.loc[ | ||
| np.random.choice( | ||
| sipp.index, | ||
| size=100_000, | ||
| replace=True, | ||
| p=sipp.household_weight / sipp.household_weight.sum(), | ||
| ) | ||
| ] | ||
|
|
||
| model = QRF() | ||
|
|
||
| model = model.fit( | ||
nikhilwoodruff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| X_train=sipp, | ||
| predictors=[ | ||
| "employment_income", | ||
| "age", | ||
| "count_under_18", | ||
| "count_under_6", | ||
| ], | ||
| imputed_variables=["tip_income"], | ||
| ) | ||
|
|
||
| return model | ||
|
|
||
|
|
||
| def get_tip_model() -> QRF: | ||
| model_path = STORAGE_FOLDER / "tips.pkl" | ||
|
|
||
| if not model_path.exists(): | ||
| model = train_tip_model() | ||
|
|
||
| with open(model_path, "wb") as f: | ||
| pickle.dump(model, f) | ||
| else: | ||
| with open(model_path, "rb") as f: | ||
| model = pickle.load(f) | ||
|
|
||
| return model | ||
2 changes: 2 additions & 0 deletions
2
policyengine_us_data/storage/download_public_prerequisites.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,13 +15,14 @@ authors = [ | |
| {name = "PolicyEngine", email = "[email protected]"}, | ||
| ] | ||
| license = {file = "LICENSE"} | ||
| requires-python = ">=3.10, <3.13.0" | ||
| requires-python = ">=3.11, <3.13.0" | ||
| dependencies = [ | ||
| "policyengine_us>=1.197.0", | ||
| "policyengine_core>=3.14.1", | ||
| "requests", | ||
| "tqdm", | ||
| "microdf_python>=0.4.3", | ||
| "microimpute", | ||
| ] | ||
|
|
||
| [project.optional-dependencies] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.