{"id":17131594,"url":"https://github.com/janteichertkluge/dmlsim","last_synced_at":"2025-04-13T07:55:20.566Z","repository":{"id":65224378,"uuid":"587930443","full_name":"JanTeichertKluge/DMLSim","owner":"JanTeichertKluge","description":"This library provides packages on DoubleML / Causal Machine Learning and Neural Networks in Python for Simulation and Case Studies.","archived":false,"fork":false,"pushed_at":"2023-06-20T09:31:28.000Z","size":148,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T07:55:14.979Z","etag":null,"topics":["beit","bert","case-study","causal","causal-inference","causal-machine-learning","deep-learning","dgp","double-machine-learning","doubleml","machine-learning","multi-modal","multimodal","multimodal-deep-learning","neural-network","simulation","transformer","transformers"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JanTeichertKluge.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-01-11T23:15:40.000Z","updated_at":"2025-02-14T09:36:33.000Z","dependencies_parsed_at":"2023-02-17T08:35:26.379Z","dependency_job_id":"5ddac022-8de2-4f11-a404-e6794e219110","html_url":"https://github.com/JanTeichertKluge/DMLSim","commit_stats":{"total_commits":92,"total_committers":4,"mean_commits":23.0,"dds":"0.32608695652173914","last_synced_commit":"6509fa179ca3eefc78df87bbc91577456f19d6dc"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanTeichertKluge%2FDMLSim","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanTeichertKluge%2FDMLSim/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanTeichertKluge%2FDMLSim/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JanTeichertKluge%2FDMLSim/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JanTeichertKluge","download_url":"https://codeload.github.com/JanTeichertKluge/DMLSim/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248681491,"owners_count":21144700,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beit","bert","case-study","causal","causal-inference","causal-machine-learning","deep-learning","dgp","double-machine-learning","doubleml","machine-learning","multi-modal","multimodal","multimodal-deep-learning","neural-network","simulation","transformer","transformers"],"created_at":"2024-10-14T19:24:14.258Z","updated_at":"2025-04-13T07:55:20.542Z","avatar_url":"https://github.com/JanTeichertKluge.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DMLSim - Library with packages on DoubleML and Neural Networks in Python\n\nThis library includes three packages:\n- dml_sim\n- dml_nn\n- dml_emb\n\nThe Python package **dml_sim** from the library **DMLSim** provides a simulation study framework using [DoubleML](https://github.com/DoubleML/doubleml-for-py)s Implementation of the double / debiased machine learning framework of\n[Chernozhukov et al. (2018)](https://doi.org/10.1111/ectj.12097).\n\nThe package **dml_nn** includes an API to use PyTorch Neural Networks together with the [skorch](https://github.com/skorch-dev/skorch) library inside the [DoubleML](https://github.com/DoubleML/doubleml-for-py). There are also several tools for creating Feed Forwards Neural Networks for use in PLR or IRM models.\n\nThe modules from the **dml_emb** package including several methods and tools to use embeddings from [transformer](https://github.com/huggingface/transformers) models as covariates in the [DoubleML](https://github.com/DoubleML/doubleml-for-py) framework. This package contains torch based architectures like a multimodal ensemble of transformer models, solutions to create embeddings like the cross-fitting method and modified APIs to use these models in a high-level way with [skorch](https://github.com/skorch-dev/skorch).\n\n## About simulation studies with dml_sim\n\nSimulation studies with Double / debiased machine learning for \n\n- Partially linear regression models (PLR)      \n- Interactive regression models (IRM)\n\nInstances of the main class 'simulation_study' can be used with all learners from sklearn. \nThe learners need a fit() and a predict() method.\nThe module 'dml_nn' can be used to create a dictionary with the corresponding models. \nYou are able to pass layer- and hyper-parameters when initializing the class 'network_builder'.\n\nThe DGP (data generating process) should take at least 'n_obs', 'dim_x' as arguments. \n'alpha' / 'theta' is necessary for all DGPs with non heterogenous treatment effect. \nThe callable should return numpy arrays:\n- X, dim(X) = (n_obs, dim_x)\n- y, dim(y) = (n_obs,)\n- d, dim(d) = (n_obs,)\n- and theta, dim(theta) = (n_obs,) in order to calculate the average treatment effect if the treatment effect is heterogenous.\n\nIn some cases, the DGP (i.e. doubleml.datasets.make_irm_data) generates a heterogenous treatment effect from an argument with fixed value for theta. In these cases, initialize your instance with specific value for alpha.\n\nThe currently supported DGPs:\n- all DGPs from doubleml.datasets\n- all DGPs from dml_sim.datasets\n- DGPs from the [opossum package](https://github.com/jgitr/opossum)\n\n### Example Simulation\nExample Code for DMLSim.dml_sim (IRM):\n\n```python\n# Make imports\nfrom sklearn.base import clone\nfrom sklearn.ensemble import RandomForestRegressor, RandomForestClassifier\nfrom sklearn.linear_model import LassoCV, LogisticRegressionCV\n\nfrom doubleml import DoubleMLIRM\n\nfrom dml_sim.simulation_base_class import simulation_study as ssession # import simulation session object\nfrom dml_sim.datasets import make_irm_farell2021 # import a data generating process\nfrom dml_nn.simulation_learner import network_builder # import network builder to build torch networks wrapped in sklearn syntax using skorch\n\nbuilder = network_builder() #Init builder with default settings\nbuilder.__dict__ #Show default settings\n\n# Define ML learner\nlasso_reg = LassoCV()\nlasso_cls = LogisticRegressionCV(penalty='l1', solver='liblinear')\nrf_reg = RandomForestRegressor()\nrf_cls = RandomForestClassifier()\n\n# Create a structured learner dict for machine learning algorithms\nlearner_dict_irm_ml = {\n        'lasso': {\n            'ml_m' : clone(lasso_cls),\n            'ml_g' : clone(lasso_reg)},\n        'random_forests': {\n            'ml_m' : clone(rf_cls),\n            'ml_g' : clone(rf_reg)}\n        }\n  \n # Create a structured learner dict for neural network algorithms\nlearner_dict_irm_nn = builder.get_irm_nn_learners()      \n\n# Combine dicts\nlearner_dict_irm = {**learner_dict_irm_ml, **learner_dict_irm_nn}        \n\n# Create a dict with n_obs and dim_x for the DGP data setting\nnp_dict = {'n_obs': [500], 'dim_x': [10, 20]}\n\n# Init a dml simulation session\nscenario_A = ssession(model = DoubleMLIRM, \n                    score = 'ATE',\n                    DGP = make_irm_farell2021, \n                    n_rep = 100,\n                    np_dict =  np_dict, \n                    lrn_dict = learner_dict_irm, \n                    alpha = None,\n                    is_heterogenous=True)\n                    \n# Perform full simulation\nscenario_A.run_simulation()\n\n# Generate boxplots\nscenario_A.boxplot()\n\n# Generate histograms\nscenario_A.histplot()\n\n# Measure performances\nscenario_A.measure_performance()\n\n# Save measures and plots to NEW_FOLDER\nscenario_A.save('/content/NEW_FOLDER')\n```\n\n## About the use of embeddings with dml_emb\nTo include image and text data as confounders within the DoubleML framework, transformer models can be used to create embeddings from this unstructured data. Assume that there is a pandas DataFrame with columns for the output variable, the treatment variable and a column for text and image data. The *TransformerEnsemble* module contains model classes that can process this multimodal input. To deal with this in the high-level syntax of skorch, the modified classes from *FeatureRegressor* are used. The embeddings can then be generated from the DataFrame. In order to avoid data loss, the class *CrossEmbeddings* can be used, which creates the embeddings according to the cross-fitting approach.\n\n### Example Embedding Generation\n```python\nfrom dml_emb.CrossEmbeddings import CrossEmbeddings\nfrom dml_emb.FeatureRegressor import NeuralNetRegressorDoubleOut\nfrom dml_emb.TransformerEnsemble import FineTuned_TransformerEnsemble\n\nIMG = 'microsoft/beit-base-patch16-224-pt22k-ft22k'\nTXT = \"bert-base-uncased\"\n\nmodule_p = FineTuned_TransformerEnsemble(image_model=IMG, \n                                         text_model=TXT,\n                                         num_labels=1)\nmodule_q = FineTuned_TransformerEnsemble(image_model=IMG, \n                                         text_model=TXT,\n                                         num_labels=1)\n\ndef r2(net, X, y):\n    return r2_score(y, net.predict(X))\n\nmodel_p = NeuralNetRegressorDoubleOut( \n    module_p,\n    criterion=torch.nn.MSELoss,\n    optimizer=torch.optim.AdamW,\n    #optimizer__amsgrad=True,\n    lr=3e-5,\n    max_epochs=1,\n    batch_size=16, #try 16\n    iterator_train__shuffle=True,\n    device='cuda' if torch.cuda.is_available() else 'cpu',\n    callbacks=[ProgressBar(),\n               EpochScoring(r2, use_caching=False, lower_is_better=False),\n               EarlyStopping(patience=3, threshold=0.01,\n                             threshold_mode='rel', lower_is_better=True,\n                             load_best=True)]\n)\n\nmodel_q = NeuralNetRegressorDoubleOut(\n    module_q,\n    criterion=torch.nn.MSELoss,\n    optimizer=torch.optim.AdamW,\n    #optimizer__amsgrad=True,\n    lr=3e-5,\n    max_epochs=1,\n    batch_size=16, #try 16\n    iterator_train__shuffle=True,\n    device='cuda' if torch.cuda.is_available() else 'cpu',\n    callbacks=[ProgressBar(),\n               EpochScoring(r2, use_caching=False, lower_is_better=False),\n               EarlyStopping(patience=3, threshold=0.01,\n                             threshold_mode='rel', lower_is_better=True,\n                             load_best=True)]\n)\n\nce = CrossEmbeddings(dataset = smpl, \n                     text_col = 'text', \n                     image_col = 'img_get', \n                     d_col = 'ln_p', \n                     y_col = 'ln_q', \n                     n_folds = 3,\n                     aux_d = model_p, \n                     aux_y = model_q, \n                     txt_str = TXT, \n                     img_str = IMG)\n\nce.fit_and_predict_embeddings()\n\nemb_df = ce.get_embedded_df()\nemb_ar = ce.get_embeddings()\n```\n\n\n\n## Installation\n\n**DMLSim** requires Python 3 with the following packages:\n- DoubleML\n- joblib\n- matplotlib\n- numpy\n- openpyxl\n- pandas\n- Pillow\n- python- dateutil\n- scikit- learn\n- scipy==1.7.3\n- seaborn\n- skorch\n- statsmodels\n- torch\n- tqdm\n- transformers\n\nTo install DMLSim use\n\n```\npip install git+https://github.com/JanTeichertKluge/DMLSim.git\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanteichertkluge%2Fdmlsim","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjanteichertkluge%2Fdmlsim","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjanteichertkluge%2Fdmlsim/lists"}