经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » R语言 » 查看文章
时间序列深度学习:状态 LSTM 模型预测太阳黑子
来源:cnblogs  作者:xuruilong100  时间:2018/9/25 20:21:41  对本文有异议

目录

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

本文翻译自《Time Series Deep Learning: Forecasting Sunspots With Keras Stateful Lstm In R》

原文链接

由于数据科学机器学习和深度学习的发展,时间序列预测在预测准确性方面取得了显着进展。随着这些 ML/DL 工具的发展,企业和金融机构现在可以通过应用这些新技术来解决旧问题,从而更好地进行预测。在本文中,我们展示了使用称为 LSTM(长短期记忆)的特殊类型深度学习模型,该模型对涉及自相关性的序列预测问题很有用。我们分析了一个名为“太阳黑子”的著名历史数据集(太阳黑子是指太阳表面形成黑点的太阳现象)。我们将展示如何使用 LSTM 模型预测未来 10 年的太阳黑子数量。

教程概览

此代码教程对应于 2018 年 4 月 19 日星期四向 SP Global 提供的 Time Series Deep Learning 演示文稿。可以下载补充本文的幻灯片。

这是一个涉及时间序列深度学习和其他复杂机器学习主题(如回测交叉验证)的高级教程。如果想要了解 R 中的 Keras,请查看:Customer Analytics: Using Deep Learning With Keras To Predict Customer Churn

本教程中,你将会学到:

  • keras 包开发一个状态 LSTM 模型,该 R 包将 R TensorFlow 作为后端。
  • 将状态 LSTM 模型应用到著名的太阳黑子数据集上。
  • 借助 rsample 包在初始抽样上滚动预测,实现时间序列的交叉检验
  • 借助 ggplot2cowplot 可视化回测和预测结果。
  • 通过自相关函数(Autocorrelation Function,ACF)图评估时间序列数据是否适合应用 LSTM 模型。

本文的最终结果是一个高性能的深度学习算法,在预测未来 10 年太阳黑子数量方面表现非常出色!这是回测后状态 LSTM 模型的结果。

商业应用

时间序列预测对营收和利润有显着影响。在商业方面,我们可能有兴趣预测每月、每季度或每年的哪一天会发生大额支出,或者我们可能有兴趣了解消费者物价指数(CPI)在未来六年个月如何变化。这些都是在微观经济和宏观经济层面影响商业组织的常见问题。虽然本教程中使用的数据集不是“商业”数据集,但它显示了工具-问题匹配的强大力量,意味着使用正确的工具进行工作可以大大提高准确性。最终的结果是预测准确性的提高将对营收和利润带来可量化的提升。

长短期记忆(LSTM)模型

长短期记忆(LSTM)模型是一种强大的递归神经网络(RNN)。博文《Understanding LSTM Networks》(翻译版)以简单易懂的方式解释了模型的复杂性机制。下面是描述 LSTM 内部单元架构的示意图,除短期状态之外,该架构使其能够保持长期状态,而这是传统的 RNN 处理起来有困难的:

来源:Understanding LSTM Networks

LSTM 模型在预测具有自相关性(时间序列和滞后项之间存在相关性)的时间序列时非常有用,因为模型能够保持状态并识别时间序列上的模式。在每次处理过程中,递归架构能使状态在更新权重时保持或者传递下去。此外,LSTM 模型的单元架构在短期持久化的基础上实现了长期持久化,进而强化了 RNN,这一点非常吸引人!

在 Keras 中,LSTM 模型可以有“状态”模式,Keras 文档中这样解释:

索引 i 处每个样本的最后状态将被用作下一次批处理中索引 i 处样本的初始状态

在正常(或“无状态”)模式下,Keras 对样本重新洗牌,时间序列与其滞后项之间的依赖关系丢失。但是,在“状态”模式下运行时,我们通常可以通过利用时间序列中存在的自相关性来获得高质量的预测结果

在完成本教程时,我们会进一步解释。就目前而言,可以认为 LSTM 模型对涉及自相关性的时间序列问题可能非常有用,而且 Keras 有能力创建完美的时间序列建模工具——状态 LSTM 模型。

太阳黑子数据集

太阳黑子是随 R 发布的著名数据集(参见 datasets 包)。数据集跟踪记录太阳黑子,即太阳表面出现黑点的事件。这是来自 NASA 的一张照片,显示了太阳黑子现象。相当酷!

来源:NASA

本教程所用的数据集称为 sunspots.month,包含了 265(1749 ~ 2013)年间每月太阳黑子数量的月度数据。

构建 LSTM 模型预测太阳黑子

让我们开动起来,预测太阳黑子。这是我们的目标:

目标:使用 LSTM 模型预测未来 10 年的太阳黑子数量。

1 若干相关包

以下是本教程所需的包,所有这些包都可以在 CRAN 上找到。如果你尚未安装这些包,可以使用 install.packages() 进行安装。注意:在继续使用此代码教程之前,请确保更新所有包,因为这些包的先前版本可能与所用代码不兼容。

  1. # Core Tidyverse
  2. library(tidyverse)
  3. library(glue)
  4. library(forcats)
  5. # Time Series
  6. library(timetk)
  7. library(tidyquant)
  8. library(tibbletime)
  9. # Visualization
  10. library(cowplot)
  11. # Preprocessing
  12. library(recipes)
  13. # Sampling / Accuracy
  14. library(rsample)
  15. library(yardstick)
  16. # Modeling
  17. library(keras)

如果你之前没有在 R 中运行过 Keras,你需要用 install_keras() 函数安装 Keras。

  1. # Install Keras if you have not installed before
  2. install_keras()

2 数据

数据集 sunspot.month 随 R 一起发布,可以轻易获得。它是一个 ts 类对象(非 tidy 类),所以我们将使用 timetk 中的 tk_tbl() 函数转换为 tidy 数据集。我们使用这个函数而不是来自 tibbleas.tibble(),用来自动将时间序列索引保存为zoo yearmon 索引。最后,我们将使用 lubridate::as_date()(使用 tidyquant 时加载)将 zoo 索引转换为日期,然后转换为 tbl_time 对象以使时间序列操作起来更容易。

  1. sun_spots <- datasets::sunspot.month %>%
  2. tk_tbl() %>%
  3. mutate(index = as_date(index)) %>%
  4. as_tbl_time(index = index)
  5. sun_spots
  1. ## # A time tibble: 3,177 x 2
  2. ## # Index: index
  3. ## index value
  4. ## <date> <dbl>
  5. ## 1 1749-01-01 58.0
  6. ## 2 1749-02-01 62.6
  7. ## 3 1749-03-01 70.0
  8. ## 4 1749-04-01 55.7
  9. ## 5 1749-05-01 85.0
  10. ## 6 1749-06-01 83.5
  11. ## 7 1749-07-01 94.8
  12. ## 8 1749-08-01 66.3
  13. ## 9 1749-09-01 75.9
  14. ## 10 1749-10-01 75.5
  15. ## # ... with 3,167 more rows

3 探索性数据分析

时间序列很长(有 265 年!)。我们可以将时间序列的全部(265 年)以及前 50 年的数据可视化,以获得该时间系列的直观感受。

3.1 使用 COWPLOT 可视化太阳黑子数据

我们将创建若干 ggplot 对象并借助 cowplot::plot_grid() 把这些对象组合起来。对于需要缩放的部分,我们使用 tibbletime::time_filter(),可以方便的实现基于时间的过滤。

  1. p1 <- sun_spots %>%
  2. ggplot(aes(index, value)) +
  3. geom_point(
  4. color = palette_light()[[1]], alpha = 0.5) +
  5. theme_tq() +
  6. labs(title = "From 1749 to 2013 (Full Data Set)")
  7. p2 <- sun_spots %>%
  8. filter_time("start" ~ "1800") %>%
  9. ggplot(aes(index, value)) +
  10. geom_line(color = palette_light()[[1]], alpha = 0.5) +
  11. geom_point(color = palette_light()[[1]]) +
  12. geom_smooth(method = "loess", span = 0.2, se = FALSE) +
  13. theme_tq() +
  14. labs(
  15. title = "1749 to 1800 (Zoomed In To Show Cycle)",
  16. caption = "datasets::sunspot.month")
  17. p_title <- ggdraw() +
  18. draw_label(
  19. "Sunspots",
  20. size = 18,
  21. fontface = "bold",
  22. colour = palette_light()[[1]])
  23. plot_grid(
  24. p_title, p1, p2,
  25. ncol = 1,
  26. rel_heights = c(0.1, 1, 1))

乍一看,这个时间序列应该很容易预测。但是,我们可以看到,周期(10 年)和振幅(太阳黑子的数量)似乎在 1780 年至 1800 年之间发生变化。这产生了一些挑战。

3.2 计算 ACF

接下来我们要做的是确定 LSTM 模型是否是一个适用的好方法。LSTM 模型利用自相关性产生序列预测。我们的目标是使用批量预测(一种在整个预测区域内创建单一预测批次的技术,不同于在未来一个或多个步骤中迭代执行的单一预测)产生未来 10 年的预测。批量预测只有在自相关性持续 10 年以上时才有效。下面,我们来检查一下。

首先,我们需要回顾自相关函数(Autocorrelation Function,ACF),它表示时间序列与自身滞后项之间的相关性。stats 包库中的 acf() 函数以曲线的形式返回每个滞后阶数的 ACF 值。但是,我们希望将 ACF 值提取出来以便研究。为此,我们将创建一个自定义函数 tidy_acf(),以 tidy tibble 的形式返回 ACF 值。

  1. tidy_acf <- function(data,
  2. value,
  3. lags = 0:20) {
  4. value_expr <- enquo(value)
  5. acf_values <- data %>%
  6. pull(value) %>%
  7. acf(lag.max = tail(lags, 1), plot = FALSE) %>%
  8. .$acf %>%
  9. .[,,1]
  10. ret <- tibble(acf = acf_values) %>%
  11. rowid_to_column(var = "lag") %>%
  12. mutate(lag = lag - 1) %>%
  13. filter(lag %in% lags)
  14. return(ret)
  15. }

接下来,让我们测试一下这个函数以确保它按预期工作。该函数使用我们的 tidy 时间序列,提取数值列,并以 tibble 的形式返回 ACF 值以及对应的滞后阶数。我们有 601 个自相关系数(一个对应时间序列自身,剩下的对应 600 个滞后阶数)。一切看起来不错。

  1. max_lag <- 12 * 50
  2. sun_spots %>%
  3. tidy_acf(value, lags = 0:max_lag)
  1. ## # A tibble: 601 x 2
  2. ## lag acf
  3. ## <dbl> <dbl>
  4. ## 1 0. 1.00
  5. ## 2 1. 0.923
  6. ## 3 2. 0.893
  7. ## 4 3. 0.878
  8. ## 5 4. 0.867
  9. ## 6 5. 0.853
  10. ## 7 6. 0.840
  11. ## 8 7. 0.822
  12. ## 9 8. 0.809
  13. ## 10 9. 0.799
  14. ## # ... with 591 more rows

下面借助 ggplot2 包把 ACF 数据可视化,以便确定 10 年后是否存在高自相关滞后项。

  1. sun_spots %>%
  2. tidy_acf(value, lags = 0:max_lag) %>%
  3. ggplot(aes(lag, acf)) +
  4. geom_segment(
  5. aes(xend = lag, yend = 0),
  6. color = palette_light()[[1]]) +
  7. geom_vline(
  8. xintercept = 120, size = 3,
  9. color = palette_light()[[2]]) +
  10. annotate(
  11. "text", label = "10 Year Mark",
  12. x = 130, y = 0.8,
  13. color = palette_light()[[2]],
  14. size = 6, hjust = 0) +
  15. theme_tq() +
  16. labs(title = "ACF: Sunspots")

好消息。自相关系数在 120 阶(10年标志)之后依然超过 0.5。理论上,我们可以使用高自相关滞后项来开发 LSTM 模型。

  1. sun_spots %>%
  2. tidy_acf(value, lags = 115:135) %>%
  3. ggplot(aes(lag, acf)) +
  4. geom_vline(
  5. xintercept = 120, size = 3,
  6. color = palette_light()[[2]]) +
  7. geom_segment(
  8. aes(xend = lag, yend = 0),
  9. color = palette_light()[[1]]) +
  10. geom_point(
  11. color = palette_light()[[1]],
  12. size = 2) +
  13. geom_label(
  14. aes(label = acf %>% round(2)),
  15. vjust = -1,
  16. color = palette_light()[[1]]) +
  17. annotate(
  18. "text", label = "10 Year Mark",
  19. x = 121, y = 0.8,
  20. color = palette_light()[[2]],
  21. size = 5, hjust = 0) +
  22. theme_tq() +
  23. labs(
  24. title = "ACF: Sunspots",
  25. subtitle = "Zoomed in on Lags 115 to 135")

