Reputation: 1008
I want to decompose a time series only in trend and residual (without seasonality). So far, I know I can use statsmodels to decompose a time series, but this includes a seasonality. Is there a way to decompose it without the seasonality?
I have looked at the documentation (https://www.statsmodels.org/dev/generated/statsmodels.tsa.seasonal.seasonal_decompose.html) seasonal_decompose allows different types of seasonalities ("additive", "multiplicative"}) but I haven't seen a keyword argument that excludes seasonality.
Below a toy model of my problem. A time series with a trend but no seasonality. If we were to remove the seasonal component I think we would get a better fit.
import numpy as np
import pandas as pd
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARMA
from matplotlib import pylab as plt
#defining the trend function
def trend(t, amp=1):
return amp*(1 + t)
n_time_steps = 100
amplitud=1
#initializing the time series
time_series = np.zeros(n_time_steps)
time_series[0] = trend(0, amplitud)
alpha = 0.1
#making the time series
for t in range(1,n_time_steps):
time_series[t] = (1 - alpha)*time_series[t - 1] + alpha*trend(t, amp=amplitud) + alpha*np.random.normal(0,25)
#passing the time series to a pandas format
dates = sm.tsa.datetools.dates_from_range('2000m1', length=len(time_series))
time_series_pd= pd.Series(time_series, index=dates)
#decomposing the time series
res = sm.tsa.seasonal_decompose(time_series_pd)
res.plot()
Upvotes: 3
Views: 4747
Reputation: 115
I think the function seasonal_decompose
can not be used without the Seasonal component.
Have you thought of using another function like statsmodels.tsa.tsatools.detrend
? This does what you want with a polynomial fit.
Upvotes: 3