Repository navigation
Open
Description
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, 13.0], [1.0, 1.0, 1.0, 1.0, 1.0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0, 1.0, 1.0, 1.0, 1.0, 0.0, 31.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 32.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 33.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 34.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 35.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 36.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 37.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 38.0], [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 39.0]])
>>> predictors = np.asarray([[1.0, 0.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 0.0, 1.0, 2.0], [1.0, 0.0, 1.0, 3.0], [1.0, 0.0, 1.0, 4.0], [1.0, 0.0, 1.0, 5.0], [1.0, 0.0, 1.0, 6.0], [1.0, 0.0, 1.0, 7.0], [1.0, 0.0, 1.0, 8.0], [1.0, 0.0, 1.0, 9.0], [1.0, 0.0, 1.0, 10.0], [1.0, 0.0, 1.0, 11.0], [1.0, 0.0, 1.0, 12.0], [1.0, 0.0, 1.0, 13.0], [1.0, 0.0, 1.0, 14.0], [1.0, 0.0, 1.0, 15.0], [1.0, 0.0, 1.0, 16.0], [1.0, 0.0, 1.0, 17.0], [1.0, 0.0, 1.0, 18.0], [1.0, 0.0, 1.0, 19.0], [1.0, 1.0, 1.0, 20.0], [1.0, 1.0, 1.0, 21.0], [1.0, 1.0, 1.0, 22.0], [1.0, 1.0, 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
chartl commentedon Mar 10, 2014
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.
josef-pkt commentedon Mar 10, 2014
is the predictor matrix singular?
does summary for one of them print a warning or looks otherwise strange?
chartl commentedon Mar 10, 2014
The predictor matrix isn't strictly singular, (all singular values > 0), but it does look like all the responses are linearly separable.
jseabold commentedon Mar 10, 2014
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.
josef-pkt commentedon Mar 10, 2014
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.
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
prediction standard errors and confidence interval :)
josef-pkt commentedon Mar 10, 2014
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)
chartl commentedon Mar 10, 2014
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.
josef-pkt commentedon Mar 10, 2014
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
josef-pkt commentedon Mar 10, 2014
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.