经过检查,最优滞后阶数位于在 125 阶。这不一定是我们将使用的,因为我们要更多地考虑使用 Keras 实现的 LSTM 模型进行批量预测。有了这个观点,以下是如何 filter() 获得最优滞后阶数。

  1. optimal_lag_setting <- sun_spots %>%
  2. tidy_acf(value, lags = 115:135) %>%
  3. filter(acf == max(acf)) %>%
  4. pull(lag)
  5. optimal_lag_setting
  1. ## [1] 125

4 回测:时间序列交叉验证

交叉验证是在子样本数据上针对验证集数据开发模型的过程,其目标是确定预期的准确度级别和误差范围。在交叉验证方面,时间序列与非序列数据有点不同。具体而言,在制定抽样计划时,必须保留对以前时间样本的时间依赖性。我们可以通过平移窗口的方式选择连续子样本,进而创建交叉验证抽样计划。在金融领域,这种类型的分析通常被称为“回测”,它需要在一个时间序列上平移若干窗口来分割成多个不间断的序列,以在当前和过去的观测上测试策略。

最近的一个发展是 rsample,它使交叉验证抽样计划非常易于实施。此外,rsample 包还包含回测功能。“Time Series Analysis Example”描述了一个使用 rolling_origin() 函数为时间序列交叉验证创建样本的过程。我们将使用这种方法。

4.1 开发一个回测策略

我们创建的抽样计划使用 50 年(initial = 12 x 50)的数据作为训练集,10 年(assess = 12 x 10)的数据用于测试(验证)集。我们选择 20 年的跳跃跨度(skip = 12 x 20),将样本均匀分布到 11 组中,跨越整个 265 年的太阳黑子历史。最后,我们选择 cumulative = FALSE 来允许平移起始点,这确保了较近期数据上的模型相较那些不太新近的数据没有不公平的优势(使用更多的观测数据)。rolling_origin_resamples 是一个 tibble 型的返回值。

  1. periods_train <- 12 * 50
  2. periods_test <- 12 * 10
  3. skip_span <- 12 * 20
  4. rolling_origin_resamples <- rolling_origin(
  5. sun_spots,
  6. initial = periods_train,
  7. assess = periods_test,
  8. cumulative = FALSE,
  9. skip = skip_span)
  10. rolling_origin_resamples
  1. ## # Rolling origin forecast resampling
  2. ## # A tibble: 11 x 2
  3. ## splits id
  4. ## <list> <chr>
  5. ## 1 <S3: rsplit> Slice01
  6. ## 2 <S3: rsplit> Slice02
  7. ## 3 <S3: rsplit> Slice03
  8. ## 4 <S3: rsplit> Slice04
  9. ## 5 <S3: rsplit> Slice05
  10. ## 6 <S3: rsplit> Slice06
  11. ## 7 <S3: rsplit> Slice07
  12. ## 8 <S3: rsplit> Slice08
  13. ## 9 <S3: rsplit> Slice09
  14. ## 10 <S3: rsplit> Slice10
  15. ## 11 <S3: rsplit> Slice11

4.2 可视化回测策略

我们可以用两个自定义函数来可视化再抽样。首先是 plot_split(),使用 ggplot2 绘制一个再抽样分割图。请注意,expand_y_axis 参数默认将日期范围扩展成整个 sun_spots 数据集的日期范围。当我们将所有的图形同时可视化时,这将变得有用。

  1. # Plotting function for a single split
  2. plot_split <- function(split,
  3. expand_y_axis = TRUE,
  4. alpha = 1,
  5. size = 1,
  6. base_size = 14) {
  7. # Manipulate data
  8. train_tbl <- training(split) %>%
  9. add_column(key = "training")
  10. test_tbl <- testing(split) %>%
  11. add_column(key = "testing")
  12. data_manipulated <- bind_rows(
  13. train_tbl, test_tbl) %>%
  14. as_tbl_time(index = index) %>%
  15. mutate(
  16. key = fct_relevel(
  17. key, "training", "testing"))
  18. # Collect attributes
  19. train_time_summary <- train_tbl %>%
  20. tk_index() %>%
  21. tk_get_timeseries_summary()
  22. test_time_summary <- test_tbl %>%
  23. tk_index() %>%
  24. tk_get_timeseries_summary()
  25. # Visualize
  26. g <- data_manipulated %>%
  27. ggplot(
  28. aes(x = index,
  29. y = value,
  30. color = key)) +
  31. geom_line(size = size, alpha = alpha) +
  32. theme_tq(base_size = base_size) +
  33. scale_color_tq() +
  34. labs(
  35. title = glue("Split: {split$id}"),
  36. subtitle = glue(
  37. "{train_time_summary$start} to {test_time_summary$end}"),
  38. y = "", x = "") +
  39. theme(legend.position = "none")
  40. if (expand_y_axis) {
  41. sun_spots_time_summary <- sun_spots %>%
  42. tk_index() %>%
  43. tk_get_timeseries_summary()
  44. g <- g +
  45. scale_x_date(
  46. limits = c(
  47. sun_spots_time_summary$start,
  48. sun_spots_time_summary$end))
  49. }
  50. return(g)
  51. }

plot_split() 函数接受一个分割(在本例中为 Slice01),并可视化抽样策略。我们使用 expand_y_axis = TRUE 将横坐标范围扩展到整个数据集的日期范围。

  1. rolling_origin_resamples$splits[[1]] %>%
  2. plot_split(expand_y_axis = TRUE) +
  3. theme(legend.position = "bottom")

第二个函数是 plot_sampling_plan(),使用 purrrcowplotplot_split() 函数应用到所有样本上。

  1. # Plotting function that scales to all splits
  2. plot_sampling_plan <- function(sampling_tbl,
  3. expand_y_axis = TRUE,
  4. ncol = 3,
  5. alpha = 1,
  6. size = 1,
  7. base_size = 14,
  8. title = "Sampling Plan") {
  9. # Map plot_split() to sampling_tbl
  10. sampling_tbl_with_plots <- sampling_tbl %>%
  11. mutate(
  12. gg_plots = map(
  13. splits, plot_split,
  14. expand_y_axis = expand_y_axis,
  15. alpha = alpha,
  16. base_size = base_size))
  17. # Make plots with cowplot
  18. plot_list <- sampling_tbl_with_plots$gg_plots
  19. p_temp <- plot_list[[1]] + theme(legend.position = "bottom")
  20. legend <- get_legend(p_temp)
  21. p_body <- plot_grid(
  22. plotlist = plot_list, ncol = ncol)
  23. p_title <- ggdraw() +
  24. draw_label(
  25. title,
  26. size = 18,
  27. fontface = "bold",
  28. colour = palette_light()[[1]])
  29. g <- plot_grid(
  30. p_title,
  31. p_body,
  32. legend,
  33. ncol = 1,
  34. rel_heights = c(0.05, 1, 0.05))
  35. return(g)
  36. }

现在我们可以使用 plot_sampling_plan() 可视化整个回测策略!我们可以看到抽样计划如何平移抽样窗口逐渐切分出训练和测试子样本。

  1. rolling_origin_resamples %>%
  2. plot_sampling_plan(
  3. expand_y_axis = T,
  4. ncol = 3, alpha = 1,
  5. size = 1, base_size = 10,
  6. title = "Backtesting Strategy: Rolling Origin Sampling Plan")

此外,我们可以让 expand_y_axis = FALSE,对每个样本进行缩放。

  1. rolling_origin_resamples %>%
  2. plot_sampling_plan(
  3. expand_y_axis = F,
  4. ncol = 3, alpha = 1,
  5. size = 1, base_size = 10,
  6. title = "Backtesting Strategy: Zoomed In")

当在太阳黑子数据集上测试 LSTM 模型准确性时,我们将使用这种回测策略(来自一个时间序列的 11 个样本,每个时间序列分为 50/10 两部分,并且样本之间有 20 年的偏移)。

5 用 Keras 构建状态 LSTM 模型

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

5.1 单个 LSTM 模型

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

  1. split <- rolling_origin_resamples$splits[[11]]
  2. split_id <- rolling_origin_resamples$id[[11]]
5.1.1 可视化该分割样本

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

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

5.1.2 数据准备

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

  1. df_trn <- training(split)
  2. df_tst <- testing(split)
  3. df <- bind_rows(
  4. df_trn %>% add_column(key = "training"),
  5. df_tst %>% add_column(key = "testing")) %>%
  6. as_tbl_time(index = index)
  7. df
  1. ## # A time tibble: 720 x 3
  2. ## # Index: index
  3. ## index value key
  4. ## <date> <dbl> <chr>
  5. ## 1 1949-11-01 144. training
  6. ## 2 1949-12-01 118. training
  7. ## 3 1950-01-01 102. training
  8. ## 4 1950-02-01 94.8 training
  9. ## 5 1950-03-01 110. training
  10. ## 6 1950-04-01 113. training
  11. ## 7 1950-05-01 106. training
  12. ## 8 1950-06-01 83.6 training
  13. ## 9 1950-07-01 91.0 training
  14. ## 10 1950-08-01 85.2 training
  15. ## # ... with 710 more rows
5.1.3 用 recipe 做数据预处理

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

  1. rec_obj <- recipe(value ~ ., df) %>%
  2. step_sqrt(value) %>%
  3. step_center(value) %>%
  4. step_scale(value) %>%
  5. prep()
  6. df_processed_tbl <- bake(rec_obj, df)
  7. df_processed_tbl
  1. ## # A tibble: 720 x 3
  2. ## index value key
  3. ## <date> <dbl> <fct>
  4. ## 1 1949-11-01 1.25 training
  5. ## 2 1949-12-01 0.929 training
  6. ## 3 1950-01-01 0.714 training
  7. ## 4 1950-02-01 0.617 training
  8. ## 5 1950-03-01 0.825 training
  9. ## 6 1950-04-01 0.874 training
  10. ## 7 1950-05-01 0.777 training
  11. ## 8 1950-06-01 0.450 training
  12. ## 9 1950-07-01 0.561 training
  13. ## 10 1950-08-01 0.474 training
  14. ## # ... with 710 more rows

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

  1. center_history <- rec_obj$steps[[2]]$means["value"]
  2. scale_history <- rec_obj$steps[[3]]$sds["value"]
  3. c("center" = center_history, "scale" = scale_history)
  1. ## center.value scale.value
  2. ## 7.549526 3.545561
5.1.4 规划 LSTM 模型

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

张量格式(Tensor Format)

  • 预测变量(X)必须是一个 3 维数组,维度分别是:samplestimestepsfeatures。第一维代表变量的长度;第二维是时间步(滞后阶数);第三维是预测变量的个数(1 表示单变量,n 表示多变量)
  • 输出或目标变量(y)必须是一个 2 维数组,维度分别是:samplestimesteps。第一维代表变量的长度;第二维是时间步(之后阶数)

训练与测试

  • 训练与测试的长度必须是可分的(训练集长度除以测试集长度必须是一个整数)

批量大小(Batch Size)

  • 批量大小是在 RNN 权重更新之前一次前向 / 后向传播过程中训练样本的数量
  • 批量大小关于训练集和测试集长度必须是可分的(训练集长度除以批量大小,以及测试集长度除以批量大小必须是一个整数)

时间步(Time Steps):

  • 时间步是训练集与测试集中的滞后阶数
  • 我们的例子中滞后 1 阶

周期(Epochs)

  • 周期是前向 / 后向传播迭代的总次数
  • 通常情况下周期越多,模型表现越好,直到验证集上的精确度或损失不再增加,这时便出现过度拟合

考虑到这一点,我们可以提出一个计划。我们将预测窗口或测试集的长度定在 120 个月(10年)。最优相关性发生在 125 阶,但这并不能被预测范围整除。我们可以增加预测范围,但是这仅提供了自相关性的最小幅度增加。我们选择批量大小为 40,它可以整除测试集和训练集的观察个数。我们选择时间步等于 1,这是因为我们只使用 1 阶滞后(只向前预测一步)。最后,我们设置 epochs = 300,但这需要调整以平衡偏差与方差。

  1. # Model inputs
  2. lag_setting <- 120 # = nrow(df_tst)
  3. batch_size <- 40
  4. train_length <- 440
  5. tsteps <- 1
  6. epochs <- 300
5.1.5 2 维与 3 维的训练、测试数组

