| nearPD {Matrix} | R Documentation |
Computes the nearest positive definite matrix to an approximate one, typically a correlation or variance-covariance matrix.
nearPD(x, corr = FALSE,
eig.tol = 1e-06, conv.tol = 1e-07, posd.tol = 1e-08,
do2eigen = TRUE, maxit = 100, verbose = FALSE)
x |
numeric n * n approximately positive definite matrix, typically an approximation to a correlation or covariance matrix. |
corr |
logical indicating if the matrix should be a correlation matrix. |
eig.tol |
defines relative positiveness of eigenvalues compared to largest one. |
conv.tol |
convergence tolerance for algorithm. |
posd.tol |
tolerance for enforcing positive definiteness. |
do2eigen |
logical indicating if a
posdefify() eigen step should be applied to
the result of the Hingham algorithm. |
maxit |
maximum number of iterations allowed. |
verbose |
logical; if TRUE the iterations are monitored by
print out. |
Note that setting corr = TRUE just sets diag(.) <- 1
within the algorithm.
an S3 object of class "nearPD", basically a list
with components
mat |
a matrix of class dpoMatrix, the
computed positive-definite matrix. |
corr |
logical, just the argument corr. |
normF |
the Frobenius norm (norm(x-X, "F")) of the
difference between the original and the resulting matrix. |
iterations |
number of iterations needed. |
converged |
logical indicating if iterations converged. |
Jens Oehlschlaegel donated a first version. Subsequent changes by the Matrix package authors.
Cheng, Sheung Hun and Higham, Nick (1998) A Modified Cholesky Algorithm Based on a Symmetric Indefinite Factorization; SIAM J. Matrix Anal. Appl., 19, 1097–1110.
Highham (2002) Computing the nearest correlation matrix - a problem from finance; IMA Journal of Numerical Analysis 22, 329–343.
More simple versions with a similar purpose by
posdefify().
set.seed(27)
m <- matrix(round(rnorm(25),2), 5, 5)
m <- m + t(m)
diag(m) <- pmax(0, diag(m)) + 1
(m <- round(cov2cor(m), 2))
str(near.m <- nearPD(m))
round(near.m$mat, 2)
norm(m - near.m$mat) # 1.102
if(require("sfsmisc")) {
m2 <- posdefify(m) # a simpler approach
norm(m - m2) # 1.185, i.e., slightly "less near"
}