Skip to content

ENH: 2-D tsa.seasonal_decompose with trend extrapolation - #3031

Merged
josef-pkt merged 2 commits into
statsmodels:masterfrom
kernc:seasonal_decompose-trend-interp
Oct 27, 2017
Merged

josef-pkt merged 2 commits into
statsmodels:masterfrom
kernc:seasonal_decompose-trend-interp

Conversation

@kernc

@kernc kernc commented Jun 17, 2016 •

Copy link
Copy Markdown
Contributor

fixes #2820
closes #3684

Includes patch from #3684, with tests.

@josef-pkt

Copy link
Copy Markdown
Member

Because I cannot block a gravatar, I blocked the user from the statsmodels organization

@josef-pkt

Copy link
Copy Markdown
Member

Thanks for the change in gravatar.

I'm currently busy with other things but try to look at it soon.

Comment thread statsmodels/tsa/seasonal.py Outdated
def _interpolate_trend(trend, npoints):
"""
Replace nan values on trend's end-opoints with least-squares interpolated
values wiith regression considering npoints closest defined points.

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.

two typos end-opoints and wiith

@josef-pkt

Copy link
Copy Markdown
Member

Thanks, looks interesting and useful.

I will have to look more closely at the details.
One issue: the plan is to vectorize the function, see #2820. There shouldn't be any problems vectorizing the linear trend estimation, but that needs verification.

(and this makes me think that we could use other methods for trend estimation in this case,
local linear regression is also a convolution filter with equal spaced time points.
signal.convolution doesn't seem to have any options on how to treat boundary points.
)

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 89.263% when pulling 2e09718 on kernc:seasonal_decompose-trend-interp into 9872ada on statsmodels:master.

@kernc

kernc commented Oct 7, 2016

Copy link
Copy Markdown
Contributor Author

Sorry for having it take so long. This has been updated to work with 2d data. I used and slightly amended your commit from #2820 (comment).
One of the tests seems to be failing, but I'm not sure whether it's my fault.

@josef-pkt

Copy link
Copy Markdown
Member

@kernc The test failure is not related to this PR.
I have seen it yesterday in another PR for the first time. Something must have changed on that version very recently.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 87.98% when pulling 30a6ab8 on kernc:seasonal_decompose-trend-interp into 7c723cf on statsmodels:master.

@coveralls

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 88.034% when pulling 0b4269e on kernc:seasonal_decompose-trend-interp into 22f2766 on statsmodels:master.

@coveralls

coveralls commented Feb 16, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.007%) to 90.304% when pulling f216498 on kernc:seasonal_decompose-trend-interp into e757e8b on statsmodels:master.

@kernc

kernc commented Feb 16, 2017

Copy link
Copy Markdown
Contributor Author

Now that 0.8.0 is released, would you care to have another look at this? It includes your 2D patch from #2820.

@coveralls

coveralls commented Oct 18, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.01%) to 81.784% when pulling 4b525d2 on kernc:seasonal_decompose-trend-interp into 49cdbbd on statsmodels:master.

@codecov-io

codecov-io commented Oct 18, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #3031 into master will increase coverage by 0.96%.
The diff coverage is 93.75%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3031      +/-   ##
==========================================
+ Coverage   79.31%   80.28%   +0.96%     
==========================================
  Files         544      544              
  Lines       81651    86828    +5177     
  Branches     9248    10795    +1547     
==========================================
+ Hits        64760    69707    +4947     
- Misses      14799    14949     +150     
- Partials     2092     2172      +80
Impacted Files Coverage Δ
statsmodels/tsa/tests/test_seasonal.py 100% <100%> (ø) ⬆️
statsmodels/tsa/seasonal.py 66.27% <88.23%> (+2.76%) ⬆️
statsmodels/stats/_lilliefors.py 82.11% <0%> (-1.04%) ⬇️
statsmodels/graphics/functional.py 76.97% <0%> (-0.45%) ⬇️
statsmodels/stats/libqsturng/make_tbls.py 0% <0%> (ø) ⬆️
statsmodels/iolib/tests/test_summary2.py 100% <0%> (ø) ⬆️
statsmodels/iolib/summary.py 54.54% <0%> (ø) ⬆️
statsmodels/sandbox/regression/treewalkerclass.py 0% <0%> (ø) ⬆️
statsmodels/graphics/tests/test_tsaplots.py 98.88% <0%> (+0.09%) ⬆️
statsmodels/discrete/tests/test_discrete.py 98.6% <0%> (+0.21%) ⬆️
... and 30 more

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 f11930d...b3c3a76. Read the comment docs.

@josef-pkt josef-pkt added this to the 0.9 milestone Oct 18, 2017
@josef-pkt

Copy link
Copy Markdown
Member

The trend extrapolation affects the estimate of the seasonal component because the detrended series is now not truncated anymore.
I think that's fine, it might create some distortion in small samples if the trend is not well estimated. (e.g. I ended up with some noisy, wiggly trend estimates where the extrapolation is sensitive to the neighboring fluctuation and transmits it to the seasonal estimate)
No problem in larger samples (I tried nobs=60) with smooth trend and extrapolation based on several, e.g. 5, observations.

One question is whether there should be an automatic bandwidth choice (number of observations) in the extrapolation trend regression, e.g. analogous to the default filt window length which is equal to freq.

@josef-pkt

Copy link
Copy Markdown
Member

just realized 'interpolation' is a misnomer in this case, should be 'extrapolation'
(what's the keyword that scipy uses?)

@josef-pkt

Copy link
Copy Markdown
Member

allowing trend_interp='freq' would allow a sensible default window, where the user doesn't need to specify the season length, if it is inferred from the date index

@josef-pkt

Copy link
Copy Markdown
Member

a nice example

# 2d case
np.random.seed(987125)
nobs=60
cl = 5
x = 0.05 * np.arange(nobs)[:,None] + np.tile(np.arange(cl), (2, nobs//cl)).T  
x = x + 0.5 * np.random.randn(*x.shape)
x[:,0] += 2
decomp = seasonal_decompose(x, freq=cl, trend_interp=cl)
trend = decomp.trend
#assert_almost_equal(trend, x)
decomp.plot()

decomp2 = seasonal_decompose(x, freq=cl, trend_interp=0)
decomp2.plot()

trend extrapolation also looks good with nobs=10 and nobs=15
at nobs=10, only two cycles, the seasonal component is not well estimated, but looks already pretty good with nobs=15

@josef-pkt

Copy link
Copy Markdown
Member

my suggestion: change name of keyword and allow freq as length
then it looks ready to merge, and works well

@coveralls

coveralls commented Oct 27, 2017 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 81.883% when pulling b3c3a76 on kernc:seasonal_decompose-trend-interp into f11930d on statsmodels:master.

@kernc

kernc commented Oct 27, 2017

Copy link
Copy Markdown
Contributor Author

All mentions of interpolation replaced with extrapolation. I knew that. 😊

A sensible inferred default sounds very reasonable. Added extrapolate_trend='freq' which takes the value of freq. Is that what you had in mind?

@josef-pkt

Copy link
Copy Markdown
Member

Yes, That looks good, and is ready for merge

Can you rebase? Not strictly necessary but makes a nicer history.

@kernc

kernc commented Oct 27, 2017

Copy link
Copy Markdown
Contributor Author

Already did. The branch splits from f11930d.

@josef-pkt
josef-pkt merged commit cc93c9e into statsmodels:master Oct 27, 2017
@josef-pkt

Copy link
Copy Markdown
Member

merged

@kernc Thanks for the improvements and I'm sorry for the long delay.

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.

ENH: seasonal_decompose for 2-D

4 participants