-
-
Save g-leech/1a63f22805053186642a4b93f7dd0f77 to your computer and use it in GitHub Desktop.
Chapter 3 of Mackay's ITILA
This file contains 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
import numpy as np | |
from scipy.stats import binom | |
import matplotlib.pyplot as plt | |
import numpy | |
from math import factorial as fac | |
from collections import Counter | |
from scipy.stats import norm | |
# P(ph | s=hth, N =3) | |
def coin_likelihood(ph, X) : | |
return ph**X["h"] * (1-ph)**X["t"] | |
def coin_bias_posterior(X, ph) : | |
N = len(X) | |
X = Counter(X) | |
z = fac(X["h"]) * fac(X["t"]) / (fac(X["h"]+ X["t"] + 1)) | |
return coin_likelihood(ph, X) / z | |
def plot_and_summarise_posterior(data) : | |
x = np.arange(0,1, 0.005) | |
y = [coin_bias_posterior(data, i) for i in x ] | |
plt.plot(x,y) | |
plt.show() | |
print(data) | |
print( "ph:", mean(x, y) ) | |
print( "ph:", mode(x, y) ) | |
def mode(x,y) : | |
i = max(zip(y, range(len(y))))[1] | |
return x[i] | |
def mean(x, y) : | |
ev = sum(p * v for p,v in zip(x,y)) | |
return ev / len(x) | |
# Ex 3.5 | |
data = ["h", "t", "h"] | |
plot_and_summarise_posterior(data) | |
data = ["t", "t", "t"] | |
plot_and_summarise_posterior(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment