Learn R Programming

lmf (version 1.2)

nearPD: Find nearest positive definite matrix

Description

Compute the nearest positive definite matrix to an approximate one, typically a correlation or variance-covariance matrix.

Usage

nearPD(x, corr = FALSE, keepDiag = FALSE, do2eigen = TRUE, doSym = FALSE, doDykstra = TRUE, only.values = FALSE, only.matrix = TRUE, eig.tol = 1e-06, conv.tol = 1e-07, posd.tol = 1e-08, maxit = 100, trace = FALSE)

Arguments

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.
keepDiag
logical, generalizing corr: if TRUE, the resulting matrix should have the same diagonal (diag(x)) as the input matrix.
do2eigen
logical indicating if a posdefify eigen step should be applied to the result of the Higham algorithm.
doSym
logical indicating if X <- (X + t(X))/2 should be done, after X <- tcrossprod(Qd, Q). Some doubt if this is necessary.
doDykstra
logical indicating if Dykstra's correction should be used; true by default. If false, the algorithm is basically the direct fixpoint iteration $Y(k) = P_U(P_S(Y(k-1)))$.
only.values
logical; if TRUE, the result is just the vector of eigen values of the approximating matrix.
only.matrix
logical indicating if only the matrix should be returned.
eig.tol
defines relative positiveness of eigenvalues compared to largest one, $\lambda_1$. Eigen values $\lambda_k$ are treated as if zero when $\lambda_k / \lambda_1 = eig.tol$.
conv.tol
convergence tolerance for Higham algorithm.
posd.tol
tolerance for enforcing positive definiteness (in the final posdefify step when do2eigen is TRUE).
maxit
maximum number of iterations allowed.
trace
logical or integer specifying if convergence monitoring should be traced.

Value

nearPD returns a numeric vector of eigen values of the approximating matrix if only.values = TRUE, returns the computed positive definite matrix if only.matrix = TRUE and else returns a list with the following componets:
mat
matrix of class "dpoMatrix", the computed positive-definite matrix.
eigenvalues
numeric vector of eigenvalues of mat.
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.

Details

This function is identical to nearPD in package Matrix as far as the algorithmic method is concerned, but has an addition of the argument only.matrix to ease its application within the function fs, has lost the argument ensureSymmetry and have a small change in the list returned when only.matrix = FALSE.

Please see nearPD in package Matrix for further details.

References

Cheng, S.H. and Higham, N. 1998. A Modified Cholesky Algorithm Based on a Symmetric Indefinite Factorization. SIAM Journal on Matrix Analysis and Applications, 19, 1097-1110.

Knol, D.L. and ten Berge, J.M.F. 1989. Least-squares approximation of an improper correlation matrix by a proper one. Psychometrika, 54, 53-61.

Higham, N. 2002. Computing the nearest correlation matrix - a problem from finance. IMA Journal of Numerical Analysis, 22, 329-343.

See Also

fs, lmf, nearPD, posdefify

Examples

Run this code
#Simulated non-positive definite (PD) matrix
nonPD <- matrix(c(2.04e-03, 3.54e-05, 7.52e-03, 3.54e-05, 6.15e-07,
  1.30e-04, 7.52e-03, 1.30e-04, 2.76e-02), ncol = 3)
#View eigenvalues (PD = only positive eigenvalues)
eigen(nonPD)
#Calculate PD matrix
PD <- nearPD(nonPD, only.matrix = TRUE)
PD
#View eigenvalues
eigen(PD)
#More thorough examples are given in the help pages for nearPD
 #in the Matrix package.

Run the code above in your browser using DataLab