#===============================================================================
# Generate the transition density of a CIR process with time dependant
# drift and volatility.
#-------------------------------------------------------------------------------
# Remove any existing coefficients
GQD.remove()
# Define drift Coefficients. Note that the limiting mean is sinusoidal.
G0 <- function(t){2*(10+sin(2*pi*(t-0.5)))}
G1 <- function(t){-2}
# Define sinusoidal diffusion coefficient with `faster' oscillation.
Q1 <- function(t){0.25*(1+0.75*(sin(4*pi*t)))}
states <- seq(5,15,1/10) # State values
initial <- 8 # Starting value of the process
Tmax <- 5 # Time horizon
Tstart <- 1 # Time starts at 1
increment <- 1/100 # Incremental time steps
# Generate the transitional density
M <- GQD.density(Xs=initial,Xt=states,s=Tstart,t=Tmax,delt=increment)
if(require(rgl, quietly = TRUE))
{
open3d(windowRect=c(50,50,640+50,50+640),zoom=0.95)
persp3d(x=M$Xt,y=M$time,z=M$density,col=3,box=FALSE,xlab='State (X_t)',
ylab='Time (t)',zlab='Density f(X_t|X_s)')
play3d(spin3d(axis=c(0,0,1), rpm=3), duration=10)
}else
{
persp(x=M$Xt,y=M$time,z=M$density,col=3,xlab='State (X_t)',ylab='Time (t)',
zlab='Density f(X_t|X_s)',border=NA,shade=0.5,theta=145)
}
#===============================================================================
# Generate the transition density for a diffusion process with restricted domain.
# The diffusion has reflective boundaries at 0 and 1.
#-------------------------------------------------------------------------------
GQD.remove()
G0 <- function(t){0.4*(0.5+sin(2*pi*t))}
G1 <- function(t){-0.4}
Q1 <- function(t){0.25}
Q2 <- function(t){-0.25}
states <- seq(0.005,0.995,1/200)
initial <- 0.5
Tmax <- 5
increment <- 1/50
# Generate the transitional density
M <- GQD.density(Xs=initial,Xt=states,s=0,t=Tmax,delt=increment,
Dtype='Beta',Trunc=c(8,8))
if(require(rgl, quietly = TRUE))
{
open3d(windowRect=c(50,50,640+50,50+640),zoom=0.95)
persp3d(x=M$Xt,y=M$time,z=M$density,col='steelblue',box=FALSE,
xlab='State (X_t)',ylab='Time (t)',zlab='Density f(X_t|X_s)')
play3d(spin3d(axis=c(0,0,1), rpm=3), duration=10)
}else
{
persp(x=M$Xt,y=M$time,z=M$density,col=3,xlab='State (X_t)',ylab='Time (t)',
zlab='Density f(X_t|X_s)',border=NA,shade=0.5,theta=145)
}
#===============================================================================
Run the code above in your browser using DataLab