我正在尝试加速蒙特卡洛功能。我想收紧这个,但我无法弄清楚如何。有什么想法吗?
initial=1400
ppinitial=0.07
assets2020=numeric(41)
actual=round(seq(from=initial, to=0, by=(-initial/40)),0)*100000
assets2020[1]=1000000*1.02-actual[1]*ppinitial
for (i in 2:length(actual)){
assets2020[i]=assets2020[i-1]*1.02-actual[i]*ppinitial}
答案 0 :(得分:1)
我认为你可以在这里使用filter
:
assets2020 <- -ppinitial * filter(actual,1.02,'rec',init=-1000000/ppinitial)
y[i]=y[i-1]*1.02-x[i]*pp ## use y ,x notation more handy than long variables
可以改写:
Y[i]= x[i]+f[1]*Y[i-1] ## recursive filter expression
Y = -y/pp
和f[1]=1.02
或Y[1]=x[1]+f[1]*1000000
所以init
等于-1000000/ppinitial
ppinitial =100
filter_asset <- -ppinitial * filter(actual,1.02,'rec',init=-1000000/ppinitial)
sum(abs(filter_asset-assets2020))
[1] 0.001937866