-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2D_example.py
More file actions
70 lines (57 loc) · 1.79 KB
/
2D_example.py
File metadata and controls
70 lines (57 loc) · 1.79 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import numpy as np
from numpy.linalg import inv
from matplotlib import pyplot as plt
from LinearReg_MissingData import *
"""
Linear regression applied to a 2D problem, where 2 of the inputs have
a single component missing. The code plots parameter convergence and
the posterior distributions of the missing parts of the data.
P.L.Green
p.l.green@liverpool.ac.uk
engineeringdataanalytics.com
"""
# Define data
N = 20
D = 2
U = np.random.uniform(size=(N, D))
theta = np.vstack(np.array([1., 2.]))
sigma = 0.1
Y = U @ theta + sigma * np.vstack(np.random.randn(N))
# Prior
mu_z = np.array([0.])
sigma_z = np.array([1.])
# Initial guess
theta_init = np.array([0.5, 0.5])
# Create some missing data
z_true = [U[2][1], U[10][0]]
U[2][1] = np.nan
U[10][0] = np.nan
# Run linear regression
lr = LinearReg_MissingData(U, Y, N, D, theta_init, mu_z, sigma_z, sigma)
lr.train()
# Plot posterior of missing data
Z_range = np.linspace(-1, 2, 1000)
p1 = lr.posterior_z[2].pdf(Z_range)
p2 = lr.posterior_z[10].pdf(Z_range)
fig, ax = plt.subplots(nrows=2)
ax[0].plot(Z_range, p1, color='black')
ax[0].plot(z_true[0], 0, 'o', color='green',
label='Actual value of missing data point')
ax[0].set_ylabel('$p(z | y)$')
ax[0].legend()
ax[1].plot(Z_range, p2, color='black')
ax[1].plot(z_true[1], 0, 'o', color='green')
ax[1].set_ylabel('$p(z | y)$')
ax[1].set_xlabel('$z$')
plt.tight_layout()
# Plot parameter convergence
fig, ax = plt.subplots(nrows=2)
ax[0].plot(lr.theta_store[:, 0], color='black', label='Max. likelihood')
ax[0].plot(np.array([0, 10]), np.repeat(theta[0], 2), 'green', label='True')
ax[0].set_ylabel('$\\theta_1$')
ax[0].legend()
ax[1].plot(lr.theta_store[:, 1], color='black')
ax[1].plot(np.array([0, 10]), np.repeat(theta[1], 2), 'green')
ax[1].set_ylabel('$\\theta_2$')
ax[1].set_xlabel('Iteration')
plt.show()