-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpractice_simple_bayes_Apr1_2021.py
109 lines (64 loc) · 2.75 KB
/
practice_simple_bayes_Apr1_2021.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 1 21:28:30 2021
@author: ricro
"""
# Simple examples of Bayesian statistics in Python
import numpy as np
import pymc3 as pm
import arviz as az
# First, we want to estimate the bias of a coin
p = 0.65 # bias of our coin
# Now generate data from that coin
n_flips = 150
compare = np.random.rand(n_flips)
heads = (compare <= p).astype(int)
# Now to fit the model
with pm.Model() as coin_model: #This creates an instance of the Model class from pm
# First, set up the priors
theta = pm.Beta('theta', alpha = 1, beta = 1)
# Now connect to the data
y = pm.Bernoulli('y', p = theta, observed = heads) # The 'observed' argument is where you input the data
# This tells PyMC3 that this variable is the likelihood of the data
coin_trace = pm.sample(500, cores = 1)
results = az.summary(coin_trace)
print(results)
az.plot_posterior(coin_trace)
# Now let's try a hierarchical model, where we flip a bunch of different coins,
# each with their own bias, and those biases are distributed a particular way
# First, simulate the data
total_data = np.array([]) # Initializing an array to store data
which_coin = np.array([])
biases = np.array([])
n_coins = 15
for coin in range(n_coins):
# First, choose a bias
# Suppose each coin's bias comes from a Beta(5,10) distribution
bias = np.random.beta(5,10)
biases = np.append(biases, bias)
compare = np.random.rand(n_flips)
heads = (compare <= bias).astype(int)
# Now add data to the total_data:
total_data = np.append(total_data, heads)
# And add index of coin:
which_coin = np.append(which_coin, coin*np.ones(n_flips))
which_coin = which_coin.astype(int)
# Now that we've generated data, let's fit a simple hierarchical model to the data
with pm.Model() as hier_coin:
# First start with hyperpriors
A = pm.Gamma('A',alpha = 5, beta = 1)
B = pm.Gamma('B', alpha = 5, beta = 1)
# Now prior on the theta's
theta = pm.Beta('theta', alpha = A, beta = B, shape = n_coins)
# The shape argument is how we tell PyMC3 that the number of coins' biases we
# want to estimate is n_coins
# This next part is a bit tricky in terms of syntax
# We need to tell Python which parameter we should use for which data point
# We can do this by using the following syntax
y = pm.Bernoulli('y', p = theta[which_coin], observed = total_data)
hier_trace = pm.sample(1000, cores = 1)
hier_results = az.summary(hier_trace)
print(hier_results)
print(hier_results["mean"])
print(biases)
az.plot_posterior(hier_trace)