时间序列深度学习:状态 LSTM 模型预测太阳黑子(中)

本文介绍了使用Keras构建状态LSTM模型预测太阳黑子的过程,详细展示了数据预处理、模型构建、训练和回测。在回测11个样本上,模型表现出色,平均RMSE为34.4,标准差为13.0。
摘要由CSDN通过智能技术生成

作者:徐瑞龙 整理分享量化投资与固定收益相关的文章

博客专栏: 

https://www.cnblogs.com/xuruilong100 



5 用 Keras 构建状态 LSTM 模型

首先,我们将在回测策略的某个样本上用 Keras 开发一个状态 LSTM 模型。然后,我们将模型套用到所有样本,以测试和验证模型性能。

5.1 单个 LSTM 模型

对单个 LSTM 模型,我们选择并可视化最近一期的分割样本(Slice11),这一样本包含了最新的数据。

split    <- rolling_origin_resamples$splits[[11]] split_id <- rolling_origin_resamples$id[[11]]

5.1.1 可视化该分割样本

我么可以用 plot_split() 函数可视化该分割,设定 expand_y_axis = FALSE 以便将横坐标缩放到样本本身的范围。

plot_split(    split,
   expand_y_axis = FALSE,    
   size = 0.5) +    
   theme(legend.position = "bottom") +    
   ggtitle(glue("Split: {split_id}"))

640?wx_fmt=png

5.1.2 数据准备

首先,我们将训练和测试数据集合成一个数据集,并使用列 key 来标记它们来自哪个集合(training 或 testing)。请注意,tbl_time 对象需要在调用 bind_rows() 时重新指定索引,但是这个问题应该很快在 dplyr 包中得到纠正。

df_trn <- training(split) df_tst <- testing(split) df <- bind_rows(    df_trn %>% add_column(key = "training"),    df_tst %>% add_column(key = "testing")) %>%
    as_tbl_time(index = index) df


## # A time tibble: 720 x 3
## # Index: index
##    index      value key    
##    <date>     <dbl> <chr>  
##  1 1949-11-01 144.  training
##  2 1949-12-01 118.  training
##  3 1950-01-01 102.  training
##  4 1950-02-01  94.8 training
##  5 1950-03-01 110.  training
##  6 1950-04-01 113.  training
##  7 1950-05-01 106.  training
##  8 1950-06-01  83.6 training
##  9 1950-07-01  91.0 training
## 10 1950-08-01  85.2 training
## # ... with 710 more rows
5.1.3 用 recipe 做数据预处理

LSTM 算法要求输入数据经过中心化并标度化。我们可以使用 recipe 包预处理数据。我们用 step_sqrt 来转换数据以减少异常值的影响,再结合 step_center 和 step_scale 对数据进行中心化和标度化。最后,数据使用 bake() 函数实现处理转换。

rec_obj <- recipe(value ~ ., df) %>%
   step_sqrt(value) %>%    
   step_center(value) %>%    
   step_scale(value) %>%    
   prep() df_processed_tbl <- bake(rec_obj, df) df_processed_tbl


## # A tibble: 720 x 3
##    index      value key    
##    <date>     <dbl> <fct>  
##  1 1949-11-01 1.25  training
##  2 1949-12-01 0.929 training
##  3 1950-01-01 0.714 training
##  4 1950-02-01 0.617 training
##  5 1950-03-01 0.825 training
##  6 1950-04-01 0.874 training
##  7 1950-05-01 0.777 training
##  8 1950-06-01 0.450 training
##  9 1950-07-01 0.561 training
## 10 1950-08-01 0.474 training
## # ... with 710 more rows

接着,记录中心化和标度化的信息,以便在建模完成之后可以将数据逆向转换回去。平方根转换可以通过乘方运算逆转回去,但要在逆转中心化和标度化之后。

center_history <- rec_obj$steps[[2]]$means["value"] scale_history  <- rec_obj$steps[[3]]$sds["value"]

c("center" = center_history, "scale" = scale_history)


## center.value  scale.value
##     7.549526     3.545561
5.1.4 规划 LSTM 模型

我们需要规划下如何构建 LSTM 模型。首先,了解几个 LSTM 模型的专业术语

张量格式(Tensor Format)

  • 预测变量(X)必须是一个 3 维数组,维度分别是:samplestimesteps 和 features。第一维代表变量的长度;第二维是时间步(滞后阶数);第三维是预测变量的个数(1 表示单变量,n 表示多变量)

  • 输出或目标变量(y)必须是一个 2 维数组,维度分别是:samples 和 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值