Skip to content

CLN/TST: Improve arima_process - #3482

Merged
josef-pkt merged 2 commits into
statsmodels:masterfrom
bashtage:test-arima-process
May 14, 2017
Merged

josef-pkt merged 2 commits into
statsmodels:masterfrom
bashtage:test-arima-process

Conversation

@bashtage

Copy link
Copy Markdown
Member

Clean and test arima process functions

@coveralls

coveralls commented Feb 13, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 90.4% when pulling 59ad76a on bashtage:test-arima-process into e757e8b on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 90.408% when pulling f5fbabe on bashtage:test-arima-process into 898ddfc on statsmodels:master.

1 similar comment
@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 90.408% when pulling f5fbabe on bashtage:test-arima-process into 898ddfc on statsmodels:master.

@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.

I went through most of the changes again, and it looks good.

There are some backwards incompatible name changes in the methods, but I don't think they will affect (m)any users, e.g. if positional argument instead of keyword is used. Also, a github search for some of the names only leads to copies of statsmodels, as far as I could see.

Comment thread statsmodels/tsa/arima_process.py Outdated
sd = np.abs(h) ** 2 / np.sqrt(2 * np.pi)
if np.any(np.isnan(h)):
# this happens with unit root or seasonal unit root'
print('Warning: nan in frequency response h, maybe a unit root')

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.

this should be a Warning not a print.

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.

(not changed in this PR)

@josef-pkt

Copy link
Copy Markdown
Member

I think it's better to rebase given that it looks far behind master, and then merge.

Comment thread statsmodels/tsa/arima_process.py Outdated
while ir[-1] > 5*1e-5:
nobs_ir = max(100, 2 * nobs) # no idea right now
ir = arma_impulse_response(ar, ma, leads=nobs_ir)
# better save than sorry (?), I have no idea about the required precision

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.

"save" --> "safe"

Comment thread statsmodels/tsa/arima_process.py Outdated
to reduce the effect of initial conditions, burnin observations at the
beginning of the sample are dropped
burnin : integer
Burnin observations at the geneated and dropped from the beginning of

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.

"geneated" --> "generated", not sure about the rest of the phrase

Comment thread statsmodels/tsa/arima_process.py Outdated
if 'nobs' in kwargs:
leads = kwargs['nobs']
import warnings
warnings.warn(DeprecationWarning, 'nobs is deprecated in favor of leads')

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.

This has to be a common thing right? Does pandas have a decorator for this?

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.

Numpy has a decorator for deprecating functions which we use.
The code here is copied 3 times or so, which we can survive.
(I'm not really a big fan of decorators on working code.)

#TODO: unify with ArmaProcess method
eta = sigma * distrvs(nsample+burnin)
# TODO: unify with ArmaProcess method
eta = sigma * distrvs(nsample + burnin)

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 distrvs always a function from np.random? If so, we might need to be more careful how we call this, since they don't have a uniform signature.

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.

In general (especially at the time I wrote these kind of things)
A distribution always refers to scipy.stats.distribution and compatible with that interface. Otherwise, a user needs to use a lambda function or similar to get a compatible function, e.g. I think in this case it can be any function that has already partialled out the distribution parameters and just needs the number of observations that should be created. For example the rvs method of a frozen distribution from scipy.stats should work.

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.

(Also, how generally these kind of things actually work depends on the unit tests and example cases, which can often be just the ones that the developer thought about or usually used.)

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.

There's a PR on deck that handles a few more variants in np.random and unifies the function arma_generate_sample with the method ArmaProcess.generate_sample as per the comment.

arma2ma = arma_impulse_response
def arma2ma(ar, ma, lags=100, **kwargs):
"""
Get the MA representation of an ARMA process

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.

Note that this gives an approximation?

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.

Why do you think so?
lfilter should be pretty accurate in this case.
(However, there are some parts in here where I didn't want to figure out the analytical formulas and how to code them, which then rely on that the truncation is negligible. One problem at the time was that I didn't find a reference package to write unit tests against.)

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.

Here I mean approximation in the sense of "not an analytic solution". Other docstrings are explicit about analytic vs approximation.

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.

It is the "analytic" solution, but it needs to be numerically computed, and lfilter does it, up to whatever numerical precision lfilter has. There is no truncation in the intermediate steps involved.

(Somewhere there is the numpy polynomial version, which just uses a different algorithm to compute the inverse lag polynomial.)

process = ArmaProcess.from_coeffs(np.array(ar), np.array(ma))

ar.insert(0, -1)
ma.insert(0, -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.

Should this be inserting a positive 1 for the ma?

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.

@bashtage Did you intentionally use a negative version of the lag polynomials here?

I'm not sure what it means for the ARMA process except that the filtered series has reversed sign, and the asserts pass.

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.

Should be fixed now.

Clean and test arima process functions
@bashtage

Copy link
Copy Markdown
Member Author

Rebased with small typo fixes suggested.

@bashtage
bashtage force-pushed the test-arima-process branch from f5fbabe to 174fdd9 Compare May 14, 2017 11:02
@bashtage

Copy link
Copy Markdown
Member Author

Nevermind. User error.

@coveralls

coveralls commented May 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 90.411% when pulling 174fdd9 on bashtage:test-arima-process into 95387b1 on statsmodels:master.

Final pass cleaning and rebasing
Fixed small typos and flake8 issues
Fixed sign in MA
@bashtage
bashtage force-pushed the test-arima-process branch from 174fdd9 to 29f1287 Compare May 14, 2017 18:02
@coveralls

coveralls commented May 14, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.1%) to 90.411% when pulling 29f1287 on bashtage:test-arima-process into 95387b1 on statsmodels:master.

@josef-pkt

Copy link
Copy Markdown
Member

as green as it gets. merging

@bashtage Thank your

@jbrockmendel Thanks for reviewing (PR and code review are still a bottleneck)

@josef-pkt
josef-pkt merged commit 86496a3 into statsmodels:master May 14, 2017
@josef-pkt josef-pkt mentioned this pull request Oct 7, 2017
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.

4 participants