This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy path01-predictive_checks.jl
62 lines (49 loc) · 1.54 KB
/
01-predictive_checks.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
using Turing
using DataFrames
using Random: seed!
seed!(123)
# simulated data
real_p = 0.75
y = rand(Bernoulli(real_p), 1_000)
# simple model for biased coin flips
@model function coin_flip(y)
# prior
p ~ Beta(1, 1)
# likelihood
for i in eachindex(y)
y[i] ~ Bernoulli(p)
end
return (; p, y)
end
# Instantiate the model
model = coin_flip(y)
# Prior Predictive Check
# Step 1: Get the draws for the priors only.
# i.e. don't condition on data.
# Just use the Prior() sampler:
prior_chain = sample(model, Prior(), 2_000)
println(DataFrame(summarystats(prior_chain)))
# Step 2: Instantiate a Vector of missing values
# with the same dimensions as y:
y_missing = similar(y, Missing)
# Step 3: Instantiate the model with the
# Vector of missing values:
model_missing = coin_flip(y_missing)
# Step 4: Predict for your model with missing values
# using the draws from the prior only:
prior_check = predict(model_missing, prior_chain)
# Posterior Predictive Check
# Step 1: Get the draws for the full model.
# i.e. do condition on data.
# Just use any sampler except the Prior():
posterior_chain = sample(model, MH(), 2_000)
println(DataFrame(summarystats(posterior_chain)))
# Step 2: Instantiate a Vector of missing values
# with the same dimensions as y:
y_missing = similar(y, Missing)
# Step 3: Instantiate the model with the
# Vector of missing values:
model_missing = coin_flip(y_missing)
# Step 4: Predict for your model with missing values
# using the draws from the full model:
posterior_check = predict(model_missing, posterior_chain)