[R] recursive function help

From: <davidr_at_rhotrading.com>
Date: Tue, 19 Feb 2008 15:07:32 -0600


I'm trying to implement a recursive function using integrate, and I suspect I need a Vectorize somewhere,
but I can't suss it out. Any help would be appreciated. I've tried traceback() and various debugging ideas to no avail (most likely due to my inexperience with these tools.)

Here's what I have.

Nk <- function(m, C) {
  if (length(m) > 1) {
    rho <- C[1, -1]
    Rmat <- C[-1, -1]

    B <- diag(1/sqrt(1 - rho*rho)) %*%
         (-rho %*% t(rho) + Rmat) %*%
         diag(1/sqrt(1 - rho*rho))

    integrate( function(x) dnorm(x) * Nk((m[-1] - rho*x)/sqrt(1 - rho*rho), B), -10, m[1] )$value
  } else {
    pnorm(m[1])
  }
}

my example is
> x2 <- c(0.0781292, -1.6403152)
> sigma2 <- matrix(c(1, -0.5054781, -0.5054781, 1), nrow=2)
> Nk(x2, sigma2)

Error in integrate(function(x) dnorm(x) * Nk((m[-1] - rho * x)/sqrt(1 - :
  non-finite function value

All the pieces work outside of the function, and the integrand is finite as far as I can see.

[Yes, this is a recursive function for multivariate cumulative normal. It seems to match (so far for k=2 without recursion) the existing R functions from packages mvtnorm and mnormt. It is from D. Cassimon, et al. Closed-form valuation of American call options on stocks paying multiple dividends. Finance Research Letters 4 (2007) 33-48.]

Thank you to anyone who can shed some light on this. David L. Reiner, PhD
Rho Trading Securities, LLC



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 19 Feb 2008 - 21:10:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 20 Feb 2008 - 20:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive