Skip to content

pinv rcond and multicollinearity, different cutoffs  #1662

Open

Description

@josef-pkt

I spend some time debugging why my Ridge/TheilGLS cannot replicate OLS.

The example uses Longley data following an example in R MASS lm.ridge.
condition number is bad

If I solve the moment equation with pinv, I get a "regularized" solution. If we use pinv/svd on the original data (as does OLS), then we get an unregularized solution.
rcond kicks in with pinv(x.T.dot(x)), but not with pinv(x)

lm in R gives the same unregularized solution as statsmodels OLS

aside: I got this because I use TheilGLS and not a more efficient direct implementation of Ridge with SVD

>>> np.linalg.pinv(x.T.dot(x)).dot(x.T.dot(y))
array([  9.36081012e-05,   2.17314250e-01,   2.13408612e-02,
         4.33978774e-03,  -1.70034590e+00,   1.17385704e-01,
        -3.10581300e-01])
>>> np.linalg.pinv(x).dot(y)
array([  2.94685636e+03,   2.63527247e-01,   3.64829137e-02,
         1.11610505e-02,  -1.73702984e+00,  -1.41879853e+00,
         2.31287851e-01])
>>> np.linalg.solve(x.T.dot(x), x.T.dot(y))
array([  2.94685632e+03,   2.63527246e-01,   3.64829135e-02,
         1.11610504e-02,  -1.73702984e+00,  -1.41879851e+00,
         2.31287844e-01])
>>> np.linalg.inv(x.T.dot(x)).dot(x.T.dot(y))
array([  2.94685632e+03,   2.63527246e-01,   3.64829135e-02,
         1.11610504e-02,  -1.73702984e+00,  -1.41879851e+00,
         2.31287845e-01])
>>> np.linalg.pinv(x.T.dot(x), rcond=0).dot(x.T.dot(y))
array([  2.94666457e+03,   2.63524239e-01,   3.64819282e-02,
         1.11606065e-02,  -1.73702745e+00,  -1.41869855e+00,
         2.31252583e-01])

part of the script that defines the model:

import numpy as np
import pandas

from statsmodels.regression.linear_model import OLS
from statsmodels.datasets import longley

ll = longley.load_pandas()
names = 'TOTEMP  GNPDEFL     GNP  UNEMP  ARMED     POP  YEAR'.split()
s = [1000, 1, 1000, 10, 10, 1000, 1]
rescale = pandas.Series(data=s, index=names)
data = ll.data / rescale

formula = 'GNPDEFL ~'+ ' + '.join(['GNP', 'UNEMP', 'ARMED', 'POP', 'YEAR', 
                                   'TOTEMP'])
res_ll_ols = OLS.from_formula(formula, data).fit()

x = res_ll_ols.model.exog
y = res_ll_ols.model.endog

numerical precision problems, but the main point
rcond depends on whether we use x or x.T x
(square root of eigenvalues versus eigenvalues)

the singular values (square root of eigenvalues of x.T x ) don't look so bad

>>> np.linalg.svd(x)[1]
array([  8.15814890e+03,   4.56186582e+02,   3.24418001e+02,
         1.34253673e+02,   2.13324009e+00,   1.36760693e+00,
         2.11512471e-04])

condition number in terms of eigenvalues are worse but smallest eigenvalue is not too bad.

>>> np.linalg.svd(x)[1]**2
array([  6.65553934e+07,   2.08106198e+05,   1.05247039e+05,
         1.80240487e+04,   4.55071327e+00,   1.87034873e+00,
         4.47375255e-08])


>>> np.linalg.eigvalsh(x.T.dot(x))[::-1]
array([  6.65553934e+07,   2.08106198e+05,   1.05247039e+05,
         1.80240487e+04,   4.55071327e+00,   1.87034873e+00,
         4.47353505e-08])
>>> np.linalg.svd(x.T.dot(x))[1]
array([  6.65553934e+07,   2.08106198e+05,   1.05247039e+05,
         1.80240487e+04,   4.55071327e+00,   1.87034873e+00,
         4.47404782e-08])
>>> sv = np.linalg.svd(x)[1]
>>> sv.max()/sv.min()
38570533.694658495
>>> (sv.max()/sv.min())**2
1487686069490786.2
>>> np.linalg.cond(x)
38570533.694658607
>>> np.linalg.cond(x.T.dot(x))
1487587887911603.5

Activity

  1. jseabold commented on May 7, 2014

    @jseabold
    Member

    I don't know if it's helpful at all to you or even really relevant. I didn't try to understand the details at the time, but apparently the way I solved the HP filter is a type of ridge regression that uses scipy.sparse. I just used the formula from someone's class notes. No idea if it's general.

    https://github.com/statsmodels/statsmodels/blob/master/statsmodels/tsa/filters/hp_filter.py

  2. josef-pkt commented on May 7, 2014

    @josef-pkt
    MemberAuthor

    It's not directly related to this issue, which is that in some edge cases like Longley we might run into different definitions of singular depending on which linear algebra we use
    (aside: looking at the variables in the regression the question is: why would you ever want to do this, except as numerical exercise and test case?)

    But thanks for the link, I didn't remember this or never looked closely enough. I'm partway through sparse version of linear regression and Ridge/Theil, and this is helpful.
    Currently, I'm stuck figuring out unit tests for the existing dense version.

  3. jseabold commented on May 7, 2014

    @jseabold
    Member

    On Wed, May 7, 2014 at 7:20 PM, Josef Perktold [email protected]:

    It's not directly related to this issue, which is that in some edge cases
    like Longley we might run into different definitions of singulardepending on which linear algebra we use
    (aside: looking at the variables in the regression the question is: why
    would you ever want to do this, except as numerical exercise and test case?)

    The Longley regression? You wouldn't. The model was from the days before
    "time series analysis," and is just a high condition number benchmark
    example.

    But thanks for the link, I didn't remember this or never looked closely
    enough. I'm partway through sparse version of linear regression and
    Ridge/Theil, and this is helpful.
    Currently, I'm stuck figuring out unit tests for the existing dense
    version.

    What does scikit-learn do for their ridge?

  4. josef-pkt commented on May 7, 2014

    @josef-pkt
    MemberAuthor

    What does scikit-learn do for their ridge?

    I'm not so far yet. I just started to look at their documentation again. I haven't looked at their Ridge code since it was initially added. I had also found the same lecture notes that they reference, but I'm not yet using a numerically efficient implementation and might borrow part of their code if it's useful.
    I'm still in the more features but less optimized code phase.
    MASS lm.ridge doesn't have cov_params and bse, And I disagree intentionally with Christopher Baum's tgmixed about the df_resid.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions