matheusfacure / python-causality-handbook

Causal Inference for the Brave and True. A light-hearted yet rigorous approach to learning about impact estimation and causality.
https://matheusfacure.github.io/python-causality-handbook/landing-page.html
MIT License
2.65k stars 463 forks source link

Conformal Inference for Synthetic Controls #270

Closed david26694 closed 1 year ago

david26694 commented 1 year ago

Hi, thanks for the book, it's very good.

In this code snippet in here:


@curry
def residuals(df, state, null, intervention_start, window, model):

    null_data = with_effect(df, state, null, intervention_start, window)

    model.fit(null_data.drop(columns=[state]), null_data[state])

    y0_est = pd.Series(model.predict(null_data.drop(columns=[state])), index=null_data.index)

    residuals = null_data[state] - y0_est

    test_mask = (null_data.index >= intervention_start) & (null_data.index < (intervention_start + window))

    return pd.DataFrame({
        "y0": null_data[state],
        "y0_est": y0_est,
        "residuals": residuals,
        "post_intervention": test_mask
    })[lambda d: d.index < (intervention_start + window)]  # just discard  points after the defined effect window

I believe the model.fit should be called on training (pre-intervention) data, while it's being called on all data. Is this right?