diff --git a/README.md b/README.md index 3d64d11..33fc054 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/docs/src/general.md b/docs/src/general.md index b8d7a9e..98bbb9b 100644 --- a/docs/src/general.md +++ b/docs/src/general.md @@ -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 @@ -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)