File size: 2,823 Bytes
96092a4
 
 
 
 
e733bb1
96092a4
9710c6f
96092a4
 
e5fe480
daa8cf6
ffac004
 
96092a4
c515b4e
 
79acf50
96092a4
1e71a64
96092a4
 
 
 
 
79acf50
 
 
 
a62a771
 
79acf50
 
cdfd582
 
7e3eb7e
cdfd582
7e3eb7e
 
 
79acf50
 
 
 
 
96092a4
50e41fc
 
 
96092a4
79acf50
96092a4
 
 
 
 
 
 
186ab80
 
 
d6fb397
186ab80
 
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
import numpy as np
import streamlit as st
import scipy.stats
import matplotlib.pyplot as plt
from matplotlib import rc
# plt.style.use('fivethirtyeight')

st.subheader("Bayesian Coin Toss")
st_col = st.columns(1)[0]

N = st.slider('N_samples', min_value=2, max_value=20, value=5, step=1)
h = st.slider('N_heads', min_value=2, max_value=N, value=4, step=1)
alpha = st.slider('Alpha', min_value=0.5, max_value=5.0, value=2.0, step=0.1)
beta = st.slider('Beta', min_value=0.5, max_value=5.0, value=2.0, step=0.1)

N_theta = 100
theta = np.linspace(0.01,0.99,N_theta)
#rc('font', size=20)
# rc('text', usetex=True)
fig, ax = plt.subplots(figsize=(10,4))
axs = ax.twinx()

def Bernoulli(theta, N, h):
    return (theta ** h) * ((1-theta) ** (N-h))

Likelihood = [Bernoulli(t,N,h) for t in theta]
ax.plot(theta, Likelihood, label='Likelihood',color='b');
axs.plot(theta, scipy.stats.beta.pdf(theta, alpha,beta), label='Prior',color='k');
ax.set_xlabel('p(head)');
# ax.vlines(h/N, *ax.get_ylim(), linestyle='--',label='MLE', color='b')
# axs.text(h/N,2,'MLE', color='b')

axs.plot(theta, [scipy.stats.beta.pdf(t, h+alpha, N-h+beta) for t in theta], color='r')
ax.text(theta[N_theta//4]+0.05, Likelihood[N_theta//4], 'Likelihood', color='b')
axs.text(theta[3*N_theta//4], scipy.stats.beta.pdf(theta, alpha,beta)[3*N_theta//4],'Prior')
# axs.text(alpha/(alpha+beta)-0.1,1,'Prior mean')
axs.text(theta[N_theta//2]-0.05,scipy.stats.beta.pdf(theta[N_theta//2], h+alpha, N-h+beta),'Posterior',color='r')
# axs.text((h+alpha)/(N+alpha+beta)-0.1,3,'Post. Mean',color='r')
# ax.vlines(alpha/(alpha+beta), *ax.get_ylim(), linestyle='--',label='Prior mean',color='k')
# ax.vlines((h+alpha)/(N+alpha+beta), *ax.get_ylim(), linestyle='--',label='Post. Mean',color='r')
# ax.set_title(f"n_samples={int(N)}, n_heads={int(h)}");
ax.tick_params(axis='y', colors='b')
axs.tick_params(axis='y', colors='r')
ax.set_ylabel('Likelihood',color='b')
axs.set_ylabel('Prior/Posterior', color='r', rotation=270, labelpad=30)

ax.spines['top'].set_visible(False)
axs.spines['top'].set_visible(False)

with st_col:
    st.pyplot(fig)
    
hide_streamlit_style = """
            <style>
            #MainMenu {visibility: hidden;}
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.markdown("""
            The above visualization shows the joint effect of data and prior on the posterior. There are some interesting observations here:
            * When prior is $Beta(1, 1)$, it becomes Uniform prior and thus **uninformative**. In this case, posterior matches with likelihood.
            * With an increase in the number of samples, the posterior gets closer to the likelihood. Thus, when the number of samples is less, prior plays an important role.
            """)