-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathemcee.jl
85 lines (75 loc) · 3.02 KB
/
emcee.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@testset "emcee.jl" begin
@testset "example" begin
@testset "untransformed space" begin
# define model
function logprob(θ)
s, m = θ
s > 0 || return -Inf
mdist = Normal(0, sqrt(s))
obsdist = Normal(m, sqrt(s))
return logpdf(InverseGamma(2, 3), s) + logpdf(mdist, m) +
logpdf(obsdist, 1.5) + logpdf(obsdist, 2.0)
end
model = DensityModel(logprob)
# perform stretch move and sample from prior in initial step
Random.seed!(100)
sampler = Ensemble(1_000, StretchProposal([InverseGamma(2, 3), Normal(0, 1)]))
chain = sample(model, sampler, 1_000;
param_names = ["s", "m"], chain_type = Chains, progress=false)
@test chain isa Chains
@test range(chain) == 1:1_000
@test mean(chain["s"]) ≈ 49/24 atol=0.1
@test mean(chain["m"]) ≈ 7/6 atol=0.1
chain2 = sample(
model,
sampler,
1_000;
param_names = ["s", "m"],
chain_type = Chains,
discard_initial=25,
thinning=4,
progress=false
)
@test chain2 isa Chains
@test range(chain2) == range(26; step=4, length=1_000)
@test mean(chain2["s"]) ≈ 49/24 atol=0.1
@test mean(chain2["m"]) ≈ 7/6 atol=0.1
end
@testset "transformed space" begin
# define model
function logprob(θ)
logs, m = θ
s = exp(logs)
sqrts = sqrt(s)
mdist = Normal(0, sqrts)
obsdist = Normal(m, sqrts)
return logpdf(InverseGamma(2, 3), s) + logpdf(mdist, m) +
logpdf(obsdist, 1.5) + logpdf(obsdist, 2.0) + logs
end
model = DensityModel(logprob)
# perform stretch move and sample from normal distribution in initial step
Random.seed!(100)
sampler = Ensemble(1_000, StretchProposal(MvNormal(zeros(2), I)))
chain = sample(model, sampler, 1_000;
param_names = ["logs", "m"], chain_type = Chains, progress=false)
@test chain isa Chains
@test range(chain) == 1:1_000
@test mean(exp, chain["logs"]) ≈ 49/24 atol=0.1
@test mean(chain["m"]) ≈ 7/6 atol=0.1
chain2 = sample(
model,
sampler,
1_000;
param_names = ["logs", "m"],
chain_type = Chains,
discard_initial=25,
thinning=4,
progress=false
)
@test chain2 isa Chains
@test range(chain2) == range(26; step=4, length=1_000)
@test mean(exp, chain2["logs"]) ≈ 49/24 atol=0.1
@test mean(chain2["m"]) ≈ 7/6 atol=0.1
end
end
end