Skip to content

ENH: robust add MQuantileNorm - #3183

Merged
josef-pkt merged 3 commits into
statsmodels:mainfrom
josef-pkt:m_quantiles
Nov 29, 2022
Merged

josef-pkt merged 3 commits into
statsmodels:mainfrom
josef-pkt:m_quantiles

Conversation

@josef-pkt

@josef-pkt josef-pkt commented Sep 1, 2016 •

Copy link
Copy Markdown
Member

work in progress, I opened the PR so I don't loose track of this

This is currently just a RobustNorm class.
no unit tests yet, and might not have correct standard errors when used in RLM.

currently I only checked that the prediction is close to quantile regression with HuberT(t=small) e.g. small=1e-6. It "works" for expectiles when t is set very large, i.e. all observations are in the quadratic least squares range.

The prediction when using splines seems to become wigly very soon as the number of knots increases (unpenalized, just plain patsy), even more so in quantile regression and its M-quantile approximation than in least-squares expectiles.

There are several R packages mentioned in some of the articles but I have not yet looked at any of them.

background and status:

This should work as smooth alternative for the optimization in quantile regression. I wanted to add this for some time because it avoids the problem with residuals that are zero or around zero in quantile regression. e.g. #2377 and #2485

Just adding the norm is ok, it works for prediction. Full support for MQuantileRegression or asymmetric least squares requires more work, especially checking the cov_params and scale estimates.

For usability, we also need additional post-estimation features, like recovering quantiles and expected shortfall for risk analysis from the expectiles or M-quantiles.

spline application in general: we need to merge penalized splines, GAM and extend to RLM following a similar pattern as for GLM.

other extensions: quantile sheets and non-crossing M-quantiles. e.g. simple example had crossing 0.25 and 0.75 M-quantile with misspecified, quadratic M-quantile function. (splines don't seem to cross, at least not in the example)

References

(some of them)
Nychka et al. have epsilon approximation to quantile regression in appendix
(they use penalization splines with a knot at each observation point and parameterize in terms of the value of the spline at the observation point, i.e. not in terms of basis functions)

Bianchi, Annamaria, and Nicola Salvati. 2015. “Asymptotic Properties and Variance Estimators of the M-Quantile Regression Coefficients Estimators.” Communications in Statistics - Theory and Methods 44 (11): 2416–29. doi:10.1080/03610926.2013.791375.

Breckling, Jens, and Ray Chambers. 1988. “M-Quantiles.” Biometrika 75 (4): 761–71. doi:10.2307/2336317.

Jones, M. C. 1994. “Expectiles and M-Quantiles Are Quantiles.” Statistics & Probability Letters 20 (2): 149–53. doi:10.1016/0167-7152(94)90031-0.

Newey, Whitney K., and James L. Powell. 1987. “Asymmetric Least Squares Estimation and Testing.” Econometrica 55 (4): 819–47. doi:10.2307/1911031.

Nychka, Doug, Gerry Gray, Perry Haaland, David Martin, and Michael O’Connell. 1995. “A Nonparametric Regression Approach to Syringe Grading for Quality Improvement.” Journal of the American Statistical Association 90 (432): 1171–78. doi:10.2307/2291509.

Reiss, Philip T., and Lei Huang. 2012. “Smoothness Selection for Penalized Quantile Regression Splines.” The International Journal of Biostatistics 8 (1). doi:10.1515/1557-4679.1381.

@josef-pkt

Copy link
Copy Markdown
Member Author

as a point for quantile regression optimization: running examples with random numbers, I see quite often convergence failures but where the predictions still look good

m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\regression\quantile_regression.py:189: ConvergenceWarning: Convergence cycle detected
  warnings.warn("Convergence cycle detected", ConvergenceWarning)

and sometimes iteration limit reached

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.02%) to 89.239% when pulling 4ea057d on josef-pkt:m_quantiles into bb1db2b on statsmodels:master.

@josef-pkt

josef-pkt commented Mar 17, 2018 •

Copy link
Copy Markdown
Member Author

no unit tests

a brief google search finds this https://www.rdocumentation.org/packages/expectreg/
expectreg is not maintained and has been removed from CRAN
old pdf files at ftp://www.postfix.org/mirror/CRAN/web/packages/expectreg/index.html

VGAM seems to have expectiles for specific distributions
https://www.rdocumentation.org/packages/VGAM/versions/1.0-4/topics/Expectiles-Uniform
I'm not sure that's the same thing as here.

