|
1 | | -# perform elliptical slice sampling for a fixed number of iterations |
2 | | -ESS_mcmc(prior, loglikelihood, N::Int; kwargs...) = |
3 | | - ESS_mcmc(Random.GLOBAL_RNG, prior, loglikelihood, N; kwargs...) |
| 1 | +# public interface |
4 | 2 |
|
5 | | -function ESS_mcmc(rng::AbstractRNG, prior, loglikelihood, N::Int; burnin::Int = 0) |
6 | | - # define the internal model |
| 3 | +""" |
| 4 | + ESS_mcmc([rng, ]prior, loglikelihood, N; kwargs...) |
| 5 | +
|
| 6 | +Create a Markov chain of `N` samples for a model with given `prior` and `loglikelihood` |
| 7 | +functions using the elliptical slice sampling algorithm. |
| 8 | +""" |
| 9 | +function ESS_mcmc( |
| 10 | + rng::Random.AbstractRNG, |
| 11 | + prior, |
| 12 | + loglikelihood, |
| 13 | + N::Integer; |
| 14 | + kwargs... |
| 15 | +) |
7 | 16 | model = Model(prior, loglikelihood) |
| 17 | + return AbstractMCMC.sample(rng, model, EllipticalSliceSampler(), N; kwargs...) |
| 18 | +end |
| 19 | + |
| 20 | +function ESS_mcmc(prior, loglikelihood, N::Integer; kwargs...) |
| 21 | + return ESS_mcmc(Random.GLOBAL_RNG, prior, loglikelihood, N; kwargs...) |
| 22 | +end |
| 23 | + |
| 24 | +# private interface |
| 25 | + |
| 26 | +""" |
| 27 | + initial_sample(rng, model) |
| 28 | +
|
| 29 | +Return the initial sample for the `model` using the random number generator `rng`. |
8 | 30 |
|
9 | | - # create the sampler |
10 | | - sampler = EllipticalSliceSampler(rng, model) |
11 | | - |
12 | | - # create MCMC chain |
13 | | - chain = Vector{eltype(sampler)}(undef, N) |
14 | | - niters = N + burnin |
15 | | - @withprogress name = "Performing elliptical slice sampling" begin |
16 | | - # discard burnin phase |
17 | | - for (i, _) in zip(1:burnin, sampler) |
18 | | - @logprogress i / niters |
19 | | - end |
20 | | - |
21 | | - for (i, f) in zip(1:N, sampler) |
22 | | - @inbounds chain[i] = f |
23 | | - @logprogress (i + burnin) / niters |
24 | | - end |
25 | | - end |
26 | | - |
27 | | - chain |
| 31 | +By default, sample from the prior by calling [`sample_prior(rng, model)`](@ref). |
| 32 | +""" |
| 33 | +function initial_sample(rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel) |
| 34 | + return sample_prior(rng, model) |
28 | 35 | end |
29 | 36 |
|
30 | | -# create an elliptical slice sampler |
31 | | -ESS_mcmc_sampler(prior, loglikelihood) = ESS_mcmc_sampler(Random.GLOBAL_RNG, prior, loglikelihood) |
32 | | -ESS_mcmc_sampler(rng::AbstractRNG, prior, loglikelihood) = |
33 | | - EllipticalSliceSampler(rng, Model(prior, loglikelihood)) |
| 37 | +""" |
| 38 | + sample_prior(rng, model) |
| 39 | +
|
| 40 | +Sample from the prior of the `model` using the random number generator `rng`. |
| 41 | +""" |
| 42 | +function sample_prior(::Random.AbstractRNG, ::AbstractMCMC.AbstractModel) end |
| 43 | + |
| 44 | +""" |
| 45 | + proposal(model, f, ν, θ) |
| 46 | +
|
| 47 | +Compute the proposal for the next sample in the elliptical slice sampling algorithm for the |
| 48 | +`model` from the previous sample `f`, the sample `ν` from the Gaussian prior, and the angle |
| 49 | +`θ`. |
| 50 | +
|
| 51 | +Mathematically, the proposal can be computed as |
| 52 | +```math |
| 53 | +\\cos θ f + ν \\sin θ ν + μ (1 - \\sin θ + \\cos θ), |
| 54 | +``` |
| 55 | +where ``μ`` is the mean of the Gaussian prior. |
| 56 | +""" |
| 57 | +function proposal(model::AbstractMCMC.AbstractModel, f, ν, θ) end |
| 58 | + |
| 59 | +""" |
| 60 | + proposal!(out, model, f, ν, θ) |
| 61 | +
|
| 62 | +Compute the proposal for the next sample in the elliptical slice sampling algorithm for the |
| 63 | +`model` from the previous sample `f`, the sample `ν` from the Gaussian prior, and the angle |
| 64 | +`θ`, and save it to `out`. |
| 65 | +
|
| 66 | +Mathematically, the proposal can be computed as |
| 67 | +```math |
| 68 | +\\cos θ f + ν \\sin θ ν + μ (1 - \\sin θ + \\cos θ), |
| 69 | +``` |
| 70 | +where ``μ`` is the mean of the Gaussian prior. |
| 71 | +""" |
| 72 | +function proposal!(out, model::AbstractMCMC.AbstractModel, f, ν, θ) end |
0 commit comments