-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitMvNorm.py
471 lines (374 loc) · 16 KB
/
fitMvNorm.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import stats_util as s_u
import numpy as _N
import pickle
import matplotlib.pyplot as _plt
import scipy.cluster.vq as scv
import scipy.stats as _ss
import fitutil as _fu
import time as _tm
mvn = _N.random.multivariate_normal
class fitMvNorm:
################## hyper parameters - do not change during Gibbs
# HYPER PARAMS for prior covariance: nu, PSI
PR_cov_nu = 3
PR_cov_PSI = None
po_cov_nu = None
po_cov_PSI = None
# HYPER PARAMS mean: nu, PSI
po_mu_mu = None
po_mu_sg = None
PR_mu_mu = None
PR_mu_sg = None
iPR_mu_sg= None
# HYPER PARAMS mixture coeff
PR_m_alp = None
po_alpha = None
#####
# how many clusters do I think there are
M = 10
M = 15
ITERS = 1
AR = 0.5 # % increase per 1 minute
AR0 = 1 # baseline AR - for messier fit, AR > 1 can still stop adaptation?
# samples of mu, cov. Storage for Gibbs samples
scov = None
smu = None
sm = None # cluster weight
mnd = None
# augmented variables
gz = None
pmdim = None
bPosInd = False
iSgs = None
i2pidcovs = None
i2pidcovsr = None
def __init__(self, ITERS, M, k):
"""
"""
oo = self
oo.M = M
oo.k = k
oo.ITERS = ITERS
# sampled variables
oo.scov = _N.zeros((oo.ITERS, M, k, k))
oo.smu = _N.empty((oo.ITERS, M, k))
oo.sm = _N.ones((oo.ITERS, M, 1))/M
###
#
# parameters of cluster covariance. Becomes prior
oo.po_cov_PSI = _N.empty((ITERS, M, k, k))
oo.po_cov_nu = _N.empty((ITERS, M), dtype=_N.int)
# priors
oo.PR_cov_PSI = _N.tile(_N.eye(k)*0.5, M).T.reshape(M, k, k)
oo.PR_cov_nu = _N.ones(M, dtype=_N.int)
# parameters of cluster mean. Becomes prior
oo.po_mu_mu = _N.zeros((ITERS, M, k))
oo.po_mu_sg = _N.empty((ITERS, M, k, k))
oo.PR_mu_mu = _N.zeros((M, k))
oo.PR_mu_sg = _N.tile(_N.eye(k)*30, M).T.reshape(M, k, k)
oo.iPR_mu_sg= _N.linalg.inv
(oo.PR_mu_sg)
# parameters of cluster weights
oo.PR_m_alp = _N.ones(M) * (1./M)
oo.po_alpha = _N.empty((ITERS, M))
### posterior parameters
oo.us = _N.zeros((M, k))
oo.covs = _N.zeros((M, k, k))
oo.ms = _N.zeros((M, 1))
# M initial guess # of clusters
# k
# pos, mk position and mark at spike time
def init0(self, pos, mk, n1, n2, sepHash=False, pctH=0.7, MS=None, sepHashMthd=0, doTouchUp=False, MF=None, kmeansinit=True):
"""
M total number of clusters
MS number of clusters assigned to signal
MF number of clusters used for initial fit
M - MF If doing touchup, number of clusters to assign to this
"""
print "init0"
oo = self
k = oo.k
MF = oo.M if MF is None else MF
print "MF %d" % MF
_x = _N.empty((n2-n1, k))
_x[:, 0] = pos
_x[:, 1:] = mk
N = n2-n1
# Gibbs sampling
################ init cluster centers
if sepHash: # treat hash spikes seperately
##########################
BINS = 20
bins = _N.linspace(-6, 6, BINS+1)
blksz = 20
unonhash, hashsp = _fu.sepHash(_x,BINS=BINS,blksz=20,xlo=-6,xhi=6)
MH = MF - MS
sigInds = unonhash
smkpos = _x[sigInds]
print smkpos
labS = _fu.spClstrs3MkCl(smkpos)
MSA = len(labS)
if MSA > MS:
MH = MF - MS
MS = MSA
labH = _fu.bestcluster(50, _x[hashsp], MH)
##################
lab = _N.array(labH.tolist() + (labS + MH).tolist())
x = _N.empty((n2-n1, k))
if sepHashMthd == 0:
x[:, 0] = _x[inds, 0]
x[:, 1:] = _x[inds, 1:]
else:
x[0:len(hashsp)] = _x[hashsp]
x[len(hashsp):] = _x[sigInds]
else: # don't separate hash from signal marks. simple kmeans2
x = _x
if not kmeansinit: # just random initial conditions
print "random initial conditions"
lab = _N.array(_N.random.rand(N)*MF, dtype=_N.int)
else:
ITERS = 20
labsAll = []
mAll = []
bics = _N.empty(ITERS)
for it in xrange(ITERS):
scr, lab = scv.kmeans2(x, MF)
_fu.contiguous_pack(lab)
bic, K = _fu.kmBIC(scr, lab, x)
bics[it] = bic
mAll.append(K)
labsAll.append(lab)
bestI = _N.where(bics == _N.max(bics))[0][0]
lab = labsAll[bestI]
MF = mAll[bestI]
# now assign the cluster we've found to Gaussian mixtures
SI = N / MF
covAll = _N.cov(x.T)
dcovMag= _N.diagonal(covAll)*0.005
for im in xrange(MF):
kinds = _N.where(lab == im)[0] # inds
if len(kinds) > 6: # problem when cov is not positive def.
oo.smu[0, im] = _N.mean(x[kinds], axis=0)
oo.scov[0, im] = _N.cov(x[kinds], rowvar=0)
oo.sm[0, im] = float(len(kinds)+1) / (N+MF)
else:
#oo.smu[0, im] = _N.mean(x[sigInds], axis=0)
oo.smu[0, im] = _N.mean(x, axis=0)
oo.scov[0, im] = covAll*0.125
oo.sm[0, im] = float(len(kinds)+1) / (N+MF)
def fit(self, M, pos, mk, n1, n2, init=False):
"""
Fit, with the inverting done in blocks
"""
oo = self
k = oo.k
mnd = oo.mnd
if (n2 - n1) > 0:
x = _N.empty((n2-n1, k))
x[:, 0] = pos
x[:, 1:] = mk
N = n2-n1
oo.pmdim = k
oo.gz = _N.zeros((oo.ITERS, N, M), dtype=_N.int)
oo.gz[:,:,:] = 0
if init:
oo.PR_m_alp[:] = 1. / M # initial
covAll = _N.cov(x.T)
dcovMag= _N.diagonal(covAll)*0.125
# termporary containers
expTrm = _N.empty((M, N))
expArg = _N.empty((M, N))
crats = _N.zeros((M+1, N))
rands = _N.random.rand(N, 1)
dirArgs = _N.empty(M, dtype=_N.int)
rsum = _N.empty((1, N))
skpM = _N.arange(0, N)*M
Nms = _N.empty((M, 1, 1), dtype=_N.int)
mcs = _N.empty((M, k))
clstxs= []
#### stuff used repeatedly
k_zeros = _N.zeros(k)
pr_iSg_Mu = _N.einsum("mjk,mk->mj", oo.iPR_mu_sg, oo.PR_mu_mu)
# if not kde:
# occ =
#
for it in xrange(oo.ITERS-1):
t1 = _tm.time()
if it % 50 == 0:
print it
iscov = _N.linalg.inv(oo.scov[it, 0:M])
#print iscov
norms = 1/_N.sqrt(2*_N.pi*_N.linalg.det(oo.scov[it, 0:M]))
norms = norms.reshape(M, 1)
t2 = _tm.time()
#### THIS IS THE BOTTLE NECK
for im in xrange(M):
expArg[im] = -0.5*_N.einsum("nj,nj->n", x - oo.smu[it, im], _N.dot(x - oo.smu[it, im], iscov[im]))
#expArg[im] = -0.5*_N.sum(_N.multiply((x-oo.smu[it, im]), _N.dot(x-oo.smu[it, im], iscov[im])), axis=1) # expArg[im] is size N
t3 = _tm.time()
rexpArg = expArg.T.reshape(M*N)
lrgInM = expArg.argmax(axis=0)
lrgstArgs = rexpArg[skpM+lrgInM]
expArg0 = expArg - lrgstArgs
expTrm = _N.exp(expArg0)
rats = oo.sm[it, 0:M]*expTrm*norms # shape is M x oo.N
_N.sum(rats, axis=0, out=rsum[0, :])
rats /= rsum # each column of "rats" sums to 1
for im in xrange(M):
crats[im+1] = rats[im] + crats[im]
t4 = _tm.time()
rands = _N.random.rand(N)
rrands = _N.tile(rands, M).reshape(M, N)
### THIS once broke because we had an empty cluster
irw, icl = _N.where((rrands >= crats[:-1]) & (rrands <= crats[1:]))
############## GENERATE cluster membership
oo.gz[it+1, icl, irw] = 1 # we must clean out gz
# For the
## For the j-th cluster, look at the std. dev in position space.
# from current it. value.
# Either put into it
# _N.sum(oo.gz...) sz M its vec of num. of obs of each state 'm'
#oo.smu[it, im]
_N.add(oo.PR_m_alp[0:M], _N.sum(oo.gz[it+1], axis=0), out=oo.po_alpha[it+1])
############## SAMPLE WEIGHTS
oo.sm[it+1, 0:M, 0] = _N.random.dirichlet(oo.po_alpha[it+1])
clstxs = []
mindss = []
mcs = _N.empty((M, k)) # cluster sample means
t5 = _tm.time()
for im in xrange(M): # 111111111111111
minds = _N.where(oo.gz[it+1, :, im] == 1)[0]
Nms[im,0,0] = minds.shape[0]
mindss.append(minds)
oo.po_mu_sg[it+1] = _N.linalg.inv(oo.iPR_mu_sg + Nms*iscov)
for im in xrange(M): # 222222222222222
if Nms[im,0,0] > 0:
clstx = x[mindss[im]]
mcs[im] = _N.mean(clstx, axis=0)
else:
clstx = k_zeros
mcs[im] = clstx
clstxs.append(clstx)
# hyp
######## POSITION
## mean of posterior distribution of cluster means
# sigma^2 and mu are the current Gibbs-sampled values
## mean of posterior distribution of cluster means
oo.po_mu_mu[it+1] = _N.einsum("mjk,mk->mj", oo.po_mu_sg[it+1], pr_iSg_Mu + Nms[:,:,0]*_N.einsum("mjk,mk->mj", iscov, mcs))
# dot(MATRIX, vector)
############## SAMPLE MEANS
# this can be done without
rn3 = _N.random.randn(M, k)
C = _N.linalg.cholesky(oo.po_mu_sg[it+1])
oo.smu[it+1] = oo.po_mu_mu[it+1] + _N.einsum("njk,nk->nj", C, rn3)
for im in xrange(M): # 3333333333333333
Nm = Nms[im,0,0]
if Nm >= oo.pmdim:
clstx = clstxs[im]
## dof of posterior distribution of cluster covariance
oo.po_cov_nu[it+1, im] = oo.PR_cov_nu[im] + Nm
## dof of posterior distribution of cluster covariance
oo.po_cov_PSI[it+1, im] = oo.PR_cov_PSI[im] + _N.dot((clstx - oo.smu[it+1, im]).T, (clstx-oo.smu[it+1, im]))
############## SAMPLE COVARIANCES
oo.scov[it+1, im] = s_u.sample_invwishart(oo.po_cov_PSI[it+1, im], oo.po_cov_nu[it+1, im])
else: # no marks assigned to this cluster
oo.scov[it+1, im] = oo.scov[it, im]
oo.smu[it+1, im] = oo.smu[it, im]
oo.po_mu_sg[it+1, im] = oo.PR_mu_sg[im]
oo.po_mu_mu[it+1, im] = oo.PR_mu_mu[im]
oo.po_cov_nu[it+1, im] = oo.PR_cov_nu[im]
## dof of posterior distribution of cluster covariance
oo.po_cov_PSI[it+1, im] = oo.PR_cov_PSI[im]
t6 = _tm.time()
# print "-----"
# print (t2-t1)
# print (t3-t2)
# print (t4-t3)
# print (t5-t4)
# print (t6-t5)
# When I say prior for mu, I mean I have hyper parameters mu_mu and mu_sg.
# hyperparameters are not sampled
print oo.po_alpha[oo.ITERS-1]
hITERS = int(oo.ITERS*0.75)
oo.us[:] = _N.mean(oo.smu[hITERS:oo.ITERS], axis=0)
oo.covs[:] = _N.mean(oo.scov[hITERS:oo.ITERS], axis=0)
oo.ms[:] = _N.mean(oo.sm[hITERS:oo.ITERS], axis=0).reshape(oo.M, 1)
oo.dat = x
else:
print "NO DATA for this encoding epoch. Not doing anything."
print "!!!!!!!!!!!!!!!!!!!!"
print oo.PR_m_alp[0:M]
print _N.sum(oo.gz[oo.ITERS-1], axis=0)
print oo.po_alpha[oo.ITERS-1]
print "^-^^^^^^^^^^^^^^^^"
###
### length of previous (encoding episode + decoding episode) * AR
### AR = adaptation rate - per minute
###
###
def set_priors_and_initial_values(self, telapse=0):
"""
after a first run,
telapse in ms. AR
"""
oo = self
mid = oo.ITERS/2
# hyperparameters describe the priors, and are estimated from the
# posterior of the parameter
# the posteriors are now priors
oo.PR_m_alp[:] = _N.mean(oo.po_alpha[mid:], axis=0)
with open('po_alpha.txt', 'a') as fh:
_N.savetxt(fh, oo.PR_m_alp.reshape(1, oo.M), fmt=("%.3e " * oo.M))
fh.close()
print "========= set_priors"
print oo.PR_m_alp
print "========= set_priors"
print oo.PR_m_alp
# prior of cluster center is current
# posterior distribution of cluster center
tS = _N.sqrt(telapse / 60000.)
oo.PR_mu_mu[:] = _N.mean(oo.po_mu_mu[mid:], axis=0)
oo.PR_mu_sg[:] = _N.mean(oo.po_mu_sg[mid:], axis=0)*(oo.AR0+oo.AR*tS)
print "adaptation factor %.3f" % (oo.AR0+oo.AR*tS)
#oo.PR_mu_sg[:] = _N.mean(oo.po_mu_sg[mid:], axis=0)*5
#oo.PR_mu_sg[:] = _N.mean(oo.po_mu_sg[mid:], axis=0)*1.08
oo.iPR_mu_sg = _N.linalg.inv(oo.PR_mu_sg)
# prior of cluster center is current
# posterior distribution of cluster center
oo.PR_cov_nu[:] = _N.mean(oo.po_cov_nu[mid:], axis=0)
oo.PR_cov_PSI[:] = _N.mean(oo.po_cov_PSI[mid:], axis=0)
# last sampled values will be starting values
oo.sm[0] = oo.sm[oo.ITERS-1]
oo.smu[0] = oo.smu[oo.ITERS-1]
oo.scov[0] = oo.scov[oo.ITERS-1]
def evalAll(self, Ngrd):
oo = self
x0 = min(oo.dat[:, 0])
x1 = max(oo.dat[:, 0])
y0 = min(oo.dat[:, 1])
y1 = max(oo.dat[:, 1])
x = _N.linspace(x0, x1, Ngrd)#.reshape(Ngrd, 1)
y = _N.linspace(y0, y1, Ngrd)#.reshape(1, Ngrd)
xg, yg = _N.meshgrid(x, y)
xy = _N.array([xg, yg])
xy = xy.reshape(oo.pmdim, Ngrd*Ngrd)
# xy.T goes from lower left, scans right, to upper right
us = _N.mean(oo.smu[100:], axis=0)
Sgs = _N.mean(oo.scov[100:], axis=0)
iSgs= _N.linalg.inv(Sgs)
cmps= _N.empty((oo.M, Ngrd, Ngrd))
for m in xrange(oo.M):
cmps[m] = 1/_N.sqrt(2*_N.pi*_N.linalg.det(Sgs[m]))*_N.exp(-0.5*_N.sum(_N.multiply(xy.T-us[m], _N.dot(iSgs[m], (xy.T - us[m]).T).T), axis=1)).reshape(Ngrd, Ngrd)
ms = _N.mean(oo.sm[100:], axis=0).reshape(oo.M, 1, 1)
zs = ms*cmps
smpMn = _N.mean(oo.dat, axis=0)
_plt.scatter(oo.dat[:, 0], oo.dat[:, 1], s=10)
_plt.imshow(_N.sum(zs, axis=0), origin="lower", extent=(x0, x1, y0, y1))
return zs
def evalAtFxdMks_new(self, fxdMks):
oo = self
Nx = fxdMks.shape[0]
fxdMksr= fxdMks.reshape(Nx, 1, oo.pmdim)
cmps = oo.i2pidcovsr*_N.exp(-0.5*_N.einsum("xmj,xmj->mx", fxdMksr-oo.us, _N.einsum("mjk,xmk->xmj", oo.iSgs, fxdMksr - oo.us)))
zs = _N.sum(oo.ms*cmps, axis=0)
return zs