【发布时间】:2016-07-07 16:13:00
【问题描述】:
我想有效地模拟漂移 d>0 的布朗运动,如果超过了一些障碍 b 或 -b,漂移的方向会发生变化(没有反射,只是漂移方向的变化!)。for-loop 是这样做的简单方法
step<-0.1 #step size
sig<-1 #sign of drift
T<-10^4 #length of process
b<-300; d<-0.5#barrier and drift
W<-rep(NA,(T/step))
W[1]<-0
for (i in 2:(T/step))
{
if (W[i-1]>b) {sig<- -1} #change drift to -1
if (W[i-1]< -b) {sig<-1} #change drift to +1
W[i]<-W[i-1]+rnorm(1,d*sig*step,sqrt(step))
}
当然,这个循环在 R 中需要很多时间,尤其是对于小步长。
因此,我对可能使用矢量运算或apply()-command 的更有效的解决方案感兴趣。 (如果是简单的布朗运动,我可以使用cumsum(rnorm()),这里有类似的解决方案吗?)
非常感谢!!
【问题讨论】:
标签: r performance simulation