py-why / dowhy

DoWhy is a Python library for causal inference that supports explicit modeling and testing of causal assumptions. DoWhy is based on a unified language for causal inference, combining causal graphical models and potential outcomes frameworks.
https://www.pywhy.org/dowhy
MIT License
7.1k stars 934 forks source link

Using dowhy with casual forests and FLAML #348

Closed EgorKraevTransferwise closed 2 years ago

EgorKraevTransferwise commented 2 years ago

Just wanted to share a code snippet on how to use DoWhy and EconML with another gem from Microsoft, FLAML:

from flaml import AutoML
class AutoMLWrapper(AutoML):
    def __init__(self, *args, fit_params=None, **kwargs):
        super().__init__(*args, **kwargs)
        if fit_params is not None:
            self.fit_params = fit_params
        else:
            self.fit_params = {}

    def fit(self, *args, **kwargs):
        used_kwargs = {**kwargs, **self.fit_params}
        super().fit(*args, **used_kwargs)

dml_estimate = model.estimate_effect(identified_estimand, method_name="backdoor.econml.dml.CausalForestDML",
                                     control_value = 0,
                                     treatment_value =1,
                                 target_units = "ate",  # condition used for CATE
                                 confidence_intervals=True,
                                method_params={"init_params":{'model_t': AutoMLWrapper(fit_params = {'time_budget': 60, 'verbose':1, 'task':'classification'}),
                                                              'model_y': AutoMLWrapper(fit_params = {'time_budget': 60, 'verbose':1, 'task':'regression'}),
                                                              "discrete_treatment":True,
                                                               },
                                               "fit_params":{}})
dml_estimate.interpret()

The only nontrivial bit is passing extra arguments to FLAML's fit() method - perhaps worth supporting that in method_params so the wrapper above is not necessary?

amit-sharma commented 2 years ago

thanks for adding this snippet, @EgorKraevTransferwise . Great to see that you've got AutoML working with DoWhy/EconML.

The integration with AutoML is not on our priority list, so for now, the wrapper seems like a good workaround.

EgorKraevTransferwise commented 2 years ago

Thanks for your reply! Actually I just realized that this once implemented would be exactly what I'm looking for.

amit-sharma commented 2 years ago

Great, closing this.