[R] recursive function help SOLVED (sort of)

davidr at rhotrading.com davidr at rhotrading.com
Wed Feb 20 20:54:06 CET 2008


Well, it turns out to be very simple - just insert a Vectorize between
integrate and function(x).
However, the special cases where C[i,j]==1 make the actual code quite
messy.
It matches pmvnorm {mvtnorm} and pmnorm {mnormt} quite well.
And the recursive method is incredibly slow for higher dimensions.
And still some cases blow up or don't converge.
So, never mind. It looked clever, but I would recommend pmvnorm for
speed and accuracy, 
even though it is non-deterministic for higher dimensions.
One little note: for the bivariate, this method (without recursion) is
as accurate as the existing methods
and a bit faster than pmvnorm.

-- David


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On Behalf Of davidr at rhotrading.com
Sent: Tuesday, February 19, 2008 3:08 PM
To: r-help at r-project.org
Subject: [R] recursive function help

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.



More information about the R-help mailing list