[Bug]: Scalar PLR rejects a treatment on a small scale: the near-zero check on E[psi_a] uses an absolute threshold
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 20/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Stale
- Tech stack
- python, scikit-learn
- Domain
- backend, machine-learning
Research direction
Read doubleml/double_ml_linear_score.py at LinearScoreMixin._est_causal_pars and review the absolute near-zero check described in the issue. Reproduce the failure with the supplied scalar PLR example at treatment scale 1e-7, then check the expected scale-invariant coefficient and standard error against DoubleMLPLR. The issue is already being worked on in open pull request #467.
Written by the indexing model from the issue text.
Description
Describe the bug
LinearScoreMixin._est_causal_pars (doubleml/double_ml_linear_score.py:88 on main) refuses to estimate when |E[psi_a]| < 1e-12:
if np.any(np.abs(mean_psi_a) < 1e-12):
raise ValueError("Division by near-zero detected in linear score estimation. ...")
The threshold is absolute, but E[psi_a] carries the units of the treatment. For the scalar PLR, psi_a = -(d - m(x))^2 (partialling out) or -(d - m(x)) * d (IV-type), so it scales with d². A treatment whose residual standard deviation is below about 1e-6 in its own units is rejected, although the estimate is well defined: in other units (e.g. multiplied by 1e-7), the same treatment has the same effect up to the scale. The legacy DoubleMLPLR estimates it fine. The error message also points at the wrong cause ("issues with the nuisance models or data").
This only affects the new scalar API on main (from #394); it is not in a release yet. The scalar IRM isn't affected in practice, since its psi_a doesn't depend on the treatment's units.
Minimum reproducible code snippet
import numpy as np
from sklearn.linear_model import LinearRegression
import doubleml as dml
from doubleml.plm.plr_scalar import PLR
np.random.seed(0)
n = 1000
x = np.random.normal(size=(n, 3))
d = x[:, 0] + np.random.normal(size=n)
y = 0.5 * d + x[:, 1] + np.random.normal(size=n)
for scale in [1.0, 1e-7]: # the same treatment in other units: theta becomes 0.5 / scale
data = dml.DoubleMLData.from_arrays(x, y, d * scale)
legacy = dml.DoubleMLPLR(data, LinearRegression(), LinearRegression(), n_folds=5).fit()
print(f"scale={scale:g}: E[psi_a] = {np.mean(legacy.psi_elements['psi_a']):.3g}")
print(f" DoubleMLPLR: coef * scale = {legacy.coef[0] * scale:.4f}, se * scale = {legacy.se[0] * scale:.4f}")
scalar = PLR(data, ml_l=LinearRegression(), ml_m=LinearRegression())
scalar.set_sample_splitting(legacy.smpls)
try:
scalar.fit()
print(f" PLR (scalar): coef * scale = {scalar.coef[0] * scale:.4f}, se * scale = {scalar.se[0] * scale:.4f}")
except ValueError as e:
print(f" PLR (scalar): ValueError: {e}")
Expected Result
The scalar PLR estimates the rescaled treatment, like DoubleMLPLR (output with a scale-free check):
scale=1: E[psi_a] = -1.04
DoubleMLPLR: coef * scale = 0.4802, se * scale = 0.0321
PLR (scalar): coef * scale = 0.4802, se * scale = 0.0321
scale=1e-07: E[psi_a] = -1.04e-14
DoubleMLPLR: coef * scale = 0.4831, se * scale = 0.0321
PLR (scalar): coef * scale = 0.4831, se * scale = 0.0321
The two DoubleMLPLR fits draw different sample splits (hence 0.4802 vs 0.4831); the scalar model reuses each one's splits.
Actual Result
scale=1: E[psi_a] = -1.04
DoubleMLPLR: coef * scale = 0.4802, se * scale = 0.0321
PLR (scalar): coef * scale = 0.4802, se * scale = 0.0321
scale=1e-07: E[psi_a] = -1.04e-14
DoubleMLPLR: coef * scale = 0.4831, se * scale = 0.0321
PLR (scalar): ValueError: Division by near-zero detected in linear score estimation. E[psi_a] is very close to zero. This may indicate issues with the nuisance models or data.
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 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 variablePossibly taken @vrjdev claimed this 5 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 35/100
DoubleML/doubleml-for-py#465 ·
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