-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayesian_lin_regress_show_region.py
56 lines (45 loc) · 1.53 KB
/
bayesian_lin_regress_show_region.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 numpy as np
import math
import matplotlib.pyplot as plt
from scipy.io import loadmat
from scipy.interpolate import spline
import sys
import bayesian_linear_regression as blr
if __name__ == '__main__':
if len(sys.argv) < 2:
print('usage: %s <inputfile>' % sys.argv[0])
sys.exit(1)
data = loadmat(sys.argv[1])
X = data['x']
T = data['t']
total = len(X)
test_N = [10, 15, 30, 80]
M = 7
s = 0.1
u = [float(j*2)/M for j in range(M)]
alpha = 1./math.pow(10, 6)
beta = 1
x = np.linspace(0, 2, 300)
y = np.empty(300)
y1 = np.empty(300)
y2 = np.empty(300)
f, axarr = plt.subplots(2, 2)
for N in test_N:
pos = blr.pos_mapping(N)
post_mean, post_var = blr.get_posterior(alpha, beta, blr.get_basis_form(u, X[:N], s), T[:N])
for i in range(300):
trans_x = np.squeeze(blr.get_basis_form(u, np.array([x[i]]), s))
y[i] = np.dot(post_mean.T, trans_x)
rand_var = 1./beta + np.linalg.multi_dot([trans_x.T, post_var, trans_x])
std = math.sqrt(rand_var)
y1[i] = y[i] + std
y2[i] = y[i] - std
axarr[pos].plot(x, y, 'r-')
axarr[pos].fill_between(x, y1, y2, facecolor='pink', edgecolor='none')
axarr[pos].scatter(X[:N], T[:N], s=80, facecolors='none', edgecolors='b')
axarr[pos].set_title('N = %d' % N)
axarr[pos].set_xlim(-0.1, 2.1)
axarr[pos].set_ylim(-15, 15)
axarr[pos].set_xlabel('x')
axarr[pos].set_ylabel('t')
plt.show()