-
Notifications
You must be signed in to change notification settings - Fork 11
/
crp_test.cc
334 lines (315 loc) · 10.6 KB
/
crp_test.cc
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
#include <iostream>
#include <vector>
#include <cassert>
#include <string>
#include "cpyp/crp.h"
#include "cpyp/mf_crp.h"
#include "cpyp/random.h"
using namespace std;
static const double p0[] = { 0.1, 0.2, 0.7 }; // actual base distribution
static const double q0[] = { 0.5, 0.4, 0.1 }; // proposal base distribution
// log likelihood of a CRP including draws from a static base distribution p0
double llh(const cpyp::crp<int>& crp, const double* pp0) {
double l = crp.log_likelihood();
for (int i = 0; i < 3; ++i)
l += crp.num_tables(i) * log(pp0[i]);
return l;
}
void test_mh1() {
cpyp::MT19937 eng;
const vector<double> ref = {0, 0, 0, 0.00466121, 0.0233846, 0.0647365, 0.125693, 0.183448, 0.204806, 0.177036, 0.119629, 0.0627523, 0.02507, 0.00725451, 0.0013911};
cpyp::crp<int> crp(0.5, 1.0);
vector<int> hist(15, 0);
double c = 0;
double ac = 0;
double tmh = 0;
for (int s = 0; s < 200000; ++s) {
for (int i = 0; i < 15; ++i) {
int y_i = i % 3;
cpyp::crp<int> prev = crp; // save old state
bool wasrem = false;
if (s > 0)
wasrem = crp.decrement(y_i, eng);
bool wasnew = crp.increment(y_i, q0[y_i], eng);
if (s > 0) {
double p_new = 1, q_new = 1;
if (wasnew) { p_new = p0[y_i]; q_new = q0[y_i]; }
double p_old = 1, q_old = 1;
if (wasrem) { p_old = p0[y_i]; q_old = q0[y_i]; }
double a = p_new / p_old * q_old / q_new;
++tmh;
if (a >= 1.0 || cpyp::sample_uniform01<double>(eng) < a) { // mh accept
++ac;
} else { // mh reject
std::swap(crp, prev); // swap is faster than =
}
}
}
if (s > 300 && s % 4 == 3) { ++c; hist[crp.num_tables()]++; }
}
ac /= tmh;
cerr << "ACCEPTANCE: " << ac << endl;
int j =0;
double te = 0;
double me = 0;
for (auto i : hist) {
double err = (i / c - ref[j++]);
cerr << err << "\t" << i/c << endl;
te += fabs(err);
if (fabs(err) > me) { me = fabs(err); }
}
te /= 12;
cerr << "Average error: " << te;
if (te > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl << " Max error: " << me;
if (me > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl;
}
// same model as test_mh1, same proposal, different way of computing
// p's and q's
void test_mh1a() {
cpyp::MT19937 eng;
const vector<double> ref = {0, 0, 0, 0.00466121, 0.0233846, 0.0647365, 0.125693, 0.183448, 0.204806, 0.177036, 0.119629, 0.0627523, 0.02507, 0.00725451, 0.0013911};
cpyp::crp<int> crp(0.5, 1.0);
vector<int> hist(15, 0);
double c = 0;
double ac = 0;
double tmh = 0;
for (int s = 0; s < 200000; ++s) {
for (int i = 0; i < 15; ++i) {
int y_i = i % 3;
cpyp::crp<int> prev = crp; // save old state
double lq_old = 0;
double lp_old = llh(crp, p0);
if (s > 0) crp.decrement(y_i, eng, &lq_old);
double lq_new = 0;
crp.increment_no_base(y_i, eng, &lq_new);
if (s > 0) {
double lp_new = llh(crp, p0);
double a = exp(lp_new - lp_old + lq_old - lq_new);
++tmh;
if (a >= 1.0 || cpyp::sample_uniform01<double>(eng) < a) { // mh accept
++ac;
} else { // mh reject
std::swap(crp, prev); // swap is faster than =
}
}
}
if (s > 300 && s % 4 == 3) { ++c; hist[crp.num_tables()]++; }
}
ac /= tmh;
cerr << "ACCEPTANCE: " << ac << endl;
int j =0;
double te = 0;
double me = 0;
for (auto i : hist) {
double err = (i / c - ref[j++]);
cerr << err << "\t" << i/c << endl;
te += fabs(err);
if (fabs(err) > me) { me = fabs(err); }
}
te /= 12;
cerr << "Average error: " << te;
if (te > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl << " Max error: " << me;
if (me > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl;
}
static const double p0_a[] = { 0.1, 0.2, 0.7 }; // actual base distribution
static const double p0_b[] = { 0.6, 0.3, 0.1 }; // actual base of d2
static const double q0_a[] = { 0.05, 0.1, 0.35 }; // estimated base distribution
static const double q0_b[] = { 0.3, 0.15, 0.05 }; // estimated base of d2
void test_mh2() {
cpyp::MT19937 eng;
cpyp::crp<int> a(0.5, 1.0);
cpyp::crp<int> b(0.5, 1.0);
vector<int> hist_a(16, 0);
vector<int> hist_b(16, 0);
vector<double> ref_a = {3.70004e-06, 0.0144611, 0.0614236, 0.138702, 0.211308, 0.231733, 0.183865, 0.103685, 0.0409419, 0.0114253, 0.00214592, 0.000281003, 2.33002e-05, 7.00007e-07, 2.12303e-07, 0};
vector<double> ref_b = {2.90003e-06, 0.00876079, 0.0471061, 0.115841, 0.187615, 0.221488, 0.196102, 0.130352, 0.063905, 0.0224984, 0.00542085, 0.000828908, 7.53008e-05, 4.90005e-06, 1.00001e-07, 0};
double ref_t0b = 1.53498;
vector<bool> z(15);
double c = 0;
double ac = 0;
double tmh = 0;
double zz = 0;
double t0b = 0;
for (int s = 0; s < 200000; ++s) {
for (int i = 0; i < 15; ++i) {
const unsigned int y_i = i % 3;
const bool old_z = z[i];
cpyp::crp<int> old_a = a;
cpyp::crp<int> old_b = b;
double lp_old = llh(a, p0_a) + llh(b, p0_b);
double lq_old = 0;
double lq_new = 0;
if (s > 0) (z[i] ? b : a).decrement(y_i, eng, &lq_old);
double aa = 0.5; // these can be better estimates
double bb = 0.5;
z[i] = cpyp::sample_bernoulli(aa, bb, eng);
(z[i] ? b : a).increment_no_base(y_i, eng, &lq_new);
lq_new += log((z[i] ? bb : aa) / (aa + bb));
lq_old += log((old_z ? bb : aa) / (aa + bb));
if (s > 0) {
double lp_new = llh(a, p0_a) + llh(b, p0_b);
double acc = exp(lp_new - lp_old + lq_old - lq_new);
++tmh;
if (acc >= 1.0 || cpyp::sample_uniform01<double>(eng) < acc) { // mh accept
++ac;
} else { // mh reject
std::swap(a, old_a);
std::swap(b, old_b);
z[i] = old_z;
}
}
}
// record sample
if (s> 200 && s % 10 == 9) {
assert(a.num_tables() < hist_a.size());
assert(b.num_tables() < hist_b.size());
hist_a[a.num_tables()]++; hist_b[b.num_tables()]++; ++c;
for (int i = 0; i < 15; ++i) {
unsigned int y_i = i % 3;
if (!z[i]) { t0b += y_i; ++zz; }
}
}
}
ac /= tmh;
cerr << "ACCEPTANCE: " << ac << endl;
int j =0;
double te = 0;
double me = 0;
for (int i = 0; i < 15; ++i) {
double a = hist_a[i];
double b = hist_b[i];
double err_a = (a / c - ref_a[j]);
double err_b = (b / c - ref_b[j++]);
cerr << err_a << "\t" << err_b << endl;
te += fabs(err_a) + fabs(err_b);
if (fabs(err_a) > me) { me = fabs(err_a); }
if (fabs(err_b) > me) { me = fabs(err_b); }
}
te /= 30;
cerr << "t0b = " << (t0b / zz) << endl;
double ee = fabs((t0b / zz) - ref_t0b);
cerr << "err t0b = " << ee;
if (ee > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl;
cerr << "Average error: " << te;
if (te > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl << " Max error: " << me;
if (me > 0.01) { cerr << " ** TOO HIGH **"; }
cerr << endl;
}
void test_mfcrp() {
vector<double> ref_up = {0.0152302, 0.0754287, 0.153957, 0.209485, 0.213071, 0.16726, 0.101233, 0.0460308, 0.0149256, 0.003075, 0.00030408};
vector<double> ref_down = {0.185252, 0.32385, 0.271888, 0.145849, 0.0548223, 0.0149361, 0.00294062, 0.0004185, 4.074e-05, 2.3e-06, 4e-08};
cpyp::MT19937 eng;
long double p0[2]{1.0,1.0}; // pa(0), pb(0)
double lam[2]{0.3,0.7};
vector<double> hist_upstairs(11, 0);
vector<double> hist_downstairs(11, 0);
int c = 0;
for (int n = 0; n < 200000; ++n) {
cpyp::mf_crp<2, unsigned> crp(0.5, 1.0);
int upstairs = 0;
int downstairs = 0;
for (int i = 0; i < 10; ++i) {
unsigned obs = 0;
pair<unsigned, int> floor_count = crp.increment(obs, p0, lam, eng);
if (floor_count.second) (floor_count.first ? upstairs : downstairs)++;
}
// resample some stuff
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < 10; ++i) {
unsigned obs = 0;
pair<unsigned, int> floor_count = crp.decrement(obs, eng);
if (floor_count.second) (floor_count.first ? upstairs : downstairs)--;
floor_count = crp.increment(obs, p0, lam, eng);
if (floor_count.second) (floor_count.first ? upstairs : downstairs)++;
}
}
hist_downstairs[downstairs] += 1.0;
hist_upstairs[upstairs] += 1.0;
++c;
}
cerr << "Upstairs:\n";
int j = 0;
double max_up = 0;
double tot_up = 0;
for (auto& d : hist_upstairs) {
double diff = d / c - ref_up[j++];
cerr << diff << endl;
if (fabs(diff) > max_up) max_up = fabs(diff);
tot_up += fabs(diff);
}
if (max_up > 0.01) { cerr << "** TOO BIG "; }
cerr << "max_up=" << max_up << endl;
tot_up /= 11;
if (tot_up > 0.005) { cerr << "*** TOO BIG "; }
cerr << "avg_up=" << tot_up << endl;
cerr << "Downstairs:\n";
j = 0;
double tot_down = 0;
double max_down = 0;
for (auto& d : hist_downstairs) {
double diff = (d / c - ref_down[j++]);
cerr << diff << endl;
if (fabs(diff) > max_down) max_down = fabs(diff);
tot_down += fabs(diff);
}
if (max_down > 0.01) { cerr << "** TOO BIG "; }
cerr << "max_down=" << max_down << endl;
tot_down /= 11;
if (tot_down > 0.005) { cerr << "*** TOO BIG "; }
cerr << "avg_down=" << tot_down << endl;
}
int main() {
cpyp::MT19937 eng;
double tot = 0;
double xt = 0;
cpyp::crp<int> crp(0.5, 1.0);
unsigned cust = 10;
vector<int> hist(cust + 1, 0);
for (unsigned i = 0; i < cust; ++i) { crp.increment(1, 1.0, eng); }
const int samples = 200000;
const bool simulate = true;
for (int k = 0; k < samples; ++k) {
if (!simulate) {
crp.clear();
for (unsigned i = 0; i < cust; ++i) { crp.increment(1, 1.0, eng); }
} else {
unsigned da = cpyp::sample_uniform01<double>(eng) * cust;
bool a = cpyp::sample_uniform01<double>(eng) < 0.5;
if (a) {
for (unsigned i = 0; i < da; ++i) { crp.increment(1, 1.0, eng); }
for (unsigned i = 0; i < da; ++i) { crp.decrement(1, eng); }
xt += 1.0;
} else {
for (unsigned i = 0; i < da; ++i) { crp.decrement(1, eng); }
for (unsigned i = 0; i < da; ++i) { crp.increment(1, 1.0, eng); }
}
}
int c = crp.num_tables(1);
++hist[c];
tot += c;
}
assert(cust == crp.num_customers());
cerr << crp << endl;
cerr << crp.log_likelihood() << endl;
cerr << "P(a) = " << (xt / samples) << endl;
cerr << "mean num tables = " << (tot / samples) << endl;
double error = fabs((tot / samples) - 5.4);
cerr << " error = " << error << endl;
for (unsigned i = 1; i <= cust; ++i)
cerr << i << ' ' << (hist[i]) << endl;
if (error > 0.1) {
cerr << "*** error is too big = " << error << endl;
return 1;
}
test_mh1();
test_mh1a();
test_mh2();
test_mfcrp();
return 0;
}