下面将训练集和测试集数据转换成合适的形式(数组)。记住,LSTM 模型要求预测变量(X)是 3 维的,输出或目标变量(y)是 2 维的。

  1. # Training Set
  2. lag_train_tbl <- df_processed_tbl %>%
  3. mutate(value_lag = lag(value, n = lag_setting)) %>%
  4. filter(!is.na(value_lag)) %>%
  5. filter(key == "training") %>%
  6. tail(train_length)
  7. x_train_vec <- lag_train_tbl$value_lag
  8. x_train_arr <- array(
  9. data = x_train_vec, dim = c(length(x_train_vec), 1, 1))
  10. y_train_vec <- lag_train_tbl$value
  11. y_train_arr <- array(
  12. data = y_train_vec, dim = c(length(y_train_vec), 1))
  13. # Testing Set
  14. lag_test_tbl <- df_processed_tbl %>%
  15. mutate(
  16. value_lag = lag(
  17. value, n = lag_setting)) %>%
  18. filter(!is.na(value_lag)) %>%
  19. filter(key == "testing")
  20. x_test_vec <- lag_test_tbl$value_lag
  21. x_test_arr <- array(
  22. data = x_test_vec,
  23. dim = c(length(x_test_vec), 1, 1))
  24. y_test_vec <- lag_test_tbl$value
  25. y_test_arr <- array(
  26. data = y_test_vec,
  27. dim = c(length(y_test_vec), 1))
5.1.6 构建 LSTM 模型

我们可以使用 keras_model_sequential() 构建 LSTM 模型,并像堆砖块一样堆叠神经网络层。我们将使用两个 LSTM 层,每层都设定 units = 50。第一个 LSTM 层接收所需的输入形状,即[时间步,特征数量]。批量大小就是我们的批量大小。我们将第一层设置为 return_sequences = TRUEstateful = TRUE。第二层和前面相同,除了 batch_sizebatch_size 只需要在第一层中指定),另外 return_sequences = FALSE 不返回时间戳维度(从第一个 LSTM 层返回 2 维数组,而不是 3 维)。我们使用 layer_dense(units = 1),这是 Keras 序列模型的标准结尾。最后,我们在 compile() 中使用 loss ="mae" 以及流行的 optimizer = "adam"

  1. model <- keras_model_sequential()
  2. model %>%
  3. layer_lstm(
  4. units = 50,
  5. input_shape = c(tsteps, 1),
  6. batch_size = batch_size,
  7. return_sequences = TRUE,
  8. stateful = TRUE) %>%
  9. layer_lstm(
  10. units = 50,
  11. return_sequences = FALSE,
  12. stateful = TRUE) %>%
  13. layer_dense(units = 1)
  14. model %>%
  15. compile(loss = 'mae', optimizer = 'adam')
  16. model
  1. ## Model
  2. ## ______________________________________________________________________
  3. ## Layer (type) Output Shape Param #
  4. ## ======================================================================
  5. ## lstm_1 (LSTM) (40, 1, 50) 10400
  6. ## ______________________________________________________________________
  7. ## lstm_2 (LSTM) (40, 50) 20200
  8. ## ______________________________________________________________________
  9. ## dense_1 (Dense) (40, 1) 51
  10. ## ======================================================================
  11. ## Total params: 30,651
  12. ## Trainable params: 30,651
  13. ## Non-trainable params: 0
  14. ## ______________________________________________________________________
5.1.7 拟合 LSTM 模型

下一步,我们使用一个 for 循环拟合状态 LSTM 模型(需要手动重置状态)。有 300 个周期要循环,运行需要一点时间。我们设置 shuffle = FALSE 来保存序列,并且我们使用 reset_states() 在每个循环后手动重置状态。

  1. for (i in 1:epochs) {
  2. model %>%
  3. fit(x = x_train_arr,
  4. y = y_train_arr,
  5. batch_size = batch_size,
  6. epochs = 1,
  7. verbose = 1,
  8. shuffle = FALSE)
  9. model %>% reset_states()
  10. cat("Epoch: ", i)
  11. }
5.1.8 使用 LSTM 模型预测

然后,我们可以使用 predict() 函数对测试集 x_test_arr 进行预测。我们可以使用之前保存的 scale_historycenter_history 转换得到的预测,然后对结果进行平方。最后,我们使用 reduce() 和自定义的 time_bind_rows() 函数将预测与一列原始数据结合起来。

  1. # Make Predictions
  2. pred_out <- model %>%
  3. predict(x_test_arr, batch_size = batch_size) %>%
  4. .[,1]
  5. # Retransform values
  6. pred_tbl <- tibble(
  7. index = lag_test_tbl$index,
  8. value = (pred_out * scale_history + center_history)^2)
  9. # Combine actual data with predictions
  10. tbl_1 <- df_trn %>%
  11. add_column(key = "actual")
  12. tbl_2 <- df_tst %>%
  13. add_column(key = "actual")
  14. tbl_3 <- pred_tbl %>%
  15. add_column(key = "predict")
  16. # Create time_bind_rows() to solve dplyr issue
  17. time_bind_rows <- function(data_1,
  18. data_2, index) {
  19. index_expr <- enquo(index)
  20. bind_rows(data_1, data_2) %>%
  21. as_tbl_time(index = !! index_expr)
  22. }
  23. ret <- list(tbl_1, tbl_2, tbl_3) %>%
  24. reduce(time_bind_rows, index = index) %>%
  25. arrange(key, index) %>%
  26. mutate(key = as_factor(key))
  27. ret
  1. ## # A time tibble: 840 x 3
  2. ## # Index: index
  3. ## index value key
  4. ## <date> <dbl> <fct>
  5. ## 1 1949-11-01 144. actual
  6. ## 2 1949-12-01 118. actual
  7. ## 3 1950-01-01 102. actual
  8. ## 4 1950-02-01 94.8 actual
  9. ## 5 1950-03-01 110. actual
  10. ## 6 1950-04-01 113. actual
  11. ## 7 1950-05-01 106. actual
  12. ## 8 1950-06-01 83.6 actual
  13. ## 9 1950-07-01 91.0 actual
  14. ## 10 1950-08-01 85.2 actual
  15. ## # ... with 830 more rows
5.1.9 评估单个分割样本上 LSTM 模型的表现

我们使用 yardstick 包里的 rmse() 函数评估表现,rmse() 返回均方误差平方根(RMSE)。我们的数据以“长”格式的形式存在(使用 ggplot2 可视化的最佳格式),所以需要创建一个包装器函数 calc_rmse() 对数据做预处理,以适应 yardstick::rmse() 的要求。

  1. calc_rmse <- function(prediction_tbl) {
  2. rmse_calculation <- function(data) {
  3. data %>%
  4. spread(key = key, value = value) %>%
  5. select(-index) %>%
  6. filter(!is.na(predict)) %>%
  7. rename(
  8. truth = actual,
  9. estimate = predict) %>%
  10. rmse(truth, estimate)
  11. }
  12. safe_rmse <- possibly(
  13. rmse_calculation, otherwise = NA)
  14. safe_rmse(prediction_tbl)
  15. }

