Skip to content

ENH: init to dimension of x matrix rather than rank - #6397

Closed
jlpolit wants to merge 1 commit into
statsmodels:mainfrom
jlpolit:quantile_init_dimension
Closed

jlpolit wants to merge 1 commit into
statsmodels:mainfrom
jlpolit:quantile_init_dimension

Conversation

@jlpolit

@jlpolit jlpolit commented Jan 8, 2020 •

Copy link
Copy Markdown

Updating quantile regression to initialize using the dimension of the X matrix rather than the rank. This might break things, so testing before finalizing the PR. Will add a test or three if current test suite passes with the change.

Notes:

  • It is essential that you add a test when making code changes. Tests are not
    needed for doc changes.
  • When adding a new function, test values should usually be verified in another package (e.g., R/SAS/Stata).
  • When fixing a bug, you must add a test that would produce the bug in master and
    then show that it is fixed with the new code.
  • New code additions must be well formatted. Changes should pass flake8. If on Linux or OSX, you can
    verify you changes are well formatted by running
    git diff upstream/master -u -- "*.py" | flake8 --diff --isolated
    
    assuming flake8 is installed. This command is also available on Windows
    using the Windows System for Linux once flake8 is installed in the
    local Linux environment. While passing this test is not required, it is good practice and it help
    improve code quality in statsmodels.
  • Docstring additions must render correctly, including escapes and LaTeX.

@coveralls

coveralls commented Jan 8, 2020 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 87.7% when pulling 24cc187 on jlpolit:quantile_init_dimension into e714513 on statsmodels:master.

@codecov

codecov Bot commented Jan 8, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #6397 into master will increase coverage by 0.27%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #6397      +/-   ##
==========================================
+ Coverage   85.05%   85.33%   +0.27%     
==========================================
  Files         640      646       +6     
  Lines      103193   104221    +1028     
  Branches    11277    11357      +80     
==========================================
+ Hits        87770    88935    +1165     
+ Misses      12945    12815     -130     
+ Partials     2478     2471       -7     
Impacted Files Coverage Δ
statsmodels/regression/quantile_regression.py 74.56% <100.00%> (ø)
...odels/regression/tests/test_quantile_regression.py 100.00% <100.00%> (ø)
statsmodels/sandbox/stats/diagnostic.py 0.00% <0.00%> (-65.98%) ⬇️
statsmodels/graphics/functional.py 79.04% <0.00%> (-5.24%) ⬇️
statsmodels/tsa/statespace/kalman_smoother.py 86.09% <0.00%> (-3.10%) ⬇️
statsmodels/tsa/statespace/simulation_smoother.py 85.29% <0.00%> (-1.75%) ⬇️
statsmodels/base/_parameter_inference.py 60.81% <0.00%> (-1.69%) ⬇️
statsmodels/tsa/holtwinters.py 89.61% <0.00%> (-1.13%) ⬇️
statsmodels/tools/grouputils.py 69.02% <0.00%> (-0.89%) ⬇️
statsmodels/tsa/tests/test_holtwinters.py 99.36% <0.00%> (-0.64%) ⬇️
... and 65 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9b0d4a6...24cc187. Read the comment docs.

@josef-pkt

Copy link
Copy Markdown
Member

closes #2597

@pep8speaks

pep8speaks commented Jan 8, 2020 •

Copy link
Copy Markdown

Hello @jlpolit! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2020-04-29 15:27:58 UTC

@jlpolit
jlpolit marked this pull request as ready for review January 8, 2020 21:05
@jlpolit jlpolit changed the title init to dimension of x matrix rather than rank ENH: init to dimension of x matrix rather than rank Jan 8, 2020
@josef-pkt

Copy link
Copy Markdown
Member

behavior at singular exog

IRLS uses pinv, so we get similar behavior to OLS and IRLS in GLM (*), with a pinv regularized solution
beta = np.dot(pinv(xtx), xty)
(QuantReg uses pinv on product matrix x'x instead of on exog directly)

So, we could verify that pinv-regularized fit works correctly, instead of raising an exception wherever it breaks.

@jlpolit

jlpolit commented Jan 9, 2020

Copy link
Copy Markdown
Author

behavior at singular exog

IRLS uses pinv, so we get similar behavior to OLS and IRLS in GLM (*), with a pinv regularized solution
beta = np.dot(pinv(xtx), xty)
(QuantReg uses pinv on product matrix x'x instead of on exog directly)

So, we could verify that pinv-regularized fit works correctly, instead of raising an exception wherever it breaks.

based on the test provided, it at least appears to work without throwing an exception....but it might be preferable to have a nontrivial test to confirm correctness (particularly in light of the runtime warning mentioned in convo above)

@josef-pkt

Copy link
Copy Markdown
Member

You could create an random sample with a repeated column (2 columns in X are identical) and a sample size, for example, nobs=100.
I usually just use this kind of random data to check how an estimator or statistic works with singular, perfectly collinear data.

As a cross check, dropping a perfectly collinear column should result in the same prediction, fittedvalues as including it with pinv regularization (IIRC).

@jlpolit

jlpolit commented Jan 9, 2020

Copy link
Copy Markdown
Author

...there seems to be a coverage test failing due to a change in statsmodels/graphics/functional.py, which I don't think is touched by this PR? did I miss something about what's going on with that test?

@josef-pkt

Copy link
Copy Markdown
Member

I don't know what causes "weird" test coverage results. In my experience, we get sometimes unrelated changes in unit test coverage for things that haven't been changed. I guess it's a problem in how they compute changes in the code, or how test results are compared.
You can ignore that.

If singular exog doesn't raise an exception, then you can remove the try except in the unit tests.

@jlpolit
jlpolit requested review from bashtage and josef-pkt January 10, 2020 15:36
@josef-pkt

Copy link
Copy Markdown
Member

LGTM, Thanks for the fix and sorry for the delay.

12 commits is too much for this
Can you do a rebase and squash the commits?

@jlpolit
jlpolit force-pushed the quantile_init_dimension branch from db3737c to 4c18014 Compare February 17, 2020 14:06
@jlpolit

jlpolit commented Feb 19, 2020

Copy link
Copy Markdown
Author

LGTM, Thanks for the fix and sorry for the delay.

12 commits is too much for this
Can you do a rebase and squash the commits?

No worries - thanks!

@jlpolit
jlpolit force-pushed the quantile_init_dimension branch from 4c18014 to 2e8614b Compare April 29, 2020 15:27
@jlpolit
jlpolit force-pushed the quantile_init_dimension branch from 2e8614b to 24cc187 Compare April 29, 2020 15:27
chellabeatrixkiddo added a commit to chellabeatrixkiddo/statsmodels that referenced this pull request Jun 9, 2021
@josef-pkt josef-pkt added this to the 0.13 milestone Aug 10, 2021
@josef-pkt

Copy link
Copy Markdown
Member

rebased version with additional unit test merged in #7694

@jlpolit Thank you for the PR. I was slow in getting back to this. It will now be in 0.13

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.

quantreg: exception with singular exog

5 participants