This repository has been archived by the owner on Aug 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logistic-probit-regression.py
486 lines (444 loc) · 18.7 KB
/
logistic-probit-regression.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# -*- coding: utf-8 -*-
""" Future warning
http://numpy-discussion.10968.n7.nabble.com/update-to-numpy-1-5-0-gives-new-warnings-from-scipy-td45974.html#a45975
"""
import numpy as np
import scipy.stats as spstats
from scipy import signal
import pickle
from multiprocessing import Pool
import multiprocessing
class potentialRegression:
""" implementing a potential U = logarithm of the posterior distribution
given by a Bayesian regression
- Linear
- Logistic
- Probit
"""
varY = 1 # Variance of the linear likelihood
varTheta = 100 # Variance of the prior Gaussian distribution
def __init__(self,Y,X,typ):
""" initialisation
Args:
Y: observations
X: covariates
typ: type of the regression, Linear, Logistic or Probit
"""
self.Y = Y
self.X = X
self.type = typ
self.p, self.d = X.shape
def loglikelihood(self,theta):
""" loglikelihood of the Bayesian regression
Args:
theta: parameter of the state space R^d where the likelihood is
evaluated
Returns:
real value of the likelihood evaluated at theta
"""
if self.type == "g": # Linear regression
return -(1. / (2*self.varY))* np.linalg.norm(self.Y-np.dot(self.X,theta))**2 \
- (self.d/2.)*np.log(2*np.pi*self.varY)
elif self.type == "l": # Logistic
XTheta = np.dot(self.X, theta)
temp1 = np.dot(self.Y, XTheta)
temp2 = -np.sum(np.log(1+np.exp(XTheta)))
return temp1+temp2
else: # Probit
cdfXTheta = spstats.norm.cdf(np.dot(self.X, theta))
cdfMXTheta = spstats.norm.cdf(-np.dot(self.X, theta))
temp1 = np.dot(self.Y, np.log(cdfXTheta))
temp2 = np.dot((1 - self.Y), np.log(cdfMXTheta))
return temp1+temp2
def gradloglikelihood(self, theta):
""" gradient of the loglikelihood of the Bayesian regression
Args:
theta: parameter of the state space R^d where the gradient of the
likelihood is evaluated
Returns:
R^d vector of the gradient of the likelihood evaluated at theta
"""
if self.type == "g": # Linear
temp1 = np.dot(np.dot(np.transpose(self.X), self.X), theta)
temp2 = np.dot(np.transpose(self.X), self.Y)
return (1. / self.varY)*(temp2 - temp1)
elif self.type == "l": # Logistic
temp1 = np.exp(np.dot(self.X, theta))
temp2 = np.dot(np.transpose(self.X), self.Y)
temp3 = np.dot(np.transpose(self.X), np.divide(temp1, 1+temp1))
return temp2 - temp3
else: # Probit
XTheta = np.dot(self.X, theta)
logcdfXTheta = np.log(spstats.norm.cdf(XTheta))
logcdfMXTheta = np.log(spstats.norm.cdf(-XTheta))
temp1 = np.multiply(self.Y, np.exp(-0.5*(np.square(XTheta)+np.log(2*np.pi)) \
-logcdfXTheta))
temp2 = np.multiply((1 -self.Y), np.exp(-0.5*(np.square(XTheta)+np.log(2*np.pi)) \
-logcdfMXTheta))
return np.dot(np.transpose(self.X), temp1-temp2)
def logprior(self, theta):
""" logarithm of the prior distribution, which is a Gaussian distribution
of variance varTheta
Args:
theta: parameter of R^d where the log prior is evaluated
Returns:
real value of the log prior evaluated at theta
"""
return -(1. / (2*self.varTheta))* np.linalg.norm(theta)**2 \
- (self.d/2.)*np.log(2*np.pi*self.varTheta)
def gradlogprior(self, theta):
""" gradient of the logarithm of the prior distribution, which is
a Gaussian distribution of variance varTheta
Args:
theta: parameter of R^d where the gradient log prior is evaluated
Returns:
R^d vector of the gradient of the log prior evaluated at theta
"""
return -(1. / self.varTheta)*theta
def potential(self, theta):
""" logarithm of the posterior distribution
Args:
theta: parameter of R^d where the log posterior is evaluated
Returns:
real value of the log posterior evaluated at theta
"""
return -self.loglikelihood(theta)-self.logprior(theta)
def gradpotential(self, theta):
""" gradient of the logarithm of the posterior distribution
Args:
theta: parameter of R^d where the gradient log posterior is evaluated
Returns:
R^d vector of the gradient log posterior evaluated at theta
"""
return -self.gradloglikelihood(theta)-self.gradlogprior(theta)
""" Samplers ULA, MALA, RWM """
def ULA(step, N, n):
""" MCMC ULA
Args:
step: stepsize of the algorithm
N: burn-in period
n: number of samples after the burn-in
Returns:
traj: a numpy array of size (n, d), where the trajectory is stored
traj_grad: numpy array of size (n, d), where the gradients of the
potential U along the trajectory are stored
"""
traj = np.zeros((n, d))
traj_grad = np.zeros((n, d))
x = np.random.normal(scale=5.0, size=d) # initial value X_0
for k in np.arange(N): # burn-in period
x = x - step * potential.gradpotential(x) \
+ np.sqrt(2*step)*np.random.normal(size=d)
for k in np.arange(n): # samples
grad = potential.gradpotential(x)
traj[k,]=x
traj_grad[k,]=grad
x = x - step * grad + np.sqrt(2*step)*np.random.normal(size=d)
return (traj, traj_grad)
def MALA(step, N, n):
""" MCMC MALA
Args:
step: stepsize of the algorithm
N: burn-in period
n: number of samples after the burn-in
Returns:
traj: a numpy array of size (n, d), where the trajectory is stored
traj_grad: numpy array of size (n, d), where the gradients of the
potential U along the trajectory are stored
"""
U = potential.potential
grad_U = potential.gradpotential
traj = np.zeros((n, d))
traj_grad = np.zeros((n, d))
x = np.random.normal(scale=5.0, size=d)
for k in np.arange(N):
y = x - step * grad_U(x) + np.sqrt(2*step)*np.random.normal(size=d)
logratio = -U(y)+U(x) + (1./(4*step))*(np.linalg.norm(y-x+step*grad_U(x))**2 \
- np.linalg.norm(x-y+step*grad_U(y))**2)
if np.log(np.random.uniform())<=logratio:
x = y
for k in np.arange(n):
traj[k,]=x
traj_grad[k,]=grad_U(x)
y = x - step * grad_U(x) + np.sqrt(2*step)*np.random.normal(size=d)
logratio = -U(y)+U(x)+(1./(4*step))*(np.linalg.norm(y-x+step*grad_U(x))**2 \
-np.linalg.norm(x-y+step*grad_U(y))**2)
if np.log(np.random.uniform())<=logratio:
x = y
return (traj, traj_grad)
def RWM(step, N, n):
""" MCMC RWM
Args:
step: stepsize of the algorithm
N: burn-in period
n: number of samples after the burn-in
Returns:
traj: a numpy array of size (n, d), where the trajectory is stored
traj_grad: numpy array of size (n, d), where the gradients of the
potential U along the trajectory are stored
"""
U = potential.potential
grad_U = potential.gradpotential # for control variates only
traj = np.zeros((n, d))
traj_grad = np.zeros((n, d))
x = np.random.normal(scale=5.0, size=d)
for k in np.arange(N):
y = x + np.sqrt(2*step)*np.random.normal(size=d)
logratio = -U(y)+U(x)
if np.log(np.random.uniform())<=logratio:
x = y
for k in np.arange(n):
traj[k,]=x
traj_grad[k,]=grad_U(x)
y = x + np.sqrt(2*step)*np.random.normal(size=d)
logratio = -U(y)+U(x)
if np.log(np.random.uniform())<=logratio:
x = y
return (traj, traj_grad)
""" Control Variates and estimators for mean, asymptotic variance """
def normalSamples(traj,traj_grad):
""" Computation of the empirical means of \theta and \theta^2, first and
second order moments, along the trajectory, and of the associated
asymptotic variance, using a spectral variance estimator
Args:
traj: numpy array (n, d) that contains the trajectory of the MCMC algorithm
traj_grad: numpy array (n, d) that contrains the gradients of the
log posterior evaluated along the trajectory
Returns:
mean_samples: numpy array of size 2*d, containing the means of \theta
and \theta^2
var_samples: numpy array of size 2*d, containing the asymptotic
variances of \theta and \theta^2
"""
n, d = traj.shape
samples = np.concatenate((traj, np.square(traj)), axis=1)
mean_samples = np.mean(samples, axis=0)
temp1 = samples - mean_samples
# Batch Means and spectral variance estimators Flegal and Jones, 2010
var_samples = np.empty(2*d)
for i in np.arange(2*d):
tp1 = (1./n)*signal.fftconvolve(temp1[:,i], temp1[::-1,i], mode="same")
tp1 = tp1[:(int(n/2)+1)]
tp1 = tp1[::-1]
gam0 = tp1[0]
bn = int(n**(1./2))
wn = 1./2 + (1./2)*np.cos((np.pi/bn)*np.arange(bn))
var_samples[i]= -gam0+2*np.dot(wn, tp1[:bn])
return (mean_samples, var_samples)
def CVpolyOne(traj,traj_grad):
""" Computation of the control variates estimator based on 1st order
polynomials, CV1, of \theta and \theta^2, first and
second order moments, along the trajectory, and of the associated
asymptotic variance, using a spectral variance estimator.
Args:
traj: numpy array (n, d) that contains the trajectory of the MCMC algorithm
traj_grad: numpy array (n, d) that contrains the gradients of the
log posterior evaluated along the trajectory
Returns:
mean_CV1: numpy array of size 2*d, containing CV1 applied with
the test functions \theta and \theta^2
var_CV1: numpy array of size 2*d, containing the asymptotic
variances of CV1 applied with the test functions \theta and \theta^2
"""
n, d = traj.shape
samples = np.concatenate((traj, np.square(traj)), axis=1)
covariance = np.cov(np.concatenate((traj, samples), axis=1), rowvar=False)
paramCV1 = covariance[:d, d:]
CV1 = samples - np.dot(traj_grad, paramCV1)
mean_CV1 = np.mean(CV1, axis=0)
CV1 -= mean_CV1
var_CV1 = np.empty(2*d)
for i in np.arange(2*d):
tp1 = (1./n)*signal.fftconvolve(CV1[:,i], CV1[::-1,i], mode="same")
tp1 = tp1[:(int(n/2)+1)]
tp1 = tp1[::-1]
gam0 = tp1[0]
bn = int(n**(1./2))
wn = 1./2 + (1./2)*np.cos((np.pi/bn)*np.arange(bn))
var_CV1[i]= -gam0+2*np.dot(wn, tp1[:bn])
return (mean_CV1, var_CV1)
def CVpolyTwo(traj, traj_grad):
""" Computation of the control variates estimator based on 2nd order
polynomials, CV2, of \theta and \theta^2, first and
second order moments, along the trajectory, and of the associated
asymptotic variance, using a spectral variance estimator.
Args:
traj: numpy array (n, d) that contains the trajectory of the MCMC algorithm
traj_grad: numpy array (n, d) that contrains the gradients of the
log posterior evaluated along the trajectory
Returns:
mean_CV2: numpy array of size 2*d, containing CV2 applied with
the test functions \theta and \theta^2
var_CV2: numpy array of size 2*d, containing the asymptotic
variances of CV2 applied with the test functions \theta and \theta^2
"""
n, d = traj.shape
samples = np.concatenate((traj, np.square(traj)), axis=1)
poisson = np.zeros((n,int(d*(d+3)/2)))
poisson[:,np.arange(d)] = traj
poisson[:,np.arange(d, 2*d)] = np.multiply(traj, traj)
k = 2*d
for j in np.arange(d-1):
for i in np.arange(j+1,d):
poisson[:,k] = np.multiply(traj[:,i], traj[:,j])
k=k+1
Lpoisson = np.zeros((n,int(d*(d+3)/2)))
Lpoisson[:,np.arange(d)] = - traj_grad
Lpoisson[:,np.arange(d, 2*d)] = 2*(1. - np.multiply(traj, traj_grad))
k=2*d
for j in np.arange(d-1):
for i in np.arange(j+1,d):
Lpoisson[:,k] = -np.multiply(traj_grad[:,i], traj[:,j]) \
-np.multiply(traj_grad[:,j], traj[:,i])
k=k+1
cov1 = np.cov(np.concatenate((poisson, -Lpoisson), axis=1), rowvar=False)
A = np.linalg.inv(cov1[0:int(d*(d+3)/2), int(d*(d+3)/2):d*(d+3)])
cov2 = np.cov(np.concatenate((poisson, samples),axis=1), rowvar=False)
B = cov2[0:int(d*(d+3)/2), int(d*(d+3)/2):]
paramCV2 = np.dot(A,B)
CV2 = samples + np.dot(Lpoisson, paramCV2)
mean_CV2 = np.mean(CV2, axis=0)
CV2 -= mean_CV2
var_CV2 = np.empty(2*d)
for i in np.arange(2*d):
tp1 = (1./n)*signal.fftconvolve(CV2[:,i], CV2[::-1,i], mode="same")
tp1 = tp1[:(int(n/2)+1)]
tp1 = tp1[::-1]
gam0 = tp1[0]
bn = int(n**(1./2))
wn = 1./2 + (1./2)*np.cos((np.pi/bn)*np.arange(bn))
var_CV2[i]= -gam0+2*np.dot(wn, tp1[:bn])
return (mean_CV2, var_CV2)
def ZVpolyOne(traj, traj_grad):
""" Computation of the zero variance estimator based on 1st order
polynomials, ZV1, of \theta and \theta^2, first and
second order moments, along the trajectory, and of the associated
asymptotic variance, using a spectral variance estimator.
Args:
traj: numpy array (n, d) that contains the trajectory of the MCMC algorithm
traj_grad: numpy array (n, d) that contrains the gradients of the
log posterior evaluated along the trajectory
Returns:
mean_ZV1: numpy array of size 2*d, containing ZV1 applied with
the test functions \theta and \theta^2
var_ZV1: numpy array of size 2*d, containing the asymptotic
variances of ZV1 applied with the test functions \theta and \theta^2
"""
n, d = traj.shape
samples = np.concatenate((traj, np.square(traj)), axis=1)
cov1 = np.cov(traj_grad, rowvar=False)
A = np.linalg.inv(cov1)
covariance = np.cov(np.concatenate((-traj_grad, samples), axis=1), rowvar=False)
paramZV1 = -np.dot(A,covariance[:d, d:])
ZV1 = samples - np.dot(traj_grad, paramZV1)
mean_ZV1 = np.mean(ZV1, axis=0)
ZV1 -= mean_ZV1
var_ZV1 = np.empty(2*d)
for i in np.arange(2*d):
tp1 = (1./n)*signal.fftconvolve(ZV1[:,i], ZV1[::-1,i], mode="same")
tp1 = tp1[:(int(n/2)+1)]
tp1 = tp1[::-1]
gam0 = tp1[0]
bn = int(n**(1./2))
wn = 1./2 + (1./2)*np.cos((np.pi/bn)*np.arange(bn))
var_ZV1[i]= -gam0+2*np.dot(wn, tp1[:bn])
return (mean_ZV1, var_ZV1)
def ZVpolyTwo(traj, traj_grad):
""" Computation of the zero variance estimator based on 2nd order
polynomials, ZV2, of \theta and \theta^2, first and
second order moments, along the trajectory, and of the associated
asymptotic variance, using a spectral variance estimator.
Args:
traj: numpy array (n, d) that contains the trajectory of the MCMC algorithm
traj_grad: numpy array (n, d) that contrains the gradients of the
log posterior evaluated along the trajectory
Returns:
mean_ZV2: numpy array of size 2*d, containing ZV2 applied with
the test functions \theta and \theta^2
var_ZV2: numpy array of size 2*d, containing the asymptotic
variances of ZV2 applied with the test functions \theta and \theta^2
"""
n, d = traj.shape
samples = np.concatenate((traj, np.square(traj)), axis=1)
Lpoisson = np.zeros((n,int(d*(d+3)/2)))
Lpoisson[:,np.arange(d)] = - traj_grad
Lpoisson[:,np.arange(d, 2*d)] = 2*(1. - np.multiply(traj, traj_grad))
k=2*d
for j in np.arange(d-1):
for i in np.arange(j+1,d):
Lpoisson[:,k] = -np.multiply(traj_grad[:,i], traj[:,j]) \
-np.multiply(traj_grad[:,j], traj[:,i])
k=k+1
cov1 = np.cov(Lpoisson, rowvar=False)
A = np.linalg.inv(cov1)
cov2 = np.cov(np.concatenate((Lpoisson, samples),axis=1), rowvar=False)
B = cov2[0:int(d*(d+3)/2), int(d*(d+3)/2):]
paramZV2 = - np.dot(A,B)
ZV2 = samples + np.dot(Lpoisson, paramZV2)
mean_ZV2 = np.mean(ZV2, axis=0)
ZV2 -= mean_ZV2
var_ZV2 = np.empty(2*d)
for i in np.arange(2*d):
tp1 = (1./n)*signal.fftconvolve(ZV2[:,i], ZV2[::-1,i], mode="same")
tp1 = tp1[:(int(n/2)+1)]
tp1 = tp1[::-1]
gam0 = tp1[0]
bn = int(n**(1./2))
wn = 1./2 + (1./2)*np.cos((np.pi/bn)*np.arange(bn))
var_ZV2[i]= -gam0+2*np.dot(wn, tp1[:bn])
return (mean_ZV2, var_ZV2)
# Logistic/Probit regression
# Data for the logistic regression
# Swiss dataset
data = np.loadtxt("data\\swiss.txt")
Y = data[:,-1]
X = data[:,0:-1]
# Normalization of the covariates
X = np.dot(X - np.mean(X, axis=0), np.diag(1./np.std(X, axis=0)))
potential = potentialRegression(Y, X, "l")
d = potential.d
# Data for the probit regression
# vaso dataset
#data = np.loadtxt("data\\vaso.txt")
#Y = data[:,-1]
#X = data[:,0:-1]
#X = np.dot(X - np.mean(X, axis=0), np.diag(1./np.std(X, axis=0)))
#X = np.insert(X, 0, 1, axis=1)
#
#potential = potentialRegression(Y, X, "p")
#d = potential.d
#-----------------
""" step size
10**(-2) ULA
5*10**(-2) MALA - 0.574 optimal scaling
5*10**(-2) RWM - optimal acceptance rate scaling 0.234
"""
N = 10**5 # Burn in period
n = 10**6 # Number of samples
step= 10**(-2) # Step size
nc = 100 # Number of independent MCMC trajectories
def func(intseed):
""" generic function that runs a MCMC trajectory
and computes means and variances for the ordinary samples,
CV1, ZV1, CV2 and ZV2 """
np.random.seed(intseed) # random seed, different for each independent
# MCMC trajectory (nc trajectories)
traj, traj_grad = ULA(step, N, n)
# to save the results of the trajectory
sauv = np.zeros((2*5,2*d))
sauv[0,:], sauv[1,:] = normalSamples(traj,traj_grad) # Normal samples
sauv[2,:], sauv[3,:] = CVpolyOne(traj,traj_grad) # CV1
sauv[4,:], sauv[5,:] = CVpolyTwo(traj, traj_grad) # CV2
sauv[6,:], sauv[7,:] = ZVpolyOne(traj,traj_grad) # ZV1
sauv[8,:], sauv[9,:] = ZVpolyTwo(traj, traj_grad) # ZV2
return sauv
inputs_seed = np.arange(nc) # input seeds
# number of cores exploited for the computation of the independent trajectories
# by deault, all available cores on the machine
nbcores = multiprocessing.cpu_count()
if __name__ == '__main__':
trav = Pool(nbcores)
res = trav.map(func, inputs_seed)
# Save the result
with open('log_ula_nc100_N5_n6.pkl', 'wb') as f:
pickle.dump(res, f)