\HeaderA{posdef.approx}{Find the Nearest Positive Definite Matrix}{posdef.approx}
\keyword{multivariate}{posdef.approx}
\begin{Description}\relax
Check if the matrix is positive definite via attempted
Cholesky decomposition and inversion.  If not, then the
\code{\LinkA{sechol}{sechol}} function is used to obtain the nearest
approximate Cholesky decomposition from which to recover an
approximate covariance matrix which is positive definite.
If that doesn't do the trick, then \code{.Machine\$double.eps} is
added to the diagonal (as a last-ditch effort) until a positive
definite matrix can be returned
\end{Description}
\begin{Usage}
\begin{verbatim}
posdef.approx(S, name = "S", quiet=FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{S}] square \code{matrix} which is supposed to be positive
definite 
\item[\code{name}] optional name of the matrix -- used for printing
errors and warnings
\item[\code{quiet}] whether or not to print a \code{warning} when a
non-positive definite matrix is found 
\end{ldescription}
\end{Arguments}
\begin{Value}
Returns a \code{matrix} which is positive definite:
either the original matrix, or a nearby positive definite
approximation
\end{Value}
\begin{Author}\relax
Robert B. Gramacy \email{bobby@statslab.cam.ac.uk}
\end{Author}
\begin{References}\relax
Micah Altman, Jeff Gill and Michael McDonald (2003).
\emph{Numerical Issues in Statistical Computing for the Social
Scientist.} John Wiley and Sons, New York.\\
\url{http://www.hmdc.harvard.edu/micah\_altman/numal/}

\url{http://www.statslab.cam.ac.uk/~bobby/monomvn.html}
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{sechol}{sechol}} in the \pkg{accuracy} package
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
## most likely generates a matrix which is close to positive definite
## but not quite
s <- matrix(rnorm(100), ncol=5)
S <- (t(s) %*% s) + matrix(rnorm(25, sd=10), ncol=5)
S

## a correction, quiet=TRUE so there is no warning printed
posdef.approx(S, quiet=TRUE)
\end{ExampleCode}
\end{Examples}

