-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
144 lines (104 loc) · 3.61 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import numpy as np
from numpy.random import randint, rand, normal, multivariate_normal
import matplotlib.pyplot as plt
def _normalize_log_probabilities(x):
"""Computes a distribution for a categorical variable based on a vector of unnormalized log probabilities."""
x = np.exp(x - max(x))
s = sum(x)
return x / s
def _categorical(x):
"""Samples from a categorical distribution."""
return np.sum(np.cumsum(x) < rand())
class GPDP(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.T = len(x)
self.mu_0 = 0.0
self.sigma2_0 = 1.0
self.sigma2_n = 1.0
self.alpha = 0.5
# Initial settings of the sampler.
self.N = 5
def _crp_sufficient_stats(self, z):
"""Computes the sufficient statistics for a CRP from a set of cluster assingments."""
ss = np.array([0.0] * (self.N + 1))
for z_i in z:
ss[z_i] += 1
ss[self.N] = self.alpha
return ss
def log_likelihood(self, z):
"""Computes a vector of log likelihood values for every assignment of a datapoint to each of the self.N clusters."""
return np.array([0.0] * (self.N + 1))
def gibbs(self, iterations):
z = randint(self.N, size=self.T)
ss = self._crp_sufficient_stats(z)
stats = {
'N': []
}
for itr in xrange(iterations):
print "Iteration %d - N = %d." % (itr, self.N)
for t in xrange(self.T):
# Compute crp sufficient stats without datapoint t.
ss[z[t]] -= 1
# Resample z_t.
log_v = np.log(ss) + self.log_likelihood(z)
v = _normalize_log_probabilities(log_v)
z[t] = _categorical(v)
if z[t] < self.N:
ss[z[t]] += 1
else:
ss[self.N] = 1.0
ss = np.append(ss, self.alpha)
self.N += 1
assert(np.sum(ss) == self.T + self.alpha)
# Compact clusters if need be.
for n in xrange(self.N - 1, -1, -1):
if ss[n] < 1.0e-5:
assert(np.sum(z == n) == 0)
z[z > n] = z[z > n] - 1
ss = np.delete(ss, n)
self.N -= 1
stats['N'].append(self.N)
plt.plot(stats['N'])
plt.show()
def generate(N=5, T=10, l=20.0, sigma_n=0.1, sigma_0=1.0):
"""Generates a GP with DP noise."""
x = np.asarray(xrange(0, T))
mu = normal(0.0, sigma_0, N)
z = np.asarray([_categorical([1.0 / N] * N) for _ in xrange(0, T)])
K = kernel_se(x)
y_base = multivariate_normal(mean=np.zeros(T), cov=K)
y = y_base + normal(mu[z], sigma_n)
return x, y_base, y
def kernel_se(x, l=20.0):
"""Computes a kernel matrix for the squared exponential kernel."""
T = len(x)
M = np.zeros(shape=(T, T))
for i in xrange(T):
for j in xrange(T):
M[i, j] = np.exp(-(x[i] - x[j]) * (x[i] - x[j]) / (2.0 * l * l))
return M
def fit_gp(x, y, sigma_n):
M = kernel_se(x)
M_noise = kernel_se(x)
for i in xrange(len(x)):
M_noise[i, i] += sigma_n * sigma_n
tmp = np.linalg.solve(M_noise, y)
f = M.dot(tmp)
return f
if __name__ == "__main__":
T = 100
x, y_base, y = generate(T=T)
plt.figure(1)
plt.subplot(2, 2, 1)
plt.title('Dataset')
plt.plot(x, y_base)
plt.plot(x, y, '.')
f = fit_gp(x, y, sigma_n=2.1)
plt.subplot(2, 2, 2)
plt.title('GP Fit')
plt.plot(x, f)
plt.show()
gpdp = GPDP(x, y)
gpdp.gibbs(50)