|
1 | 1 | # private interface |
2 | 2 |
|
3 | 3 | """ |
4 | | - initial_sample(rng, model) |
| 4 | + isgaussian(dist) |
5 | 5 |
|
6 | | -Return the initial sample for the `model` using the random number generator `rng`. |
| 6 | +Check if distribution `dist` is a Gaussian distribution. |
| 7 | +""" |
| 8 | +isgaussian(dist) = false |
| 9 | +isgaussian(::Type{<:Distributions.Normal}) = true |
| 10 | +isgaussian(::Type{<:Distributions.NormalCanon}) = true |
| 11 | +isgaussian(::Type{<:Distributions.AbstractMvNormal}) = true |
7 | 12 |
|
8 | | -By default, sample from the prior by calling [`sample_prior(rng, model)`](@ref). |
9 | 13 | """ |
10 | | -function initial_sample(rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel) |
11 | | - return sample_prior(rng, model) |
12 | | -end |
| 14 | + prior(model) |
| 15 | +
|
| 16 | +Return the prior distribution of the `model`. |
| 17 | +""" |
| 18 | +function prior(::AbstractMCMC.AbstractModel) end |
13 | 19 |
|
14 | 20 | """ |
15 | | - sample_prior(rng, model) |
| 21 | + initial_sample(rng, model) |
16 | 22 |
|
17 | | -Sample from the prior of the `model` using the random number generator `rng`. |
| 23 | +Return the initial sample for the `model` using the random number generator `rng`. |
| 24 | +
|
| 25 | +By default, sample from [`prior(model)`](@ref). |
18 | 26 | """ |
19 | | -function sample_prior(::Random.AbstractRNG, ::AbstractMCMC.AbstractModel) end |
| 27 | +function initial_sample(rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel) |
| 28 | + return Random.rand(rng, prior(model)) |
| 29 | +end |
20 | 30 |
|
21 | 31 | """ |
22 | | - proposal(model, f, ν, θ) |
| 32 | + proposal(prior, f, ν, θ) |
23 | 33 |
|
24 | | -Compute the proposal for the next sample in the elliptical slice sampling algorithm for the |
25 | | -`model` from the previous sample `f`, the sample `ν` from the Gaussian prior, and the angle |
26 | | -`θ`. |
| 34 | +Compute the proposal for the next sample in the elliptical slice sampling algorithm. |
27 | 35 |
|
28 | 36 | Mathematically, the proposal can be computed as |
29 | 37 | ```math |
30 | | -\\cos θ f + ν \\sin θ ν + μ (1 - \\sin θ + \\cos θ), |
| 38 | +f \\cos θ + ν \\sin θ + μ (1 - (\\sin θ + \\cos θ)), |
31 | 39 | ``` |
32 | | -where ``μ`` is the mean of the Gaussian prior. |
| 40 | +where ``μ`` is the mean of the Gaussian `prior`, `f` is the previous sample, and `ν` is a |
| 41 | +sample from the Gaussian `prior`. |
| 42 | +
|
| 43 | +See also: [`proposal!`](@ref) |
33 | 44 | """ |
34 | | -function proposal(model::AbstractMCMC.AbstractModel, f, ν, θ) end |
| 45 | +function proposal(prior, f, ν, θ) |
| 46 | + sinθ, cosθ = sincos(θ) |
| 47 | + a = 1 - (sinθ + cosθ) |
| 48 | + μ = Statistics.mean(prior) |
| 49 | + return @. cosθ * f + sinθ * ν + a * μ |
| 50 | +end |
35 | 51 |
|
36 | 52 | """ |
37 | 53 | proposal!(out, model, f, ν, θ) |
38 | 54 |
|
39 | | -Compute the proposal for the next sample in the elliptical slice sampling algorithm for the |
40 | | -`model` from the previous sample `f`, the sample `ν` from the Gaussian prior, and the angle |
41 | | -`θ`, and save it to `out`. |
| 55 | +Compute the proposal for the next sample in the elliptical slice sampling algorithm, and |
| 56 | +save it to `out`. |
42 | 57 |
|
43 | 58 | Mathematically, the proposal can be computed as |
44 | 59 | ```math |
45 | | -\\cos θ f + ν \\sin θ ν + μ (1 - \\sin θ + \\cos θ), |
| 60 | +f \\cos θ + ν \\sin θ + μ (1 - (\\sin θ + \\cos θ)), |
46 | 61 | ``` |
47 | | -where ``μ`` is the mean of the Gaussian prior. |
| 62 | +where ``μ`` is the mean of the Gaussian `prior`, `f` is the previous sample, and `ν` is a |
| 63 | +sample from the Gaussian `prior`. |
| 64 | +
|
| 65 | +See also: [`proposal`](@ref) |
48 | 66 | """ |
49 | | -function proposal!(out, model::AbstractMCMC.AbstractModel, f, ν, θ) end |
| 67 | +function proposal!(out, prior, f, ν, θ) |
| 68 | + sinθ, cosθ = sincos(θ) |
| 69 | + a = 1 - (sinθ + cosθ) |
| 70 | + μ = Statistics.mean(prior) |
| 71 | + @. out = cosθ * f + sinθ * ν + a * μ |
| 72 | + return out |
| 73 | +end |
0 commit comments