如何从TBAT的预测中提取趋势成分(在Python中)?

发布于 2025-01-24 09:36:55 字数 1733 浏览 5 评论 0原文

我正在尝试使用TBAT来预测一个非常简单的时代系列(如所建议的在这里)。这是我正在使用的代码:

#imports
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from tbats import BATS, TBATS

#build a time series
t_max = 500
np.random.seed(2342)
t = np.array(range(0, t_max))

y = 5 * np.sin(t * 2 * np.pi / 7) + \
    2 * np.cos(t * 2 * np.pi / 30.5) + \
    ((t / 20) ** 1.5 + np.random.normal(size = t_max) * t / 50) + 10

# Create a train-test set. The forecast begins at t_fcst + 1
t_fcst = int(t_max * .75)

# this is how long we want to forecast in the future
forecast_period = t_max - t_fcst

train = y[:t_fcst]
t_train = t[:t_fcst]

test  = y[-forecast_period:]
t_test = t[-forecast_period:]

# Create estimator
estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = True)

# Fit model
fitted_model = estimator.fit(train)

y = fitted_model.forecast(steps = forecast_period)

fig, ax = plt.subplots(figsize = (14, 8))

ax.plot(t_train, train, label = 'Train')
ax.plot(t_test,  test, label = 'Test')
ax.plot(t_test,  y, label = 'Forecast')

ax.legend(loc="upper left")

这就是我得到的:

在这里输入图像描述

如果代替:

estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = True)

我使用:

estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = False)

这是我得到的:

在这里输入图像描述

知道我在做什么错吗?谢谢

I'm trying to use TBATS to forecast a very simple times series (as suggested here).This is the code I'm using:

#imports
import pandas as pd
import numpy as np
import matplotlib.pylab as plt
from tbats import BATS, TBATS

#build a time series
t_max = 500
np.random.seed(2342)
t = np.array(range(0, t_max))

y = 5 * np.sin(t * 2 * np.pi / 7) + \
    2 * np.cos(t * 2 * np.pi / 30.5) + \
    ((t / 20) ** 1.5 + np.random.normal(size = t_max) * t / 50) + 10

# Create a train-test set. The forecast begins at t_fcst + 1
t_fcst = int(t_max * .75)

# this is how long we want to forecast in the future
forecast_period = t_max - t_fcst

train = y[:t_fcst]
t_train = t[:t_fcst]

test  = y[-forecast_period:]
t_test = t[-forecast_period:]

# Create estimator
estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = True)

# Fit model
fitted_model = estimator.fit(train)

y = fitted_model.forecast(steps = forecast_period)

fig, ax = plt.subplots(figsize = (14, 8))

ax.plot(t_train, train, label = 'Train')
ax.plot(t_test,  test, label = 'Test')
ax.plot(t_test,  y, label = 'Forecast')

ax.legend(loc="upper left")

And this is what I get:

enter image description here

If instead of:

estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = True)

I use:

estimator = TBATS(seasonal_periods=[7, 30.5], use_trend = False)

This is what I get:

enter image description here

Any idea what I'm doing wrong? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文