相似文件
换一批
经管之家送您一份
应届毕业生专属福利!
求职就业群
感谢您参与论坛问题回答
经管之家送您两个论坛币!
+2 论坛币
Simulation of the short rate in the Vasicek model in R | 转自http://delta9hedge.blogspot.com/2013/05/simulation-of-vasicek-interest-rates-in.html | - #==================
- # model parameters
- # 初始参数设定
- #==================
- r0 <- 0.02
- thetaQ <- 0.05
- kappa <- 0.1
- sigma <- 0.015
- T0 <- 1
- #=======================
- # simulation parameters
- # Euler离散化(40次模拟)
- #=======================
- hedge.points <-252
- simulations.total <- 40
- maturity <- T0
- dt <- maturity/(hedge.points+1)
- timeline <- seq(0,maturity, dt)
- f <- matrix(r0,(hedge.points+2),simulations.total)
- vasicek_rate <- function(r,kappa,theta,sigma,dt){
- expkappadt <- exp(-kappa*dt)
- vasi_vola <- (sigma^2)*(1-expkappadt^2)/(2*kappa)
- result <- r*expkappadt+theta*(1-expkappadt)+sqrt(vasi_vola)*rnorm(1)
- return(result)
- }
- for(i in 2:(hedge.points+2)){
- for(j in 1:simulations.total){
- f[i,j]<- vasicek_rate(f[i-1,j],kappa,thetaQ,sigma,dt)
- }
- }
- #==================================
- # plot of interest rate simulations
- # 绘制利率路径
- #==================================
- plot(timeline,f[,1], ylim=range(f,thetaQ), type="l", col="mediumorchid2")
- for(j in 2:simulations.total){
- lines(timeline,f[,j], col=colors()[floor(runif(1,1,657))] )
- }
- abline( h = thetaQ, col = "red")
- text(0, thetaQ+0.005,paste("long term interest level: theta =",thetaQ),adj=0)
- title(main="Simulation of Vasicek interest rate", col.main="red", font.main=4)
复制代码
|
| Simulation of a Geometric Brownian Motion in R | - maturity <- 15
- simulation.length <- 10001
- dt <- maturity/(simulation.length-1)
- timeline <- seq(0,maturity, dt)
- S0<-1
- r<-0.05
- mu<-0.1
- mu0<-0.2
- sigma<-0.2
- sigma0<-0.375
- f <- g <- g0 <- h <- h0 <- rep(0, times=simulation.length)
- g0[1] <- h0[1] <- g[1] <- h[1] <- S0
- for(i in 2:simulation.length){
- f[i] <- f[i-1]+sqrt(dt)*rnorm(1)
- g[i] <- g[1]*exp((mu-(sigma^2)/2)*(i-1)*dt+sigma*f[i])
- g0[i] <- g0[1]*exp(mu*(i-1)*dt)
- h[i] <- h[1]*exp((mu0-sigma0^2/2)*(i-1)*dt+sigma0*f[i])
- h0[i] <- h0[1]*exp(mu0*(i-1)*dt)
- }
- o_range <- range(f,g,g0,h,h0)
- plot(timeline,f, ylim=o_range, type="l", col="coral1")
- lines(timeline,g0, col="chartreuse3")
- lines(timeline,g, col="chartreuse2")
- lines(timeline,h, col="deepskyblue1")
- lines(timeline,h0, col="deepskyblue3")
- title(main="Geometric Brownian Motion trajectories", col.main="red", font.main=4)
- legend(1, o_range[2], c("mu = 0.2, sigma = 0.375","mu = 0.1, sigma = 0.2","Brownian motion"), cex=0.8,
- col=c("deepskyblue1","chartreuse2","coral1"), pch=1, lty=1);
复制代码
|
|
扫码加我 拉你入群
请注明:姓名-公司-职位
以便审核进群资格,未注明则拒绝
|