Last active
December 11, 2023 09:23
-
-
Save ugo-nama-kun/76de144a0fa714f7c98438804fdbcf15 to your computer and use it in GitHub Desktop.
Calculating Bayesian Posterior using Counts
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
import matplotlib.pyplot as plt | |
import numpy as np | |
n_bins = 100 | |
def get_dist(center, scale, bins): | |
dist = np.ones(bins) | |
s_list = [15, 10, 5, 2] | |
power_list = [10, 20, 40, 50] | |
for s, pow in zip(s_list, power_list): | |
for i in range(int(center - s * scale), int(center + s * scale)): | |
if bins <= i or i < 0: | |
continue | |
dist[i] = pow | |
dist[center] = 60 | |
return dist / dist.sum() | |
prior = get_dist(center=30, scale=2, bins=n_bins) | |
likelihood = get_dist(center=50, scale=2, bins=n_bins) | |
def posterior(p_prior, p_likelihood): | |
p_posterior = p_prior * p_likelihood | |
print(p_posterior) | |
p_posterior = p_posterior / p_posterior.sum() | |
return p_posterior | |
x = np.arange(n_bins) | |
plt.bar(x, prior, color="b") | |
plt.bar(x, likelihood, color="r") | |
plt.bar(x, posterior(prior, likelihood), color="g") | |
plt.legend(["prior", "likelihood", "posterior"]) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
result