Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cov(u,dims=2) not cov(u,u,dims=2) #269

Closed
odunbar opened this issue Mar 23, 2023 · 0 comments · Fixed by #270
Closed

Use cov(u,dims=2) not cov(u,u,dims=2) #269

odunbar opened this issue Mar 23, 2023 · 0 comments · Fixed by #270

Comments

@odunbar
Copy link
Collaborator

odunbar commented Mar 23, 2023

return cov(u, u, dims = 2)

In some examples (e.g. with additive inflation) I have encountered non-positive empirical covariance matrices from calls such as the above.

Even though this should be equal to cov(u,dims=2) in some instances of u the call to cov(u,u,dims=2) produces order(machine precision) asymmetry, and thus isposdef(cov(u,u,dims=2))=false. meanwhile, cov(u,dims=2) seems to always return symmetric matrices and is a safer option as isposdef(cov(u,dims=2))=true.

e.g.

julia> y = rand( MvNormal(I(5)),10);

julia> cyy = cov(y,y,dims=2);

julia> cy = cov(y,dims=2);

julia> cyy - permutedims(cyy,(2,1))
5×5 Matrix{Float64}:
 0.0           0.0          0.0           0.0          -1.38778e-17
 0.0           0.0          0.0           0.0           2.77556e-17
 0.0           0.0          0.0           0.0          -5.55112e-17
 0.0           0.0          0.0           0.0           2.77556e-17
 1.38778e-17  -2.77556e-17  5.55112e-17  -2.77556e-17   0.0

julia> cy - permutedims(cy,(2,1))
5×5 Matrix{Float64}:
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0
 0.0  0.0  0.0  0.0  0.0

And so the importantly, cyy fails the checks:

julia> cy  cyy
true

julia> isposdef(cy)
true

julia> isposdef(cyy)
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant