我想预测时间序列数据.我在之前的帖子中读到模块statsmodels具有使用ARMA方法进行预测所需的工具,这正是我一直在寻找的.尽管如此,我在预测数据方面遇到了麻烦.有人可以解释模型中使用的各种参数和/或提供示例吗?
dplyr中的do-function让你可以快速轻松地制作很多很酷的模型,但我很难将这些模型用于良好的滚动预测.
# Data illustration
require(dplyr)
require(forecast)
df <- data.frame(
Date = seq.POSIXt(from = as.POSIXct("2015-01-01 00:00:00"),
to = as.POSIXct("2015-06-30 00:00:00"), by = "hour"))
df <- df %>% mutate(Hour = as.numeric(format(Date, "%H")) + 1,
Wind = runif(4320, min = 1, max = 5000),
Temp = runif(4320, min = - 20, max = 25),
Price = runif(4320, min = -15, max = 45)
)
Run Code Online (Sandbox Code Playgroud)
我的因子变量是Hour,我的外生变量是Wind和temp,我想要预测的是Price.所以,基本上,我有24个模型,我希望能够进行滚动预测.
现在,我的数据框包含180天.我想回到100天,做一天滚动预测,然后能够将其与实际进行比较Price.
这种暴力行为看起来像这样:
# First I fit …Run Code Online (Sandbox Code Playgroud) 我正在使用Facebook发布的名为Prophet的新软件包.它做时间序列预测,我想通过组应用此功能.
向下滚动到R部分.
https://facebookincubator.github.io/prophet/docs/quick_start.html
这是我的尝试:
grouped_output = df %>% group_by(group) %>%
do(m = prophet(df[,c(1,3)])) %>%
do(future = make_future_dataframe(m, period = 7)) %>%
do(forecast = prophet:::predict.prophet(m, future))
grouped_output[[1]]
Run Code Online (Sandbox Code Playgroud)
然后,我需要从我遇到的每个组的列表中提取结果.
以下是没有组的原始数据框:
ds <- as.Date(c('2016-11-01','2016-11-02','2016-11-03','2016-11-04',
'2016-11-05','2016-11-06','2016-11-07','2016-11-08',
'2016-11-09','2016-11-10','2016-11-11','2016-11-12',
'2016-11-13','2016-11-14','2016-11-15','2016-11-16',
'2016-11-17','2016-11-18','2016-11-19','2016-11-20',
'2016-11-21','2016-11-22','2016-11-23','2016-11-24',
'2016-11-25','2016-11-26','2016-11-27','2016-11-28',
'2016-11-29','2016-11-30'))
y <- c(15,17,18,19,20,54,67,23,12,34,12,78,34,12,3,45,67,89,12,111,123,112,14,566,345,123,567,56,87,90)
y<-as.numeric(y)
df <- data.frame(ds, y)
df
ds y
1 2016-11-01 15
2 2016-11-02 17
3 2016-11-03 18
4 2016-11-04 19
5 2016-11-05 20
6 2016-11-06 54
7 2016-11-07 67
8 2016-11-08 23
9 2016-11-09 12
10 2016-11-10 34
11 …Run Code Online (Sandbox Code Playgroud) 我在Keras使用LSTM实现了预测模型.数据集分离15分钟,我预测12个未来步骤.
该模型对该问题表现良好.但预测有一个小问题.它显示出一个小的移位效果.要获得更清晰的图片,请参见下图.
如何处理这个问题.如何转换数据来处理这类问题.
我使用的模型如下
init_lstm = RandomUniform(minval=-.05, maxval=.05)
init_dense_1 = RandomUniform(minval=-.03, maxval=.06)
model = Sequential()
model.add(LSTM(15, input_shape=(X.shape[1], X.shape[2]), kernel_initializer=init_lstm, recurrent_dropout=0.33))
model.add(Dense(1, kernel_initializer=init_dense_1, activation='linear'))
model.compile(loss='mae', optimizer=Adam(lr=1e-4))
history = model.fit(X, y, epochs=1000, batch_size=16, validation_data=(X_valid, y_valid), verbose=1, shuffle=False)
Run Code Online (Sandbox Code Playgroud)
我做了这样的预测
my_forecasts = model.predict(X_valid, batch_size=16)
Run Code Online (Sandbox Code Playgroud)
时间序列数据被转换为监督以使用该函数来馈送LSTM
# convert time series into supervised learning problem
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
# input sequence (t-n, ... t-1)
for i in …Run Code Online (Sandbox Code Playgroud) 我有一个数据系列,有季节性组件,趋势和arma部分.我想根据历史来预测这个系列.
我可以使用这个程序
data_ts <- ts(data, frequency = 24)
data_deseason <- stl(data_ts, t.window=50, s.window='periodic', robust=TRUE)
f <- forecast(data_deseason, method='arima', h = N)
Run Code Online (Sandbox Code Playgroud)
但是在这样做的时候,我无法选择Arima部件的参数,我想这样做.以上似乎是使用像auto.arima这样的东西,因为我自己选择arima参数 - 但它运行速度非常快,比auto.arima快得多 - 所以不确定会发生什么.
或者,我可以使用上面的内容将数据分成趋势和剩余部分.但那我该怎么预测呢?我应该为趋势和其余部分制作一个arma模型吗?
trend_arima <- Arima(data_deseason$time.series[,'trend'], order = c(1,1,1))
remainder_arima <- Arima(data_deseason$time.series[,'remainder'], order = c(1,1,1))
Run Code Online (Sandbox Code Playgroud)
然后使用forecast()并添加上面两个组件和季节.或者有没有办法提取stl找到的趋势模型?
感谢任何提示:)本杰明
我正在使用statsmodels.tsa.SARIMAX()来训练具有外生变量的模型.当使用外生变量训练模型时,是否存在等效的get_prediction(),以便返回的对象包含预测的平均值和置信区间而不仅仅是一组预测的平均值结果?predict()和forecast()方法采用外生变量,但只返回预测的平均值.
SARIMA_model = sm.tsa.SARIMAX(endog=y_train.astype('float64'),
exog=ExogenousFeature_train.values.astype('float64'),
order=(1,0,0),
seasonal_order=(2,1,0,7),
simple_differencing=False)
model_results = SARIMA_model.fit()
pred = model_results.predict(start=train_end_date,
end=test_end_date,
exog=ExogenousFeature_test.values.astype('float64').reshape(343,1),
dynamic=False)
Run Code Online (Sandbox Code Playgroud)
这里的pred是一个预测值数组,而不是一个包含预测平均值和置信区间的对象,如果你运行get_predict(),你会得到它们.注意,get_predict()不接受外生变量.
我的statsmodels版本是0.8
python time-series confidence-interval forecasting statsmodels
我正在尝试使用tf.contrib.seq2seq模块对某些数据进行预测(只是float32向量),但我使用TensorFlow中的seq2seq模块找到的所有示例都用于转换和嵌入.
我很难理解tf.contrib.seq2seq.Helper究竟为Seq2Seq架构做了什么,以及如何在我的案例中使用CustomHelper.
这就是我现在所做的:
import tensorflow as tf
from tensorflow.python.layers import core as layers_core
input_seq_len = 15 # Sequence length as input
input_dim = 1 # Nb of features in input
output_seq_len = forecast_len = 20 # horizon length for forecasting
output_dim = 1 # nb of features to forecast
encoder_units = 200 # nb of units in each cell for the encoder
decoder_units = 200 # nb of units in each cell for the decoder
attention_units = 100
batch_size = …Run Code Online (Sandbox Code Playgroud) 我想编写一个将时间序列和标准偏差作为参数并返回调整后的时间序列的函数,该时间序列看起来像是预测。
使用此功能,我想测试系统的稳定性,该系统将获取天气预报的时间序列表作为输入参数。
我对这种功能的使用方法,如下所述:
vector<tuple<datetime, double>> get_adjusted_timeseries(vector<tuple<datetime, double>>& timeseries_original, const double stddev, const double dist_mid)
{
auto timeseries_copy(timeseries_original);
int sign = randInRange(0, 1) == 0 ? 1 : -1;
auto left_limit = normal_cdf_inverse(0.5 - dist_mid, 0, stddev);
auto right_limit = normal_cdf_inverse(0.5 + dist_mid, 0, stddev);
for (auto& pair : timeseries_copy)
{
double number;
do
{
nd_value = normal_distribution_r(0, stddev);
}
while (sign == -1 && nd_value > 0.0 || sign == 1 && nd_value < 0.0);
pair = make_tuple(get<0>(pair), get<1>(pair) + …Run Code Online (Sandbox Code Playgroud) c++ normal-distribution prediction forecasting standard-deviation
我尝试使用holt-winters model如下所示的预测,但我不断得到与我期望的预测不一致的预测.我还展示了情节的可视化
Train = Airline[:130]
Test = Airline[129:]
from statsmodels.tsa.holtwinters import Holt
y_hat_avg = Test.copy()
fit1 = Holt(np.asarray(Train['Passengers'])).fit()
y_hat_avg['Holt_Winter'] = fit1.predict(start=1,end=15)
plt.figure(figsize=(16,8))
plt.plot(Train.index, Train['Passengers'], label='Train')
plt.plot(Test.index,Test['Passengers'], label='Test')
plt.plot(y_hat_avg.index,y_hat_avg['Holt_Winter'], label='Holt_Winter')
plt.legend(loc='best')
plt.savefig('Holt_Winters.jpg')
Run Code Online (Sandbox Code Playgroud)
我不确定我在这里缺少什么.

预测似乎适用于训练数据的早期部分
forecasting ×10
time-series ×5
python ×4
r ×3
statsmodels ×3
dplyr ×2
apply ×1
c++ ×1
holtwinters ×1
keras ×1
prediction ×1
statistics ×1
stl ×1
tensorflow ×1