Repository navigation
CLN/TST: Improve arima_process - #3482
Conversation
59ad76a to
f5fbabe
Compare
1 similar comment
josef-pkt
left a comment
There was a problem hiding this comment.
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.
| 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') |
There was a problem hiding this comment.
this should be a Warning not a print.
|
I think it's better to rebase given that it looks far behind master, and then merge. |
| 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 |
| 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 |
There was a problem hiding this comment.
"geneated" --> "generated", not sure about the rest of the phrase
| if 'nobs' in kwargs: | ||
| leads = kwargs['nobs'] | ||
| import warnings | ||
| warnings.warn(DeprecationWarning, 'nobs is deprecated in favor of leads') |
There was a problem hiding this comment.
This has to be a common thing right? Does pandas have a decorator for this?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
(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.)
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Note that this gives an approximation?
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
Here I mean approximation in the sense of "not an analytic solution". Other docstrings are explicit about analytic vs approximation.
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
Should this be inserting a positive 1 for the ma?
There was a problem hiding this comment.
@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.
Clean and test arima process functions
|
Rebased with small typo fixes suggested. |
f5fbabe to
174fdd9
Compare
|
Nevermind. User error. |
Final pass cleaning and rebasing Fixed small typos and flake8 issues Fixed sign in MA
174fdd9 to
29f1287
Compare
|
as green as it gets. merging @bashtage Thank your @jbrockmendel Thanks for reviewing (PR and code review are still a bottleneck) |
Clean and test arima process functions