#=========================================================================
# Generate the first passage time density of a CIR process with time
# dependant drift to a fixed barrier.
#-------------------------------------------------------------------------
# Remove any existing coefficients.
GQD.remove()
# Define the coefficients of the process.
G0 <- function(t){10+0.5*sin(2*pi*t)}
G1 <- function(t){-1}
Q1 <- function(t){0.25}
#Define the parameters of the first passage time problem.
delt <- 1/100 # The stepsize for the solution
X0 <- 8 # The initial value of the process
BB <- 11 # Fixed barrier
T0 <- 2 # Starting time of the diffusion
TT <- 10 # Time horizon of the computation
# Run the calculation
res1 <- GQD.TIpassage(X0,BB,T0,TT,delt)
# Remove any existing coefficients.
GQD.remove()
# Redefine the coefficients.
G0 <- function(t){ 0.1*(10+0.2*sin(2*pi*t)+0.3*prod(sqrt(t),1+cos(3*pi*t)))}
G1 <- function(t){-0.1*(1+0.2*sin(2*pi*t))}
Q1 <- function(t){0.25}
# Redefine the parameters of the f.p.t. problem.
delt <- 1/100
X0 <- 8
BB <- 11
T0 <- 1
TT <- 10
# Run the calculation
res2 <- GQD.TIpassage(X0,BB,T0,TT,delt)
#===============================================================================
# Plot the two solutions.
#===============================================================================
expr1 <- expression(dX[t]==(10+0.5*sin(2*pi*t)-X[t])*dt+0.25*sqrt(X[t])*dW[t])
expr2 <- expression(dX[t]==(0.1*(10+0.2*sin(2*pi*t)+0.3*sqrt(t)*(1+cos(3*pi*t))
-0.1*(1+0.2*sin(2*pi*t))*X[t])*dt+0.25*sqrt(X[t])*dW[t]))
par(mfrow=c(1,1))
plot(res1$density~res1$time,type='l',col='blue',
ylab='Density',xlab='Time',main=expr1,cex.main=0.95)
plot(res2$density~res2$time,type='l',col='red',
ylab='Density',xlab='Time',main =expr2,cex.main=0.95)
#===============================================================================
# Let's see how sensitive the first passage density is w.r.t a speed parameter
# of a non-linear diffusion.
#===============================================================================
GQD.remove()
# Redefine the coefficients with a parameter theta:
G1 <- function(t){theta[1]*(10+0.2*sin(2*pi*t)+0.3*prod(sqrt(t),1+cos(3*pi*t)))}
G2 <- function(t){-theta[1]}
Q2 <- function(t){0.1}
# Now just give a value for the parameter in the standard fashion:
res3=GQD.TIpassage(8,12,1,4,1/100,theta=c(0.5))
plot(res3$density~res3$time,type='l',col=2,ylim=c(0,1.0),
main='First Passage Time Density',ylab='Density',xlab='Time',cex.main=0.95)
# Change the parameter and see the effect on the f.p.t. density.
th.seq=seq(0.1,0.5,1/20)
for(i in 2:length(th.seq))
{
res3=GQD.TIpassage(8,12,1,4,1/100,,theta=c(th.seq[i]))
lines(res3$density~res3$time,type='l',col=rainbow(10)[i])
}
lines(res3$density~res3$time,type='l',col=rainbow(10)[i],lwd=2)
legend('topright',legend=th.seq,col=rainbow(10),lty='solid',cex=0.75,
title=expression(theta[1]))
#===============================================================================Run the code above in your browser using DataLab