[Bug]: Scalar API with multi-way clustering: fit(n_folds=k) re-draws splits drawn with the same k, and fit(n_rep=r) re-draws them with k² folds per cluster variable
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- python, scikit-learn
- Domain
- backend-api-design, machine-learning
Research direction
Start in doubleml/double_ml_scalar.py, especially the fit() logic at lines 484–498 and fold-count storage at line 658. Trace how cluster fold counts are represented when fit() compares requested values and redraws splits; then check the multi-way clustering tests. Done when the supplied two-way clustering example keeps the original splits for fit(n_folds=2) and uses two folds per cluster variable for fit(n_rep=2). An open linked pull request (#468) is already addressing this issue.
Written by the indexing model from the issue text.
Description
Describe the bug
With multi-way cluster data, DoubleMLScalar.fit() (doubleml/double_ml_scalar.py:484-498 on main) compares the requested n_folds with self.n_folds. But for cluster data, draw_sample_splitting(n_folds=k) draws k folds per cluster variable and stores self._n_folds = k ** n_cluster_vars (:658). With two-way clustering, self.n_folds is k², so:
fit(n_folds=k), with the k the splits were drawn with, counts as a change. The splits are re-drawn (new random splits; the fit state is discarded), with a warning that reports the old value as k² ("was n_folds=4").fit(n_rep=r)re-draws withn_folds=self.n_folds, i.e. with k² folds per cluster variable: here 4 instead of 2 per cluster variable, 16 folds in total instead of 4. This silently changes the cross-fitting design.
One-way clustering and data without clusters are unaffected (k¹ = k).
This only affects the new scalar API on main (from #394); it is not in a release yet.
Minimum reproducible code snippet
import warnings
import numpy as np
from sklearn.linear_model import LinearRegression
import doubleml as dml
from doubleml.plm.datasets import make_pliv_multiway_cluster_CKMS2021
from doubleml.plm.plr_scalar import PLR
np.random.seed(1)
df = make_pliv_multiway_cluster_CKMS2021(N=10, M=10, dim_X=5, return_type="DataFrame")
x_cols = [c for c in df.columns if c.startswith("X")]
dml_data = dml.DoubleMLData(df, "Y", "D", x_cols, cluster_cols=["cluster_var_i", "cluster_var_j"])
def fit_and_report(**kwargs):
model = PLR(dml_data, ml_l=LinearRegression(), ml_m=LinearRegression())
model.draw_sample_splitting(n_folds=2) # 2 folds per cluster variable: 2 x 2 = 4 folds
smpls = model.smpls
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
model.fit(**kwargs)
print(f"fit({', '.join(f'{k}={v}' for k, v in kwargs.items())}): splits kept: {model.smpls is smpls}, "
f"n_folds: {model.n_folds}, n_rep: {model.n_rep}")
for msg in [str(x.message) for x in w if "Re-drawing" in str(x.message)]:
print(" UserWarning:", msg)
fit_and_report(n_folds=2) # the n_folds the splits were drawn with
fit_and_report(n_rep=2)
Expected Result
fit(n_folds=2): splits kept: True, n_folds: 4, n_rep: 1
fit(n_rep=2): splits kept: False, n_folds: 4, n_rep: 2
UserWarning: Re-drawing sample splitting (was n_folds=2, n_rep=1; now n_folds=2, n_rep=2). Existing splits and fit state are discarded.
Actual Result
fit(n_folds=2): splits kept: False, n_folds: 4, n_rep: 1
UserWarning: Re-drawing sample splitting (was n_folds=4, n_rep=1; now n_folds=2, n_rep=1). Existing splits and fit state are discarded.
fit(n_rep=2): splits kept: False, n_folds: 16, n_rep: 2
UserWarning: Re-drawing sample splitting (was n_folds=4, n_rep=1; now n_folds=4, n_rep=2). Existing splits and fit state are discarded.
Versions
Linux x86_64 (glibc 2.34)
Python 3.12.16
DoubleML main @ 9c7dbea (unreleased; not in 0.11.4)
Scikit-Learn 1.9.1
- Dominant language
- Python
- Stars
- 800
- Forks
- 133
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 29
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DoubleML/doubleml-for-py
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finitePossibly taken @leninworld claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
DoubleML/doubleml-for-py#471 ·
Maintainers usually reply within 1 day
-
[Bug]: Scalar PLR rejects a treatment on a small scale: the near-zero check on E[psi_a] uses an absolute thresholdPossibly taken @vrjdev claimed this 5 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 20/100
DoubleML/doubleml-for-py#464 ·
Maintainers usually reply within 1 day
-
[Feature Request]:Openenhancement new feature
Difficulty 5/5 Over a week Newbie friendliness 35/100
DoubleML/doubleml-for-py#403 · 3 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Remove trimming_rule and trimming_thresholdPossibly taken A pull request linked to this issue is open or already merged. Openrefactoring
Difficulty 3/5 1-2 days Newbie friendliness 35/100
DoubleML/doubleml-for-py#364 ·
Maintainers usually reply within 1 day
-
[Feature Request]: Multiple Treatment variables and multiple instrument variables in DoubleMLPLIVOpenenhancement new feature
Difficulty 5/5 Over a week Newbie friendliness 25/100
DoubleML/doubleml-for-py#351 · 1 comment ·
Maintainers usually reply within 1 day
All issues in DoubleML/doubleml-for-py
Similar issues
-
pydanty:is-working
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pydantic/pydantic-ai#14187 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
[Bug]: legacy_json_to_doc crashes on persisted docs with "relationships": nullPossibly taken @Harsh23Kashyap claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
run-llama/llama_index#24392 ·
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
pathintegral-institute/mcpm.sh#568 ·
Maintainers usually reply within 21 days