Skip to content

Commit 9f7ad45

Browse files
committed
Format structural mortgage support files
1 parent 35817a5 commit 9f7ad45

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

policyengine_us_data/datasets/puf/puf.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,10 @@ def generate(self):
646646
self.holder[key] = np.array(self.holder[key]).astype(float)
647647
assert not np.isnan(self.holder[key]).any(), f"{key} has NaNs."
648648

649-
holder_tp = {variable: {self.time_period: values} for variable, values in self.holder.items()}
649+
holder_tp = {
650+
variable: {self.time_period: values}
651+
for variable, values in self.holder.items()
652+
}
650653
holder_tp = convert_mortgage_interest_to_structural_inputs(
651654
holder_tp,
652655
self.time_period,

policyengine_us_data/tests/test_calibration/test_mortgage_interest.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,8 @@ def test_structural_mortgage_conversion_keeps_balance_hints_for_non_itemizers():
117117

118118
converted = convert_mortgage_interest_to_structural_inputs(data, 2024)
119119

120-
assert converted["first_home_mortgage_balance"][2024][0] == pytest.approx(
121-
250_000.0
122-
)
123-
assert converted["second_home_mortgage_balance"][2024][0] == pytest.approx(
124-
25_000.0
125-
)
120+
assert converted["first_home_mortgage_balance"][2024][0] == pytest.approx(250_000.0)
121+
assert converted["second_home_mortgage_balance"][2024][0] == pytest.approx(25_000.0)
126122
assert converted["first_home_mortgage_interest"][2024][0] == 0
127123
assert converted["second_home_mortgage_interest"][2024][0] == 0
128124
assert converted["first_home_mortgage_origination_year"][2024][0] > 0

policyengine_us_data/utils/mortgage_interest.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,9 @@ def _build_tax_unit_mortgage_receiver(
327327
person_tax_unit_ids = np.asarray(person_tax_unit_ids)
328328
is_head = np.asarray(is_head, dtype=bool)
329329
n_tax_units = len(tax_unit_ids)
330-
tax_unit_index = {int(tax_unit_id): idx for idx, tax_unit_id in enumerate(tax_unit_ids)}
330+
tax_unit_index = {
331+
int(tax_unit_id): idx for idx, tax_unit_id in enumerate(tax_unit_ids)
332+
}
331333
person_tax_unit_idx = np.array(
332334
[tax_unit_index[int(tax_unit_id)] for tax_unit_id in person_tax_unit_ids],
333335
dtype=np.int32,
@@ -418,13 +420,9 @@ def _build_scf_mortgage_donor(scf: pd.DataFrame) -> pd.DataFrame:
418420
donor["is_female"] = _frame_column(scf, "is_female")
419421
donor["cps_race"] = _frame_column(scf, "cps_race")
420422
donor["is_married"] = _frame_column(scf, "is_married")
421-
donor["own_children_in_household"] = _frame_column(
422-
scf, "own_children_in_household"
423-
)
423+
donor["own_children_in_household"] = _frame_column(scf, "own_children_in_household")
424424
donor["employment_income"] = _frame_column(scf, "employment_income")
425-
donor["interest_dividend_income"] = _frame_column(
426-
scf, "interest_dividend_income"
427-
)
425+
donor["interest_dividend_income"] = _frame_column(scf, "interest_dividend_income")
428426
donor["social_security_pension_income"] = _frame_column(
429427
scf, "social_security_pension_income"
430428
)
@@ -527,7 +525,11 @@ def _tax_unit_mortgage_owner_status(
527525
spm_tenure = data.get("spm_unit_tenure_type", {}).get(time_period)
528526
person_spm_unit_id = data.get("person_spm_unit_id", {}).get(time_period)
529527
spm_unit_ids = data.get("spm_unit_id", {}).get(time_period)
530-
if spm_tenure is not None and person_spm_unit_id is not None and spm_unit_ids is not None:
528+
if (
529+
spm_tenure is not None
530+
and person_spm_unit_id is not None
531+
and spm_unit_ids is not None
532+
):
531533
spm_map = {
532534
int(spm_unit_id): _decode_owner_status(value)
533535
for spm_unit_id, value in zip(spm_unit_ids, spm_tenure)
@@ -723,9 +725,13 @@ def _split_interest_by_balance(
723725

724726
with_second = total_balance > 0
725727
first_interest[with_second] = (
726-
total_interest[with_second] * first_balance[with_second] / total_balance[with_second]
728+
total_interest[with_second]
729+
* first_balance[with_second]
730+
/ total_balance[with_second]
731+
)
732+
second_interest[with_second] = (
733+
total_interest[with_second] - first_interest[with_second]
727734
)
728-
second_interest[with_second] = total_interest[with_second] - first_interest[with_second]
729735

730736
no_second = second_balance == 0
731737
first_interest[no_second] = total_interest[no_second]

0 commit comments

Comments
 (0)