# Numerical issues causing wrong determinant values

**URL:** https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711
**Category:** General Usage
**Created:** [April 30, 2019, 7:27pm UTC](https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711 "2019-04-30T19:27:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mr.Robot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mr.robot/32/8052_2.png) [@Mr.Robot](https://discourse.julialang.org/u/Mr.Robot)
#### Post date: [April 30, 2019, 7:27pm UTC](https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711/1 "2019-04-30T19:27:49Z")

</div>

## Problem

I am practicing writing efficient Julia code that could (hopefully) comparable with the function provided in libraries in term of speed. Specifically, I am trying to write `logpdf` function, which returns the log probability of given input.

However, when I evaluate the determinant of random vector’s covariance, i.e. \Sigma=\sigma\_0^2 \mathbf{I} + \sigma\_1^2 \mathbf{ZZ}^T where \mathbf{Z} \in \mathbb{R}^{n\times q}. The determinant is 0.

I think this is due to the matrix is not well-conditioned, but I am not sure how to resolve this issue.

```julia
using Random, LinearAlgebra

Random.seed!(280)
n, q = 1000, 10
Z = randn(n, q)
σ0, σ1 = 0.5, 2.0
Σ = σ1^2 * Z * Z' + σ0^2 * I
det(Σ)

```

Could anyone help me, thank you in advance.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [April 30, 2019, 8:00pm UTC](https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711/2 "2019-04-30T20:00:13Z")

</div>

Not sure if this is relavant to your actual problem (i.e. real input data) but just by the look at your generated data, I expect the matrix to mostly have eigenvalues around `σ0^2` which is `0.25`. This means that the determinant should be around `0.25^1000` which is simply not representable by a 64bit floating point number.

Edit: Oh, and forgot to mention that I got non-zero results when I set `σ0` to `1`. (without seeding the RNG)

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [April 30, 2019, 8:12pm UTC](https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711/3 "2019-04-30T20:12:33Z")

</div>

Try using `logdet` instead.

---

<div class="post-metadata">

### Author: ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)
#### Post date: [April 30, 2019, 8:14pm UTC](https://discourse.julialang.org/t/numerical-issues-causing-wrong-determinant-values/23711/4 "2019-04-30T20:14:46Z")

</div>

Also, for this specific case you can compute it much more efficiently using the [Matrix determinant lemma](https://en.wikipedia.org/wiki/Matrix_determinant_lemma). You can either do this directly, or make use of the WoodburyMatrices.jl package:  
[https://github.com/timholy/WoodburyMatrices.jl](https://github.com/timholy/WoodburyMatrices.jl)
