A Python implementation of dynamic Double Machine Learning (DML).
Project description
dynamicDML
A Python implementation of dynamic Double Machine Learning (DML) as
developed in Bodory, Huber & Laffers (2022) and Bradic, Ji & Zhang
(2024). The dynamicDML
package allows to flexibly estimate counterfactual outcomes
and treatment effects of sequential policies from observational data, where
treatment assignment may dynamically depend on time-varying characteristics.
For a detailed overview of these methods, see Muny (2025).
Installation
To install the dynamicDML
package run
pip install dynamicDML
in the terminal. dynamicDML
requires the following dependencies:
- flaml[automl]>=2.3.3
- matplotlib>=3.10.0
- mgzip>=0.2.1
- numpy>=2.2.3
- pandas>=2.2.3
- scikit-learn>=1.6.1
- scipy>=1.15.2
- seaborn>=0.13.2
The implementation has been developed and tested in Python version 3.12.
Examples
The following examples demonstrate the basic usage of the dynamicDML
package with default settings.
# load packages
import dynamicDML
import numpy as np
from sklearn.linear_model import LinearRegression, LogisticRegression
# Seed
seed = 999
# Generate data
data = dynamicDML.dyn_data_example(n=2000, random_state=seed)
# Define counterfactual contrasts of interest
all_treat = np.ones_like(data['D1'])
all_control = np.zeros_like(data['D1'])
# Basic setting with Linear and Logistic regression
model = dynamicDML.dml2periods(dynamic_confounding=True, random_state=seed)
# APO treat-treat
model = model.init_sequence(
d1treat='treat',
d2treat='treat',
MLmethod_p1=LogisticRegression(),
MLmethod_p2=LogisticRegression(),
MLmethod_mu=LinearRegression(),
MLmethod_nu=LinearRegression()
)
model = model.fit_sequence(
'treat', 'treat', data['Y'], data['D1'], data['D2'], data['X0'],
data['X1'], g1t=all_treat, g2t=all_treat)
model.sequence_summary()
model = model.compute_APO(d1treat='treat', d2treat='treat')
# APO control-control
model = model.init_sequence(
d1treat='control',
d2treat='control',
MLmethod_p1=LogisticRegression(),
MLmethod_p2=LogisticRegression(),
MLmethod_mu=LinearRegression(),
MLmethod_nu=LinearRegression()
)
model = model.fit_sequence(
'control', 'control', data['Y'], data['D1'], data['D2'], data['X0'],
data['X1'], g1t=all_control, g2t=all_control)
model.sequence_summary()
model = model.compute_APO(d1treat='control', d2treat='control')
# ATE treat-treat vs. control-control
model = model.compute_ATE(
d1treat='treat', d2treat='treat', d1control='control', d2control='control')
# GATE treat-treat vs. control-control for first covariate
model = model.compute_GATEmATE(
d1treat='treat', d2treat='treat', d1control='control', d2control='control',
groupvar=(data['X1'][:, 0] > 0), name_groupvar='X1')
Release Notes
- Version 0.1.0: Unpublished
- Version 0.2.0: Initial release of
dynamicDML
python package
References
- Bodory, H., Huber, M., & Laffers, L. (2022). Evaluating (weighted) dynamic treatment effects by double machine learning. The Econometrics Journal, 25(3), 648. [1]
- Bradic, J., Ji, W., & Zhang, Y. (2024). High-dimensional inference for dynamic treatment effects. The Annals of Statistics, 52(2), 415-440. [2]
- Muny, F. (2025). Evaluating Program Sequences with Double Machine Learning: An Application to Labor Market Policies. arXiv preprint arXiv:2506.11960. [3]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dynamicdml-0.2.0.tar.gz
.
File metadata
- Download URL: dynamicdml-0.2.0.tar.gz
- Upload date:
- Size: 31.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
b624df0b6c14eb7a0fb9217873c2053502f001e9731a046a3f59432508cddcc5
|
|
MD5 |
5d1bdb78c7ed86d37069f2eb1c1f7960
|
|
BLAKE2b-256 |
e5578b8f8566650f92b57c093f03c785c14855585f462fcba54010eccba6924f
|
File details
Details for the file dynamicdml-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: dynamicdml-0.2.0-py3-none-any.whl
- Upload date:
- Size: 32.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
8e3301a5198b1599aea2340136aab353fcc8514de3fb0a404ed4675c3f1b4896
|
|
MD5 |
1c9afac20f84daab4021a6398de22fea
|
|
BLAKE2b-256 |
7aee9609c745d4fb7bd2e251d80418b6664a09b0b1495099c3f823e1c3648121
|