Repository navigation
ENH: Add nlag argument to acovf - #4937
Conversation
|
The proposal to move to fft=True might be a little controversial, but given it is an order of magnitude faster than correlate, it should be preferred. |
| divisor[0] = notmask_int.sum() | ||
| for i in range(lag_len): | ||
| divisor[i + 1] = notmask_int[i + 1:].dot(notmask_int[:-(i + 1)]) | ||
| # divisor[i + 1] = [notmask_int[i + 1:] * notmask_int[:-(i + 1)].sum() |
There was a problem hiding this comment.
Is this commented out line needed?
There was a problem hiding this comment.
No, it was an experiment I forgot to run about performance
| maxlag = max(lags) | ||
| acfx = acf(x, nlags=maxlag) # normalize by nobs not (nobs-nlags) | ||
| # SS: unbiased=False is default now | ||
| # normalize by nobs not (nobs-nlags) |
There was a problem hiding this comment.
Doesn’t need to be in this PR, but this code is actually used, does not belong in the sandbox.
There was a problem hiding this comment.
I am just silencing a warning I added about future changes.
There was a problem hiding this comment.
I meant the entire function belongs in stats.diagnostic (or possibly tsa.???); nothing specific to this particular code.
|
Quick skim this looks nice. Perf improvements are always a nice perk. |
c15f51f to
251e23e
Compare
Codecov Report
@@ Coverage Diff @@
## master #4937 +/- ##
==========================================
+ Coverage 81.4% 81.42% +0.01%
==========================================
Files 582 582
Lines 90063 90124 +61
Branches 10087 10096 +9
==========================================
+ Hits 73318 73384 +66
+ Misses 14439 14436 -3
+ Partials 2306 2304 -2
Continue to review full report at Codecov.
|
65f9c33 to
f9cd45f
Compare
| acf(x, nlags=40) | ||
|
|
||
|
|
||
| if __name__ == "__main__": |
There was a problem hiding this comment.
you could delete this when you are in the neighborhood.
AFAIK, we don't use the test runner in main anymore
josef-pkt
left a comment
There was a problem hiding this comment.
Other than my comments it looks fine to me.
The new option is useful, and LGTM
I don't have a strong opinion on fft default and the change is fine with me.
A long time ago Warren did some timing and correlate was a bit faster than fft for smaller arrays, maybe up to one or a few thousand, but AFAIR there wasn't much difference.
| def test_acovf_error(): | ||
| with pytest.raises(ValueError): | ||
| x = np.random.randn(250) | ||
| acovf(x, nlag=250, fft=False) |
| @pytest.mark.parametrize("unbiased", [True, False]) | ||
| def test_acovf_nlags(unbiased, demean, fft, missing): | ||
| rnd = np.random.RandomState(12345) | ||
| x = rnd.randn(250) |
There was a problem hiding this comment.
my guess is that this would be better as a class with x created as a setup class
IIUC, we are creating the same randn a large number of times, both in this and the other test functions that are added here
| else: | ||
| acov /= notmask_int.sum() | ||
| return acov | ||
|
|
There was a problem hiding this comment.
add a comment line here, about the case that's left "either fft or nlag is None", i.e. when we use correlate or fft.
When reading through the function, the return in this path is not obvious enough.
Add argument to limit computation and output Propose settig fft=True in future version closes statsmodels#4135
Add missing argument to ensure warning is triggered where expected
Final clean up Switch to fixture rather than regenerating data
d6c10d6 to
8f4b9da
Compare
|
green enough, merging @bashtage Thank you |
| cls.res_conservative = acf(cls.x, nlags=40, qstat=True, alpha=.05, | ||
| missing='conservative') | ||
| cls.acf_none = np.empty(40) * np.nan # lags 1 to 40 inclusive | ||
| fft=False, missing='conservative') |
There was a problem hiding this comment.
elsewhere fft kwarg goes after missing. any reason for differenc ehere?
| A string in ['none', 'raise', 'conservative', 'drop'] specifying how the NaNs | ||
| are to be treated. | ||
| A string in ['none', 'raise', 'conservative', 'drop'] specifying how | ||
| the NaNs are to be treated. |
There was a problem hiding this comment.
the NaNs --> any NaNs ?
| notmask_int = notmask_bool.astype(int) #int | ||
| else: # 'drop' | ||
| x = x[notmask_bool] # copies non-missing | ||
| notmask_int = notmask_bool.astype(int) # int |
There was a problem hiding this comment.
I think the # int comment here is noise
| divisor = np.empty(lag_len + 1, dtype=np.int64) | ||
| divisor[0] = notmask_int.sum() | ||
| for i in range(lag_len): | ||
| divisor[i + 1] = notmask_int[i + 1:].dot(notmask_int[:-(i + 1)]) |
| elif nlag > n - 1: | ||
| raise ValueError('nlag must be smaller than nobs - 1') | ||
|
|
||
| if not fft and nlag is not None: |
There was a problem hiding this comment.
candidate for breaking out into a more focused function? This function is getting pretty big
Add argument to limit computation and output
Propose settig fft=True in future version
closes #4135