我一直在刷新我的时间序列技能,但我在创建傅立叶级数时遇到了麻烦。以下是数据(如果你把所有数据都放在一起运行,它会给予你同样的图和最终表格):
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.deterministic import CalendarFourier, DeterministicProcess
from sklearn.linear_model import LinearRegression
df = pd.DataFrame({'Pax': {Period('1949-01', 'M'): 112, Period('1949-02', 'M'): 118, Period('1949-03', 'M'): 132, Period('1949-04', 'M'): 129, Period('1949-05', 'M'): 121, Period('1949-06', 'M'): 135, Period('1949-07', 'M'): 148, Period('1949-08', 'M'): 148, Period('1949-09', 'M'): 136, Period('1949-10', 'M'): 119, Period('1949-11', 'M'): 104, Period('1949-12', 'M'): 118, Period('1950-01', 'M'): 115, Period('1950-02', 'M'): 126, Period('1950-03', 'M'): 141, Period('1950-04', 'M'): 135, Period('1950-05', 'M'): 125, Period('1950-06', 'M'): 149, Period('1950-07', 'M'): 170, Period('1950-08', 'M'): 170, Period('1950-09', 'M'): 158, Period('1950-10', 'M'): 133, Period('1950-11', 'M'): 114, Period('1950-12', 'M'): 140, Period('1951-01', 'M'): 145, Period('1951-02', 'M'): 150, Period('1951-03', 'M'): 178, Period('1951-04', 'M'): 163, Period('1951-05', 'M'): 172, Period('1951-06', 'M'): 178, Period('1951-07', 'M'): 199, Period('1951-08', 'M'): 199, Period('1951-09', 'M'): 184, Period('1951-10', 'M'): 162, Period('1951-11', 'M'): 146, Period('1951-12', 'M'): 166, Period('1952-01', 'M'): 171, Period('1952-02', 'M'): 180, Period('1952-03', 'M'): 193, Period('1952-04', 'M'): 181, Period('1952-05', 'M'): 183, Period('1952-06', 'M'): 218, Period('1952-07', 'M'): 230, Period('1952-08', 'M'): 242, Period('1952-09', 'M'): 209, Period('1952-10', 'M'): 191, Period('1952-11', 'M'): 172, Period('1952-12', 'M'): 194, Period('1953-01', 'M'): 196, Period('1953-02', 'M'): 196, Period('1953-03', 'M'): 236, Period('1953-04', 'M'): 235, Period('1953-05', 'M'): 229, Period('1953-06', 'M'): 243, Period('1953-07', 'M'): 264, Period('1953-08', 'M'): 272, Period('1953-09', 'M'): 237, Period('1953-10', 'M'): 211, Period('1953-11', 'M'): 180, Period('1953-12', 'M'): 201, Period('1954-01', 'M'): 204, Period('1954-02', 'M'): 188, Period('1954-03', 'M'): 235, Period('1954-04', 'M'): 227, Period('1954-05', 'M'): 234, Period('1954-06', 'M'): 264, Period('1954-07', 'M'): 302, Period('1954-08', 'M'): 293, Period('1954-09', 'M'): 259, Period('1954-10', 'M'): 229, Period('1954-11', 'M'): 203, Period('1954-12', 'M'): 229, Period('1955-01', 'M'): 242, Period('1955-02', 'M'): 233, Period('1955-03', 'M'): 267, Period('1955-04', 'M'): 269, Period('1955-05', 'M'): 270, Period('1955-06', 'M'): 315, Period('1955-07', 'M'): 364, Period('1955-08', 'M'): 347, Period('1955-09', 'M'): 312, Period('1955-10', 'M'): 274, Period('1955-11', 'M'): 237, Period('1955-12', 'M'): 278, Period('1956-01', 'M'): 284, Period('1956-02', 'M'): 277, Period('1956-03', 'M'): 317, Period('1956-04', 'M'): 313, Period('1956-05', 'M'): 318, Period('1956-06', 'M'): 374, Period('1956-07', 'M'): 413, Period('1956-08', 'M'): 405, Period('1956-09', 'M'): 355, Period('1956-10', 'M'): 306, Period('1956-11', 'M'): 271, Period('1956-12', 'M'): 306, Period('1957-01', 'M'): 315, Period('1957-02', 'M'): 301, Period('1957-03', 'M'): 356, Period('1957-04', 'M'): 348, Period('1957-05', 'M'): 355, Period('1957-06', 'M'): 422, Period('1957-07', 'M'): 465, Period('1957-08', 'M'): 467, Period('1957-09', 'M'): 404, Period('1957-10', 'M'): 347, Period('1957-11', 'M'): 305, Period('1957-12', 'M'): 336, Period('1958-01', 'M'): 340, Period('1958-02', 'M'): 318, Period('1958-03', 'M'): 362, Period('1958-04', 'M'): 348, Period('1958-05', 'M'): 363, Period('1958-06', 'M'): 435, Period('1958-07', 'M'): 491, Period('1958-08', 'M'): 505, Period('1958-09', 'M'): 404, Period('1958-10', 'M'): 359, Period('1958-11', 'M'): 310, Period('1958-12', 'M'): 337, Period('1959-01', 'M'): 360, Period('1959-02', 'M'): 342, Period('1959-03', 'M'): 406, Period('1959-04', 'M'): 396, Period('1959-05', 'M'): 420, Period('1959-06', 'M'): 472, Period('1959-07', 'M'): 548, Period('1959-08', 'M'): 559, Period('1959-09', 'M'): 463, Period('1959-10', 'M'): 407, Period('1959-11', 'M'): 362, Period('1959-12', 'M'): 405, Period('1960-01', 'M'): 417, Period('1960-02', 'M'): 391, Period('1960-03', 'M'): 419, Period('1960-04', 'M'): 461, Period('1960-05', 'M'): 472, Period('1960-06', 'M'): 535, Period('1960-07', 'M'): 622, Period('1960-08', 'M'): 606, Period('1960-09', 'M'): 508, Period('1960-10', 'M'): 461,Period('1960-11', 'M'): 390,Period('1960-12', 'M'): 432}})
df.head()
我创建了一个常量和一个趋势:
dp = DeterministicProcess(
index=df.index,
constant=True,
order=1,
seasonal=False,
#additional_terms=[fourier],
drop=True,
)
X = dp.in_sample()
y = df.squeeze()
我用线性回归拟合,去趋势化时间序列,并绘制结果:
model_pax = LinearRegression().fit(X, y)
y_pred_pax = pd.Series(model_pax.predict(X), index=X.index)
y_detrended = y-y_pred_pax
fig, (ax1, ax2) = plt.subplots(2,1, sharex=True, figsize=(10, 4))
ax1 = y.plot(label='Pax', ax=ax1)
ax1 = y_pred_pax.plot(label='trend', ax=ax1)
ax1.legend()
ax2 = y_detrended.plot(label='Pax detrended', ax=ax2)
ax2.legend()
plt.show()
现在我想捕捉季节性,为此我需要做一个傅立叶序列,但是当我创建确定性过程并包括傅立叶序列时,傅立叶序列列没有出现。
fourier = CalendarFourier(freq="M", order=4)
dp = DeterministicProcess(
index=y_detrended.index,
constant=True,
order=0,
seasonal=False,
additional_terms=[fourier]
drop=True,
)
dp.in_sample().head()
只显示常量而没有傅立叶列。为什么?我已经在其他数据集上尝试过了,效果很好,我看不出有什么不同。我错过了什么?
2条答案
按热度按时间vwkv1x7d1#
我找到了解决办法。我只需要把
M
从至
Y
:我不明白它具体是怎么工作的,好像是
CalendarFourier()
函数在推导index参数的输入是否与我们给函数的频率一致,但我不能肯定,希望有人能找到更好的解释。zbq4xfa02#
来进一步解释你自己的答案。
freq=“M”表示生成月度傅立叶序列,这意味着该序列将每月重复一次。
freq=“Y”表示每年重复一次。因此,在这里,您显然希望使用每年重复一次。
日历傅立叶(频率=“M”,阶数=4)日历傅立叶(频率=“Y”,阶数=4)