Skip to content

ENH: closed form leave-one-out measures in OLSInfluence - #10367

Merged
bashtage merged 3 commits into
statsmodels:mainfrom
bhaskargurram-ai:enh/olsinfluence-closed-form-looo
Oct 6, 2026
Merged

bashtage merged 3 commits into
statsmodels:mainfrom
bhaskargurram-ai:enh/olsinfluence-closed-form-looo

Conversation

@bhaskargurram-ai

@bhaskargurram-ai bhaskargurram-ai commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

For OLS, OLSInfluence._res_looo now computes the leave-one-observation-out params, mse_resid and det(cov_params) from the closed-form updates in Belsley, Kuh and Welsch (1980), instead of fitting nobs auxiliary regressions. With residual e_i and hat diagonal h_i:

  • params - params_(i) = (X'X)^{-1} x_i e_i / (1 - h_i). Column i of pinv_wexog is (X'X)^{-1} x_i, so this is a single broadcasted product.
  • mse_resid_(i) = (ssr - e_i**2 / (1 - h_i)) / (nobs - 1 - k_vars).
  • det(cov_params_(i)) = mse_resid_(i)**k_vars * det((X'X)^{-1}) / (1 - h_i), because det(X'X - x_i x_i') = det(X'X) (1 - h_i).

dfbeta, dfbetas, resid_studentized_external, dffits, cov_ratio and summary_frame all use these results, so all of them get faster.

The closed form is used only when the model is OLS and exog has full rank. It is not used for:

  • Rows with 1 - h_i <= sqrt(eps): dropping that observation makes exog rank deficient, so those rows still get the auxiliary regression. Their results are the same as before.
  • Other models (WLS, GLS, subclasses) and rank-deficient exog: these keep the existing loop unchanged. The current loop refits model_class(endog_i, exog_i) without weights, so I didn't want to change what those return in this PR. A weighted version could be a follow-up if that's wanted.

Benchmark

Time to compute each attribute on a fresh OLS(...).fit().get_influence(), random data with a constant (4-core Xeon @ 2.1 GHz, numpy 2.5.3):

nobs, k_vars attribute main this PR
100, 3 dfbetas 19 ms 0.21 ms
1000, 5 dfbetas 332 ms 0.24 ms
4000, 5 dfbetas 3.75 s 0.96 ms
10000, 10 dfbetas 35.4 s 2.7 ms
10000, 10 resid_studentized_external 35.2 s 2.2 ms
10000, 10 cov_ratio 36.1 s 1.9 ms

Tests

  • The new test_olsinfluence_looo_closed_form compares params_not_obsi, sigma2_not_obsi, det_cov_params_not_obsi, dfbeta, dfbetas, resid_studentized_external and cov_ratio against explicit leave-one-out OLS fits, to rtol=1e-10. The data include a few high-leverage rows, and one case adds a single-observation dummy so that h_i = 1 and that row takes the loop. Changing the df_resid, the determinant factor, or removing the h_i = 1 fallback each makes the test fail.
  • The existing TestDiagnosticG comparison with R lm.influence/influence.measures passes unchanged at decimal=12.
  • pytest -m "not slow and not example" statsmodels/stats/tests statsmodels/regression/tests statsmodels/multivariate/tests statsmodels/graphics/tests/test_regressionplots.py: 4371 passed.
  • ruff check is clean on both changed files.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code.
    Used for: drafting the implementation and the test, and running the benchmarks.
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Thanks, the AI Disclosure section looks complete.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Possibly) some changes needed.

without auxiliary regressions, and are fast also for large data sets.

For other models, using the LOO measures is currently only recommended if
the data set is not too large. One possible approach for LOOO measures

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an extra o here? LOOO?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra "o" is intentional. LOOO stands for leave-one-observation-out, as defined at the start of the class notes, and it's the name already used throughout this module (_res_looo and the property docstrings). The inconsistent one was the existing "LOO measures" a few lines further down, so I changed that one to LOOO in e182b9b.

res_i = self.model_class(endog[inidx], exog[inidx]).fit()
params[outidx] = res_i.params
mse_resid[outidx] = res_i.mse_resid
det_cov_params[outidx] = get_det_cov_params(res_i)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is get_det_cov_params used anywhere else? If not, it woudl be good to clean this up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a nested helper local to OLSInfluence._res_looo, and nothing else used it. This PR removes it and computes np.linalg.det(res_i.cov_params()) inline in the fallback loop. GLMInfluence._res_looo still has an identical nested helper. I left it alone because GLM isn't part of this change, but I can inline it there as well if you'd like.


# dropping the observation with hat diagonal one gives a rank deficient exog
@pytest.mark.filterwarnings(
"ignore::statsmodels.tools.sm_exceptions.SingularMatrixWarning"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is expected, shouldn't it be trapped or skipped in the function that produces it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, done in e182b9b. The warning comes from the auxiliary OLS fits that _res_looo still runs for observations with hat diagonal ≈ 1. Dropping such an observation leaves exog rank-deficient by construction, so the warning is expected there. _res_looo now suppresses SingularMatrixWarning only around those fits. The test now turns that warning into an error, so any unexpected occurrence fails it. Only the test's own brute-force reference loop ignores it locally.

closes statsmodels#9009

For OLS with a full rank exog, OLSInfluence._res_looo now computes
params, mse_resid and det_cov_params of the leave-one-observation-out
regressions from the closed form updates (Belsley, Kuh and Welsch 1980)
instead of fitting nobs auxiliary regressions. dfbeta, dfbetas,
resid_studentized_external, dffits, cov_ratio and summary_frame use
these results.

Observations with a hat matrix diagonal of numerically one, where
dropping the observation makes exog rank deficient, and models other
than OLS still use the auxiliary regressions.
The auxiliary regressions that remain in the closed form path drop an
observation with hat diagonal one, so exog is rank deficient by
construction. Ignore the warning there instead of in the test, and make
the test error on it. Use LOOO consistently in the class notes.
@bashtage
bashtage force-pushed the enh/olsinfluence-closed-form-looo branch from e182b9b to 0e3d626 Compare October 6, 2026 08:19

@bashtage bashtage left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are aiming for 100% coverage on new code. Ther ei s a branch that gets missed, and it woudl be great to cover it.

# by construction, so the warning is expected.
ignore_singular = True
else:
loop_idx = range(self.nobs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test that will hit this. You could do it wiht a custom OLS that isn't actually the sm.OLS class, and then compare to regular OLS.

@bashtage

bashtage commented Oct 6, 2026

Copy link
Copy Markdown
Member

Thanks

@bashtage
bashtage merged commit 8278e2d into statsmodels:main Oct 6, 2026
12 checks passed
bashtage pushed a commit that referenced this pull request Oct 24, 2026
OLSInfluence on WLS results refit the leave-one-observation-out
regressions without weights and used an unweighted hat matrix, so
dfbeta, dfbetas, cooks_distance, dffits, cov_ratio and the studentized
residuals were wrong without any warning.

For WLS the measures are those of OLS on the whitened data, so the hat
matrix, the studentized residuals and the LOOO regressions now use
wexog, wendog and wresid, and the closed form LOOO updates from #10367
also cover WLS. resid_press and influence stay on the scale of endog.

closes #9067
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: OLS Influence, more statistics without explicit looo loop

2 participants