-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprecompile_funcs.jl
48 lines (41 loc) · 1.25 KB
/
precompile_funcs.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
module PrecompileFuncs
using TuringDash
using TuringDash.Turing
using TuringDash.Random
using TuringDash.CSV, TuringDash.DataFrames
function precompile_turing()
chn = Base.Channel{Vector{Float64}}(Inf)
df1 = nothing
global df1
df1 = CSV.read(download("https://raw.githubusercontent.com/efmanu/TuringDashApp.jl/master/TuringDash/datasets/data1.csv"), DataFrame)
model_str1 = """
@model regression_model(x, y) = begin
a ~ Normal()
b ~ Normal()
for i in 1:10
y[i] ~ Normal(a + b * x[i], 1.0)
end
end
"""
model_str2 = "regression_model(x, y)"
for colname in Symbol.(names(df1))
@eval $colname = df1.$colname
end
eval(Meta.parse(model_str1))
model = eval(Meta.parse(model_str2))
alg = MH(
[0.25 0.05;
0.05 0.50]
)
rng = Random.GLOBAL_RNG
nperiteration = 50
nsamples = 20
r = sample(rng, model, alg, nperiteration; chain_type=MCMCChains.Chains, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
for i in (nperiteration + 1):nperiteration:nsamples
r = Turing.Inference.resume(r, nperiteration, save_state=true, progress=false)
put!(chn, Array(r)[end,1:2])
end
return "success"
end
end