14

Have been working with time series in Python, and using sm.tsa.seasonal_decompose. In the docs they introduce the function like this:

We added a naive seasonal decomposition tool in the same vein as R’s decompose.

Here is a copy of the code from the docs and its output:

import statsmodels.api as sm

dta = sm.datasets.co2.load_pandas().data
# deal with missing values. see issue
dta.co2.interpolate(inplace=True)

res = sm.tsa.seasonal_decompose(dta.co2)
res.plot()

seasonal decomposition plot

They say it is naive but there is no disclaimer about what is wrong with it. Does anyone know?

3
  • There is nothing wrong with it. I guess it's "naive" because it uses simple convolution and averages to separate out trend and seasonal structure, i.e. it's not a fancy algorithm.
    – Josef
    Commented Nov 2, 2017 at 14:49
  • Thanks, @user333700 , I wonder if R's decompose is also "naive" and if and when it is ever necessary to go fancy. Get the impression given that .seasonal_decompose worked on my data that it's probably all you typically need.
    – cardamom
    Commented Nov 2, 2017 at 15:10
  • 1
    One possibility where it is most likely too "naive", i.e. doesn't capture the features of the data appropriately, is shifting seasonal patterns, for example because of a trend or break in the pattern. Sometimes those can also be removed by a box-cox or similar transformation. Seasonal adjustments for macro economic data (e.g. by X11) also take shifts in holidays and similar calendar effects into account.
    – Josef
    Commented Nov 2, 2017 at 16:02

2 Answers 2

23

I made some (aehm...naive) researches, and, according to the reference, it seems that StatsModels uses the classic moving average method to detect trends and apply seasonal decomposition (you can check more here, specifically about Moving Average and Classical Decomposition).

However, other advanced seasonal decomposition techniques are available, such as STL decomposition, which also has some Python implementations. (UPDATE - 11/04/2019 as pointed out in the comments by @squarespiral, such implementations appear to have been merged in the master branch of StatsModels).

At the above links, you can find a complete reference on the advantages and disadvantages of each one of the proposed methods.

Hope it helps!

3
0

According to the source codes, statsmodels.tsa.seasonal() de-constructs a time series by:

  1. Trend is computed by a convolutional window, decided by the argument period=12, for example;
  2. The detrended time-series is obtained by either Raw-Trend or Raw/Trend, controlled by the argument model;
  3. The seasonal is finally computed by averaging all temporal segments within a single period (e.g., 12 here);
  4. Finally, the residual is the noise left.

I would say, it is somehow conventional and like a feature-engineering work for machine learning tasks. Not sure whether it will improve the performance for some advanced deep learning architectures (e.g. Transformer).

Not the answer you're looking for? Browse other questions tagged or ask your own question.