Guest User

Untitled

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