我们计算模型的 RMSE。

  1. calc_rmse(ret)
  1. ## [1] 31.81798

RMSE 提供的信息有限,我们需要可视化。注意:当我们扩展到回测策略中的所有样本时,RMSE 将在确定预期误差时派上用场。

5.1.10 可视化一步预测

下一步,我们创建一个绘图函数——plot_prediction(),借助 ggplot2 可视化单一样本上的结果。

  1. # Setup single plot function
  2. plot_prediction <- function(data,
  3. id,
  4. alpha = 1,
  5. size = 2,
  6. base_size = 14) {
  7. rmse_val <- calc_rmse(data)
  8. g <- data %>%
  9. ggplot(aes(index, value, color = key)) +
  10. geom_point(alpha = alpha, size = size) +
  11. theme_tq(base_size = base_size) +
  12. scale_color_tq() +
  13. theme(legend.position = "none") +
  14. labs(
  15. title = glue(
  16. "{id}, RMSE: {round(rmse_val, digits = 1)}"),
  17. x = "", y = "")
  18. return(g)
  19. }

我们设置 id = split_id,在 Slice11 上测试函数。

  1. ret %>%
  2. plot_prediction(id = split_id, alpha = 0.65) +
  3. theme(legend.position = "bottom")

LSTM 模型表现相对较好! 我们选择的设置似乎产生了一个不错的模型,可以捕捉到数据中的趋势。预测在下一个上升趋势前抢跑了,但总体上好过了我的预期。现在,我们需要通过回测来查看随着时间推移的真实表现!

5.2 在 11 个样本上回测 LSTM 模型

一旦我们有了能在一个样本上工作的 LSTM 模型,扩展到全部 11 个样本上就相对简单。我们只需创建一个预测函数,再套用到 rolling_origin_resamples 中抽样计划包含的数据上。

5.2.1 构建一个 LSTM 预测函数

这一步看起来很吓人,但实际上很简单。我们将 5.1 节的代码复制到一个函数中。我们将它作为一个安全函数,对于任何长时间运行的函数来说,这是一个很好的做法,可以防止单个故障停止整个过程。

  1. predict_keras_lstm <- function(split,
  2. epochs = 300,
  3. ...) {
  4. lstm_prediction <- function(split,
  5. epochs,
  6. ...) {
  7. # 5.1.2 Data Setup
  8. df_trn <- training(split)
  9. df_tst <- testing(split)
  10. df <- bind_rows(
  11. df_trn %>% add_column(key = "training"),
  12. df_tst %>% add_column(key = "testing")) %>%
  13. as_tbl_time(index = index)
  14. # 5.1.3 Preprocessing
  15. rec_obj <- recipe(value ~ ., df) %>%
  16. step_sqrt(value) %>%
  17. step_center(value) %>%
  18. step_scale(value) %>%
  19. prep()
  20. df_processed_tbl <- bake(rec_obj, df)
  21. center_history <- rec_obj$steps[[2]]$means["value"]
  22. scale_history <- rec_obj$steps[[3]]$sds["value"]
  23. # 5.1.4 LSTM Plan
  24. lag_setting <- 120 # = nrow(df_tst)
  25. batch_size <- 40
  26. train_length <- 440
  27. tsteps <- 1
  28. epochs <- epochs
  29. # 5.1.5 Train/Test Setup
  30. lag_train_tbl <- df_processed_tbl %>%
  31. mutate(
  32. value_lag = lag(value, n = lag_setting)) %>%
  33. filter(!is.na(value_lag)) %>%
  34. filter(key == "training") %>%
  35. tail(train_length)
  36. x_train_vec <- lag_train_tbl$value_lag
  37. x_train_arr <- array(
  38. data = x_train_vec, dim = c(length(x_train_vec), 1, 1))
  39. y_train_vec <- lag_train_tbl$value
  40. y_train_arr <- array(
  41. data = y_train_vec, dim = c(length(y_train_vec), 1))
  42. lag_test_tbl <- df_processed_tbl %>%
  43. mutate(
  44. value_lag = lag(value, n = lag_setting)) %>%
  45. filter(!is.na(value_lag)) %>%
  46. filter(key == "testing")
  47. x_test_vec <- lag_test_tbl$value_lag
  48. x_test_arr <- array(
  49. data = x_test_vec, dim = c(length(x_test_vec), 1, 1))
  50. y_test_vec <- lag_test_tbl$value
  51. y_test_arr <- array(
  52. data = y_test_vec, dim = c(length(y_test_vec), 1))
  53. # 5.1.6 LSTM Model
  54. model <- keras_model_sequential()
  55. model %>%
  56. layer_lstm(
  57. units = 50,
  58. input_shape = c(tsteps, 1),
  59. batch_size = batch_size,
  60. return_sequences = TRUE,
  61. stateful = TRUE) %>%
  62. layer_lstm(
  63. units = 50,
  64. return_sequences = FALSE,
  65. stateful = TRUE) %>%
  66. layer_dense(units = 1)
  67. model %>%
  68. compile(loss = 'mae', optimizer = 'adam')
  69. # 5.1.7 Fitting LSTM
  70. for (i in 1:epochs) {
  71. model %>%
  72. fit(x = x_train_arr,
  73. y = y_train_arr,
  74. batch_size = batch_size,
  75. epochs = 1,
  76. verbose = 1,
  77. shuffle = FALSE)
  78. model %>% reset_states()
  79. cat("Epoch: ", i)
  80. }
  81. # 5.1.8 Predict and Return Tidy Data
  82. # Make Predictions
  83. pred_out <- model %>%
  84. predict(x_test_arr, batch_size = batch_size) %>%
  85. .[,1]
  86. # Retransform values
  87. pred_tbl <- tibble(
  88. index = lag_test_tbl$index,
  89. value = (pred_out * scale_history + center_history)^2)
  90. # Combine actual data with predictions
  91. tbl_1 <- df_trn %>%
  92. add_column(key = "actual")
  93. tbl_2 <- df_tst %>%
  94. add_column(key = "actual")
  95. tbl_3 <- pred_tbl %>%
  96. add_column(key = "predict")
  97. # Create time_bind_rows() to solve dplyr issue
  98. time_bind_rows <- function(data_1, data_2, index) {
  99. index_expr <- enquo(index)
  100. bind_rows(data_1, data_2) %>%
  101. as_tbl_time(index = !! index_expr)
  102. }
  103. ret <- list(tbl_1, tbl_2, tbl_3) %>%
  104. reduce(time_bind_rows, index = index) %>%
  105. arrange(key, index) %>%
  106. mutate(key = as_factor(key))
  107. return(ret)
  108. }
  109. safe_lstm <- possibly(lstm_prediction, otherwise = NA)
  110. safe_lstm(split, epochs, ...)
  111. }

我们测试下 predict_keras_lstm() 函数,设置 epochs = 10。返回的数据为长格式,在 key 列中标记有 actualpredict

  1. predict_keras_lstm(split, epochs = 10)
  1. ## # A time tibble: 840 x 3
  2. ## # Index: index
  3. ## index value key
  4. ## <date> <dbl> <fct>
  5. ## 1 1949-11-01 144. actual
  6. ## 2 1949-12-01 118. actual
  7. ## 3 1950-01-01 102. actual
  8. ## 4 1950-02-01 94.8 actual
  9. ## 5 1950-03-01 110. actual
  10. ## 6 1950-04-01 113. actual
  11. ## 7 1950-05-01 106. actual
  12. ## 8 1950-06-01 83.6 actual
  13. ## 9 1950-07-01 91.0 actual
  14. ## 10 1950-08-01 85.2 actual
  15. ## # ... with 830 more rows
5.2.2 将 LSTM 预测函数应用到 11 个样本上

既然 predict_keras_lstm() 函数可以在一个样本上运行,我们现在可以借助使用 mutate()map() 将函数应用到所有样本上。预测将存储在名为 predict 的列中。注意,这可能需要 5-10 分钟左右才能完成。

  1. sample_predictions_lstm_tbl <- rolling_origin_resamples %>%
  2. mutate(predict = map(splits, predict_keras_lstm, epochs = 300))

现在,我们得到了 11 个样本的预测,数据存储在列 predict 中。

  1. sample_predictions_lstm_tbl
  1. ## # Rolling origin forecast resampling
  2. ## # A tibble: 11 x 3
  3. ## splits id predict
  4. ## * <list> <chr> <list>
  5. ## 1 <S3: rsplit> Slice01 <tibble [840 x 3]>
  6. ## 2 <S3: rsplit> Slice02 <tibble [840 x 3]>
  7. ## 3 <S3: rsplit> Slice03 <tibble [840 x 3]>
  8. ## 4 <S3: rsplit> Slice04 <tibble [840 x 3]>
  9. ## 5 <S3: rsplit> Slice05 <tibble [840 x 3]>
  10. ## 6 <S3: rsplit> Slice06 <tibble [840 x 3]>
  11. ## 7 <S3: rsplit> Slice07 <tibble [840 x 3]>
  12. ## 8 <S3: rsplit> Slice08 <tibble [840 x 3]>
  13. ## 9 <S3: rsplit> Slice09 <tibble [840 x 3]>
  14. ## 10 <S3: rsplit> Slice10 <tibble [840 x 3]>
  15. ## 11 <S3: rsplit> Slice11 <tibble [840 x 3]>
5.2.3 评估回测表现

通过将 calc_rmse() 函数应用到 predict 列上,我们可以得到所有样本的 RMSE。

  1. sample_rmse_tbl <- sample_predictions_lstm_tbl %>%
  2. mutate(rmse = map_dbl(predict, calc_rmse)) %>%
  3. select(id, rmse)
  4. sample_rmse_tbl
  1. ## # Rolling origin forecast resampling
  2. ## # A tibble: 11 x 2
  3. ## id rmse
  4. ## * <chr> <dbl>
  5. ## 1 Slice01 48.2
  6. ## 2 Slice02 17.4
  7. ## 3 Slice03 41.0
  8. ## 4 Slice04 26.6
  9. ## 5 Slice05 22.2
  10. ## 6 Slice06 49.0
  11. ## 7 Slice07 18.1
  12. ## 8 Slice08 54.9
  13. ## 9 Slice09 28.0
  14. ## 10 Slice10 38.4
  15. ## 11 Slice11 34.2
  1. sample_rmse_tbl %>%
  2. ggplot(aes(rmse)) +
  3. geom_histogram(
  4. aes(y = ..density..),
  5. fill = palette_light()[[1]], bins = 16) +
  6. geom_density(
  7. fill = palette_light()[[1]], alpha = 0.5) +
  8. theme_tq() +
  9. ggtitle("Histogram of RMSE")

而且,我们可以总结 11 个样本的 RMSE。专业提示:使用 RMSE(或其他类似指标)的平均值和标准差是比较各种模型表现的好方法。

  1. sample_rmse_tbl %>%
  2. summarize(
  3. mean_rmse = mean(rmse),
  4. sd_rmse = sd(rmse))
  1. ## # Rolling origin forecast resampling
  2. ## # A tibble: 1 x 2
  3. ## mean_rmse sd_rmse
  4. ## <dbl> <dbl>
  5. ## 1 34.4 13.0
5.2.4 可视化回测的结果

我们可以创建一个 plot_predictions() 函数,把 11 个回测样本的预测结果绘制在一副图上!!!

  1. plot_predictions <- function(sampling_tbl,
  2. predictions_col,
  3. ncol = 3,
  4. alpha = 1,
  5. size = 2,
  6. base_size = 14,
  7. title = "Backtested Predictions") {
  8. predictions_col_expr <- enquo(predictions_col)
  9. # Map plot_split() to sampling_tbl
  10. sampling_tbl_with_plots <- sampling_tbl %>%
  11. mutate(
  12. gg_plots = map2(
  13. !! predictions_col_expr, id,
  14. .f = plot_prediction,
  15. alpha = alpha,
  16. size = size,
  17. base_size = base_size))
  18. # Make plots with cowplot
  19. plot_list <- sampling_tbl_with_plots$gg_plots
  20. p_temp <- plot_list[[1]] + theme(legend.position = "bottom")
  21. legend <- get_legend(p_temp)
  22. p_body <- plot_grid(plotlist = plot_list, ncol = ncol)
  23. p_title <- ggdraw() +
  24. draw_label(
  25. title,
  26. size = 18,
  27. fontface = "bold",
  28. colour = palette_light()[[1]])
  29. g <- plot_grid(
  30. p_title,
  31. p_body,
  32. legend,
  33. ncol = 1,
  34. rel_heights = c(0.05, 1, 0.05))
  35. return(g)
  36. }

结果在这里。在一个不容易预测的数据集上,这是相当令人印象深刻的!

  1. sample_predictions_lstm_tbl %>%
  2. plot_predictions(
  3. predictions_col = predict,
  4. alpha = 0.5,
  5. size = 1,
  6. base_size = 10,
  7. title = "Keras Stateful LSTM: Backtested Predictions")

5.3 预测未来 10 年的数据

我们可以通过调整预测函数来使用完整的数据集预测未来 10 年的数据。新函数 predict_keras_lstm_future() 用来预测未来 120 步(或 10 年)的数据。

  1. predict_keras_lstm_future <- function(data,
  2. epochs = 300,
  3. ...) {
  4. lstm_prediction <- function(data,
  5. epochs,
  6. ...) {
  7. # 5.1.2 Data Setup (MODIFIED)
  8. df <- data
  9. # 5.1.3 Preprocessing
  10. rec_obj <- recipe(value ~ ., df) %>%
  11. step_sqrt(value) %>%
  12. step_center(value) %>%
  13. step_scale(value) %>%
  14. prep()
  15. df_processed_tbl <- bake(rec_obj, df)
  16. center_history <- rec_obj$steps[[2]]$means["value"]
  17. scale_history <- rec_obj$steps[[3]]$sds["value"]
  18. # 5.1.4 LSTM Plan
  19. lag_setting <- 120 # = nrow(df_tst)
  20. batch_size <- 40
  21. train_length <- 440
  22. tsteps <- 1
  23. epochs <- epochs
  24. # 5.1.5 Train Setup (MODIFIED)
  25. lag_train_tbl <- df_processed_tbl %>%
  26. mutate(
  27. value_lag = lag(value, n = lag_setting)) %>%
  28. filter(!is.na(value_lag)) %>%
  29. tail(train_length)
  30. x_train_vec <- lag_train_tbl$value_lag
  31. x_train_arr <- array(
  32. data = x_train_vec, dim = c(length(x_train_vec), 1, 1))
  33. y_train_vec <- lag_train_tbl$value
  34. y_train_arr <- array(
  35. data = y_train_vec, dim = c(length(y_train_vec), 1))
  36. x_test_vec <- y_train_vec %>% tail(lag_setting)
  37. x_test_arr <- array(
  38. data = x_test_vec, dim = c(length(x_test_vec), 1, 1))
  39. # 5.1.6 LSTM Model
  40. model <- keras_model_sequential()
  41. model %>%
  42. layer_lstm(
  43. units = 50,
  44. input_shape = c(tsteps, 1),
  45. batch_size = batch_size,
  46. return_sequences = TRUE,
  47. stateful = TRUE) %>%
  48. layer_lstm(
  49. units = 50,
  50. return_sequences = FALSE,
  51. stateful = TRUE) %>%
  52. layer_dense(units = 1)
  53. model %>%
  54. compile(loss = 'mae', optimizer = 'adam')
  55. # 5.1.7 Fitting LSTM
  56. for (i in 1:epochs) {
  57. model %>%
  58. fit(x = x_train_arr,
  59. y = y_train_arr,
  60. batch_size = batch_size,
  61. epochs = 1,
  62. verbose = 1,
  63. shuffle = FALSE)
  64. model %>% reset_states()
  65. cat("Epoch: ", i)
  66. }
  67. # 5.1.8 Predict and Return Tidy Data (MODIFIED)
  68. # Make Predictions
  69. pred_out <- model %>%
  70. predict(x_test_arr, batch_size = batch_size) %>%
  71. .[,1]
  72. # Make future index using tk_make_future_timeseries()
  73. idx <- data %>%
  74. tk_index() %>%
  75. tk_make_future_timeseries(n_future = lag_setting)
  76. # Retransform values
  77. pred_tbl <- tibble(
  78. index = idx,
  79. value = (pred_out * scale_history + center_history)^2)
  80. # Combine actual data with predictions
  81. tbl_1 <- df %>%
  82. add_column(key = "actual")
  83. tbl_3 <- pred_tbl %>%
  84. add_column(key = "predict")
  85. # Create time_bind_rows() to solve dplyr issue
  86. time_bind_rows <- function(data_1,
  87. data_2,
  88. index) {
  89. index_expr <- enquo(index)
  90. bind_rows(data_1, data_2) %>%
  91. as_tbl_time(index = !! index_expr)
  92. }
  93. ret <- list(tbl_1, tbl_3) %>%
  94. reduce(time_bind_rows, index = index) %>%
  95. arrange(key, index) %>%
  96. mutate(key = as_factor(key))
  97. return(ret)
  98. }
  99. safe_lstm <- possibly(lstm_prediction, otherwise = NA)
  100. safe_lstm(data, epochs, ...)
  101. }

下一步,在 sun_spots 数据集上运行 predict_keras_lstm_future() 函数。

  1. future_sun_spots_tbl <- predict_keras_lstm_future(sun_spots, epochs = 300)

最后,我们使用 plot_prediction() 可视化预测结果,需要设置 id = NULL。我们使用 filter_time() 函数将数据集缩放到 1900 年之后。

  1. future_sun_spots_tbl %>%
  2. filter_time("1900" ~ "end") %>%
  3. plot_prediction(
  4. id = NULL, alpha = 0.4, size = 1.5) +
  5. theme(legend.position = "bottom") +
  6. ggtitle(
  7. "Sunspots: Ten Year Forecast",
  8. subtitle = "Forecast Horizon: 2013 - 2023")

结论

本文演示了使用 keras 包构建的状态 LSTM 模型的强大功能。令人惊讶的是,提供的唯一特征是滞后 120 阶的历史数据,深度学习方法依然识别出了数据中的趋势。回测模型的 RMSE 均值等于 34,RMSE 标准差等于 13。虽然本文未显示,但我们对比测试1了 ARIMA 模型和 prophet 模型(Facebook 开发的时间序列预测模型),LSTM 模型的表现优越:平均误差减少了 30% 以上,标准差减少了 40%。这显示了机器学习工具-应用适合性的好处。

除了使用的深度学习方法之外,文章还揭示了使用 ACF 图确定 LSTM 模型对于给定时间序列是否适用的方法。我们还揭示了时间序列模型的准确性应如何通过回测来进行基准测试,这种策略保持了时间序列的连续性,可用于时间序列数据的交叉验证。


  1. 测试结果:https://github.com/business-science/presentations/blob/master/2018_04_19_SP_Global_Time_Series_Deep_Learning/SP_Global_Presentation_final.pdf?

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号