Guest User

Untitled

a guest
Nov 22nd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. # Initializing vectors
  2.  
  3. X1 <- matrix(rnorm(n=S$n.traj.obs*S$n.step.obs*S$n.days, mean=0, sd=1),
  4. nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days)
  5. X2 <- matrix(rnorm(n=S$n.traj.obs*S$n.step.obs*S$n.days, mean=0, sd=1),
  6. nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days)
  7.  
  8. V.start <- rep(P$v0, S$n.traj.obs)
  9. Y.start <- rep(P$y0, S$n.traj.obs)
  10.  
  11. W.v <- X1
  12. Z.v <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days)
  13.  
  14. V_ <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days+1)
  15. V <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days+1)
  16. V.close <- matrix(nrow=S$n.traj.obs, ncol=S$n.days+1)
  17. V_[,1] <- V.start
  18. V[,1] <- V.start
  19. V.close[,1] <- V.start
  20. sum.jump.v <- matrix(ncol=S$n.days,nrow=S$n.traj.obs)
  21.  
  22.  
  23. W.y <- P$rho * X1 + sqrt(1-P$rho^2) * X2
  24. Z.y <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days)
  25.  
  26. Y <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days+1)
  27. Y_ <- matrix(nrow=S$n.traj.obs, ncol=S$n.step.obs*S$n.days+1)
  28. Y.close <- matrix(nrow=S$n.traj.obs, ncol=S$n.days+1)
  29. Y.close[,1] <- Y.start
  30. Y[,1] <- Y.start
  31. Y_[,1] <- Y.start
  32. sum.jump.y <- matrix(ncol=S$n.days,nrow=S$n.traj.obs)
  33.  
  34. dI <- matrix(ncol=S$n.step.obs*S$n.days,nrow=S$n.traj.obs)
  35. idx <- matrix(ncol=S$n.days,nrow=S$n.traj.obs)
  36. quad.var <- matrix(ncol=S$n.days,nrow=S$n.traj.obs)
  37. int.var <- matrix(ncol=S$n.days,nrow=S$n.traj.obs)
  38.  
  39. day <- 1
  40.  
  41.  
  42. # SIMULATION
  43.  
  44. for (j in 1:(S$n.days*S$n.step.obs)){
  45.  
  46.  
  47. # Variance jumps
  48. # ----------------------------------------------------------------------- #
  49.  
  50.  
  51. # is there a jump in the variance for this step?
  52. prob.jump.v <- 1 - exp(-P$lambda.v0 * S$h.obs)
  53. unif.jump.v <- runif(S$n.traj.obs,0,1)
  54.  
  55. # for experiment #2 and experiment #3
  56. mat <- cbind(unif.jump.v, prob.jump.v)
  57. idx.T <- which(mat[,1] < mat[,2])
  58. ind.jump.v <- rep(0, S$n.traj.obs)
  59. ind.jump.v[idx.T] <- 1
  60.  
  61. # which trajectory jumped for this step? which did not?
  62. idx.T <- which(ind.jump.v == 1)
  63. idx.F <- which(ind.jump.v == 0)
  64.  
  65. # Jump size: for trajectories that jumped, generate a exponential
  66. Z.v[idx.T,j] <- rexp(n=length(idx.T),rate=1/P$mu.v)
  67. Z.v[idx.F,j] <- 0
  68.  
  69.  
  70.  
  71. # Variance
  72. # ----------------------------------------------------------------------- #
  73.  
  74. # pre-jump variance
  75. V_[,j+1] <- V[,j] + P$kappa * (P$theta - V[,j]) * S$h.obs +
  76. P$sigma * sqrt(V[,j]) * W.v[,j] *sqrt(S$h.obs)
  77.  
  78. # If pre-jump variance is negative, set to 0
  79. idx.T <- which(V_[,j+1] < exp(-12))
  80. V_[idx.T,j+1] <- exp(-12)
  81.  
  82. # add jump
  83. V[,j+1] <- V_[,j+1] + Z.v[,j]
  84.  
  85. # Integrated variance
  86. # ----------------------------------------------------------------------- #
  87.  
  88.  
  89. dI[,j] <- S$h.obs * (V[,j] + V_[,j+1])/2
  90.  
  91. # Log price jumps
  92. # ----------------------------------------------------------------------- #
  93.  
  94.  
  95.  
  96. if ( j == 1){
  97. lambda.y <- P$lambda.y0
  98. }else {
  99. if ( j == 2){
  100. lambda.y <- P$lambda.y1}
  101. else{
  102. lambda.y <- P$lambda.y0 + P$lambda.y1 * V[,j]}
  103. }
  104.  
  105. # is there a jump in the variance for this step?
  106. prob.jump.y <- 1 - exp(-lambda.y * S$h.obs)
  107. unif.jump.y <- runif(S$n.traj.obs,0,1)
  108.  
  109. # If unif < jump prob, jump indicator = 1
  110. mat <- cbind(unif.jump.y, prob.jump.y)
  111. idx.T <- which(mat[,1] < mat[,2])
  112. ind.jump.y <- rep(0, S$n.traj.obs)
  113. ind.jump.y[idx.T] <- 1
  114.  
  115. # which trajectory jumped for this step? which did not?
  116. idx.T <- which(ind.jump.y == 1)
  117. idx.F <- which(ind.jump.y == 0)
  118.  
  119. # Jump size: for trajectories that jumped, generate a exponential
  120. Z.y[idx.T,j] <- rnorm(n=length(idx.T), mean=P$mu.y, sd=P$sigma.y)
  121. Z.y[idx.F,j] <- 0
  122.  
  123.  
  124.  
  125.  
  126. # Log price
  127. # ----------------------------------------------------------------------- #
  128.  
  129. mgfy <- exp(P$mu.y * 1 + 0.5 * P$sigma.y^2 * 1^2)
  130. alpha <- P$r - P$q + (RP$eta.y - 0.5)*V[,j] +
  131. ( RP$gamma.y.min - (mgfy - 1))*lambda.y
  132. Y_[,j+1] <- Y[,j] + alpha *S$h.obs + sqrt(V[,j])*W.y[,j] *sqrt(S$h.obs)
  133. Y[,j+1] <- Y_[,j+1] + Z.y[,j]
  134.  
  135. if( (j %% S$n.step.obs == 0) == TRUE){
  136. V.close[,day+1] <- V[,j+1]
  137. Y.close[,day+1] <- Y[,j+1]
  138. int.var[,day] <- apply(dI[,(j-S$n.step.obs+1):j], 1, sum)
  139. sum.jump.y[,day] <- apply(Z.y[,(j-S$n.step.obs+1):j]^2, 1, sum)
  140. sum.jump.v[,day] <- apply(Z.v[,(j-S$n.step.obs+1):j]^2, 1, sum)
  141. quad.var[,day] <- int.var[,day] + sum.jump.y[,day]
  142. day <- day + 1
  143. }
  144.  
  145. print(paste0("iteration: ",j))
  146.  
  147. }
Add Comment
Please, Sign In to add comment