Skip to content

ENH: Add naive seasonal decomposition function - #1484

Merged
jseabold merged 27 commits into
statsmodels:masterfrom
jseabold:seaonal-decomp
Apr 4, 2014
Merged

jseabold merged 27 commits into
statsmodels:masterfrom
jseabold:seaonal-decomp

Conversation

@jseabold

Copy link
Copy Markdown
Member

This adds a function seasonal_decompose that does a naive seasonal decomposition. It's pretty much the same as R's decompose function as far as functionality goes. I doubt anyone would ever want to use this in practice vs. model-based approaches, but it's ok for a quick look I suppose, and it gets the ball rolling a bit more. It needs a namespace decision. I'm thinking tsa.seasonal. It's not right/obvious in the filters namespace really. I also need to add a note and example to the release docs.

I expect that the first commit will be more contentious. I renamed arfilter -> convolution_filter and I added a true arfilter as recursive_filter. Unfortunately, they don't handle missing data well because of scipy/scipy#3454. I probably won't tackle that on this PR. I also didn't add any tests for these because I ran out of steam, and I just wan't sure what you were after for the 2d cases. I think recursive_filter will need to consider many cases 2d x, 2d filt, and/or 2d init. I think it'd be trivial to add some tests. E.g., just do the same thing over and over again in different dimensions. I'm just not seeing it in the code yet. It also needs to handle pandas-in pandas-out and dates with the wrapper functions.

@josef-pkt

Copy link
Copy Markdown
Member

I will look at it tonight.

overall for 2d, I was thinking of two or three versions

  • parallel: filter many time series at the same time
  • VAR like filter filter several time series with cross-feedback
  • ARX, distributed lag, miso_filter (multi input single output), with univariate y but including exog

several things I didn't manage because scipy didn't have proper multivariate filters. scipy.signal has improved a lot since then.

for some parts I tried to replicate built-in functions in GAUSS, to try to replicate some research code of econometricians I knew.

for the usecases the filter can go in two directions depending whether we want to estimate (get y_predicted/one-step-ahead prediction, unknown error) or we want to simulate (get y, observed error)

@jseabold

Copy link
Copy Markdown
Member Author

Ok, yeah. That's roughly what I thought. Parallel is easy. The rest, I didn't really look at. Might be broken now. convolution_filter should do whatever it did. I took out the 3d stuff since the code never got there, and I just wanted to get what I had working.

Looks like I'm on too new numpy. Hard to beleive nanmean was added in 1.8.0.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling a2e75ee on jseabold:seaonal-decomp into e1be28e on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member Author

I'd like to merge this to use it for the tests for exponential smoothing. Once we have a decomposed function it's easy to make many different types of series. Trend, no-trend, exponential trend, no-trend seasonality, etc.

Should I just pull out the untested cases from recursive_filter and convolution_filter?

Alternatively, we leave them in. They never really were tested and should work as they were (with correct names now). The only thing I know doesn't work right is init for greater than 1d series and/or filt in recursive_filter. I think init 2d + filt 2d + x 2d requires many permutations. (More than I care to work on.)

@jseabold

Copy link
Copy Markdown
Member Author

Actually, I can just rebase on top of this and work off that then rebase off master once it's settled. Not pressing.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling ebac972 on jseabold:seaonal-decomp into e1be28e on statsmodels:master.

@jseabold

Copy link
Copy Markdown
Member Author

I'm wanting to go ahead and merge this. What was there wasn't strictly correct (it wasn't an AR filter). Now the part's that used at least we know is correct. and things are correctly delineated. One possible option is just to disable the parts I'm not sure about if they're correct. I really don't want to take the time to write tests for the other parts of this code.

@jseabold

Copy link
Copy Markdown
Member Author

Cherry-picked a few commits from #1489. That will still need a rebase before merging. Might still want to consider using the pandas wrappers decorators that tsa (and predict, eventually) as actual decorators. I'm just mocking the decorator pattern with two lines rather than using it for what it's for right now.

I decided that fun is overrated and added tests and pandas wrappers to recursive_filter and convolution_filter. I still am not testing the case for filt 2d and init 2d for recursive. I don't really want to get into this for this PR...

There's also a plot method for the result now. This is good to merge as far as I'm concerned. 2d filtered coefficients should be looked and made to work (if they don't) at some point though.

@jseabold

Copy link
Copy Markdown
Member Author

I also added a note about the backwards incompatible changes to removing the incorrect arfilter.

@jseabold

jseabold commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

I went ahead and removed the 2d stuff for the recursive filter. signal.lfilter doesn't handle 2d inputs and the user can just as easily get what they want with a loop instead of our doing it. I cleared up and tested what is going on with the convolution_filter in the case of 2d inputs (also just a loop, but I left it since we handle the case of single column y and 2d filter). Our use case is different than just handing off 2d inputs to traditional signal.convolve I think. We can revisit in the future, if there are use cases it should be expanded for.

Comment thread statsmodels/tsa/filters/filtertools.py Outdated

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 worth saving two letters filter

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.

actually, better would be filter_coeff because it's not the polynomial it's the AR coefficients (negative and no leading 1)

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.

filter is a "reserved word"

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.

Eh, really? It makes everything more verbose without really adding much IMO given it's laid out for you in the docs.

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'm NOT reading docs, or at least, I don't want to look at them each time I use a function.

Don't use the same word to mean two different things. If I ever have to use this function, my first guess would be that it's a AR filter, i.e. a lag polynomial. (ar versus ar_coeffs is very clear in my opinion.)

(However, pandas became very popular with 2 and 3 letter names, -- I still haven't figured it out.)

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.

Again, not reading the docs isn't a valid argument for me. I don't write them to not be looked at.

Where are we using the same word to mean two different things? The docs should be cleared up to call them AR coefficients (as it is in R).

I'd rather go back to a than filter_coeffs.

@josef-pkt

Copy link
Copy Markdown
Member

I don't really have a strong opinion, since I haven't been working on this for a long time.

Dropping the extra dimension handling is fine, until we run into usecases where we need it. (I don't remember which multivariate dynamic model I had in mind.)

@jseabold

jseabold commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

I just wanted tested code in there and didn't want to spend anymore time correcting what was only a helper function for my original need. Easy to extend later, if someone comes knocking, but no one noticed before. We'll see.

@jseabold

jseabold commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

I addressed all of the comments. We'll see what travis says. I'd like to merge this asap to get it off my plate and we can come back around to it when/if we need to.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage remained the same when pulling 91d0682 on jseabold:seaonal-decomp into b2cefd2 on statsmodels:master.

@jseabold

jseabold commented Apr 4, 2014

Copy link
Copy Markdown
Member Author

Test failure is unrelated. I'm going to rebase and merge this.

jseabold added a commit that referenced this pull request Apr 4, 2014
ENH: Add naive seasonal decomposition function
@jseabold
jseabold merged commit ec765bb into statsmodels:master Apr 4, 2014
@jseabold
jseabold deleted the seaonal-decomp branch April 4, 2014 04:22
@josef-pkt josef-pkt added the PR label Apr 14, 2014
PierreBdR pushed a commit to PierreBdR/statsmodels that referenced this pull request Sep 2, 2014
ENH: Add naive seasonal decomposition function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants