/
CGppe.cpp
executable file
·505 lines (390 loc) · 15.1 KB
/
CGppe.cpp
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// Copyright (c) 2012, National ICT Australia
// All rights reserved.
//
// The contents of this file are subject to the Mozilla Public License
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
#include "CGppe.h"
CGppe::CGppe(Covfunc *Covfunc_t, Covfunc *Covfunc_x)
{
covfunc_t = Covfunc_t;
covfunc_x = Covfunc_x;
}
CGppe::CGppe(const CGppe & g)
{
f = g.f;
Kx = g.Kx;
Kinv = g.Kinv;
W = g.W;
L = g.L;
}
CGppe::CGppe(VectorXd fnew, MatrixXd Kxnew, MatrixXd Kinvnew, MatrixXd Wnew, MatrixXd Lnew)
{
f = fnew;
Kx = Kxnew;
Kinv = Kinvnew;
W = Wnew;
L = Lnew;
}
CGppe::~CGppe()
{
}
VectorXd CGppe::Getf()
{
return f;
}
MatrixXd CGppe::GetW()
{
return W;
}
MatrixXd CGppe::GetL()
{
return L;
}
LLT<MatrixXd> CGppe::Getllt()
{
return llt;
}
MatrixXd CGppe::GetKinv()
{
return Kinv;
}
MatrixXd CGppe::GetKx()
{
return Kx;
}
VectorXd CGppe::Getmustar()
{
return mustar;
}
VectorXd CGppe::Getvarstar()
{
return varstar;
}
double CGppe::Getp()
{
return p;
}
double CGppe::get_fbest(int N)
{
VectorXd ftest;
double fbest;
ftest = f.segment(f.rows() - N, N - 1);
fbest = ftest.maxCoeff();
if (fbest != fbest)
fbest = f.maxCoeff();
return fbest;
}
double CGppe::maximum_expected_improvement(const VectorXd & theta_t, const VectorXd& theta_x, const double& sigma,
const MatrixXd& t, const MatrixXd & x, const VectorXd& idx_global, const VectorXd& ind_t, const VectorXd& ind_x, MatrixXd tstar, int N, double fbest)
{
VectorXd idx_xstar=Nfirst(N);
int Kt_ss = 1;
double mei;
MatrixXd Kx_star, Kx_star_star, kstar, Kss, Css;
MatrixXd Kt_star = covfunc_t->Compute(t, tstar);
//dsp(GetKinv(),"Kinv");
Kx_star = GetMatRow(Kx, idx_xstar.transpose()); //maybe need some transpose?
Kx_star_star = GetMat(Kx, idx_xstar.transpose(), idx_xstar.transpose()); // test to test
kstar = Kron(Kt_star, Kx_star);
kstar = GetMatRow(kstar, idx_global);
Kss = Kt_ss * Kx_star_star;
mustar = kstar.transpose() * Kinv * GetVec(f, idx_global);
Css = Kss - kstar.transpose() * W * llt.solve(Kinv * kstar);
varstar = Css.diagonal();
VectorXd sigmastar = sqrt(varstar.array());
VectorXd z = (fbest - mustar.array()) / sigmastar.array();
VectorXd pdfval = normpdf(z);
VectorXd cdfval = normcdf(z);
VectorXd inter = z.array() * (1 - cdfval.array());
VectorXd el = sigmastar.cwiseProduct(inter - pdfval);
el=-1*el;
mei = el.maxCoeff();
//dsp(mei,"mei");
return mei;
}
double CGppe::expected_voi(const VectorXd & theta_x, const VectorXd& theta_t, const double& sigma,
const MatrixXd& t, const MatrixXd & x, TypePair train_pairs, VectorXd& idx_global, VectorXd& ind_t, VectorXd& ind_x, MatrixXd test_pair, double fbest, double p_12)
{
int M = t.rows();
int N = x.rows();
CGppe gnew= CGppe(new CovSEard(),new CovSEard());
VectorXd idx_global_1, idx_global_2;
MatrixXd tstar = t.row(M-1);
double p_21, mei_12, mei_21;
p_21 = 1. - p_12;
train_pairs(M-1)=MatAdd(train_pairs(M-1),test_pair);
compute_global_index(idx_global_1, idx_global_2, train_pairs, N);
unique(idx_global, idx_global_1, idx_global_2);
ind2sub(ind_x, ind_t, N, M, idx_global);
gnew.Approx_CGppe_Laplace( theta_x, theta_t, sigma,
t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, M, N);
mei_12 = gnew.maximum_expected_improvement(theta_t, theta_x, sigma, t, x, idx_global, ind_t, ind_x, tstar, N, fbest);
//dsp(mei_12,"mei12");
//recomputation
fliplr(test_pair);
train_pairs(M-1).bottomRows(1)=test_pair;
compute_global_index(idx_global_1, idx_global_2, train_pairs, N);
unique(idx_global, idx_global_1, idx_global_2);
ind2sub(ind_x, ind_t, N, M, idx_global);
gnew.Approx_CGppe_Laplace( theta_x, theta_t, sigma,
t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, M, N);
mei_21 = gnew.maximum_expected_improvement(theta_t, theta_x, sigma, t, x, idx_global, ind_t, ind_x, tstar, N, fbest);
return (p_12*mei_12 + p_21*mei_21) ;
}
void CGppe::Elicit( const VectorXd & theta_x, const VectorXd& theta_t, const double& sigma, const MatrixXd& train_t, const MatrixXd &x, TypePair & train_pairs
, const MatrixXd & test_t, int test_user_idx, MatrixXd idx_pairs, int Maxiter, const TypePair& Oracle , MatrixXd& F)
{
train_pairs.conservativeResize(train_pairs.rows()+1);
int N = x.rows();
int Mtrain = train_t.rows();
int M = Mtrain + 1;
int Npairs = idx_pairs.rows();
int Lgood;
VectorXd vrand, idx_good;
//VectorXd is_selected(Npairs);
Matrix<bool, Dynamic, 1> is_selected(Npairs);
is_selected.fill(false);
VectorXd loss = VectorXd::Zero(Maxiter + 1);
double loss_current;
VectorXd evoi(Npairs), ind_t, ind_x;
VectorXd idx_global_1, idx_global_2, idx_global;
compute_global_index(idx_global_1, idx_global_2, train_pairs, N);
unique(idx_global, idx_global_1, idx_global_2);
ind2sub(ind_x, ind_t, N, M, idx_global);
bool stop = false;
double foo, val;
int count = 0;
MatrixXd new_pair;
MatrixXd t;
VectorXd p_12(Npairs);
t.resize(M, train_t.cols());
t << train_t, test_t;
for (int iter = 0;iter <= Maxiter;iter++)
{
Approx_CGppe_Laplace( theta_x, theta_t, sigma,
t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, Mtrain, N);
Predictive_Utility_Distribution(t, test_t, N, idx_global );
//dsp(mustar,"mustar");
//dsp(varstar,"varstar");
std::ptrdiff_t best_item_idx;
foo = mustar.maxCoeff(&best_item_idx);
double fbest = get_fbest(N);
//dsp(fbest,"fbest");
MatrixXd test_pair;
for (int i = 0;i < Npairs;i++)
{
Predict_CGppe_Laplace(sigma, t, x, idx_global, ind_t, ind_x, t.row(M-1), idx_pairs.row(i));
p_12(i)=p;
}
for (int i = 0;i < Npairs;i++)
{
if (is_selected(i))
{
evoi(i) = INT_MIN;
continue;
}
test_pair = idx_pairs.row(i);
evoi(i) = expected_voi(theta_x, theta_t, sigma, t, x, train_pairs, idx_global, ind_t, ind_x, test_pair, fbest,p_12(i));
}
//dsp(evoi,"evoi");
std::ptrdiff_t query_idx;
val = evoi.maxCoeff(&query_idx);
idx_good = find(evoi, val);
//dsp(val,"val");
Lgood = idx_good.rows();
if ( Lgood > 1)
{
//vrand = randperm(Lgood);
cout<<"Solving clashes at random"<<endl;
//query_idx = idx_good(vrand(0));
query_idx = idx_good(0);
}
is_selected(query_idx) = true;
//dsp(query_idx,"queryidx");
new_pair = make_query_toydata(Oracle, query_idx, test_user_idx);
//adding the new pair
train_pairs(M-1)=MatAdd(train_pairs(M-1),new_pair);
compute_global_index(idx_global_1, idx_global_2, train_pairs, N);
unique(idx_global, idx_global_1, idx_global_2);
ind2sub(ind_x, ind_t, N, M, idx_global);
//Computes the loss of making a recommendation at this point
loss_query_toydata(loss_current, F, stop, test_user_idx, best_item_idx);
loss(iter)=loss_current;
count++;
cout << "Query " << count << "[" << new_pair(0) << " " << new_pair(1) << "] done, Recommended Item= " << best_item_idx << ", loss=" << loss(iter) << endl;
}
}
void CGppe::Make_Predictions_New_User(const VectorXd & theta_x, const VectorXd& theta_t, double& sigma, const MatrixXd& train_t, const MatrixXd &x, const TypePair & train_pairs,
const VectorXd & idx_global, const VectorXd& idx_global_1, const VectorXd& idx_global_2,
const VectorXd& ind_t, const VectorXd& ind_x, const MatrixXd & test_t, const MatrixXd& idx_pairs, const VectorXd& ftrue, const VectorXd& ytrue)
{
int N = x.rows();
int Mtrain = train_t.rows();
int Npairs = idx_pairs.rows();
VectorXd fstar;
MatrixXd pair;
VectorXd P = VectorXd::Zero(Npairs);
VectorXd ypred = VectorXd::Zero(Npairs);
VectorXd sum = VectorXd::Zero(N);
VectorXd count = VectorXd::Zero(N);
Approx_CGppe_Laplace( theta_x, theta_t, sigma,
train_t, x, train_pairs, idx_global, idx_global_1, idx_global_2, ind_t, ind_x, Mtrain, N);
for (int i = 0;i < Npairs;i++)
{
pair = idx_pairs.row(i);
Predict_CGppe_Laplace(sigma, train_t, x, idx_global, ind_t, ind_x,
test_t, pair);
P(i) = p;
sum(pair(0)) += mustar(0);
count(pair(0)) += 1;
sum(pair(1)) += mustar(1);
count(pair(1)) += 1;
}
for (int i = 0;i < P.rows();i++)
{
if (P(i) > 0.5000001)
ypred(i) = 1;
else
ypred(i)=0;
}
fstar = sum.array() / count.array();
dsp(fstar,"fstar");
cout << endl << endl << "error = " << (GetDiff(ytrue, ypred)).sum() / ytrue.rows()<<endl;
// need for a plot function here ?
}
void CGppe::Predictive_Utility_Distribution(MatrixXd t, MatrixXd tstar, int N, VectorXd idx_global)
{
int Kt_ss = 1;
VectorXd idx_xstar(N);
MatrixXd Kstar, Kx_star_star, Kx_star, Kss, Css, Kt_star;
for (int i = 0;i < N;i++)
{
idx_xstar(i) = i;
}
Kt_star = covfunc_t->Compute(t, tstar);
Kx_star = GetMatRow(Kx, idx_xstar);//need to check for tranpose later?
Kx_star_star = GetMat(Kx, idx_xstar, idx_xstar);
Kstar = Kron(Kt_star, Kx_star);
Kstar = GetMatRow(Kstar, idx_global);
Kss = Kt_ss * Kx_star_star;
mustar = Kstar.transpose() * Kinv * GetVec(f, idx_global);
Css = Kss - Kstar.transpose() * W * llt.solve(Kinv * Kstar);
varstar = Css.diagonal();
}
void CGppe::Predict_CGppe_Laplace(double sigma, MatrixXd t, MatrixXd x, VectorXd idx_global, VectorXd ind_t, VectorXd ind_x,
MatrixXd tstar, MatrixXd test_pair)
{
int Kt_ss = 1;
double sigma_star, val;
MatrixXd Kx_star, Kx_star_star, kstar, Kss, Css;
MatrixXd Kt_star = covfunc_t->Compute(t, tstar);
Kx_star = GetMatRow(Kx, test_pair.transpose()).transpose(); //maybe need some transpose?
Kx_star_star = GetMat(Kx, test_pair.transpose(), test_pair.transpose()); // test to test
kstar = Kron(Kt_star, Kx_star);
kstar = GetMatRow(kstar, idx_global);
Kss = Kt_ss * Kx_star_star;
mustar = kstar.transpose() * Kinv * GetVec(f, idx_global);
Css = Kss - kstar.transpose() * W * llt.solve(Kinv * kstar);
sigma_star = sqrt(Css(0, 0) + Css(1, 1) - 2 * Css(0, 1) + pow(sigma, 2));
val = ( mustar(0) - mustar(1) ) / sigma_star;
p = normcdf(val);
}
void CGppe::Approx_CGppe_Laplace(const VectorXd & theta_x, const VectorXd& theta_t, const double& sigma, const MatrixXd& t, const MatrixXd &x, const TypePair & all_pairs,
const VectorXd & idx_global, const VectorXd& idx_global_1, const VectorXd& idx_global_2,
const VectorXd& ind_t, const VectorXd& ind_x, int M, int N)
{
//Parameters function initialization
double eps = 1E-6, psi_new, psi_old;
M = all_pairs.rows();
int n = M * N;
f = VectorXd::Zero(n);
VectorXd fvis = VectorXd::Zero(idx_global.rows());
VectorXd deriv;
double loglike = 0;
covfunc_t->SetTheta(theta_t);
covfunc_x->SetTheta(theta_x);
MatrixXd Kt = covfunc_t->ComputeGrandMatrix(t);
Kx = covfunc_x->ComputeGrandMatrix(x);
MatrixXd K = GetMat(Kt, ind_t, ind_t).array() * GetMat(Kx, ind_x, ind_x).array();
loglike = log_likelihood( sigma, all_pairs, idx_global_1, idx_global_2, M, N);
Kinv = K.inverse();
psi_new = loglike - 0.5 * fvis.transpose() * Kinv * fvis;
psi_old = INT_MIN;
while ((psi_new - psi_old) > eps)
{
psi_old = psi_new;
deriv = deriv_log_likelihood_CGppe_fast( sigma, all_pairs, idx_global_1, idx_global_2, M, N);
W = -deriv2_log_likelihood_CGppe_fast(sigma, all_pairs, idx_global_1, idx_global_2, M, N);
W = GetMat(W, idx_global, idx_global);
llt.compute(W + Kinv);
L = llt.matrixL(); //no need to extract the triangular matrix here
fvis = llt.solve(GetVec(deriv, idx_global) + W * fvis);
for (int w = 0;w < idx_global.rows();w++)
{
f(idx_global(w)) = fvis(w);
}
loglike = log_likelihood( sigma, all_pairs, idx_global_1, idx_global_2, M, N);
psi_new = loglike - 0.5 * fvis.transpose() * Kinv * fvis;
}
}
double CGppe::log_likelihood(double sigma, TypePair all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N)
{
M = all_pairs.rows();
VectorXd idx_1, idx_2, z;
double loglike = 0;
for (int j = 0;j < M;j++)
{
if (all_pairs(j).rows() == 0)
continue;
idx_1 = ind2global(all_pairs(j).col(0), j, N);
idx_2 = ind2global(all_pairs(j).col(1), j, N);
z = (GetVec(f, idx_1) - GetVec(f, idx_2)) / sigma;
z = normcdf(z);
loglike += log(z.array()).sum();
}
return loglike;
}
VectorXd CGppe::deriv_log_likelihood_CGppe_fast(double sigma, const TypePair& all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N)
{
VectorXd deriv_loglike, z, cdf_val, pdf_val, val;
// test if idx vectors are empty ?
M = all_pairs.rows();
int n = M * N;
z = (GetVec(f, idx_global_1) - GetVec(f, idx_global_2)) / sigma;
cdf_val = normcdf(z);
pdf_val = normpdf(z);
val = pow(sigma,-1) * (pdf_val.cwiseQuotient(cdf_val));
return Get_Cumulative_Val(idx_global_1, val, n) - Get_Cumulative_Val(idx_global_2, val, n);
}
MatrixXd CGppe::deriv2_log_likelihood_CGppe_fast(double sigma, TypePair all_pairs, VectorXd idx_global_1, VectorXd idx_global_2, int M, int N)
{
VectorXd deriv_loglike, z, cdf_val, pdf_val, val, ratio, all_diag_idx, ind, ind_trans;
M = all_pairs.rows();
int n = M * N;
VectorXd consec(n);
MatrixXd Deriv2 = MatrixXd::Zero(n, n);
for (int i = 0;i < n;i++)
{
consec(i) = i;
}
all_diag_idx = sub2ind(n, n, consec, consec);
z = (GetVec(f, idx_global_1) - GetVec(f, idx_global_2)) / sigma;
cdf_val = normcdf(z);
pdf_val = normpdf(z);
ratio = pdf_val.array() / cdf_val.array();
val = -(1. / pow(sigma, 2)) * (ratio.array() * (z + ratio).array());
ind = sub2ind(n, n, idx_global_1, idx_global_2);
Deriv2 = SetMatGenIdx(Deriv2, ind, -val);
ind_trans = sub2ind(n, n, idx_global_2, idx_global_1);
Deriv2 = SetMatGenIdx(Deriv2, ind_trans, -val);
Deriv2 = SetMatGenIdx(Deriv2, all_diag_idx, GetMatGenIdx(Deriv2, all_diag_idx) + Get_Cumulative_Val(idx_global_1, val, n));
Deriv2 = SetMatGenIdx(Deriv2, all_diag_idx, GetMatGenIdx(Deriv2, all_diag_idx) + Get_Cumulative_Val(idx_global_2, val, n));
return Deriv2;
}