Skip to content

Commit

Permalink
add LogDensityProblems example
Browse files Browse the repository at this point in the history
  • Loading branch information
Red-Portal committed May 29, 2024
1 parent 3aee5a1 commit e18de0e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ end
sampler = RandPermGibbs(SliceSteppingOut(2.))
n_samples = 10000
model = demo()
sample(model, externalsampler(sampler), n_samples; initial_params=[1.0, 0.0])
sample(model, externalsampler(sampler), n_samples; initial_params=[exp(1.0), 0.0])
```

[^N2003]: Neal, R. M. (2003). Slice sampling. The annals of statistics, 31(3), 705-767.
Expand Down
45 changes: 40 additions & 5 deletions docs/src/general.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,45 @@
This package implements the `AbstractMCMC` [interface](https://github.com/TuringLang/AbstractMCMC.jl).
`AbstractMCMC` provides a unifying interface for MCMC algorithms applied to [LogDensityProblems](https://github.com/tpapp/LogDensityProblems.jl).

## Drawing Samples From `Turing` Models
`SliceSampling.jl` can be used to sample from [Turing](https://github.com/TuringLang/Turing.jl) models through `Turing`'s `externalsampler` interface.
See the following example:
## Examples
### Drawing Samples From a `LogDensityProblems` Through `AbstractMCMC`
`SliceSampling.jl` implements the [`AbstractMCMC`](https://github.com/TuringLang/AbstractMCMC.jl) interface through [`LogDensityProblems`](https://github.com/tpapp/LogDensityProblems.jl).
That is, one simply needs to define a `LogDensityProblems` and pass it to `AbstractMCMC`:

```@example logdensityproblems
using AbstractMCMC
using Distributions
using LinearAlgebra
using LogDensityProblems
using Plots
using SliceSampling
struct Target{D}
dist::D
end
LogDensityProblems.logdensity(target::Target, x) = logpdf(target.dist, x)
LogDensityProblems.dimension(target::Target) = length(target.distx)
LogDensityProblems.capabilities(::Type{<:Target}) = LogDensityProblems.LogDensityOrder{0}()
sampler = GibbsPolarSlice(2.0)
n_samples = 10000
model = Target(MvTDist(5, zeros(10), Matrix(I, 10, 10)))
logdensitymodel = AbstractMCMC.LogDensityModel(model)
chain = sample(logdensitymodel, sampler, n_samples; initial_params=randn(10))
samples = hcat([transition.params for transition in chain]...)
plot(samples[1,:], xlabel="Iteration", ylabel="Trace")
savefig("abstractmcmc_demo.svg")
```
![](logdensityproblems_demo.svg)

### Drawing Samples From `Turing` Models
`SliceSampling.jl` can also be used to sample from [Turing](https://github.com/TuringLang/Turing.jl) models through `Turing`'s `externalsampler` interface:

```@example turing
using Distributions
Expand All @@ -21,11 +57,10 @@ end
sampler = RandPermGibbs(SliceSteppingOut(2.))
n_samples = 10000
model = demo()
sample(model, externalsampler(sampler), n_samples; initial_params=[1.0, 0.0])
sample(model, externalsampler(sampler), n_samples; initial_params=[exp(1.0), 0.0])
```

## Drawing Samples

For drawing samples using the algorithms provided by `SliceSampling`, the user only needs to call:
```julia
sample([rng,] model, slice, N; initial_params)
Expand Down

0 comments on commit e18de0e

Please sign in to comment.