我对data.table
有点生疏,我想找到一个系列X_t
的最大缩图(任何时候t的最大缩幅都可以定义为argmin {X_T-X_t}其中{ {1}},用英语,对于时间系列的每个元素,我需要找到T>t
的系列元素,以便最小化X_T-Xt,如果系列增加则没有缩减)。
T>t
如何使用library(data.table)
set.seed(1)
DT = data.table(x=rnorm(10))
DT[,cumsumX:=cumsum(x)]
#I am interested in the drawdown for cumsumX
DT
x cumsumX
1: -0.6264538107 -0.6264538107 => max drawdown is -1.27.. index 3 (-1.27+0.62 is mini)
2: 0.1836433242 -0.4428104865 => max drawdown is -1.27.. index 3 (-1.27+0.62 is mini)
3: -0.8356286124 -1.2784390989 => max drawdown is NA (-1.27 is global minima)
4: 1.5952808021 0.3168417032 => max drawdown is -0.17.. index 6
5: 0.3295077718 0.6463494750 ...
6: -0.8204683841 -0.1741189091
7: 0.4874290524 0.3133101433
8: 0.7383247051 1.0516348485
9: 0.5757813517 1.6274162001
10: -0.3053883872 1.3220278130
这是我的解决方案
data.table
答案 0 :(得分:4)
好像你想要:
DT[, maxdrawdown := rev(cummin(rev(cumsumX)))]
DT[, index := .I[.N], by = maxdrawdown]
DT[cumsumX == min(cumsumX), `:=`(index = NA, maxdrawdown = NA)]
# x cumsumX maxdrawdown index
# 1: -0.6264538 -0.6264538 -1.2784391 3
# 2: 0.1836433 -0.4428105 -1.2784391 3
# 3: -0.8356286 -1.2784391 NA NA
# 4: 1.5952808 0.3168417 -0.1741189 6
# 5: 0.3295078 0.6463495 -0.1741189 6
# 6: -0.8204684 -0.1741189 -0.1741189 6
# 7: 0.4874291 0.3133101 0.3133101 7
# 8: 0.7383247 1.0516348 1.0516348 8
# 9: 0.5757814 1.6274162 1.3220278 10
#10: -0.3053884 1.3220278 1.3220278 10
答案 1 :(得分:2)
这是一种可能的方法:
# Load package
library(data.table)
# Generate data
set.seed(1)
DT = data.table(x=rnorm(10))
DT[,cumsumX:=cumsum(x)]
# Define number of rows in data table and index variable
DT$index <- rownames(DT)
length.DT <- nrow(DT)
# Calculate maxdrawdown
DT[ ,maxdrawdown:=min(DT$cumsumX[index:length.DT]), by=index]
# Substitute the minimum value of the entire column to be NA
DT$maxdrawdown[DT$cumsumX==min(DT$cumsumX)] <- NA
结果如下:
> DT
x cumsumX index maxdrawdown
1: -0.6264538 -0.6264538 1 -1.2784391
2: 0.1836433 -0.4428105 2 -1.2784391
3: -0.8356286 -1.2784391 3 NA
4: 1.5952808 0.3168417 4 -0.1741189
5: 0.3295078 0.6463495 5 -0.1741189
6: -0.8204684 -0.1741189 6 -0.1741189
7: 0.4874291 0.3133101 7 0.3133101
8: 0.7383247 1.0516348 8 1.0516348
9: 0.5757814 1.6274162 9 1.3220278
10: -0.3053884 1.3220278 10 1.3220278