Repository navigation
ENH: White spec test (issue1126) - #4721
JimVaranelli wants to merge 8 commits into
Conversation
| # chi-square test | ||
| dof = devx.shape[1] | ||
| pval = stats.chi2.sf(stat, dof) | ||
| return dof, stat, pval |
There was a problem hiding this comment.
stat, pval, dof
if we return tuple
There was a problem hiding this comment.
This was updated in the previous commit - forgot to update this note.
josef-pkt
left a comment
There was a problem hiding this comment.
A quick first review with comments about the things that I can see quickly.
I will do a fuller review over the next days.
I think it looks good overall.
Thanks
| i0, i1 = np.triu_indices(x.shape[1]) | ||
| exog = np.delete(x[:,i0]*x[:,i1], 0, 1) | ||
| assert exog.shape[1] == nvar + nvar * (nvar + 1) / 2 | ||
| # collinearity check - see _fit_collinear |
There was a problem hiding this comment.
blank line before makes it easier to quickly skim the blocks
| sqmndevs = sqe - np.mean(sqe) | ||
| D = np.dot(exog.T, sqmndevs) | ||
| devx = exog - np.mean(exog, axis=0) | ||
| B = np.linalg.multi_dot([devx.T, np.diag(np.square(sqmndevs)), devx]) |
There was a problem hiding this comment.
we can avoid creating diagonal matrices
e.g. x.T dot diag(s) dot x = (x.T * s).dot(x)
however, in this case I would do something like
after
devx = exog - np.mean(exog, axis=0)
devx *= sqmndevs[:, None]
B = devx.T.dot(devx)
There was a problem hiding this comment.
This looks reasonable but I want to experiment with it some more. I will update it the next commit.
There was a problem hiding this comment.
Thanks for the pointer to the newaxis function it was a new one for me and it works great in this particular instance. Updated.
| nvar = x.shape[1] - 1 | ||
| # add interaction terms | ||
| i0, i1 = np.triu_indices(x.shape[1]) | ||
| exog = np.delete(x[:,i0]*x[:,i1], 0, 1) |
There was a problem hiding this comment.
whitespace around operators except **
| mask = np.abs(r.diagonal()) < np.sqrt(tol) | ||
| exog = exog[:,np.where(~mask)[0]] | ||
| # calculate test statistic | ||
| sqe = np.square(e) |
There was a problem hiding this comment.
Is there an advantage to using square instead of just e*e or e**2?
(I needed to look up what square does because I never used it.
There was a problem hiding this comment.
I actually had e * e in the first cut but np.square may be faster for ;large arrays. We have the same square function in our c++ vector / matrix classes so I went with it. But I think in the majority of cases there will be no real difference so I switched it back.
| def test_spec_white(self): | ||
| resdir = os.path.join(cur_dir, "results") | ||
| wsfiles = ['wspec1.csv', 'wspec2.csv', 'wspec3.csv', 'wspec4.csv'] | ||
| for file in os.listdir(resdir): |
There was a problem hiding this comment.
I think this os.listdir can be skipped
just loop over wsfiles directly
There was a problem hiding this comment.
I chose the method shown more out of fault tolerance than any performance concerns but now that I think about it if someone accidentally removes one of the test files the correct action is to error out. I will change it in the next commit.
| D = np.dot(exog.T, sqmndevs) | ||
| devx = exog - np.mean(exog, axis=0) | ||
| B = np.linalg.multi_dot([devx.T, np.diag(np.square(sqmndevs)), devx]) | ||
| stat = np.linalg.multi_dot([D, np.linalg.inv(B), D]) |
There was a problem hiding this comment.
if we don't need the explicit inverse, then using linalg.solve is better
stats = D.dot.(np.linalg.solve(B, D)
There was a problem hiding this comment.
I will take a closer look and update in the next commit.
There was a problem hiding this comment.
I took a closer look at the np.solve and it indeed has speed/accuracy advantage for large arrays. Our local modeling tends to be very small sample sizes (nobs<50) so this has no practical significance in our environment but it is definitely helpful in the general case here. Updated.
|
This pull request introduces 1 alert when merging 271b828 into 8928f8d - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
BTW: You can ignore the LGTM circular import alerts. |
|
This pull request introduces 1 alert when merging bb23f81 into 8928f8d - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
This pull request introduces 1 alert when merging 5adc464 into 8928f8d - view on LGTM.com new alerts:
Comment posted by LGTM.com |
|
This pull request introduces 1 alert when merging 256bf53 into 8928f8d - view on LGTM.com new alerts:
Comment posted by LGTM.com |
Codecov Report
@@ Coverage Diff @@
## master #4721 +/- ##
==========================================
+ Coverage 80.88% 81.38% +0.49%
==========================================
Files 577 581 +4
Lines 88572 90234 +1662
Branches 9885 10082 +197
==========================================
+ Hits 71642 73435 +1793
+ Misses 14672 14505 -167
- Partials 2258 2294 +36
Continue to review full report at Codecov.
|
|
@JimVaranelli Thank you LGTM, the linear algebra looks good, but I didn't try to understand the computation. Can you squash this? |
| # add interaction terms | ||
| i0, i1 = np.triu_indices(x.shape[1]) | ||
| exog = np.delete(x[:,i0] * x[:,i1], 0, 1) | ||
| assert exog.shape[1] == nvar + nvar * (nvar + 1) / 2 |
There was a problem hiding this comment.
Should not leave assertions in final code
| return lm, lm_pval, ftest | ||
|
|
||
|
|
||
| def spec_white(resid, exog): |
There was a problem hiding this comment.
This should be added to the main code, not sandbox. Virtually nothing gets out of the sb, and if tested, then it should be in main.
| # calculate test statistic | ||
| sqe = e * e | ||
| sqmndevs = sqe - np.mean(sqe) | ||
| D = np.dot(exog.T, sqmndevs) |
There was a problem hiding this comment.
should use lowercase for better pep compliance.
| D = np.dot(exog.T, sqmndevs) | ||
| devx = exog - np.mean(exog, axis=0) | ||
| devx *= sqmndevs[:, None] | ||
| B = devx.T.dot(devx) |
| --------- | ||
| White, H. (1980). A heteroskedasticity-consistent covariance matrix | ||
| estimator and a direct test for heteroscedasticity. Econometrica, | ||
| 48: 817-838. |
There was a problem hiding this comment.
@bashtage @josef-pkt - Sorry for the delay. I made the changes above except for relocating out of sandbox. I believe the reason it's in sandbox is due to the still experimental nature of the collinearity check. Is this correct?
Also, there's been no further comments regarding the Zivot-Andrews test. Is there still interest in my generating a PR for that? See #4746
|
Cleared the Codacy unused variable check. Not sure what to do about the test_spec_white() method could be a function complaint? |
You added the test as a method to a test class, but you are not actually using anything in self, e.g. any of the data or results attached to the class/instance. |
|
Moved the static function outside of the class. |
|
@josef-pkt Looks ready. @JimVaranelli We can wait to move it out of the sandbox. This is a nice function and should be in main sm. There are a few other diagnostic functions in sandbox that can be moved at the same time in a later PR once this is merged. |
|
LGTM, the only issue is to clean up the commit history. |
|
@josef-pkt Sorry for the delay I've been swamped with deliverable deadlines these past few weeks. I should be able to get the new PR out this weekend or early next week. I will also finalize the code for the ZA test (#4746) and generate a PR. With @bashtage code optimization I think SM will have the best publically available version of that particular test. |
|
@josef-pkt Sorry again for the delay here. Would you still like me to create a new clean PR for this? I expect to submit a new PR for #4746 in the next few days. |
|
@JimVaranelli Yes, please do. I put on my personal todo list to rebase PRs that are essentially ready for merging but have fallen too far behind master. |
|
@josef-pkt OK will do. I'll definitely get that done this weekend. I'll also try to push out the ZA test PR this weekend as well but if not this weekend then early next week. |
|
@josef-pkt Added PR 5452 for clean commit history |
White's two-moment specification test. Unit test compares to SAS 9.3.
#1126