Turing.jl icon indicating copy to clipboard operation
Turing.jl copied to clipboard

Gibbs with closed-form conditionals

Open Red-Portal opened this issue 7 months ago • 4 comments

The new version for the Gibbs sampler is missing a feature that was present in the previous version. It would be nice to implement an equivalent (or improved!) feature.

Interface

GibbsConditional(sym, conditional)

A "pseudo-sampler" to manually provide analytical Gibbs conditionals to Gibbs. GibbsConditional(:x, cond) will sample the variable x according to the conditional cond, which must therefore be a function from a NamedTuple of the conditioned variables to a Distribution.

Examples

α_0 = 2.0
θ_0 = inv(3.0)
x = [1.5, 2.0]
N = length(x)

@model function inverse_gdemo(x)
    λ ~ Gamma(α_0, θ_0)
    σ = sqrt(1 / λ)
    m ~ Normal(0, σ)
    @. x ~ \$(Normal(m, σ))
end

# The conditionals can be formulated in terms of the following statistics:
x_bar = mean(x) # sample mean
s2 = var(x; mean=x_bar, corrected=false) # sample variance
m_n = N * x_bar / (N + 1)

function cond_m(c)
    λ_n = c.λ * (N + 1)
    σ_n = sqrt(1 / λ_n)
    return Normal(m_n, σ_n)
end

function cond_λ(c)
    α_n = α_0 + (N - 1) / 2 + 1
    β_n = s2 * N / 2 + c.m^2 / 2 + inv(θ_0)
    return Gamma(α_n, inv(β_n))
end

m = inverse_gdemo(x)

sample(m, Gibbs(GibbsConditional(:λ, cond_λ), GibbsConditional(:m, cond_m)), 10)

Red-Portal avatar May 08 '25 22:05 Red-Portal

I frankly don't know why this got removed at the Gibbs makeover, might have been me being careless. Should be easy to add back.

mhauru avatar May 09 '25 08:05 mhauru

This is another place where NamedTuple won't quite cut it, unless we restrict ourselves to identity-optic-VarNames...

https://github.com/TuringLang/DynamicPPL.jl/issues/900

penelopeysm avatar May 10 '25 22:05 penelopeysm

@mhauru / @AoifeHughes, can you help take a look after you finish ongoing PRs?

yebai avatar Jun 04 '25 10:06 yebai

Yep, I'm happy to add this to my list

AoifeHughes avatar Jun 04 '25 10:06 AoifeHughes

Somebody asked about this feature on slack recently. How are we making progress on this?

Red-Portal avatar Aug 21 '25 14:08 Red-Portal

Closed by #2647

mhauru avatar Nov 21 '25 18:11 mhauru