Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
"""
2
This is an example of using Bayesian A/B testing
3
4
"""
5
6
import pymc as pm
7
8
# these two quantities are unknown to us.
9
true_p_A = 0.05
10
true_p_B = 0.04
11
12
# notice the unequal sample sizes -- no problem in Bayesian analysis.
13
N_A = 1500
14
N_B = 1000
15
16
# generate data
17
observations_A = pm.rbernoulli(true_p_A, N_A)
18
observations_B = pm.rbernoulli(true_p_B, N_B)
19
20
21
# set up the pymc model. Again assume Uniform priors for p_A and p_B
22
p_A = pm.Uniform("p_A", 0, 1)
23
p_B = pm.Uniform("p_B", 0, 1)
24
25
26
# define the deterministic delta function. This is our unknown of interest.
27
28
@pm.deterministic
29
def delta(p_A=p_A, p_B=p_B):
30
return p_A - p_B
31
32
33
# set of observations, in this case we have two observation datasets.
34
obs_A = pm.Bernoulli("obs_A", p_A, value=observations_A, observed=True)
35
obs_B = pm.Bernoulli("obs_B", p_B, value=observations_B, observed=True)
36
37
# to be explained in chapter 3.
38
mcmc = pm.MCMC([p_A, p_B, delta, obs_A, obs_B])
39
mcmc.sample(20000, 1000)
40
41