-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathenhanced_cps.py
More file actions
354 lines (315 loc) · 13.4 KB
/
enhanced_cps.py
File metadata and controls
354 lines (315 loc) · 13.4 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
from policyengine_core.data import Dataset
import pandas as pd
from policyengine_us_data.utils import (
build_loss_matrix,
HardConcrete,
print_reweighting_diagnostics,
set_seeds,
)
import gc
import numpy as np
from tqdm import trange
from typing import Type
from policyengine_us_data.storage import STORAGE_FOLDER
from policyengine_us_data.datasets.cps.extended_cps import (
ExtendedCPS_2024_Half,
CPS_2024,
)
from policyengine_us_data.utils.randomness import seeded_rng
from policyengine_us_data.utils.takeup import (
ACA_POST_CALIBRATION_PERSON_TARGETS,
extend_aca_takeup_to_match_target,
)
import logging
try:
import torch
except ImportError:
torch = None
def _get_period_array(period_values: dict, period: int) -> np.ndarray:
"""Get a period array from a TIME_PERIOD_ARRAYS variable dict."""
value = period_values.get(period)
if value is None:
value = period_values.get(str(period))
if value is None:
raise KeyError(f"Missing period {period}")
return np.asarray(value)
def create_aca_2025_takeup_override(
base_takeup: np.ndarray,
person_enrolled_if_takeup: np.ndarray,
person_weights: np.ndarray,
person_tax_unit_ids: np.ndarray,
tax_unit_ids: np.ndarray,
target_people: float = ACA_POST_CALIBRATION_PERSON_TARGETS[2025],
) -> np.ndarray:
"""Add 2025 ACA takers until weighted APTC enrollment hits target."""
tax_unit_id_to_idx = {
int(tax_unit_id): idx for idx, tax_unit_id in enumerate(tax_unit_ids)
}
person_tax_unit_idx = np.array(
[tax_unit_id_to_idx[int(tax_unit_id)] for tax_unit_id in person_tax_unit_ids],
dtype=np.int64,
)
enrolled_person_weights = np.zeros(len(tax_unit_ids), dtype=np.float64)
np.add.at(
enrolled_person_weights,
person_tax_unit_idx,
person_enrolled_if_takeup.astype(np.float64) * person_weights,
)
draws = seeded_rng("takes_up_aca_if_eligible").random(len(tax_unit_ids))
return extend_aca_takeup_to_match_target(
base_takeup=np.asarray(base_takeup, dtype=bool),
entity_draws=draws,
enrolled_person_weights=enrolled_person_weights,
target_people=target_people,
)
def reweight(
original_weights,
loss_matrix,
targets_array,
log_path="calibration_log.csv",
epochs=500,
l0_lambda=2.6445e-07,
init_mean=0.999, # initial proportion with non-zero weights
temperature=0.25,
seed=1456,
):
target_names = np.array(loss_matrix.columns)
is_national = loss_matrix.columns.str.startswith("nation/")
loss_matrix = torch.tensor(loss_matrix.values, dtype=torch.float32)
nation_normalisation_factor = is_national * (1 / is_national.sum())
state_normalisation_factor = ~is_national * (1 / (~is_national).sum())
normalisation_factor = np.where(
is_national, nation_normalisation_factor, state_normalisation_factor
)
normalisation_factor = torch.tensor(normalisation_factor, dtype=torch.float32)
targets_array = torch.tensor(targets_array, dtype=torch.float32)
inv_mean_normalisation = 1 / np.mean(normalisation_factor.numpy())
def loss(weights):
if torch.isnan(weights).any():
raise ValueError("Weights contain NaNs")
if torch.isnan(loss_matrix).any():
raise ValueError("Loss matrix contains NaNs")
estimate = weights @ loss_matrix
if torch.isnan(estimate).any():
raise ValueError("Estimate contains NaNs")
rel_error = (((estimate - targets_array) + 1) / (targets_array + 1)) ** 2
rel_error_normalized = inv_mean_normalisation * rel_error * normalisation_factor
if torch.isnan(rel_error_normalized).any():
raise ValueError("Relative error contains NaNs")
return rel_error_normalized.mean()
logging.info(
f"Sparse optimization using seed {seed}, temp {temperature} "
+ f"init_mean {init_mean}, l0_lambda {l0_lambda}"
)
set_seeds(seed)
weights = torch.tensor(
np.log(original_weights), requires_grad=True, dtype=torch.float32
)
gates = HardConcrete(
len(original_weights), init_mean=init_mean, temperature=temperature
)
# NOTE: Results are pretty sensitve to learning rates
# optimizer breaks down somewhere near .005, does better at above .1
optimizer = torch.optim.Adam([weights] + list(gates.parameters()), lr=0.2)
start_loss = None
iterator = trange(epochs * 2) # lower learning rate, harder optimization
performance = pd.DataFrame()
for i in iterator:
optimizer.zero_grad()
masked = torch.exp(weights) * gates()
l_main = loss(masked)
loss_value = l_main + l0_lambda * gates.get_penalty()
if (log_path is not None) and (i % 10 == 0):
gates.eval()
estimates = (torch.exp(weights) * gates()) @ loss_matrix
gates.train()
estimates = estimates.detach().numpy()
df = pd.DataFrame(
{
"target_name": target_names,
"estimate": estimates,
"target": targets_array.detach().numpy(),
}
)
df["epoch"] = i
df["error"] = df.estimate - df.target
df["rel_error"] = df.error / df.target
df["abs_error"] = df.error.abs()
df["rel_abs_error"] = df.rel_error.abs()
df["loss"] = df.rel_abs_error**2
performance = pd.concat([performance, df], ignore_index=True)
if (log_path is not None) and (i % 1000 == 0):
performance.to_csv(log_path, index=False)
if start_loss is None:
start_loss = loss_value.item()
loss_rel_change = (loss_value.item() - start_loss) / start_loss
loss_value.backward()
iterator.set_postfix(
{"loss": loss_value.item(), "loss_rel_change": loss_rel_change}
)
optimizer.step()
if log_path is not None:
performance.to_csv(log_path, index=False)
gates.eval()
final_weights_sparse = (torch.exp(weights) * gates()).detach().numpy()
print_reweighting_diagnostics(
final_weights_sparse,
loss_matrix,
targets_array,
"L0 Sparse Solution",
)
return final_weights_sparse
class EnhancedCPS(Dataset):
data_format = Dataset.TIME_PERIOD_ARRAYS
input_dataset: Type[Dataset]
start_year: int
end_year: int
def generate(self):
from policyengine_us import Microsimulation
sim = Microsimulation(dataset=self.input_dataset)
data = sim.dataset.load_dataset()
base_year = int(sim.default_calculation_period)
data["household_weight"] = {}
original_weights = sim.calculate("household_weight")
original_weights = original_weights.values + np.random.normal(
1, 0.1, len(original_weights)
)
bad_targets = [
"nation/irs/adjusted gross income/total/AGI in 10k-15k/taxable/Head of Household",
"nation/irs/adjusted gross income/total/AGI in 15k-20k/taxable/Head of Household",
"nation/irs/adjusted gross income/total/AGI in 10k-15k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/adjusted gross income/total/AGI in 15k-20k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/count/count/AGI in 10k-15k/taxable/Head of Household",
"nation/irs/count/count/AGI in 15k-20k/taxable/Head of Household",
"nation/irs/count/count/AGI in 10k-15k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/count/count/AGI in 15k-20k/taxable/Married Filing Jointly/Surviving Spouse",
"state/RI/adjusted_gross_income/amount/-inf_1",
"nation/irs/adjusted gross income/total/AGI in 10k-15k/taxable/Head of Household",
"nation/irs/adjusted gross income/total/AGI in 15k-20k/taxable/Head of Household",
"nation/irs/adjusted gross income/total/AGI in 10k-15k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/adjusted gross income/total/AGI in 15k-20k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/count/count/AGI in 10k-15k/taxable/Head of Household",
"nation/irs/count/count/AGI in 15k-20k/taxable/Head of Household",
"nation/irs/count/count/AGI in 10k-15k/taxable/Married Filing Jointly/Surviving Spouse",
"nation/irs/count/count/AGI in 15k-20k/taxable/Married Filing Jointly/Surviving Spouse",
"state/RI/adjusted_gross_income/amount/-inf_1",
"nation/irs/exempt interest/count/AGI in -inf-inf/taxable/All",
]
# Run the optimization procedure to get (close to) minimum loss weights
for year in range(self.start_year, self.end_year + 1):
loss_matrix, targets_array = build_loss_matrix(self.input_dataset, year)
zero_mask = np.isclose(targets_array, 0.0, atol=0.1)
bad_mask = loss_matrix.columns.isin(bad_targets)
keep_mask_bool = ~(zero_mask | bad_mask)
keep_idx = np.where(keep_mask_bool)[0]
loss_matrix_clean = loss_matrix.iloc[:, keep_idx]
targets_array_clean = targets_array[keep_idx]
del loss_matrix, targets_array
gc.collect()
assert loss_matrix_clean.shape[1] == targets_array_clean.size
loss_matrix_clean = loss_matrix_clean.astype(np.float32)
optimised_weights = reweight(
original_weights,
loss_matrix_clean,
targets_array_clean,
log_path="calibration_log.csv",
epochs=250,
seed=1456,
)
data["household_weight"][year] = optimised_weights
# Validate dense weights
w = optimised_weights
if np.any(np.isnan(w)):
raise ValueError(f"Year {year}: household_weight contains NaN values")
if np.any(w < 0):
raise ValueError(
f"Year {year}: household_weight contains negative values"
)
weighted_hh_count = float(np.sum(w))
if not (1e8 <= weighted_hh_count <= 2e8):
raise ValueError(
f"Year {year}: weighted household count "
f"{weighted_hh_count:,.0f} outside expected range "
f"[100M, 200M]"
)
logging.info(
f"Year {year}: weights validated — "
f"{weighted_hh_count:,.0f} weighted households, "
f"{int(np.sum(w > 0))} non-zero"
)
if 2025 in ACA_POST_CALIBRATION_PERSON_TARGETS:
sim.set_input(
"household_weight",
base_year,
_get_period_array(data["household_weight"], base_year).astype(
np.float32
),
)
sim.set_input(
"takes_up_aca_if_eligible",
2025,
np.ones(
len(_get_period_array(data["tax_unit_id"], base_year)),
dtype=bool,
),
)
sim.delete_arrays("aca_ptc")
data["takes_up_aca_if_eligible"][2025] = create_aca_2025_takeup_override(
base_takeup=_get_period_array(
data["takes_up_aca_if_eligible"],
base_year,
),
person_enrolled_if_takeup=np.asarray(
sim.calculate(
"aca_ptc",
map_to="person",
period=2025,
use_weights=False,
)
)
> 0,
person_weights=np.asarray(
sim.calculate(
"person_weight",
period=2025,
use_weights=False,
)
),
person_tax_unit_ids=_get_period_array(
data["person_tax_unit_id"],
base_year,
),
tax_unit_ids=_get_period_array(data["tax_unit_id"], base_year),
)
logging.info("Post-generation weight validation passed")
self.save_dataset(data)
class ReweightedCPS_2024(Dataset):
data_format = Dataset.ARRAYS
file_path = STORAGE_FOLDER / "reweighted_cps_2024.h5"
name = "reweighted_cps_2024"
label = "Reweighted CPS 2024"
input_dataset = CPS_2024
time_period = 2024
def generate(self):
from policyengine_us import Microsimulation
sim = Microsimulation(dataset=self.input_dataset)
data = sim.dataset.load_dataset()
original_weights = sim.calculate("household_weight")
original_weights = original_weights.values + np.random.normal(
1, 0.1, len(original_weights)
)
for year in [2024]:
loss_matrix, targets_array = build_loss_matrix(self.input_dataset, year)
optimised_weights = reweight(original_weights, loss_matrix, targets_array)
data["household_weight"] = optimised_weights
self.save_dataset(data)
class EnhancedCPS_2024(EnhancedCPS):
input_dataset = ExtendedCPS_2024_Half
start_year = 2024
end_year = 2024
name = "enhanced_cps_2024"
label = "Enhanced CPS 2024"
file_path = STORAGE_FOLDER / "enhanced_cps_2024.h5"
url = "hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5"
if __name__ == "__main__":
EnhancedCPS_2024().generate()