# PosDefException, increasing loss of precision

**URL:** <https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809>\
**Category:** General Usage\
**Created:** [March 13, 2019, 8:00am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809 "2019-03-13T08:00:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 13, 2019, 8:00am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809/1 "2019-03-13T08:00:30Z")

</div>

I’m trying to construct an mcmc sampler for a HMM model which has a Bayesian Linear Regression component. However, as the mcmc iteration progresses, i quickly run into PosDefException errors when trying to simulate a multivariate normal or inverse wishart. Example of error:  
`ERROR: PosDefException: matrix is not positive definite; Cholesky factorization failed.`

Checks on my some of the matrices causing the error reveal that with each update, the values of the matrices get larger and larger, in what i think leads to increasing error in numerical precision. Hence, the minimum eigenvalues of the matrices also increases, up to the point that the tolerance is no longer acceptable for generating multivariate normal or inverse wishart random variables.

Any idea how I can circumvent this problem? for example by perturbing the matrices such that it is still representative enough and also within tolerance. Another possibility is that there is a bug in my algorithm leading to the big numbers and consequential loss of precision, but I wonder if there is a way to deal with the `PosDefException` issue as other bugs in the code are not apparent to me now.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [March 13, 2019, 8:11am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809/2 "2019-03-13T08:11:10Z")

</div>

If you know the matrix is (analytically) Hermitian you can pass `Hermitian(A)` to `cholesky` instead, which ignores the upper/lower part of the matrix and assumes it is Hermitian.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 13, 2019, 8:22am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809/3 "2019-03-13T08:22:26Z")

</div>

thanks @fredrikekre, i’ve actually done that but was still getting the errors. Although analytically i believe that the matrices are positive definite, as they should be for a Bayesian Multivariate Linear Regression. Any thoughts?

P.S. I’ve managed to find an unrelated bug that is causing the elements of the matrices to grow very fast and avoided the error.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [March 13, 2019, 8:31am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809/4 "2019-03-13T08:31:08Z")

</div>

> [@krenova](#):
>
> Any idea how I can circumvent this problem?

It is unclear what is happening without code, especially the construction of this matrix. Ideally, it would be _PD by construction_, using a suitable transformation, then as @fredrikekre suggested you can just wrap it in a `Hermitian`.

However, if you can decompose the PSD matrix into a variance and a Cholesky factor of the correlations, you can also use [`TransformVariables.CorrCholeskyFactor`](https://tpapp.github.io/TransformVariables.jl/dev/#TransformVariables.CorrCholeskyFactor).

> [@krenova](#):
>
> with each update, the values of the matrices get larger and larger

If you are doing MCMC, this could be a warning sign for an ill-defined problem, eg an improper posterior.

---

<div class="post-metadata">

**Author:** ![krenova](https://avatars.discourse-cdn.com/v4/letter/k/53a042/32.png) [@krenova](https://discourse.julialang.org/u/krenova)\
**Post date:** [March 13, 2019, 8:42am UTC](https://discourse.julialang.org/t/posdefexception-increasing-loss-of-precision/21809/5 "2019-03-13T08:42:37Z")

</div>

thanks for your tips again @Tamas_Papp. I should definitely look into my model specifications again.
