Repository navigation
Fix sensitivity_benchmark() of DID models - #435
AsadGudzevic94 wants to merge 2 commits into
Conversation
…itivity_benchmark drops them
There was a problem hiding this comment.
Hi @AsadGudzevic94, thank you very much for the fix!
The fix appears to miss models fitted with n_jobs_models > 1.
>>> import copy
... import numpy as np
... import doubleml as dml
... from doubleml.did.datasets import make_did_CS2021
... from sklearn.linear_model import LinearRegression, LogisticRegression
...
... np.random.seed(42)
... df = make_did_CS2021(n_obs=500, dgp_type=1, time_type="float")
... data = dml.DoubleMLPanelData(
... df, y_col="y", d_cols="d", id_col="id", t_col="t",
... x_cols=["Z1", "Z2", "Z3", "Z4"],
... )
... base = dml.did.DoubleMLDIDMulti(
... data, LinearRegression(), LogisticRegression(), gt_combinations="standard"
... )
...
... serial = copy.deepcopy(base).fit(n_jobs_models=1)
... parallel = copy.deepcopy(base).fit(n_jobs_models=2)
...
... print("Long-model coefficients equal:", np.allclose(serial.coef, parallel.coef))
... print("Serial delta_theta:", serial.sensitivity_benchmark(["Z1"])["delta_theta"].iloc[0])
... print("Parallel delta_theta:", parallel.sensitivity_benchmark(["Z1"])["delta_theta"].iloc[0])On the PR head, this prints:
Long-model coefficients equal: True
Serial delta_theta: -3.162367713523086
Parallel delta_theta: 0.0`
On main the behavior is different and returns:
Long-model coefficients equal: True
Serial delta_theta: 0.0
Parallel delta_theta: 0.0
it looks like parallel fitting returns group-time models with separate copies of the data object. Updating the parent models x_cols for benchmarking therefore does NOT update those copies.
Could you please check/fix this and add a parallel-fitting test?
Thank you!
Best
Jan
With n_jobs_models > 1, joblib returns copies of the group-time models with their own data object, so sensitivity_benchmark() did not drop the covariates in them. Test the benchmark with n_jobs_models=2 too.
|
Thanks @JanTeichertKluge, good catch! With I added |
Description
_x_data_subsetofDoubleMLDIDBinaryandDoubleMLDIDCSBinaryis now a property that reads the currentx_cols, instead of an array stored at initialization. The short model ofsensitivity_benchmark()(also the one ofDoubleMLDIDMulti, which refits its group-time models) is then fitted without the benchmarking covariates.New test
doubleml/did/tests/test_did_sensitivity_benchmark.py: thedelta_thetaof the benchmark equals the difference to a model fitted without the covariates, forDoubleMLDIDMultiand the binary models, with panel data and repeated cross-sections. It fails onmain(all zeros).Reference to Issues or PRs
Fixes #430
PR Checklist