[R] help for code in jump diffusion

Bernardo Rangel Tura tura at centroin.com.br
Sat Nov 22 09:20:55 CET 2008


On Fri, 2008-11-21 at 22:37 -0800, subbudas wrote:
> hello everyone ,
> i have written some code in R for jump diffusion model.
> the code generates answer as 
> " NaN 
> There were 50 or more warnings (use warnings() to see the first 50)"
> my code is 
> 
> mu<-0.2
> sig<-0.2
> S0<-100
> j<-0.2
> dt<-1/252
> int<-0.1
> i<-0
> while(i<=1)
> {
>   is.nan
>   k<-rnorm(1,0,1)
>   theta<-ifelse((k<(int*dt)),1,0)
>   m<-rnorm(1)
>   gam<-qnorm(m,0,1)
>   S0<-abs(S0*((1+mu*dt+sig*sqrt(dt)+ gam)- j*theta))
>   if(!is.nan (S0 <= 0))
>      warning("S0 must be positive")
>      cat("NaN","\n")
>   cat(S0,"\n")
>   i<-i+(1/252)
> }
> 
> the problem i am facing is i am not able to find out the reason for this NaN
> output.
> please help 
> 
> thanks in advance.


Hi 

I see two problems in your script:

1- m<-rnorm(1) will produce a random number with distribution normal
mean =0 and sd=1 so m can >1 or <0
In this cases gam<-qnorm(m,0,1) is NAN because m is not a probability

2- I think  if(!is.nan (S0 <= 0)) ...  is wrong 


Try this script :


mu<-0.2
sig<-0.2
S0<-100
j<-0.2
dt<-1/252
int<-0.1
i<-0
while(i<=1){
	k<-rnorm(1,0,1)
	theta<-ifelse((k<(int*dt)),1,0)
#	m<-rnorm(1)
	m<-runif(1,0,1)
	gam<-qnorm(m,0,1)
	S0<-abs(S0*((1+mu*dt+sig*sqrt(dt)+ gam)- j*theta))
	if(!is.nan(S0)&&(S0 <= 0)){
		warning("S0 must be positive")
		cat("NaN","\n")
	}
	cat(S0,"\n")
	i<-i+(1/252)
}

-- 
Bernardo Rangel Tura, M.D,MPH,Ph.D
National Institute of Cardiology
Brazil



More information about the R-help mailing list