Skip to content

Commit 8afc70b

Browse files
authored
Merge pull request #22 from DoubleML/s-update-did-rdd-scripts
Update DiD and RDD Scripts
2 parents e7a0d17 + aed5713 commit 8afc70b

26 files changed

+860
-490
lines changed

.github/workflows/did_sim.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
- name: Install uv
5353
uses: astral-sh/setup-uv@v5
5454
with:
55-
version: "0.6.11"
55+
version: "0.7.8"
5656

5757
- name: Set up Python
5858
uses: actions/setup-python@v5

.github/workflows/rdd_sim.yml

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717
strategy:
1818
matrix:
1919
script: [
20-
'scripts/rdd/rdd_sharp_coverage.py',
21-
'scripts/rdd/rdd_fuzzy_coverage.py',
20+
'scripts/rdd/rdd_sharp.py',
21+
'scripts/rdd/rdd_fuzzy.py',
2222
]
2323

2424
steps:
@@ -48,34 +48,42 @@ jobs:
4848
with:
4949
ref: ${{ env.TARGET_BRANCH }}
5050

51+
- name: Install uv
52+
uses: astral-sh/setup-uv@v5
53+
with:
54+
version: "0.7.8"
55+
5156
- name: Set up Python
5257
uses: actions/setup-python@v5
5358
with:
54-
python-version: '3.12'
59+
python-version-file: "monte-cover/pyproject.toml"
5560

56-
- name: Install dependencies
61+
- name: Install Monte-Cover
5762
run: |
58-
python -m pip install --upgrade pip
59-
pip install -r requirements.txt
63+
cd monte-cover
64+
uv venv
65+
uv sync
6066
61-
- name: Install DoubleML from correct branch
62-
run: |
63-
pip uninstall -y doubleml
64-
pip install "doubleml[rdd] @ git+https://github.com/DoubleML/doubleml-for-py@${{ env.DML_BRANCH }}"
67+
- name: Set up Python
68+
uses: actions/setup-python@v5
69+
with:
70+
python-version: '3.12'
6571

66-
- name: Install RDFlex from main branch
72+
- name: Install DoubleML from correct branch
6773
run: |
68-
pip uninstall -y doubleml
69-
pip install git+https://github.com/DoubleML/doubleml-rdflex.git@main
70-
pip install rdrobust
74+
source monte-cover/.venv/bin/activate
75+
uv pip uninstall doubleml
76+
uv pip install "doubleml[rdd] @ git+https://github.com/DoubleML/doubleml-for-py@${{ env.DML_BRANCH }}"
7177
7278
- name: Set up Git configuration
7379
run: |
7480
git config --global user.name 'github-actions'
7581
git config --global user.email 'github-actions@github.com'
7682
7783
- name: Run scripts
78-
run: python ${{ matrix.script }}
84+
run: |
85+
source monte-cover/.venv/bin/activate
86+
uv run ${{ matrix.script }}
7987
8088
- name: Commit any existing changes
8189
run: |

doc/rdd/rdd.qmd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ df_sharp = pd.read_csv("../../results/rdd/rdd_sharp_coverage.csv", index_col=Non
4747
assert df_sharp["repetition"].nunique() == 1
4848
n_rep_sharp = df_sharp["repetition"].unique()[0]
4949
50-
display_columns_sharp = ["Method", "Learner g", "fs specification", "Bias", "CI Length", "Coverage"]
50+
display_columns_sharp = ["Method", "Learner g", "fs_specification", "Bias", "CI Length", "Coverage"]
5151
```
5252

5353
```{python}
@@ -99,7 +99,7 @@ df_fuzzy = pd.read_csv("../../results/rdd/rdd_fuzzy_coverage.csv", index_col=Non
9999
assert df_fuzzy["repetition"].nunique() == 1
100100
n_rep_fuzzy = df_fuzzy["repetition"].unique()[0]
101101
102-
display_columns_fuzzy = ["Method", "Learner g", "Learner m", "fs specification", "Bias", "CI Length", "Coverage"]
102+
display_columns_fuzzy = ["Method", "Learner g", "Learner m", "fs_specification", "Bias", "CI Length", "Coverage"]
103103
```
104104

105105
```{python}

monte-cover/src/montecover/did/did_pa_multi.py

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import numpy as np
55
import pandas as pd
66
from doubleml.did.datasets import make_did_CS2021
7-
from lightgbm import LGBMClassifier, LGBMRegressor
8-
from sklearn.linear_model import LinearRegression, LogisticRegression
97

108
from montecover.base import BaseSimulation
9+
from montecover.utils import create_learner_from_config
1110

1211

1312
class DIDMultiCoverageSimulation(BaseSimulation):
@@ -36,39 +35,13 @@ def __init__(
3635
def _process_config_parameters(self):
3736
"""Process simulation-specific parameters from config"""
3837
# Process ML models in parameter grid
38+
# Process ML models in parameter grid
39+
assert "learners" in self.dml_parameters, "No learners specified in the config file"
3940

40-
assert (
41-
"learners" in self.dml_parameters
42-
), "No learners specified in the config file"
41+
required_learners = ["ml_g", "ml_m"]
4342
for learner in self.dml_parameters["learners"]:
44-
assert "ml_g" in learner, "No ml_g specified in the config file"
45-
assert "ml_m" in learner, "No ml_m specified in the config file"
46-
47-
# Convert ml_g strings to actual objects
48-
if learner["ml_g"][0] == "Linear":
49-
learner["ml_g"] = ("Linear", LinearRegression())
50-
elif learner["ml_g"][0] == "LGBM":
51-
learner["ml_g"] = (
52-
"LGBM",
53-
LGBMRegressor(
54-
n_estimators=500, learning_rate=0.02, verbose=-1, n_jobs=1
55-
),
56-
)
57-
else:
58-
raise ValueError(f"Unknown learner type: {learner['ml_g']}")
59-
60-
# Convert ml_m strings to actual objects
61-
if learner["ml_m"][0] == "Linear":
62-
learner["ml_m"] = ("Linear", LogisticRegression())
63-
elif learner["ml_m"][0] == "LGBM":
64-
learner["ml_m"] = (
65-
"LGBM",
66-
LGBMClassifier(
67-
n_estimators=500, learning_rate=0.02, verbose=-1, n_jobs=1
68-
),
69-
)
70-
else:
71-
raise ValueError(f"Unknown learner type: {learner['ml_m']}")
43+
for ml in required_learners:
44+
assert ml in learner, f"No {ml} specified in the config file"
7245

7346
def _calculate_oracle_values(self):
7447
"""Calculate oracle values for the simulation."""
@@ -102,8 +75,9 @@ def _calculate_oracle_values(self):
10275
def run_single_rep(self, dml_data, dml_params) -> Dict[str, Any]:
10376
"""Run a single repetition with the given parameters."""
10477
# Extract parameters
105-
learner_g_name, ml_g = dml_params["learners"]["ml_g"]
106-
learner_m_name, ml_m = dml_params["learners"]["ml_m"]
78+
learner_config = dml_params["learners"]
79+
learner_g_name, ml_g = create_learner_from_config(learner_config["ml_g"])
80+
learner_m_name, ml_m = create_learner_from_config(learner_config["ml_m"])
10781
score = dml_params["score"]
10882
in_sample_normalization = dml_params["in_sample_normalization"]
10983

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""Monte Carlo coverage simulations for RDD."""
2+
3+
from montecover.rdd.rdd import RDDCoverageSimulation
4+
5+
__all__ = [
6+
"RDDCoverageSimulation",
7+
]

0 commit comments

Comments
 (0)