[R] recursive function help

davidr at rhotrading.com davidr at rhotrading.com
Tue Feb 19 22:07:32 CET 2008


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



More information about the R-help mailing list