Repository navigation
Conversation
|
Thanks, the AI Disclosure section looks complete. |
bashtage
left a comment
There was a problem hiding this comment.
(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 |
There was a problem hiding this comment.
Is there an extra o here? LOOO?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Is get_det_cov_params used anywhere else? If not, it woudl be good to clean this up.
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
If this is expected, shouldn't it be trapped or skipped in the function that produces it?
There was a problem hiding this comment.
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.
e182b9b to
0e3d626
Compare
bashtage
left a comment
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
|
Thanks |
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
NumPy's guide.
For OLS,
OLSInfluence._res_looonow computes the leave-one-observation-out params,mse_residanddet(cov_params)from the closed-form updates in Belsley, Kuh and Welsch (1980), instead of fittingnobsauxiliary regressions. With residuale_iand hat diagonalh_i:params - params_(i) = (X'X)^{-1} x_i e_i / (1 - h_i). Columniofpinv_wexogis(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), becausedet(X'X - x_i x_i') = det(X'X) (1 - h_i).dfbeta,dfbetas,resid_studentized_external,dffits,cov_ratioandsummary_frameall use these results, so all of them get faster.The closed form is used only when the model is
OLSandexoghas full rank. It is not used for:1 - h_i <= sqrt(eps): dropping that observation makesexogrank deficient, so those rows still get the auxiliary regression. Their results are the same as before.exog: these keep the existing loop unchanged. The current loop refitsmodel_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):Tests
test_olsinfluence_looo_closed_formcomparesparams_not_obsi,sigma2_not_obsi,det_cov_params_not_obsi,dfbeta,dfbetas,resid_studentized_externalandcov_ratioagainst explicit leave-one-outOLSfits, tortol=1e-10. The data include a few high-leverage rows, and one case adds a single-observation dummy so thath_i = 1and that row takes the loop. Changing thedf_resid, the determinant factor, or removing theh_i = 1fallback each makes the test fail.TestDiagnosticGcomparison with Rlm.influence/influence.measurespasses unchanged atdecimal=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 checkis clean on both changed files.AI Disclosure
Contributions must comply with the statsmodels AI Policy.
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.