Compute the nearest positive definite matrix to an approximate one, typically a correlation or variancecovariance matrix.
1 2 3 
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(k1))). 
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 positivedefinite 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, 10971110.
Knol, D.L. and ten Berge, J.M.F. 1989. Leastsquares approximation of an improper correlation matrix by a proper one. Psychometrika, 54, 5361.
Higham, N. 2002. Computing the nearest correlation matrix  a problem from finance. IMA Journal of Numerical Analysis, 22, 329343.
1 2 3 4 5 6 7 8 9 10 11 12  #Simulated nonpositive definite (PD) matrix
nonPD < matrix(c(2.04e03, 3.54e05, 7.52e03, 3.54e05, 6.15e07,
1.30e04, 7.52e03, 1.30e04, 2.76e02), 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.

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.
Please suggest features or report bugs with the GitHub issue tracker.
All documentation is copyright its authors; we didn't write any of that.