Skip to content
Open
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c0158ea
Add GibbsConditional sampler and corresponding tests
Aug 7, 2025
a972b5a
clarified comment
Aug 7, 2025
bdb7f73
Merge branch 'main' into gibbs-sampler
mhauru Aug 7, 2025
c3cc773
add MHs suggestions
Aug 8, 2025
714c1e8
formatter
Aug 8, 2025
97c571d
Merge branch 'gibbs-sampler' of github.com:TuringLang/Turing.jl into …
Aug 8, 2025
94b723d
fixed exporting thing
Aug 8, 2025
891ac14
Merge branch 'main' into gibbs-sampler
AoifeHughes Aug 18, 2025
2058ae5
Refactor Gibbs sampler to use inverse of parameters for Gamma distrib…
Sep 23, 2025
b0812a3
removed file added by mistake
Sep 25, 2025
d910312
Add safety checks and error handling in find_global_varinfo and Abstr…
Sep 29, 2025
4b1dc2f
imports?
Oct 9, 2025
1e84309
Merge remote-tracking branch 'origin/main' into gibbs-sampler
mhauru Nov 17, 2025
a33d8a9
Fixes and improvements for GibbsConditional
mhauru Nov 17, 2025
f41fc6e
Move GibbsConditional tests to their own file
mhauru Nov 17, 2025
bd5ff0b
More GibbsConditional tests
mhauru Nov 17, 2025
34acad7
Bump patch version to 0.41.2, add HISTORY.md entry
mhauru Nov 17, 2025
4786a59
Remove spurious change
mhauru Nov 17, 2025
8951d98
Code style and documentation
mhauru Nov 18, 2025
45ab5f8
Add one test_throws, tweak test thresholds and dimensions
mhauru Nov 18, 2025
805bc60
Apply suggestions from code review
mhauru Nov 18, 2025
d0c3cf4
Add links for where to get analytical posteriors
mhauru Nov 18, 2025
98f4213
Update TODO note
mhauru Nov 18, 2025
c74b0a0
Fix a GibbsConditional bug, add a test
mhauru Nov 18, 2025
4a7d08c
Set seeds better
mhauru Nov 18, 2025
744d254
Use getvalue in docstring
mhauru Nov 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 40 additions & 112 deletions src/mcmc/gibbs_conditional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,48 @@ chain = sample(model, Gibbs(
), 1000)
```
"""
struct GibbsConditional{S,C} <: InferenceAlgorithm
struct GibbsConditional{C} <: InferenceAlgorithm
conditional::C

function GibbsConditional(sym::Symbol, conditional::C) where {C}
return new{sym,C}(conditional)
return new{C}(conditional)
end
end

# Mark GibbsConditional as a valid Gibbs component
isgibbscomponent(::GibbsConditional) = true

# Required methods for Gibbs constructor
Base.length(::GibbsConditional) = 1 # Each GibbsConditional handles one variable

"""
find_global_varinfo(context, fallback_vi)

Traverse the context stack to find global variable information from
GibbsContext, ConditionContext, FixedContext, etc.
"""
function find_global_varinfo(context, fallback_vi)
# Start with the given context and traverse down
current_context = context

while current_context !== nothing
if current_context isa GibbsContext
# Found GibbsContext, return its global varinfo
return get_global_varinfo(current_context)
elseif hasproperty(current_context, :childcontext) &&
isdefined(DynamicPPL, :childcontext)
# Move to child context if it exists
current_context = DynamicPPL.childcontext(current_context)
else
# No more child contexts
break
end
end

# If no GibbsContext found, use the fallback
return fallback_vi
end

"""
DynamicPPL.initialstep(rng, model, sampler::GibbsConditional, vi)

Expand Down Expand Up @@ -97,12 +128,10 @@ function AbstractMCMC.step(
alg = sampler.alg

# For GibbsConditional within Gibbs, we need to get all variable values
# Check if we're in a Gibbs context
global_vi = if hasproperty(model, :context) && model.context isa GibbsContext
# We're in a Gibbs context, get the global varinfo
get_global_varinfo(model.context)
# Traverse the context stack to find all conditioned/fixed/Gibbs variables
global_vi = if hasproperty(model, :context)
find_global_varinfo(model.context, state)
else
# We're not in a Gibbs context, use the current state
state
end

Expand All @@ -119,34 +148,10 @@ function AbstractMCMC.step(
updated = rand(rng, conddist)

# Update the variable in state
# We need to get the actual VarName for this variable
# The symbol S tells us which variable to update
vn = VarName{S}()

# Check if the variable needs to be a vector
new_vi = if haskey(state, vn)
# Update the existing variable
DynamicPPL.setindex!!(state, updated, vn)
else
# Try to find the variable with indices
# This handles cases where the variable might have indices
local updated_vi = state
found = false
for key in keys(state)
if DynamicPPL.getsym(key) == S
updated_vi = DynamicPPL.setindex!!(state, updated, key)
found = true
break
end
end
if !found
error("Could not find variable $S in VarInfo")
end
updated_vi
end

# Update log joint probability
new_vi = last(DynamicPPL.evaluate!!(model, new_vi, DynamicPPL.DefaultContext()))
# The Gibbs sampler ensures that state only contains one variable
# Get the variable name from the keys
varname = first(keys(state))
new_vi = DynamicPPL.setindex!!(state, updated, varname)

return nothing, new_vi
end
Expand All @@ -166,80 +171,3 @@ function setparams_varinfo!!(
# the state is nothing and we don't need to update anything
return params
end

"""
gibbs_initialstep_recursive(
rng, model, sampler::GibbsConditional, target_varnames, global_vi, prev_state
)

Initialize the GibbsConditional sampler.
"""
function gibbs_initialstep_recursive(
rng::Random.AbstractRNG,
model::DynamicPPL.Model,
sampler_wrapped::DynamicPPL.Sampler{<:GibbsConditional},
target_varnames::AbstractVector{<:VarName},
global_vi::DynamicPPL.AbstractVarInfo,
prev_state,
)
# GibbsConditional doesn't need any special initialization
# Just perform one sampling step
return gibbs_step_recursive(
rng, model, sampler_wrapped, target_varnames, global_vi, nothing
)
end

"""
gibbs_step_recursive(
rng, model, sampler::GibbsConditional, target_varnames, global_vi, state
)

Perform a single step of GibbsConditional sampling.
"""
function gibbs_step_recursive(
rng::Random.AbstractRNG,
model::DynamicPPL.Model,
sampler_wrapped::DynamicPPL.Sampler{<:GibbsConditional{S}},
target_varnames::AbstractVector{<:VarName},
global_vi::DynamicPPL.AbstractVarInfo,
state,
) where {S}
sampler = sampler_wrapped.alg

# Extract conditioned values as a NamedTuple
# Include both random variables and observed data
condvals_vars = DynamicPPL.values_as(DynamicPPL.invlink(global_vi, model), NamedTuple)
condvals_obs = NamedTuple{keys(model.args)}(model.args)
condvals = merge(condvals_vars, condvals_obs)

# Get the conditional distribution
conddist = sampler.conditional(condvals)

# Sample from the conditional distribution
updated = rand(rng, conddist)

# Update the variable in global_vi
# We need to get the actual VarName for this variable
# The symbol S tells us which variable to update
vn = VarName{S}()

# Check if the variable needs to be a vector
if haskey(global_vi, vn)
# Update the existing variable
global_vi = DynamicPPL.setindex!!(global_vi, updated, vn)
else
# Try to find the variable with indices
# This handles cases where the variable might have indices
for key in keys(global_vi)
if DynamicPPL.getsym(key) == S
global_vi = DynamicPPL.setindex!!(global_vi, updated, key)
break
end
end
end

# Update log joint probability
global_vi = last(DynamicPPL.evaluate!!(model, global_vi, DynamicPPL.DefaultContext()))

return nothing, global_vi
end
Loading