r - 如何使用 dplyr 中的累积函数在 R 中按行重现数据帧或 tibble?

标签 r dataframe dplyr

我想复制赫尔的《期权与衍生品》一书中的下表。

我相信 dplyr 中的累积函数可能合适,但我无法重现它。

我的努力如下。

library(tidyverse)
initial_deposit = 12000
contract_value = 9000
closing_stock_indices = c(1250,1241,1238.3,1244.6,1241.3,1240.1, 1236.2, 1229.9,
                         1230.8,1225.4,1228.1,1211,1211,1214.3,1216.1,1223,1226.9)

(seq_along(closing_stock_indices) - 1) %>%
 as.data.frame() %>%
 setNames('Day') %>%
 mutate(Closing_SI = closing_stock_indices,
        Daily_change = c(0, diff(Closing_SI*200)),
        Cum_gain = cumsum( Daily_change),
        Margin_balance = accumulate(Cum_gain[-1], .init = initial_deposit,
                                    ~ if (.x >= initial_deposit).x + .y else initial_deposit + .y),
        Variation_Margin = -1 * pmin(Margin_balance - initial_deposit, 0),
        margin_call = c(0, Variation_Margin[-n()]))

enter image description here

tibble(closing_stock_indices)%>%
 dplyr::mutate(Day = row_number())%>%
 mutate(Close =closing_stock_indices )%>%
 dplyr::mutate(y = as.numeric( Close - (dplyr::lag(Close, 1))))%>%
 dplyr::select(-closing_stock_indices)%>%
 dplyr::mutate(y = replace_na(y,0),
               Cum_gain = 200*cumsum(y))%>%
 dplyr::mutate(Margin_balance = initial_deposit+Cum_gain)

是否有使用 dplyr 中的 rowwise() 的其他替代方法?

如果有任何帮助,我将不胜感激。

最佳答案

您需要累积每日变化而不是累积变化,否则您将累积两次。为了表达能力,我使用了一个基本 lambda 函数而不是 purrr 函数:

library(tidyverse)
initial_deposit = 12000
contract_value = 9000
closing_stock_indices = c(1250,1241,1238.3,1244.6,1241.3,1240.1, 1236.2, 1229.9,
                          1230.8,1225.4,1228.1,1211,1211,1214.3,1216.1,1223,1226.9)



(seq_along(closing_stock_indices) - 1) %>%
  as.data.frame() %>%
  setNames('Day') %>%
  mutate(
    Closing_SI = closing_stock_indices,
    Daily_change = c(0, diff(Closing_SI*200)),
    Cum_gain = cumsum(Daily_change),
    Margin_balance = accumulate(
      Daily_change[-1], 
      .init = initial_deposit,
      \(bal, gain) if (bal > contract_value) bal + gain
                   else initial_deposit + gain
      ),
    margin_call = ifelse(Margin_balance < contract_value, 
                         initial_deposit - Margin_balance, 
                         0)
    )
#>    Day Closing_SI Daily_change Cum_gain Margin_balance margin_call
#> 1    0     1250.0            0        0          12000           0
#> 2    1     1241.0        -1800    -1800          10200           0
#> 3    2     1238.3         -540    -2340           9660           0
#> 4    3     1244.6         1260    -1080          10920           0
#> 5    4     1241.3         -660    -1740          10260           0
#> 6    5     1240.1         -240    -1980          10020           0
#> 7    6     1236.2         -780    -2760           9240           0
#> 8    7     1229.9        -1260    -4020           7980        4020
#> 9    8     1230.8          180    -3840          12180           0
#> 10   9     1225.4        -1080    -4920          11100           0
#> 11  10     1228.1          540    -4380          11640           0
#> 12  11     1211.0        -3420    -7800           8220        3780
#> 13  12     1211.0            0    -7800          12000           0
#> 14  13     1214.3          660    -7140          12660           0
#> 15  14     1216.1          360    -6780          13020           0
#> 16  15     1223.0         1380    -5400          14400           0
#> 17  16     1226.9          780    -4620          15180           0

关于r - 如何使用 dplyr 中的累积函数在 R 中按行重现数据帧或 tibble?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73909489/

相关文章:

python 将每小时数据 [1-24] 添加到日期时间数据

r - 将某些行保留在具有条件的数据框中

python - Pandas 将列拆分为多级

r - 指定 dplyr 列名称

r - 如何使用 dplyr 估算 R 中的缺失变量?

r - 在 Ubuntu 服务器上将 .R 文件转换为实际的 Shiny 应用程序

r - 无法在 R 中编译 RcppArmadillo

r - 将日期列映射到 R 中从 1 开始的唯一序列号

r - 如何在 R 中保留/重新评估 data.frame 的因子水平?

python - 使用 pandas 聚合函数按空格连接字符串