Created
May 19, 2021 09:41
-
-
Save 18182324/3c7101a566066d7ae734b41af036de78 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
theta=np.arange(0.05,1,0.05) | |
print(theta) | |
prior = 1/len(theta) #The normalizing constant of the prior | |
#Evaluate joint likelihood and unnormalized posterior at one specific #$\theta = 0.5$ | |
dist = tfp.distributions.Bernoulli(probs=0.5) #one specific theta | |
print(np.prod(dist.prob(obs_data))) #joint likelihood | |
print(np.prod(dist.prob(obs_data))*prior) #unnormalized posterior | |
#Repeat the process for all thetas, range 0.05 - 0.95 | |
res = np.zeros((len(theta),5)) | |
for i in range(0,len(theta)): | |
dist = tfp.distributions.Bernoulli(probs=theta[i]) | |
res[i,0:4]=np.array((theta[i],np.prod(dist.prob(obs_data)),prior,np.prod(dist.prob(obs_data))*prior)) | |
#Normalize posterior | |
import pandas as pd | |
res=pd.DataFrame(res,columns=["theta","jointlik","prior","unnorm_post","post"]) | |
res["post"]=res["unnorm_post"]/np.sum(res["unnorm_post"]) | |
res | |
#Plot prior and posterior for parameter θ | |
plt.figure(figsize=(16,6)) | |
plt.subplot(1,2,1) | |
plt.stem(res["theta"],res["prior"]) | |
plt.xlabel("theta") | |
plt.ylabel("probability") | |
plt.ylim([0,0.5]) | |
plt.title("prior distribution") | |
plt.subplot(1,2,2) | |
plt.stem(res["theta"],res["post"]) | |
plt.ylim([0,0.5]) | |
plt.xlabel("theta") | |
plt.ylabel("probability") | |
plt.title("posterior distribution") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment