Skip to content

Instantly share code, notes, and snippets.

@katsugeneration
Created May 22, 2016 12:07
Show Gist options
  • Save katsugeneration/5cc34c5b0e199f61e0c181c0a74f5a68 to your computer and use it in GitHub Desktop.
Save katsugeneration/5cc34c5b0e199f61e0c181c0a74f5a68 to your computer and use it in GitHub Desktop.
Bayse Fitting Sample
import math
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
I = 100
data = np.random.normal(0, 2, I)
y = np.ones(I)
plt.hold(False)
plt.plot(data, y, "o")
plt.savefig("data.png")
mean = np.mean(data)
var = np.var(data)
alpha = 1.0
beta = 1.0
gamma = 1.0
delta = 0.0
lin = np.linspace(-5, 5, 200)
norm = mlab.normpdf(lin, 0, 2)
plt.plot(lin, norm, "red")
plt.hold(True)
norm = mlab.normpdf(lin, mean, math.sqrt(var))
plt.plot(lin, norm, "blue")
mu_hat = (I * mean + gamma * delta) / (I + gamma)
norm = mlab.normpdf(lin, mu_hat, math.sqrt((I * var + 2 * beta + gamma * (delta - mu_hat) ** 2) / (I + 3 + 2 * alpha)))
plt.plot(lin, norm, "green")
plt.savefig("graph.png")
plt.show()
plt.hold(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment