-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayes_227.py
36 lines (27 loc) · 1.13 KB
/
bayes_227.py
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
from pymc3 import Model, Normal, HalfNormal, Uniform, Bernoulli, find_MAP, NUTS, sample, Slice, Deterministic
from scipy import optimize
import pymc3 as pm
N = 100
basic_model = Model()
with basic_model:
p = Uniform("freq_cheating", 0, 1)
true_answers = Bernoulli("truths", p)
first_coin_flips = Bernoulli("first_flips", 0.5)
second_coin_flips = Bernoulli("second_flips", 0.5)
determin_val1 = Deterministic('determin_val1', first_coin_flips * true_answers + (1 - first_coin_flips)*second_coin_flips)
determin_val = determin_val1.sum()/float(N)
start = find_MAP(fmin=optimize.fmin_powell)
# instantiate sampler
step = Slice(vars=[true_answers])
# draw 5000 posterior samples
trace = sample(100, step=step, start=start)
step = Slice(vars=[first_coin_flips])
# draw 5000 posterior samples
trace = sample(100, step=step, start=start)
step = Slice(vars=[second_coin_flips])
# draw 5000 posterior samples
trace = sample(100, step=step, start=start)
#print(first_coin_flips.getattr_value())
print(determin_val)
map_estimate = find_MAP(model=basic_model)
print(map_estimate.values())