-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gibbs_sampler_multivariate_Gaussian_prot.py
353 lines (258 loc) · 14 KB
/
Gibbs_sampler_multivariate_Gaussian_prot.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
from __future__ import division
from pylab import *
import numpy as np
import scipy.stats as sts
import useful_functions as uf
def make_param_dict(k_0, v_0, mu_0, Sigma_0,d):
'''make a dictionary with the prior parameters for Gibbs sampling.
updatated parameters will be the 'up_...' entries of the dictionary,
they begin with the same values as the priors.
Parameters
--------
k_0: float-like
prior for the 'confidence' (pseudo-counts) on the prior mean.
v_0: float-like
prior for the 'confidence' (pseudo-counts) on the prior covariance matrix.
mu_0: array-like
prior for the mean. An n-dimensional vector representing the center of the distribution.
Sigma_0: array like.
Prior for the covariance matrix. An nxn positive definite matrix.
Output
--------
Parameter dictionary with all the relevant parameters for the Normal Wishart model.
'''
return {'k_0': k_0, 'up_k_0': k_0 , 'v_0': v_0, 'up_v_0': v_0, 'mu_0':mu_0, 'up_mu_0':mu_0, \
'Sigma_0':Sigma_0, 'up_Sigma_0':Sigma_0, 'd':d}
def update_param_dict(X, param_dict, chol=False):
'''Update the parameter dictionary with sufficient statistics for the multivariate
Gaussian model, obtained from some data (assumed to be iid mv Gaussian).
Parameters
--------
X: array-like
Vector of observations assumed to be iid mvGaussian. len(X) should return the number of observations,
and len(X[0]) the dimensionallity. (shape = (n, d)).
param_dict: python-dict
dictionary created by the function 'make_param_dict' with the relevant prior values.
Output
--------
updates the 'up_...' entries of the param_dict many of the features may be commented
out if only a specific subset is needed'''
#obtain sufficient statistics
SS = uf.store_sufficient_statistics_mvGaussian(X)
k_n = param_dict['k_0']+SS['n']
v_n = param_dict['v_0']+SS['n']
mu_prec = SS['E_mu']-param_dict['mu_0']
mu_prec.shape = (len(param_dict['mu_0']),1)
mu_prec = mu_prec.dot(mu_prec.T)
mu_n = ((param_dict['k_0']*param_dict['mu_0'])+(k_n*SS['E_mu']))/k_n
Sigma_n = param_dict['Sigma_0'] + SS['S_m'] + ((param_dict['k_0']*SS['n'])/k_n)*mu_prec
#invert the precision matrix and take the Cholesky decomposition
if chol:
chol_Sigma, chol_Prec, Prec_n = uf.inv_and_chol(Sigma_n, chol_of_A = 1, chol_of_invA=1)
else:
Prec_n =np.linalg.inv(Sigma_n)
#update the parameters
param_dict['up_k_0']= k_n #relative precision of the mean
param_dict['up_v_0']= v_n #relative precision of the variance
param_dict['up_mu_0']= mu_n #model mean
param_dict['up_Sigma_0']= Sigma_n #model covm
param_dict['up_Prec_0'] = Prec_n #model precision matrix
param_dict['chol_Sigma'] = chol_Sigma #Cholesky dec of the model covm
param_dict['chol_Prec'] = chol_Prec #Cholesky dec of the model precision matrix
param_dict['d'] = len(mu_n) #dimensions
param_dict['n'] = SS['n'] #number of samples
param_dict['E_mu'] = SS['E_mu'] #empirical mean for non-informative priors
param_dict['S_m'] = SS['S_m'] #scatter matrix for non-informative priors
param_dict['chol_S_m'], param_dict['chol_invS_m'], param_dict['invS_m'] = uf.inv_and_chol(SS['S_m'], chol_of_A=1, chol_of_invA=1) #Cholesky dec
#of the inverse of the scatter matrix and the inverse of the scatter matrix.
def Gibbs_sample(param_dict, chol=False, non_inf = False):
'''Draw a sample from the Normal-Wishart model.
Based on Gelman et al.(2013, 3 ed., chapter 4).
Parameters
---------
param_dict: python-dict
dictionary with sufficient statistics and parameters from a multivariate data-set,
obtained through the functions 'make_param_dict' and 'update_param_dict'.
chol: Boolean.
Assume the the input matrices were provided in the form of the lower Cholesky decomposition.
non_inf: Boolean.
This models tends weight heavily the distance from the prior to the empirical mean.
If a non-informative prior is used, it's advisable to use this option. The parametrization
will correspond to the multivariate Jeffreys prior density.
Output
--------
Returns a d-dimensional draw from the Normal-Wishart model. For multiple samples,
use the function 'Gibbs_sampler' with one of options:
('full'): chol=False, non_inf = False
('nonInfFull'): chol=False, non_inf = True
('chol'): chol=True, non_inf = False
('nonInfChol'): chol=True, non_inf = True'''
if chol and non_inf:#sample from a non-informative prior, using the Cholesky decomposition
Chol_prec_m = uf.Wishart_rvs(df = param_dict['n']-1., S = param_dict['chol_invS_m'], chol=1)
mu = uf.multivariate_Gaussian_rvs(param_dict['E_mu'], sqrt((param_dict['n']))*Chol_prec_m, chol=1)
return uf.multivariate_Gaussian_rvs(mu, Chol_prec_m, chol=1)
elif chol and not non_inf: #sample from an informative prior using the Cholesky decomposition
Chol_prec_m = uf.Wishart_rvs(param_dict['up_v_0'], S = param_dict['chol_Prec'], chol=1)
mu = uf.multivariate_Gaussian_rvs(param_dict['up_mu_0'], sqrt((param_dict['up_k_0']))*Chol_prec_m, chol=1)
return uf.multivariate_Gaussian_rvs(mu, Chol_prec_m, chol=1)
elif non_inf and not chol: #sample from a non-informative prior
prec_m = uf.Wishart_rvs(df=param_dict['n']-1, S=param_dict['invS_m'])
mu = uf.multivariate_Gaussian_rvs(mu=param_dict['E_mu'], prec_m=(param_dict['n'])*prec_m)
return uf.multivariate_Gaussian_rvs(mu, prec_m)
else: #sample from an informative prior
prec_m = uf.Wishart_rvs(df=param_dict['up_v_0'], S=param_dict['up_Prec_0'])
mu = uf.multivariate_Gaussian_rvs(mu=param_dict['up_mu_0'], prec_m=(param_dict['up_k_0'])*prec_m)
return uf.multivariate_Gaussian_rvs(mu, prec_m)
def Gibbs_sample_slow_scipy_version(param_dict, non_inf=False):
'''Draw a sample from the Normal-Wishart model using the buit-in scipy distributions( significantly slower).
Based on Gelman et al.(2013, 3 ed., chapter 4).
Parameters
---------
param_dict: python-dict
dictionary with sufficient statistics and parameters from a multivariate data-set,
obtained through the functions 'make_param_dict' and 'update_param_dict'.
non_inf: Boolean.
This models tends weight heavily the distance from the prior to the empirical mean.
If a non-informative prior is used, it's advisable to use this option. The parametrization
will correspond to the multivariate Jeffreys prior density.
Output
--------
Returns a d-dimensional draw from the Normal-Wishart model. For multiple samples,
use the function 'Gibbs_sampler' with one of the options:
('slow'): non_inf = False
('nonInfSlow'): non_inf = True
'''
if non_inf:
Prec_m = sts.wishart(df = param_dict['n']-1., scale=param_dict['invS_m']).rvs()
Sigma_m = np.linalg.inv(Prec_m)
mu = sts.multivariate_normal(mean= param_dict['E_mu'], cov=(1./param_dict['n'])*Sigma_m).rvs()
return sts.multivariate_normal(mean=mu, cov = Sigma_m).rvs()
else:
Prec_m = sts.wishart(df = param_dict['up_v_0'], scale=param_dict['up_Prec_0']).rvs()
Sigma_m = np.linalg.inv(Prec_m)
mu = sts.multivariate_normal(mean= param_dict['up_mu_0'], cov=(1./param_dict['up_k_0'])*Sigma_m).rvs()
return sts.multivariate_normal(mean=mu, cov = Sigma_m).rvs()
def collapsed_Gibbs_sampler(t, param_dict, chol=False, non_inf=False):
'''Draw a sample from the Normal-Wishart model using a collpased Gibbs sampler.
Based on Gelman et al.(2013, 3 ed., chapter 4) and Murphy (2007, eq. 240 for the non-informative prior).
Parameters
---------
param_dict: python-dict
dictionary with sufficient statistics and parameters from a multivariate data-set,
obtained through the functions 'make_param_dict' and 'update_param_dict'.
non_inf: Boolean.
This models tends weight heavily the distance from the prior to the empirical mean.
If a non-informative prior is used, it's advisable to use this option. The parametrization
will correspond to the multivariate Jeffreys prior density.
Output
--------
Returns t draws d-dimensional from the collapesed version of Normal-Wishart model.
use the function 'Gibbs_sampler' with one of options:
('ColpsedFull'): chol=False, non_inf = False
('ColpsedNonInfFull'): chol=False, non_inf = True
('ColpsedChol'): chol=True, non_inf = False
('ColpsedNonInfChol'): chol=True, non_inf = True'''
if chol and non_inf:#sample from a non-informative prior, using the Cholesky decomposition
df = param_dict['n']-param_dict['d']
var_coeff = math.sqrt((param_dict['n']+1.)/(param_dict['n']*df))
return uf.multivariate_t_rvs_chol(mu = param_dict['E_mu'], L = var_coeff*param_dict['chol_S_m'], df=df, n=t)
elif chol and not non_inf: #sample from an informative prior using the Cholesky decomposition
df = param_dict['up_v_0']-param_dict['d']+1.
var_coeff = math.sqrt((param_dict['up_k_0']+1.)/(param_dict['up_k_0']*df))
return uf.multivariate_t_rvs_chol(mu=param_dict['up_mu_0'], L= var_coeff*param_dict['chol_Sigma'], df=df, n=t)
elif non_inf and not chol: #sample from a non-informative prior
df = param_dict['n']-param_dict['d']
var_coeff = (param_dict['n']+1.)/(param_dict['n']*df)
return uf.multivariate_t_rvs(mu = param_dict['E_mu'], S = var_coeff*param_dict['S_m'], df=df, n=t)
else: #sample from an informative prior
df = param_dict['up_v_0']-param_dict['d']+1.
var_coeff = (param_dict['up_k_0']+1.)/(param_dict['up_k_0']*df)
return uf.multivariate_t_rvs(param_dict['up_mu_0'], var_coeff*param_dict['up_Sigma_0'], df=df, n=t)
def Gibbs_sampler(param_dict, t, v='chol'):
'''
Gibbs_sample(param_dict, chol=False, non_inf = False)
('full'): chol=False, non_inf = False
('nonInfFull'): chol=False, non_inf = True
('chol'): chol=True, non_inf = False
('nonInfChol'): chol=True, non_inf = True
Gibbs_sample_slow_scipy_version(param_dict, non_inf=False)
('slow'): non_inf = False
('nonInfSlow'): non_inf = True
collapsed_Gibbs_sampler(t, param_dict, chol=False, non_inf=False)
('ColpsedFull'): chol=False, non_inf = False
('ColpsedNonInfFull'): chol=False, non_inf = True
('ColpsedChol'): chol=True, non_inf = False
('ColpsedNonInfChol'): chol=True, non_inf = True'''
if v=='full':
s_dict = {i: Gibbs_sample(param_dict, chol=0, non_inf=0) for i in xrange(t)}
elif v=='nonInfFull':
s_dict = {i: Gibbs_sample(param_dict, chol=0, non_inf=1) for i in xrange(t)}
elif v== 'chol':
s_dict = {i: Gibbs_sample(param_dict, chol=1, non_inf=0) for i in xrange(t)}
elif v== 'nonInfChol':
s_dict = {i: Gibbs_sample(param_dict, chol=1, non_inf=1) for i in xrange(t)}
elif v=='slow':
s_dict = {i: Gibbs_sample_slow_scipy_version(param_dict, non_inf=0) for i in xrange(t)}
elif v=='nonInfSlow':
s_dict = {i: Gibbs_sample_slow_scipy_version(param_dict, non_inf=1) for i in xrange(t)}
elif v=='ColpsedFull':
s = collapsed_Gibbs_sampler(t=t, param_dict=param_dict, chol=0, non_inf=0)
s_dict={i:s[i] for i in xrange(t)}
elif v=='ColpsedNonInfFull':
s = collapsed_Gibbs_sampler(t=t, param_dict=param_dict, chol=0, non_inf=1)
s_dict={i:s[i] for i in xrange(t)}
elif v=='ColpsedChol':
s = collapsed_Gibbs_sampler(t=t, param_dict=param_dict, chol=1, non_inf=0)
s_dict={i:s[i] for i in xrange(t)}
elif v=='ColpsedNonInfChol':
s = collapsed_Gibbs_sampler(t=t, param_dict=param_dict, chol=1, non_inf=1)
s_dict={i:s[i] for i in xrange(t)}
return np.array([s_dict[i] for i in xrange(t)]), s_dict
d = 2
n=600
#A = np.random.randint(2,10)*np.random.rand(d,d)
#A = A.dot(A.T)
#A = np.diag(np.random.uniform(0,1000,d))
mu = np.random.uniform(0,1000,d)
A = np.array([[11, 0.5*(sqrt(11*2))],[0.5*(sqrt(11*2)), 2]])
#mu = np.array([2, 33])
X = np.random.multivariate_normal(mu, A, n)
param_dict = make_param_dict(k_0=1, v_0=-1, mu_0=np.zeros(d), Sigma_0=np.eye(d),d=d)
update_param_dict(X, param_dict, chol=1)
import time
t= time.time()
draws=50000
s1, s_d1 = Gibbs_sampler(param_dict, draws, v = 'full')
s2, s_d2 = Gibbs_sampler(param_dict, draws, v = 'nonInfFull')
s3, s_d3 = Gibbs_sampler(param_dict, draws, v = 'chol')
s4, s_d4 = Gibbs_sampler(param_dict, draws, v = 'nonInfChol')
s5, s_d5 = Gibbs_sampler(param_dict, draws, v = 'slow')
s6, s_d6 = Gibbs_sampler(param_dict, draws, v = 'nonInfSlow')
s7, s_d7 = Gibbs_sampler(param_dict, draws, v = 'ColpsedFull')
s8, s_d8 = Gibbs_sampler(param_dict, draws, v = 'ColpsedNonInfFull')
s9, s_d9 = Gibbs_sampler(param_dict, draws, v = 'ColpsedChol')
s10, s_d10 = Gibbs_sampler(param_dict, draws, v = 'ColpsedNonInfChol')
print time.time() -t
for i in xrange(len(X.T)):
print 'data', '\t', var(X.T[i]), '\n'
print 'noninffull', '\t', var(s2.T[i]), '\n'
print 'noninfchol', '\t', var(s4.T[i]), '\n'
print 'noninfslow', '\t', var(s6.T[i]), '\n'
print 'colpsednoninffull', '\t', var(s8.T[i]), '\n'
print 'colpsednoninfchol', '\t', var(s10.T[i]), '\n'
print 'full', '\t', var(s1.T[i]), '\n'
print 'chol', '\t', var(s3.T[i]), '\n'
print 'slow', '\t', var(s5.T[i]), '\n'
print 'clpsedfull', '\t', var(s7.T[i]), '\n'
print 'clpsedchol', '\t', var(s9.T[i]), '\n'
scatter(s1.T[0], s1.T[1], c='g', alpha=0.2)
scatter(s3.T[0], s3.T[1], c='g', alpha=0.2)
scatter(s5.T[0], s5.T[1], c='g', alpha=0.2)
scatter(s7.T[0], s7.T[1], c='g', alpha=0.2)
scatter(s9.T[0], s9.T[1], c='g', alpha=0.2)
scatter(s2.T[0], s2.T[1], c='r', alpha=0.2)
scatter(s4.T[0], s4.T[1], c='r', alpha=0.2)
scatter(s6.T[0], s6.T[1], c='r', alpha=0.2)
scatter(s8.T[0], s8.T[1], c='r', alpha=0.2)
scatter(s10.T[0], s10.T[1], c='r', alpha=0.2)
scatter(X.T[0], X.T[1], c='b',)