Skip to main content

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]

Metadata

Release files for dynamicDML 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dynamicDML 0.2.0
File Size Uploaded
dynamicdml-0.2.0.tar.gz 31.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dynamicDML 0.2.0
File Interpreter ABI Platform
dynamicdml-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 63.7 kB

Release files / dynamicdml-0.2.0.tar.gz

Download URL dynamicdml-0.2.0.tar.gz
Size 31.1 kB
Tags Source
SHA-256 checksum
How to use checksums
b624df0b6c14eb7a0fb9217873c2053502f001e9731a046a3f59432508cddcc5
BLAKE2b-256 checksum
How to use checksums
e5578b8f8566650f92b57c093f03c785c14855585f462fcba54010eccba6924f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release files / dynamicdml-0.2.0-py3-none-any.whl

Download URL dynamicdml-0.2.0-py3-none-any.whl
Size 32.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e3301a5198b1599aea2340136aab353fcc8514de3fb0a404ed4675c3f1b4896
BLAKE2b-256 checksum
How to use checksums
7aee9609c745d4fb7bd2e251d80418b6664a09b0b1495099c3f823e1c3648121
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.9

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page