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. ----- analyze and live life -- View this message in context: http://www.nabble.com/help-for-code-in-jump-diffusion-tp20633880p20633880.html Sent from the R help mailing list archive at Nabble.com.
i tried to run your code, this is how/why you got NaN:> mu<-0.2 > sig<-0.2 > S0<-100 > j<-0.2 > dt<-1/252 > int<-0.1 > i<-0 > is.nanfunction (x) .Primitive("is.nan")> k<-rnorm(1,0,1) > k[1] 0.3214954> theta<-ifelse((k<(int*dt)),1,0) > theta[1] 0> m<-rnorm(1) > m[1] -0.4525731> gam<-qnorm(m,0,1)Warning message: In qnorm(p, mean, sd, lower.tail, log.p) : NaNs produced> gam[1] NaN> S0<-abs(S0*((1+mu*dt+sig*sqrt(dt)+ gam)- j*theta)) > S0[1] NaN> if(!is.nan (S0 <= 0))+ warning("S0 must be positive") Warning message: S0 must be positive> cat("NaN","\n")NaN> cat(S0,"\n")NaN>On Sat, Nov 22, 2008 at 2:37 PM, subbudas <subratdas0407sssu at gmail.com> 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. > > >
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