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

Problems with Cholesky in MvNormal using a g prior #2157

Closed
gregorsteiner opened this issue Jan 23, 2024 · 2 comments
Closed

Problems with Cholesky in MvNormal using a g prior #2157

gregorsteiner opened this issue Jan 23, 2024 · 2 comments

Comments

@gregorsteiner
Copy link

Hello,

I am trying to implement a simple Bayesian Model Averaging procedure. When I use a g prior on the coefficient vector in a linear model, I keep getting this error: PosDefException: matrix is not Hermitian; Cholesky factorization failed.

When I wrap the covariance matrix with Symmetric() or Hermitian(), I get this error instead: PosDefException: matrix is not positive definite; Cholesky factorization failed. However, when manually checking the eigenvalues, they are all positive.

The solutions suggested in here or here did not help. A similar problem has been solved here, but I am not sure how to use that solution within a Turing model.

My code is below, any help would be highly appreciated. Thank you!

using Random, Turing, LinearAlgebra
using MCMCChains, Plots, StatsPlots
using SpecialFunctions, Optim

# simulate data
n = 50
p = 10
beta = zeros(p)
beta[1:5] .= 1

Random.seed!(42)
X = rand(MvNormal(zeros(p), 10 * I), n)'
y = 10 .+ X * beta + rand(Normal(0, 2), n)

# create model
@model function linmod(y, X)
    p = size(X, 2)
    n = size(X, 1)
    g = min(n, p^2)

    # priors 
    σ² ~ Exponential(10)
    α ~ Normal(0, 100)

    Σ = σ² * g * inv(X'X)
    β ~ MvNormal(zeros(p), Σ)

    # model
    y ~ MvNormal(α .+ X*β, σ² * I)
    
end

model = linmod(y, X)
optimize(model, MAP())
@torfjelde
Copy link
Member

Sorry, I just saw this. Did you manage to address the issue?

@gregorsteiner
Copy link
Author

No worries, I managed to fix the issue in this case by wrapping the covariance matrix in PDMat(Symmetric( )) , but I have encountered a few versions of this problem. I should have probably posted this in Distributions.jl or StaticArrays.jl, but it seems that they are already aware of this issue: JuliaStats/Distributions.jl#1826 and JuliaArrays/StaticArrays.jl#1218. Many thanks!

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

No branches or pull requests

2 participants