-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassignment5-second.py
56 lines (50 loc) · 1.85 KB
/
assignment5-second.py
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
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import norm
import seaborn as sns
"""
basic Gibbs sampler with conditionals
"""
def gibbs_sampler(x_init, means, std_devs, num_samples):
x1_samples = []
x2_samples = []
for i in range(0, num_samples):
mean_x1_x2 = means[0] + (std_devs[0][-1] / std_devs[-1][-1]) * (x_init - means[-1])
std_dev_x1_x2 = std_devs[0][0] - (std_devs[0][-1] / std_devs[-1][-1]) * std_devs[-1][0]
x1 = np.random.normal(mean_x1_x2, std_dev_x1_x2)
mean_x2_x1 = means[-1] + (std_devs[-1][0] / std_devs[0][0]) * (x1 - means[0])
std_dev_x2_x1 = std_devs[-1][-1] - (std_devs[-1][0] / std_devs[0][0]) * std_devs[0][-1]
x2 = np.random.normal(mean_x2_x1, std_dev_x2_x1)
x1_samples.append(x1)
x2_samples.append(x2)
if i % 5000 == 0:
pass
#plot_samples(x1_samples,x2_samples)
return x1_samples, x2_samples
def plot_samples(dist_x,dist_y):
ex = np.linspace(-6, 6, 100)
ex2 = np.linspace(-10, 10, 100)
plt.subplot(1, 2, 1)
plt.hist(dist_x, 50, normed=True)
title = "number of samples = "+ str(len(dist_x))
plt.plot(ex, norm.pdf(ex, 1, 1), 'r-', label=title)
plt.title(title)
plt.subplot(1, 2, 2)
plt.hist(dist_y, 50, normed=True)
plt.plot(ex2, norm.pdf(ex2, 0, 3), 'r-', label=title)
plt.title(title)
plt.show()
if __name__ == "__main__":
samples = 100000
x_in = np.random.rand()%2
mean = [1, 0]
std_dev = np.array([[1, -0.5], [-0.5, 3]])
dist_x, dist_y = gibbs_sampler(x_in, mean, std_dev, samples)
plot_samples(dist_x,dist_y)
"""
x_in = 0
dist_x, dist_y = gibbs_sampler(x_in, mean, std_dev, samples)
plot_samples(dist_x,dist_y)
"""
grid = (sns.jointplot(np.array(dist_x), np.array(dist_y),kind='kde',color='k'))
grid.savefig("second.png")