Skip to content

perfect prediction: OLS produces very different results on different systems #1459

Open

Description

@chartl

OLS in statsmodels produces different results when run on OSX or Ubuntu. Code:

ython 2.7.5+ (default, Sep 19 2013, 13:48:49) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import statsmodels
>>> import statsmodels.api as stats
>>> import numpy as np
>>> responses = np.asarray([[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 3.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 4.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 5.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 6.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 7.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 8.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 9.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 10.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 11.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 12.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 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, 1.0, 23.0], [1.0, 1.0, 1.0, 24.0], [1.0, 1.0, 1.0, 25.0], [1.0, 1.0, 1.0, 26.0], [1.0, 1.0, 1.0, 27.0], [1.0, 1.0, 1.0, 28.0], [1.0, 1.0, 1.0, 29.0], [1.0, 1.0, 0.0, 30.0], [1.0, 1.0, 0.0, 31.0], [1.0, 1.0, 0.0, 32.0], [1.0, 1.0, 0.0, 33.0], [1.0, 1.0, 0.0, 34.0], [1.0, 1.0, 0.0, 35.0], [1.0, 1.0, 0.0, 36.0], [1.0, 1.0, 0.0, 37.0], [1.0, 1.0, 0.0, 38.0], [1.0, 1.0, 0.0, 39.0]])
>>> def calc_p(idx):
...  fit = stats.regression.linear_model.OLS(responses[:, idx], predictors)
...  pval = fit.fit().pvalues[1]
...  return -np.log10(pval)
... 
>>> [calc_p(i) for i in xrange(12)]

Results (OSX):
[0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, 0.048289753396822455, -0.0, 1.8705516032101939]

Results(Ubuntu):
[0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.076789855682299193, 0.39363496152986921, 0.15508352013515456]

