nearPD | R Documentation |
Compute the nearest positive definite matrix to an approximate one, typically a correlation or variance-covariance matrix.
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)
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 |
do2eigen |
logical indicating if a |
doSym |
logical indicating if |
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 |
only.matrix |
logical indicating if only the matrix should be returned. |
eig.tol |
defines relative positiveness of eigenvalues compared to largest one, λ_1. Eigen values λ_k are treated as if zero when λ_k / λ_1 = eig.tol. |
conv.tol |
convergence tolerance for Higham algorithm. |
posd.tol |
tolerance for enforcing positive definiteness (in the
final |
maxit |
maximum number of iterations allowed. |
trace |
logical or integer specifying if convergence monitoring should be traced. |
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.
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 |
corr |
logical, just the argument |
normF |
the Frobenius norm ( |
iterations |
number of iterations needed. |
converged |
logical indicating if iterations converged. |
Jens Oehlschlaegel donated a first version. Subsequent changes by the Matrix package authors and present modifications by Thomas Kvalnes.
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.
fs
, lmf
, nearPD
,
posdefify
#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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.