Skip to content

Add SamplingOutput for samples and chain metadata - #218

Merged
yebai merged 5 commits into
mainfrom
samplingoutput
Sep 20, 2026
Merged

yebai merged 5 commits into
mainfrom
samplingoutput

Conversation

@yebai

@yebai yebai commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

Add SamplingOutput for draws and sampling metadata. Chain packages can define conversions to their own formats, as illustrated below for MCMCChains and FlexiChains.

using AbstractMCMC, Random

struct NormalModel <: AbstractMCMC.AbstractModel end
struct RandomWalk <: AbstractMCMC.AbstractSampler end

function AbstractMCMC.step(rng, ::NormalModel, ::RandomWalk, x=0.0; kwargs...)
    proposal = x + randn(rng)
    if log(rand(rng)) < (x^2 - proposal^2) / 2
        x = proposal
    end
    return (x=x,), x
end

chain = sample(
    Xoshiro(42), NormalModel(), RandomWalk(), 100;
    chain_type=SamplingOutput, save_state=true,
)

draws = AbstractMCMC.to_samples(NamedTuple, chain)
restored = AbstractMCMC.from_samples(SamplingOutput, draws)

Chain conversion:

using MCMCChains: Chains
using FlexiChains: FlexiChain, Parameter

function Base.convert(::Type{Chains}, output::SamplingOutput{<:NamedTuple{(:x,)}})
    values = getproperty.(output.samples, :x)
    return Chains(
        reshape(values, size(output, 1), 1, size(output, 2)), [:x];
        start=first(output.iterations),
        thin=step(output.iterations),
    )
end

function Base.convert(::Type{FlexiChain{Symbol}}, output::SamplingOutput{<:NamedTuple{(:x,)}})
    return FlexiChain{Symbol}(
        size(output)..., Dict(Parameter(:x) => getproperty.(output.samples, :x));
        iter_indices=output.iterations,
    )
end

mc = convert(Chains, chain)
fc = convert(FlexiChain{Symbol}, chain)

These example conversions preserve draws and iteration indices. Timing and saved states remain in chain; the matrix round trip retains draws only.

Assisted-by: Codex <codex@openai.com>
@github-actions

Copy link
Copy Markdown
Contributor

AbstractMCMC.jl documentation for PR #218 is available at:
https://TuringLang.github.io/AbstractMCMC.jl/previews/PR218/

Assisted-by: Codex <codex@openai.com>
Preserve the raw-sample fallback for other chain types. Conversion from
`SamplingOutput` remains available explicitly after sampling.

Assisted-by: Codex <codex@openai.com>
yebai and others added 2 commits September 20, 2026 19:31
Reuse sample conversion while retaining sampling metadata. Document
explicit conversion and direct bundling as chain construction options.

Assisted-by: Codex <codex@openai.com>
@yebai
yebai merged commit 418afe0 into main Sep 20, 2026
15 checks passed
@yebai
yebai deleted the samplingoutput branch September 20, 2026 19:25
yebai added a commit to TuringLang/DynamicPPL.jl that referenced this pull request Sep 21, 2026
This PR adds support for
[`SamplingOutput`](TuringLang/AbstractMCMC.jl#218):
model `returned`, `predict`, and `pointwise log densities`, plus
conversion to `MCMCChains.Chains`. It also fixes a few minor issues in
`MCMCChains` (see code).

```julia
@model demo(y) = (x ~ Normal(); y ~ Normal(x))

chain = AbstractMCMC.SamplingOutput(fill(VarNamedTuple(; x=0.5), 10, 1))

values = returned(demo(1.0), chain)
densities = pointwise_logdensities(demo(1.0), chain)
likelihoods = pointwise_loglikelihoods(demo(1.0), chain)
priors = pointwise_prior_logdensities(demo(1.0), chain)
predictions = predict(demo(missing), chain)
mcmcchain = convert(MCMCChains.Chains, predictions)
```
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 this pull request may close these issues.

1 participant