Reputation:
let us suppose we have following code :
import yfinance as yf
import matplotlib.pyplot as plt
data = yf.download(tickers="FB", start="2016-1-1",end="2022-05-15",progress=False)
data.drop(["Open","Low","Close","Adj Close","Volume"],axis=1,inplace=True)
data.sort_index(inplace=True)
print(data.head())
from statsmodels.tsa.seasonal import seasonal_decompose
result =seasonal_decompose(data["High"],model='multiplicative',freq=36)
estimated_trend_add = result.trend
estimated_seasonal_add = result.seasonal
estimated_residual_add = result.resid
fig, axes = plt.subplots(4, 1)
fig.set_figheight(10)
fig.set_figwidth(15)
axes[0].plot(data['High'], label='Original')
axes[0].legend(loc='upper left')
axes[1].plot(estimated_trend_add, label='Trend')
axes[1].legend(loc='upper left')
axes[2].plot(estimated_seasonal_add, label='Cyclic')
axes[2].legend(loc='upper left')
axes[3].plot(estimated_residual_add, label='Residuals')
axes[3].legend(loc='upper left')
plt.show()
result of dataframe is given below :
High
Date
2015-12-31 106.169998
2016-01-04 102.239998
2016-01-05 103.709999
2016-01-06 103.769997
2016-01-07 101.430000
also it returns following error :
Traceback (most recent call last):
File "C:\Users\User\PycharmProjects\DataScience\facebook_real_prices.py", line 8, in <module>
result =seasonal_decompose(data["High"],model='multiplicative',freq=36)
TypeError: seasonal_decompose() got an unexpected keyword argument 'freq'
instead i know that there is keyword period, are they same?generally we know that period is 1/frequency, but in this case how should i define period? or how can i create frequency index? please help me
Upvotes: 0
Views: 1603
Reputation: 248
The period argument in seasonal_decompose is periodicity i.e after how long things start to repeat.
For example, time series data with a frequency of 1 hour would have a period of 24. If frequency was 3 hours, period would be 8 = (24 / 3).
In your case, it seems that the frequency is business days (Mon - Fri), which implies a period of 5.
Yeah, it seems that both weekends and holidays have been removed. But the asfreq
method can be used to forwad-fill/back-fill blanks.
Specifying a period of 5 means that you expect values e.g on Mondays to be somewhat similar
If the input data is a pandas.Series
with a datetime index, you might not need to specify a period:
data = data.asfreq(pd.offsets.BDay(), method="pad")
But be warned, the statsmodels.tsa.tsatools.freq_to_period
function used internally in seasonal_decompose
ignores the scale of frequency i.e. 3H and H are both treated as H. You can check this with:
from statsmodels.tsa.tsatools import freq_to_period
assert freq_to_period("H") == freq_to_period("3H") == 24
I would recommend specifying the period
argument directly in seasonal_decompose
(5 in this case).
Upvotes: 1