@josef-pkt

Copy link
Copy Markdown
Member Author

idea: generic unit test for Norm, testing for consistency with numerical derivatives.
That's most of what we need for a new norm class (and similar for link, var, ... classes).

then this could be merged

@josef-pkt

Copy link
Copy Markdown
Member Author

looks like there is a new package in R for m-quantile regression Mqrcm
https://cran.r-project.org/web/packages/Mqrcm/

expectreg didn't get resurrected, still in archive with problems in c/cpp code

@josef-pkt josef-pkt added this to the 0.14 milestone Feb 16, 2022
@josef-pkt

Copy link
Copy Markdown
Member Author

bump for 0.14

just adding this as robust norm doesn't require a lot of unit testing.
based on my old example it seems to work well.

actual usage can be left to other PRs

@lorentzenchr

Copy link
Copy Markdown
Contributor

The R package expectreg might be a good reference for unit tests.

@josef-pkt

Copy link
Copy Markdown
Member Author

Thanks,
expectreg package is back, someone fixed the C++ errors
changelog indicates fixed in May 2021

@josef-pkt

Copy link
Copy Markdown
Member Author

rebased and force pushed, no merge conflicts or problems with rebase

old example script files
"\josef\scripts\ex_quantile_regression_polynomial.py"
"\josef\scripts\ex_quantile_regression_polynomial_reduced.py"

I converted the former to notebook

  • expectiles at q=0.5 agrees with OLS params and bse at a few decimals (need ols use_t=False to compare p-values and confint)
  • huber norm based m-quantiles agrees with quantreg for q=0.5 (params) and q=0.25 (predictions, but not very close in params), bse for those m-quantiles are very small with small huber norm threshold (1e-6 to 1e-10), much smaller than quantreg bse
  • if I make the curvature of quantiles (heteroscedasticity) less strong then huber-MQuantiles and quantreg params agree pretty well. (looks good for unit test)

@josef-pkt

Copy link
Copy Markdown
Member Author

aside:
why is def estimate_location a function in norms module?
according to blame it has been there for 14 years

what's a better location?

@josef-pkt

Copy link
Copy Markdown
Member Author

amended with style fixes, mainly too many empty lines

@josef-pkt

Copy link
Copy Markdown
Member Author

a bit strange:
in the example huberT mquantiles highly agree with quantreg for q in [0.25, 0.75] but not for q=0.5. Fittedvalues also don't agree very closely (compared to the other two q).
same result if I use HuberT(t=1e-6) directly as norm instead of mquantile norm, those two highly agree

This seems to be a problem or feature related to HuberT and not to MQuantileNorm.
So, I ignore this for this PR, and skip unit test versus quantreg for 0.5

@josef-pkt

Copy link
Copy Markdown
Member Author

related: HuberT with t=1e-6 has problems with cov

m and var_psiprime are both zero
I don't get the warning if t=1e-2

I guess this happens if we don't have any residuals in the t neighborhood of zero, (in quadratic segment)

    @cache_readonly
    def bcov_scaled(self):
        model = self.model
        m = np.mean(model.M.psi_deriv(self.sresid))
        var_psiprime = np.var(model.M.psi_deriv(self.sresid))
>       k = 1 + (self.df_model + 1) / self.nobs * var_psiprime / m ** 2
E       RuntimeWarning: invalid value encountered in double_scalars

..\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\robust\robust_linear_model.py:445: RuntimeWarning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> PDB post_mortem (IO-capturing turned off) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> d:\josef\eclipse-workspace\statsmodels_gh\statsmodels\statsmodels\robust\robust_linear_model.py(445)bcov_scaled()
-> k = 1 + (self.df_model + 1) / self.nobs * var_psiprime / m ** 2
(Pdb) p var_psiprime
0.0
(Pdb) p m
0.0
(Pdb) self.df_model + 1, self.nobs * var_psiprime / m
*** RuntimeWarning: invalid value encountered in double_scalars
(Pdb) self.df_model + 1, self.nobs * var_psiprime, m
(3.0, 0.0, 0.0)

@josef-pkt

Copy link
Copy Markdown
Member Author

docstrings fixed

should be ready to merge when green

@josef-pkt
josef-pkt merged commit 6756cad into statsmodels:main Nov 29, 2022
@josef-pkt josef-pkt changed the title WIP ENH: robust add MQuantileNorm ENH: robust add MQuantileNorm Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants