[R-SIG-Finance] fCopula and mnormt
Joe W. Byers
ecjbosu at aol.com
Thu Jan 3 17:29:42 CET 2008
>
>
I have found several technical inconsistencies with covariance matrices and
correlation matrices parameters in the functions and documentations.
for example rcopulae.gauss
> print(rcopula.gauss)
function (n, Sigma = equicorr(d, rho), d = 2, rho = 0.7)
{
d <- dim(Sigma)[1]
if (sum(diag(Sigma)) != d)
stop("Sigma should be correlation matrix")
mnorm <- rmnorm(n, Sigma = Sigma)
matrix(pnorm(mnorm), ncol = d)
}
has sigma as a parameter but wants a correlation matrix. rmnorm in mnormt is
the same:
> print(rmnorm)
function (n, Sigma = equicorr(d, rho), mu = rep(0, d), d = 2,
rho = 0.7)
{
d <- dim(Sigma)[1]
A <- t(chol(Sigma))
X <- matrix(rnorm(n * d), nrow = n, ncol = d)
mu.matrix <- matrix(mu, nrow = n, ncol = d, byrow = TRUE)
return(t(A %*% t(X)) + mu.matrix)
}
This inconsistency is really confusing and has caused me stress in determining
if I am actually providing the correct parameter to the function.
rmvnorm in mvtnorm clearly requires a covariance parameter when the default
method is used.
function (n, mean = rep(0, nrow(sigma)), sigma = diag(length(mean)),
method = c("svd", "chol"))
{
if (nrow(sigma) != ncol(sigma)) {
stop("sigma must be a square matrix")
}
if (length(mean) != nrow(sigma)) {
stop("mean and sigma have non-conforming size")
}
method <- match.arg(method)
if (method == "svd") {
ev <- eigen(sigma, sym = TRUE)$values
if (!all(ev >= -sqrt(.Machine$double.eps) * abs(ev[1]))) {
warning("sigma is numerically not positive definite")
}
sigsvd <- svd(sigma)
retval <- t(sigsvd$v %*% (t(sigsvd$u) * sqrt(sigsvd$d)))
}
if (method == "chol") {
retval <- chol(sigma, pivot = T)
o <- order(attr(retval, "pivot"))
retval <- retval[, o]
}
retval <- matrix(rnorm(n * ncol(sigma)), nrow = n) %*% retval
retval <- sweep(retval, 2, mean, "+")
retval
}
<environment: namespace:mvtnorm>
This function is much cleaner, easier to understand thus less confusing because
it differentiates between two generating methods. I only wanted to point this
because package:mnormt is a dependency of many of the Rmetrics packages. I also
may be really off base with this comment, but I was taught to use clear and
consistent parameter names as much as possible.
If this is determined a fix that needs to be done, I would love to assist with
any modifications of the code or documents.
Thank you
Joe
More information about the R-SIG-Finance
mailing list