Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init_params keyword argument #26

Merged
merged 10 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/abstractmcmc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ end

# first step of the elliptical slice sampler
function AbstractMCMC.step(
rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel, ::ESS; kwargs...
rng::Random.AbstractRNG, model::AbstractMCMC.AbstractModel, ::ESS; init_params=nothing, kwargs...
theogf marked this conversation as resolved.
Show resolved Hide resolved
)
# initial sample from the Gaussian prior
f = initial_sample(rng, model)
f = init_params === nothing ? initial_sample(rng, model) : init_params

# compute log-likelihood of the initial sample
loglikelihood = Distributions.loglikelihood(model, f)
Expand Down
10 changes: 10 additions & 0 deletions test/simple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

# model
prior = Normal(0.5, 1)
init_x = randn()

# true posterior
μ = 0.9
Expand All @@ -62,13 +63,18 @@
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
end

# initial parameter
sample = sample(ESSModel(prior, ℓ), ESS(), 10; progress=false, init_params=init_x)
@test first(samples) == init_x
end

@testset "Scalar model (vectorized)" begin
Random.seed!(1)

# model
prior = MvNormal([0.0], I)
init_x = randn(1)

# true posterior
μ = [0.8]
Expand All @@ -91,6 +97,10 @@
@test mean(mean, samples) ≈ μ atol = 0.05
@test mean(var, samples) ≈ σ² atol = 0.05
end

# initial parameter
sample = sample(ESSModel(prior, ℓ), ESS(), 10; progress=false, init_params=init_x)
@test first(samples) == init_x
end

@testset "Scalar model with nonzero mean (vectorized)" begin
Expand Down