Created
December 3, 2017 07:48
-
-
Save WilsonMongwe/e6ac2a95626cb0550c2127f5238ae336 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
simulateJump=function(mu_,ss_,lambda_,mu2_,sigma_,TotalTime,delta) | |
{ | |
Sn=0 | |
times <- c(0) | |
while(Sn <= TotalTime) | |
{ | |
n <- length(times) | |
u <- runif(1) | |
expon <- -log(u)/lambda_ | |
Sn <- times[n]+expon | |
times <- c(times, Sn) | |
} | |
times=times[-length(times)] | |
t=seq(from=0,to=TotalTime,by=delta) | |
#the last time is beyond TotalTime, so i delete from the vector times | |
indicator=seq(from=0,to=0,length=length(t)) # if there are jumps or not between two times | |
jumpSize=seq(from=0,to=0,length=length(t)) # stores the size of the jump | |
for(m in 2:length(times)) | |
{ | |
for(k in 2: length(t)) | |
{ | |
if( t[k-1]<=times[m] && times[m]<=t[k]) | |
{ | |
indicator[k]=1 | |
jumpSize[k]=rnorm(1,mean=mu2_,sd=sigma_) | |
} | |
} | |
} | |
stock=seq(from=0,to=0,length=length(t)) | |
stock[1]=10 | |
for(i in 2:length(t)) | |
{ | |
stock[i]=stock[i-1]+(mu_)*delta+ss_*sqrt(delta)*rnorm(1)+jumpSize[i]*indicator[i] | |
} | |
final=cbind(t,stock,jumpSize,indicator) | |
return(final) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment