[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

Open
#465 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@vrjdev is already working on this.

Since Nov 17, 2026.

  • #468 by @vrjdev — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale

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:

  1. 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").
  2. fit(n_rep=r) re-draws with n_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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DoubleML/doubleml-for-py

All issues in DoubleML/doubleml-for-py

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.