Activity

  1. chartl commented on Mar 10, 2014

    @chartl
    Author

    You know, this is a pretty unstable example (there's no actual variance in the response). I'm not sure these differences matter in practice. But they're there.

  2. josef-pkt commented on Mar 10, 2014

    @josef-pkt
    Member

    is the predictor matrix singular?

    does summary for one of them print a warning or looks otherwise strange?

  3. chartl commented on Mar 10, 2014

    @chartl
    Author

    The predictor matrix isn't strictly singular, (all singular values > 0), but it does look like all the responses are linearly separable.

  4. jseabold commented on Mar 10, 2014

    @jseabold
    Member

    The betas are all zero except for the constant because there's no variance. In the latter two cases the responses are actually columns in the predictors. My guess is that the linear algebra libraries return a slightly different results here depending on the platform and compilation. This leads to slightly different betas, standard errors, and t-values. Not really sure there's anything we can do here other than refusing to estimate models without variance in the response or drop columns that are exactly the same as the response.

    [~/]
    [48]: np.set_printoptions(suppress=False)
    
    [~/]
    [49]: sm.OLS(responses[:,9], predictors).fit().params
    [49]: 
    array([  1.00000000e+00,  -2.77555756e-16,  -6.93889390e-17,
            -4.16333634e-17])
    
  5. josef-pkt commented on Mar 10, 2014

    @josef-pkt
    Member

    The problem is perfect prediction not the missing variation in endog. (For example for some diagnostic tests we regress a vector of ones on the explanatory variables without constant.)

    scale estimate is zero.

    >>> responses.var(0)
    array([   0.    ,    0.    ,    0.    ,    0.    ,    0.    ,    0.    ,
              0.    ,    0.    ,    0.    ,    0.    ,    0.1875,  133.25  ])
    

    The summary() just has zeros infs and nans. Trying to make sense of numerical noise doesn't work, and as Skipper said, numerical noise is platform, version dependent.
    Except for maybe Rsquared, all results look fine with the infs and nans from a brief look.

    prediction still works

    >>> res.predict(predictors.mean(0))
    array([ 1.])
    

    prediction standard errors and confidence interval :)

    >>> print res.t_test(predictors)
                                 Test for Constraints                             
    ==============================================================================
                     coef    std err          t      P>|t|      [95.0% Conf. Int.]
    ------------------------------------------------------------------------------
    c0             1.0000          0          0      1.000         1.000     1.000
    c1             1.0000          0          0      1.000         1.000     1.000
    c2             1.0000          0          0      1.000         1.000     1.000
    c3             1.0000          0          0      1.000         1.000     1.000
    c4             1.0000          0          0      1.000         1.000     1.000
    c5             1.0000          0          0      1.000         1.000     1.000
    c6             1.0000          0          0      1.000         1.000     1.000
    c7             1.0000          0          0      1.000         1.000     1.000
    c8             1.0000          0          0      1.000         1.000     1.000
    c9             1.0000          0          0      1.000         1.000     1.000
    c10            1.0000          0          0      1.000         1.000     1.000
    c11            1.0000          0          0      1.000         1.000     1.000
    c12            1.0000          0          0      1.000         1.000     1.000
    c13            1.0000          0          0      1.000         1.000     1.000
    c14            1.0000          0          0      1.000         1.000     1.000
    c15            1.0000          0          0      1.000         1.000     1.000
    c16            1.0000          0          0      1.000         1.000     1.000
    c17            1.0000          0          0      1.000         1.000     1.000
    c18            1.0000          0          0      1.000         1.000     1.000
    c19            1.0000          0          0      1.000         1.000     1.000
    c20            1.0000          0          0      1.000         1.000     1.000
    c21            1.0000          0          0      1.000         1.000     1.000
    c22            1.0000          0          0      1.000         1.000     1.000
    c23            1.0000          0          0      1.000         1.000     1.000
    c24            1.0000          0          0      1.000         1.000     1.000
    c25            1.0000          0          0      1.000         1.000     1.000
    c26            1.0000          0          0      1.000         1.000     1.000
    c27            1.0000          0          0      1.000         1.000     1.000
    c28            1.0000          0          0      1.000         1.000     1.000
    c29            1.0000          0          0      1.000         1.000     1.000
    c30            1.0000          0          0      1.000         1.000     1.000
    c31            1.0000          0          0      1.000         1.000     1.000
    c32            1.0000          0          0      1.000         1.000     1.000
    c33            1.0000          0          0      1.000         1.000     1.000
    c34            1.0000          0          0      1.000         1.000     1.000
    c35            1.0000          0          0      1.000         1.000     1.000
    c36            1.0000          0          0      1.000         1.000     1.000
    c37            1.0000          0          0      1.000         1.000     1.000
    c38            1.0000          0          0      1.000         1.000     1.000
    c39            1.0000          0          0      1.000         1.000     1.000
    ==============================================================================
    >>> 
    
  6. josef-pkt commented on Mar 10, 2014

    @josef-pkt
    Member

    I think you get exactly the same kind of behavior if response has some variation, but is perfectly predicted, for example if response is also included in exog.

    If scale is zero, then the t-values and p-values depend on numerical noise, I think in this case all p-values should be zero (or 1e-xxx, with xxx > a large number). But that's the usual problem what do to with 0/0. (scipy's t_test was changed to return nan if variance is zero)

  7. chartl commented on Mar 10, 2014

    @chartl
    Author

    I agree. I'm explicitly including response in exog in this test case, so this behavior is not unexpected. Feel free to mark this as closed.

    Thanks.

  8. josef-pkt commented on Mar 10, 2014

    @josef-pkt
    Member

    correction to my statement above

    the standard errors of all parameters should be zero, but the tvalues and pvalues are for the test that the parameter is zero. Explanatory variables that are not contributing to the perfect prediction should have tvalue=0/0. The inf tvalues and zero pvalues are numerical noise, but I don't know if we can identify when we only have noise and nan tvalues and pvalues in those cases.

    keep it open for now because we might still clean up more perfect prediction results.

    label corner case

  9. josef-pkt commented on Mar 10, 2014

    @josef-pkt
    Member

    I'm adding the summary printout for one example as reference.

    I think at least we can add and explanatory sentence to the summary string at the end.

    >>> idx = 0
    >>> mod = stats.regression.linear_model.OLS(responses[:, idx], predictors)
    >>> res = mod.fit()
    >>> print res.summary()
                                OLS Regression Results                            
    ==============================================================================
    Dep. Variable:                      y   R-squared:                         nan
    Model:                            OLS   Adj. R-squared:                    nan
    Method:                 Least Squares   F-statistic:                       nan
    Date:                Mon, 10 Mar 2014   Prob (F-statistic):                nan
    Time:                        15:31:56   Log-Likelihood:                    inf
    No. Observations:                  40   AIC:                              -inf
    Df Residuals:                      36   BIC:                              -inf
    Df Model:                           3                                         
    Covariance Type:            nonrobust                                         
    ==============================================================================
                     coef    std err          t      P>|t|      [95.0% Conf. Int.]
    ------------------------------------------------------------------------------
    const          1.0000          0        inf      0.000         1.000     1.000
    x1                  0          0        nan        nan             0         0
    x2          5.551e-17          0        inf      0.000      5.55e-17  5.55e-17
    x3          1.735e-18          0        inf      0.000      1.73e-18  1.73e-18
    ==============================================================================
    Omnibus:                        1.073   Durbin-Watson:                     nan
    Prob(Omnibus):                  0.585   Jarque-Bera (JB):               15.000
    Skew:                           0.000   Prob(JB):                     0.000553
    Kurtosis:                       0.000   Cond. No.                         148.
    ==============================================================================
    
    Warnings:
    [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
    
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