-
Notifications
You must be signed in to change notification settings - Fork 0
/
bernoulli.jl
55 lines (41 loc) · 1.27 KB
/
bernoulli.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
######### StanPathfinder Bernoulli example ###########
using StanPathfinder
using StanIO: read_csvfiles
bernoulli_model = "
data {
int<lower=1> N;
array[N] int<lower=0,upper=1> y;
}
parameters {
real<lower=0,upper=1> theta;
}
model {
theta ~ beta(1,1);
y ~ bernoulli(theta);
}
";
data = Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1])
# Keep tmpdir across multiple runs to prevent re-compilation
tmpdir = joinpath(@__DIR__, "tmp")
sm = PathfinderModel("bernoulli", bernoulli_model)
rc = stan_pathfinder(sm; data)
if all(success.(rc))
str = read(joinpath(sm.tmpdir, "$(sm.name)_log_1.log"), String)
findfirst("Path [1]", str)
str = split(str[findfirst("Path [1]", str)[1]:end], "\n")
display(str)
df = read_pathfinder(sm)
profile_df = create_pathfinder_profile_df(sm)
display(profile_df)
end
sm2 = PathfinderModel("bernoulli2", bernoulli_model, tmpdir)
rc2 = stan_pathfinder(sm2; data, seed=rand(1:200000000, 2), num_chains=2)
if all(success.(rc2))
str = read(joinpath(sm2.tmpdir, "$(sm2.name)_log_1.log"), String)
findfirst("Path [1]", str)
str = split(str[findfirst("Path [1]", str)[1]:end], "\n")
display(str)
df = read_pathfinder(sm2)
profile_df = create_pathfinder_profile_df(sm2)
display(profile_df)
end