Skip to content

ENH: Add nlag argument to acovf - #4937

Merged
josef-pkt merged 3 commits into
statsmodels:masterfrom
bashtage:limit-acf-computation
Aug 26, 2018
Merged

josef-pkt merged 3 commits into
statsmodels:masterfrom
bashtage:limit-acf-computation

Conversation

@bashtage

Copy link
Copy Markdown
Member

Add argument to limit computation and output
Propose settig fft=True in future version

closes #4135

@bashtage

Copy link
Copy Markdown
Member Author

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.

Comment thread statsmodels/tsa/stattools.py Outdated
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this commented out line needed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn’t need to be in this PR, but this code is actually used, does not belong in the sandbox.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just silencing a warning I added about future changes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the entire function belongs in stats.diagnostic (or possibly tsa.???); nothing specific to this particular code.

@jbrockmendel

Copy link
Copy Markdown
Contributor

Quick skim this looks nice. Perf improvements are always a nice perk.

@coveralls

coveralls commented Aug 18, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 83.988% when pulling 8f4b9da on bashtage:limit-acf-computation into 195b723 on statsmodels:master.

@bashtage
bashtage force-pushed the limit-acf-computation branch from c15f51f to 251e23e Compare August 20, 2018 20:50
@codecov-io

codecov-io commented Aug 21, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #4937 into master will increase coverage by 0.01%.
The diff coverage is 98.92%.

Impacted file tree graph

@@            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
Impacted Files Coverage Δ
statsmodels/tsa/statespace/tests/test_tools.py 99.63% <ø> (ø) ⬆️
statsmodels/sandbox/stats/diagnostic.py 58.85% <100%> (ø) ⬆️
statsmodels/tsa/tests/test_tsa_tools.py 99.8% <100%> (ø) ⬆️
statsmodels/tsa/tests/test_stattools.py 98.42% <100%> (+0.96%) ⬆️
statsmodels/tsa/stattools.py 84.65% <98.03%> (+1.86%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 195b723...8f4b9da. Read the comment docs.

@bashtage
bashtage force-pushed the limit-acf-computation branch 3 times, most recently from 65f9c33 to f9cd45f Compare August 24, 2018 07:18
Comment thread statsmodels/tsa/tests/test_stattools.py Outdated
acf(x, nlags=40)


if __name__ == "__main__":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could delete this when you are in the neighborhood.
AFAIK, we don't use the test runner in main anymore

@josef-pkt josef-pkt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread statsmodels/tsa/tests/test_stattools.py Outdated
def test_acovf_error():
with pytest.raises(ValueError):
x = np.random.randn(250)
acovf(x, nlag=250, fft=False)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these have no random seed

Comment thread statsmodels/tsa/tests/test_stattools.py Outdated
@pytest.mark.parametrize("unbiased", [True, False])
def test_acovf_nlags(unbiased, demean, fft, missing):
rnd = np.random.RandomState(12345)
x = rnd.randn(250)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@bashtage
bashtage force-pushed the limit-acf-computation branch from d6c10d6 to 8f4b9da Compare August 26, 2018 13:53
@josef-pkt

Copy link
Copy Markdown
Member

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')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrap long line pls

elif nlag > n - 1:
raise ValueError('nlag must be smaller than nobs - 1')

if not fft and nlag is not None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

candidate for breaking out into a more focused function? This function is getting pretty big

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

O(T^2) complexity of stattools.acf

5 participants