You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
White specification test, sanity test, robust standard errors #1126
I thought I already added White's general (mis-)specification test somewhere, but I don't find it in the statsmodels source.
Test is based on comparing robust and non-robust cov_params.
Hi Josef - I placed a version of White's spec test in my repository JimVaranelli/WhiteSpec. It operates the same way as the SAS test in that linearly dependent exogenous variables are dropped with a warning. This is useful if the model has dummies. It's been verified extensively against SAS for stat/pval correctness. I have not compared it to other packages like R, etc. I'd be more than happy to add it to statsmodels if interested. Jim
@JimVaranelli Thanks, yes I'm interested and it would make a good addition.
How to treat categorical when squaring is still an open issue #1061. Currently, we use matrix rank and pinv to avoid having to check, AFAIR.
I think your spec_white could go with small changes into diagnostics.
The prep_white_spec_design_matrix looks computationally a bit inefficient. (numpy append shouldn't be used in a loop because it will most likely need to create several intermediate arrays during appending.) Also it might be better to do a full colinearity check in this case instead of just checking individual squared columns, e.g. if the original exog/design already includes a squared x. So this check doesn't guarantee that the rank is equal to the number of columns (dof = devdesign.shape[1])
Test data for unit tests should go into csv files, so they are easier to use for non-Excel users.
(I'm planning to do one "round" of working on diagnostics for 0.10, for cases where I have larger conceptual setup cost and need to read up again.)
My guess is that we want to add in the colinearity or dummy checks at some point. That would be computationally more efficient when there are many categorical/dummy variables.
I didn't realize or didn't pay much attention to the possible problem of creating colinear squared variables when I originally wrote those diagnostic and specification tests.
Agreed the check as written is highly limited in scope. A more general approach will probably be a todo item. Tomorrow I’ll upload the spec_white function alone as it would appear in the diagnostic src tree (including Sphinx comment) without the collin check. It will look quite similar to het_white.
On May 16, 2018, at 11:37 AM, Josef Perktold ***@***.***> wrote:
Another point about dummy check x == x**2:
This will not identify categorical variables that have a non-{0, 1} encoding.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
OK I've uploaded the function in near-final form in file WhiteSpecFn.py. It follows the same methodology as het_white. I removed the simple collinearity check but of course the two test cases with dummies now fail with a singular matrix error. Does it make sense to spend the time/resources to do a full rank calculation (QR decomp or something similar) to remove the dependent columns in the general sense? The OLS function appears to do this but I don't think the reduced design matrix is accessible? Let me know your thoughts. Thanks.
I've been experimenting with different methods to extract the set of linearly independent column vectors from a design matrix in order to complete this enhancement. The most obvious method is a QR decomp as used in the _fit_collinear function above. My main objection to this approach is the high computational cost, which is best-case 2mn^2 for an m x n matrix. Compared to QR, a repeated application of Cauchy-Schwartz would instead yield complexity cost m^2n. Given the vast majority of OLS design matrices have n >> m, one would expect a reasonable speedup for the C-S method over QR. Unfortunately (or fortunately?) I was seeing no appreciable time savings with random test cases. For example, the output below is for a random test case with nobs=10,000 and m=9 (simulating a full-rank White spec test with k=3 original IVs). This surprised me since the example would have roughly 810k FLOPs for the C-S method and 1.8B FLOPs for the QR method. My working assumption is this discrepancy is due to the native LAPACK code executing much (MUCH) faster than the native Python/numpy code running my C-S routine. Both methods match SAS dof/stat/pvals but both methods also suffer from threshold uncertainty (_fit_collinear uses 1e-14 so I went with that). If there is no further input I will add the QR decomp for collinearity check and submit a pull request.
No linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(nocheck) = 0.37703609466552734
C-S linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(cs check) = 0.42704176902770996
QR linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(qr check) = 0.4440443515777588
One property of using linalg is that in most cases by default parallel processing is used by the BLAS/LAPACK libraries for large arrays, which can make it faster for large arrays or slow it down for not large arrays if other parallel processing is desired.
Yeah I tried a bunch of different code optimizations and I could not get the expected speedup for CS over QR even though QR performs an order of magnitude more operations than CS. So I left the QR in the final version of the code. I will merge the most current version of WhiteSpec.py into my repo later tonight so you can see how I did the CS check. WhiteSpecFn.py will show the function as it would appear in sm. I'll also add the individual .csv test cases to the repo.
Administrative questions:
should the collinear removal be a user option? default=no? or always run with no option?
should a warning be issued if collinear removal?
should the dof be a return value along with stat/pval?
should the function go into outliers_influence (with RESET) or diagnostic (with het_white)?
(I forgot to reply last time after getting distracted with qr)
I don't really now how much computation those linalg functions are doing, but my guess was that "r" only qr should be doing about as much work as Cauchy-Schwarz or do I think of something else.
(The main difference is whether we work on decomposition of the original nobs data or on the moment matrix, X' X. The latter is faster for nobs >> k_vars, but numerically less precise. E.g. cholesky would to the same thing as "r" only QR.)
should the collinear removal be a user option? default=no? or always run with no option?
No, this is just an implementation detail. If there are dummy variables the extended array will be collinear, but it's not something that the user needs to worry about.
If we use different ways of computing it, then we might add a method option if it is relevant for the user.
should a warning be issued if collinear removal?
no, as above
should the dof be a return value along with stat/pval?
degrees of freedom should be returned,
In general I would like to move to returning a simple class with attributes by default (still not generally used yet.
should the function go into outliers_influence (with RESET) or diagnostic (with het_white)?
in diagnostics, it has a different purpose than outliers_influence
(RESET test is misplaced in there #3690 )
Activity
JimVaranelli commentedon May 16, 2018
Hi Josef - I placed a version of White's spec test in my repository JimVaranelli/WhiteSpec. It operates the same way as the SAS test in that linearly dependent exogenous variables are dropped with a warning. This is useful if the model has dummies. It's been verified extensively against SAS for stat/pval correctness. I have not compared it to other packages like R, etc. I'd be more than happy to add it to statsmodels if interested. Jim
josef-pkt commentedon May 16, 2018
@JimVaranelli Thanks, yes I'm interested and it would make a good addition.
How to treat categorical when squaring is still an open issue #1061. Currently, we use matrix rank and pinv to avoid having to check, AFAIR.
I think your
spec_whitecould go with small changes into diagnostics.The
prep_white_spec_design_matrixlooks computationally a bit inefficient. (numpy append shouldn't be used in a loop because it will most likely need to create several intermediate arrays during appending.) Also it might be better to do a full colinearity check in this case instead of just checking individual squared columns, e.g. if the original exog/design already includes a squared x. So this check doesn't guarantee that the rank is equal to the number of columns (dof = devdesign.shape[1])Test data for unit tests should go into csv files, so they are easier to use for non-Excel users.
(I'm planning to do one "round" of working on diagnostics for 0.10, for cases where I have larger conceptual setup cost and need to read up again.)
josef-pkt commentedon May 16, 2018
about checking colinearity:
There are several PRs that add functions to drop colinear columns which most likely make it into 0.10.
One addition that I am currently working on will integrate it with a fit method, fit_colinear
https://github.com/statsmodels/statsmodels/pull/4576/files#diff-b165b4bd4e10edd0dfb485e47c562b2cR646
Those function will then be available for reuse by other function.
JimVaranelli commentedon May 16, 2018
josef-pkt commentedon May 16, 2018
My guess is that we want to add in the colinearity or dummy checks at some point. That would be computationally more efficient when there are many categorical/dummy variables.
I didn't realize or didn't pay much attention to the possible problem of creating colinear squared variables when I originally wrote those diagnostic and specification tests.
josef-pkt commentedon May 16, 2018
Another point about dummy check
x == x**2:This will not identify categorical variables that have a non-{0, 1} encoding.
JimVaranelli commentedon May 17, 2018
JimVaranelli commentedon May 18, 2018
OK I've uploaded the function in near-final form in file WhiteSpecFn.py. It follows the same methodology as het_white. I removed the simple collinearity check but of course the two test cases with dummies now fail with a singular matrix error. Does it make sense to spend the time/resources to do a full rank calculation (QR decomp or something similar) to remove the dependent columns in the general sense? The OLS function appears to do this but I don't think the reduced design matrix is accessible? Let me know your thoughts. Thanks.
JimVaranelli commentedon May 22, 2018
I've been experimenting with different methods to extract the set of linearly independent column vectors from a design matrix in order to complete this enhancement. The most obvious method is a QR decomp as used in the _fit_collinear function above. My main objection to this approach is the high computational cost, which is best-case 2mn^2 for an m x n matrix. Compared to QR, a repeated application of Cauchy-Schwartz would instead yield complexity cost m^2n. Given the vast majority of OLS design matrices have n >> m, one would expect a reasonable speedup for the C-S method over QR. Unfortunately (or fortunately?) I was seeing no appreciable time savings with random test cases. For example, the output below is for a random test case with nobs=10,000 and m=9 (simulating a full-rank White spec test with k=3 original IVs). This surprised me since the example would have roughly 810k FLOPs for the C-S method and 1.8B FLOPs for the QR method. My working assumption is this discrepancy is due to the native LAPACK code executing much (MUCH) faster than the native Python/numpy code running my C-S routine. Both methods match SAS dof/stat/pvals but both methods also suffer from threshold uncertainty (_fit_collinear uses 1e-14 so I went with that). If there is no further input I will add the QR decomp for collinearity check and submit a pull request.
No linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(nocheck) = 0.37703609466552734
C-S linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(cs check) = 0.42704176902770996
QR linear independence check
White spec: dof = 9 stat = 8.80503162028 pval = 0.455462832625
time(qr check) = 0.4440443515777588
josef-pkt commentedon May 22, 2018
About speed: scipy linalg qr has a 'r' only option which should be faster
https://docs.scipy.org/doc/scipy-0.16.1/reference/generated/scipy.linalg.qr.html
One property of using linalg is that in most cases by default parallel processing is used by the BLAS/LAPACK libraries for large arrays, which can make it faster for large arrays or slow it down for not large arrays if other parallel processing is desired.
josef-pkt commentedon May 22, 2018
actually numpy has it also
josef-pkt commentedon May 22, 2018
my mistake, I already use 'r' option
JimVaranelli commentedon May 24, 2018
Yeah I tried a bunch of different code optimizations and I could not get the expected speedup for CS over QR even though QR performs an order of magnitude more operations than CS. So I left the QR in the final version of the code. I will merge the most current version of WhiteSpec.py into my repo later tonight so you can see how I did the CS check. WhiteSpecFn.py will show the function as it would appear in sm. I'll also add the individual .csv test cases to the repo.
Administrative questions:
josef-pkt commentedon May 24, 2018
(I forgot to reply last time after getting distracted with qr)
I don't really now how much computation those linalg functions are doing, but my guess was that "r" only qr should be doing about as much work as Cauchy-Schwarz or do I think of something else.
(The main difference is whether we work on decomposition of the original nobs data or on the moment matrix, X' X. The latter is faster for nobs >> k_vars, but numerically less precise. E.g. cholesky would to the same thing as "r" only QR.)
No, this is just an implementation detail. If there are dummy variables the extended array will be collinear, but it's not something that the user needs to worry about.
If we use different ways of computing it, then we might add a
methodoption if it is relevant for the user.no, as above
degrees of freedom should be returned,
In general I would like to move to returning a simple class with attributes by default (still not generally used yet.
in diagnostics, it has a different purpose than outliers_influence
(RESET test is misplaced in there #3690 )
JimVaranelli commentedon May 24, 2018
Thanks for the quick response! I will incorporate the above and submit a pull request in the next few days.
JimVaranelli commentedon Jun 7, 2018
Pull request submitted.