Skip to content

PDMat error when sampling from prior of model with LKJCholesky #2316

Open
@simonsteiger

Description

@simonsteiger

Hi,

I have encountered an error while trying to sample from the prior of a Turing model with an LKJCholesky prior. The error seems to stem from PDMats calling oneunit(Any) at some point.

The code example below follows this blog post.

using Distributions, Turing, LinearAlgebra, PDMats

M = rand(MvNormal(zeros(3), I), 30)

@model function test(X, y)
    predictors, _ = size(X)
	
    Ω ~ LKJCholesky(predictors, 2.0)
    σ ~ Exponential(1)
    τ ~ filldist(truncated(Cauchy(0, 2); lower=0), predictors)
    γ ~ filldist(Normal(0, 2), predictors)

    Σ_L = Diagonal(τ) * Ω.L
    Σ = PDMat(Cholesky(Σ_L + 1e-6 * I))
    β = Σ * γ

    return y ~ arraydist([Normal(X[:, i]  β, σ) for i in 1:length(y)])
end

m1 = test(M[:, 2:end], M[:, 1])

sample(m1, Prior(), 1000)

The error raised after the last line is

MethodError: no method matching oneunit(::Type{Any})

with PDMat somewhere further down in the stack trace

PDMats.PDMat(fac::LinearAlgebra.Cholesky{Any, Matrix{Any}}) @ pdmat.jl:20

Swapping out the above model for one where Ω is distributed as LKJ no longer raises an error during prior sampling.

@model function test2(X, y)

    predictors, n = size(X)
	
    Ω ~ LKJ(predictors, 2.0)
    σ ~ Exponential(1)
    τ ~ filldist(truncated(Cauchy(0, 2); lower=0), predictors)
    γ ~ filldist(Normal(0, 2), predictors)

    Σ = Diagonal(τ) * Ω * Diagonal(τ)
    β = Σ * γ

    return y ~ arraydist([Normal(X[:, i]  β, σ) for i in 1:length(y)])
end

Sampling with sample(m1, NUTS(), 1000) works normally.

EDIT
I ran the above examples in Pluto with Distributions v0.25.111, Turing v0.33.3, and PDMats v0.11.31.

When running them in the REPL (Turing v0.28.3 here) I could sample from the prior of m1 without error.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions