# \[Solved\] Determinant and random matrix generation

**URL:** <https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975>\
**Category:** Performance\
**Tags:** linearalgebra\
**Created:** [February 19, 2019, 2:14pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975 "2019-02-19T14:14:39Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gdestouet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdestouet/32/6175_2.png) [@gdestouet](https://discourse.julialang.org/u/gdestouet)\
**Post date:** [February 19, 2019, 2:14pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/1 "2019-02-19T14:14:39Z")

</div>

Hello,  
I was playing with random matrices when I noticed this strange behaviour :

```julia
julia> using LinearAlgebra

julia> a=randn(10,11);

julia> isposdef(a*a')
true

julia> isposdef(a'*a)
false

```

I expected to have either way positive definite matrices since both matrices can be seen as covariance matrices of random data.

Am I doing something wrong here ?

---

<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:** [February 19, 2019, 2:20pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/2 "2019-02-19T14:20:34Z")

</div>

> [@gdestouet](#):
>
> I expected to have either way positive definite matrices since both matrices can be seen as covariance matrices of random data.

Strictly speaking, this is not correct, since they can have zero eigenvalues. Eg

```julia
julia> isposdef(zeros(3,3) |> x -> x'*x)
false

```

What you are seeing is probably near-0, possibly negative, eigenvalues as a result of floating point error, which is normal.

Normally, reproducing the exact matrix would be needed to help investigate this issue, but in this case, one can easily generate examples with

```julia
using LinearAlgebra
global x
for _ in 1:1000
    a = randn(10, 11);
    if !isposdef(a'*a)
        @info "found one"
        global x = a
        break
    end
end
eigen(x'*x) # observe the eigenvalues

```

---

<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:** [February 19, 2019, 2:22pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/3 "2019-02-19T14:22:17Z")

</div>

a’a has 0 as eigenvalue. (It is at most rank 10)

---

<div class="post-metadata">

**Author:** ![gdestouet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdestouet/32/6175_2.png) [@gdestouet](https://discourse.julialang.org/u/gdestouet)\
**Post date:** [February 19, 2019, 2:28pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/4 "2019-02-19T14:28:06Z")

</div>

But it seems that the smallest dimension is favored

```julia
julia> using Statistics

julia> mean([isposdef(randn(11,10) |> x->x*x') for i in 1:1000])
0.463

julia> mean([isposdef(randn(10,11) |> x->x*x') for i in 1:1000])
1.0

```

---

<div class="post-metadata">

**Author:** ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)\
**Post date:** [February 19, 2019, 2:33pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/5 "2019-02-19T14:33:23Z")

</div>

Think of it this way: if a = [1;0;0], a\*a’ = diag(1,0,0), which is not positive definite (but is semidefinite). However, a’a = 1, which is positive definite. In general, if a is N x M, then a a’ has rank at most M.

---

<div class="post-metadata">

**Author:** ![gdestouet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdestouet/32/6175_2.png) [@gdestouet](https://discourse.julialang.org/u/gdestouet)\
**Post date:** [February 19, 2019, 2:37pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/6 "2019-02-19T14:37:37Z")

</div>

Thanks ! I forgot about that 🤐

---

<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:** [February 19, 2019, 2:45pm UTC](https://discourse.julialang.org/t/solved-determinant-and-random-matrix-generation/20975/7 "2019-02-19T14:45:48Z")

</div>

And numerical error is not how you get `!isposdef`. In fact, it’s the other way around. The reason you get positive definate `a'a` at all is due to numerical error shifting the `0` eigen value to the positive side…
