-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRfunctions.cpp
More file actions
399 lines (367 loc) · 10.1 KB
/
Copy pathRfunctions.cpp
File metadata and controls
399 lines (367 loc) · 10.1 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
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
#include "Rfunctions.h"
#include "matrices.h"
#include "regmodels.h"
//compute the inverse of logit function
double inverseLogit(double x)
{
double inverse = exp(x) / (exp(x) + 1.0);
return(inverse);
}
double inverseLogit2(double x)
{
double inverse = exp(x) / pow(exp(x) + 1.0, 2.0);
return(inverse);
}
//compute Pi_i
gsl_matrix* getPi(gsl_matrix* x, gsl_matrix* beta, int n)
{
gsl_matrix* result = gsl_matrix_alloc(n, 1);
gsl_matrix* x0 = gsl_matrix_alloc(n, 2);
//set x0
for(int i = 0; i < n; i++)
{
gsl_matrix_set(x0, i, 0, 1.0);
gsl_matrix_set(x0, i, 1, gsl_matrix_get(x, i, 0));
}
//compute the multipy of x0 and beta
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x0, beta, 0.0, result);
//compute the inverse logit function
for(int j = 0; j < n; j++)
{
double inverseLog = inverseLogit(gsl_matrix_get(result, j, 0));
gsl_matrix_set(result, j, 0, inverseLog);
}
//free the memory
gsl_matrix_free(x0);
return(result);
}
//#another function for the computation of the Hessian
gsl_matrix* getPi2(gsl_matrix* x, gsl_matrix* beta, int n)
{
gsl_matrix* result = gsl_matrix_alloc(n, 1);
gsl_matrix* x0 = gsl_matrix_alloc(n, 2);
//compute x0
for(int i = 0; i < n; i++)
{
gsl_matrix_set(x0, i, 0, 1.0);
gsl_matrix_set(x0, i, 1, gsl_matrix_get(x, i, 0));
}
//compute the multipy of x0 and beta
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x0, beta, 0.0, result);
//compute the inverse Logit
for(int j = 0; j < n; j++)
{
double inverseLog2 = inverseLogit2(gsl_matrix_get(result, j, 0));
gsl_matrix_set(result, j, 0, inverseLog2);
}
//free the memory
gsl_matrix_free(x0);
return(result);
}
//#logistic log-likelihood
double logisticLogLik(gsl_matrix* y, gsl_matrix* x, gsl_matrix* beta, int n)
{
gsl_matrix* Pi = getPi(x, beta, n);
//compute the log-like function
double pi;
double yi;
double result = 0;
for(int i = 0; i < n; i++)
{
yi = gsl_matrix_get(y, i, 0);
pi = gsl_matrix_get(Pi, i, 0);
result += yi * log(pi) + (1.0 - yi) * log(1.0 - pi);
}
gsl_matrix_free(Pi);
return(result);
}
//#calculates l^*(\beta_0,\beta_1)
double lStar(gsl_matrix* y, gsl_matrix* x, gsl_matrix* beta, int n)
{
int bsize1 = beta -> size1;
int bsize2 = beta -> size2;
gsl_matrix* Pi = getPi(x, beta, n);
double yi, pi, result;
double logLogLike = 0;
double sum = 0;
//compute logLogLike
for(int i = 0; i < n; i++)
{
yi = gsl_matrix_get(y, i, 0);
pi = gsl_matrix_get(Pi, i, 0);
logLogLike += yi * log(pi) + (1.0 - yi) * log(1.0 - pi);
}
for(int i = 0; i < bsize1; i++)
{
for(int j = 0; j < bsize2; j++)
{
sum += pow(gsl_matrix_get(beta, i, j), 2.0);
}
}
//compute the result
result = -log(2.0 * M_PI) - 0.5 * sum + logLogLike;
gsl_matrix_free(Pi);
return(result);
}
//compute or update the gradient
void getGradient(gsl_matrix* y, gsl_matrix* x, gsl_matrix* beta, gsl_matrix* grad, int n)
{
double g1 = 0, g2 = 0;
double xi, yi, pi;
// Get Pi
gsl_matrix* Pi = getPi(x, beta, n);
for(int i = 0; i < n; i++)
{
xi = gsl_matrix_get(x, i, 0);
yi = gsl_matrix_get(y, i, 0);
pi = gsl_matrix_get(Pi, i, 0);
g1 += (yi - pi);
g2 += (yi - pi) * xi;
}
g1 -= gsl_matrix_get(beta, 0, 0);
g2 -= gsl_matrix_get(beta, 1, 0);
//set the gradient
gsl_matrix_set(grad, 0, 0, g1);
gsl_matrix_set(grad, 1, 0, g2);
//free the memory
gsl_matrix_free(Pi);
}
//compute Hesian
void getHessian(gsl_matrix* x, gsl_matrix* beta, gsl_matrix* Hess, int n)
{
double h1 = 0, h2 = 0, h3 = 0, h4 = 0;
double xi, pi;
//compute Pi2
gsl_matrix* Pi2 = getPi2(x, beta, n);
for(int i = 0; i < n; i++)
{
xi = gsl_matrix_get(x, i, 0);
pi = gsl_matrix_get(Pi2, i, 0);
h1 += pi;
h2 += pi * xi;
h4 += pi * pow(xi, 2.0);
}
h1 += 1.0;
h3 = h2;
h4 += 1.0;
//set the hessian matrix
gsl_matrix_set(Hess, 0, 0, -h1);
gsl_matrix_set(Hess, 0, 1, -h2);
gsl_matrix_set(Hess, 1, 0, -h3);
gsl_matrix_set(Hess, 1, 1, -h4);
//free memory
gsl_matrix_free(Pi2);
}
//#this function implements our own Newton-Raphson procedure
gsl_matrix* getcoefNR(gsl_matrix* y, gsl_matrix* x, int n, int max)
{
double oldLog, newLog;
//set the beta matrix
gsl_matrix* beta = gsl_matrix_alloc(2, 1);
gsl_matrix_set_zero(beta);
gsl_matrix* nextBeta = gsl_matrix_alloc(2, 1);
//set the needed matries
gsl_matrix* Hess = gsl_matrix_alloc(2, 2);
gsl_matrix* Grad = gsl_matrix_alloc(2, 1);
gsl_matrix* inverHess = gsl_matrix_alloc(2, 2);
gsl_matrix* hessGrad = gsl_matrix_alloc(2, 1);
//compute the l* function
oldLog = lStar(y, x, beta, n);
//start the iteration
for(int i = 0; i < max; i++)
{
getHessian(x, beta, Hess, n);
getGradient(y, x, beta, Grad, n);
inverHess = inverse(Hess);
//matrix multiply
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, inverHess, Grad, 0.0, hessGrad);
//set new beta
gsl_matrix_memcpy(nextBeta, beta);
gsl_matrix_sub(nextBeta, hessGrad);
newLog = lStar(y, x, nextBeta, n);
//see if any progress has been made
if(newLog < oldLog)
{
printf("CODING ERROR!!");
exit(1);
}
//update the beta
gsl_matrix_memcpy(beta, nextBeta);
//see if it is time to break
if((newLog - oldLog) < 0.00001)
{
break;
}
else
{
oldLog = newLog;
}
}
//free the memory
gsl_matrix_free(nextBeta);
gsl_matrix_free(Hess);
gsl_matrix_free(Grad);
gsl_matrix_free(inverHess);
gsl_matrix_free(hessGrad);
return(beta);
}
//implement the MH algorithm
gsl_matrix* MHlogistic(gsl_rng* mystream, gsl_matrix* y, gsl_matrix* x, gsl_matrix* betaMode, int iter, int n)
{
double oldLog, newLog;
//set sample matrix
gsl_matrix* sample = gsl_matrix_alloc(iter, 2);
gsl_matrix_set_zero(sample);
//set hessian matrix
gsl_matrix* Hess = gsl_matrix_alloc(2, 2);
getHessian(x, betaMode, Hess, n);
//set the sigma
gsl_matrix* sigma = inverse(Hess);
gsl_matrix_scale(sigma, -1.0);
//set the old and new beta
gsl_matrix* oldBeta = gsl_matrix_alloc(2, 1);
gsl_matrix_memcpy(oldBeta, betaMode);
gsl_matrix* newBeta = gsl_matrix_alloc(2, 1);
for(int i = 0; i < iter; i++)
{
randomMVN(mystream, newBeta, oldBeta, sigma);
//compute the old and new Loglik
newLog = lStar(y, x, newBeta, n);
oldLog = lStar(y, x, oldBeta, n);
//decide if there is big enough progress
if(newLog - oldLog >= 1)
{
gsl_matrix_memcpy(oldBeta, newBeta);
}
else
{
double u = gsl_ran_flat(mystream, 0.0, 1.0);
if(log(u) <= newLog - oldLog)
{
gsl_matrix_memcpy(oldBeta, newBeta);
}
}
gsl_matrix_set(sample, i, 0, gsl_matrix_get(oldBeta, 0, 0));
gsl_matrix_set(sample, i, 1, gsl_matrix_get(oldBeta, 1, 0));
}
//free the memory
gsl_matrix_free(Hess);
gsl_matrix_free(sigma);
gsl_matrix_free(oldBeta);
gsl_matrix_free(newBeta);
return(sample);
}
// approximate the marginal likelihood using the Laplace approximation
double getLaplaceApprox(gsl_matrix* y, gsl_matrix* x, gsl_matrix* betaMode, int n)
{
double lstar, result;
//compute the hessian matrix
gsl_matrix* Hess = gsl_matrix_alloc(2,2);
getHessian(x, betaMode, Hess, n);
gsl_matrix_scale(Hess, -1.0);
//compute l*
lstar = lStar(y, x, betaMode, n);
//compute the loglik
result = log(2.0 * M_PI) + lstar - 0.5 * logdet(Hess);
// Free memory
gsl_matrix_free(Hess);
return(result);
}
//calculate the marginal likelihood using Monte Carlo approximation
double getMonteCarlo(gsl_rng* mystream, gsl_matrix* y, gsl_matrix* x, int size, int n)
{
double result, sum;
//set the mean and sigma and beta
gsl_matrix* mean = gsl_matrix_alloc(2, 1);
gsl_matrix_set_zero(mean);
gsl_matrix* sigma = gsl_matrix_alloc(2, 2);
gsl_matrix_set_identity(sigma);
gsl_matrix* beta = gsl_matrix_alloc(2, 1);
//start the sample
sum = 0;
for(int i = 0; i < size; i++)
{
randomMVN(mystream, beta, mean, sigma);
sum += exp(logisticLogLik(y, x, beta, n));
}
//compute the result
result = sum / (double) size;
//free the memory
gsl_matrix_free(mean);
gsl_matrix_free(sigma);
gsl_matrix_free(beta);
return(result);
}
//compute the post mean
gsl_matrix* getPosteriorMeans(gsl_rng* mystream, gsl_matrix* y, gsl_matrix* x, gsl_matrix* betaMode, int iter, int n)
{
gsl_vector_view c;
//simulate the sample matrix and set the mean matrix
gsl_matrix* sample = MHlogistic(mystream, y, x, betaMode, iter, n);
gsl_matrix* mean = gsl_matrix_alloc(2, 1);
int p = sample -> size1;
int k = sample -> size2;
//start iteration
for(int i = 0; i < k; i++)
{
c = gsl_matrix_column(sample, i);
gsl_matrix_set(mean, i, 0, gsl_stats_mean(c.vector.data, c.vector.stride, p));
}
//free the memory
gsl_matrix_free(sample);
return(mean);
}
//creates the Cholesky decomposition of a matrix
gsl_matrix* makeCholesky(gsl_matrix* K)
{
int i,j;
gsl_matrix* Phi = gsl_matrix_alloc(K->size1,K->size1);
if(GSL_SUCCESS!=gsl_matrix_memcpy(Phi,K))
{
printf("GSL failed to copy a matrix.\n");
exit(1);
}
if(GSL_SUCCESS!=gsl_linalg_cholesky_decomp(Phi))
{
printf("GSL failed Cholesky decomposition.\n");
exit(1);
}
for(i=0;i<Phi->size1;i++)
{
for(j=i+1;j<Phi->size2;j++)
{
gsl_matrix_set(Phi,i,j,0.0);
}
}
return(Phi);
}
//samples from the multivariate normal distribution N(mean,Sigma)
//the samples are saved in the matrix "Samples"
void randomMVN(gsl_rng* mystream, gsl_matrix* Samples, gsl_matrix* mean, gsl_matrix* Sigma)
{
gsl_matrix* Psi = makeCholesky(Sigma);
gsl_matrix* Z = gsl_matrix_alloc(Sigma->size1,1);
gsl_matrix* X = gsl_matrix_alloc(Sigma->size1,1);
gsl_vector* V = gsl_vector_alloc(Sigma->size1);
for(int asample=0;asample<Samples->size2;asample++)
{
for(int i = 0; i < Sigma->size1; i++)
{
gsl_matrix_set(Z,i,0,gsl_ran_ugaussian(mystream));
}
gsl_blas_dgemm (CblasNoTrans, CblasNoTrans,
1.0, Psi, Z,
0.0, X);
//add the mean
gsl_matrix_add(X, mean);
//record the sample we just generated
gsl_matrix_get_col(V, X, 0);
gsl_matrix_set_col(Samples, asample, V);
}
//free memory
gsl_matrix_free(Psi);
gsl_matrix_free(Z);
gsl_matrix_free(X);
gsl_vector_free(V);
return;
}