-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbethel.cpp
More file actions
510 lines (459 loc) · 14.1 KB
/
Copy pathbethel.cpp
File metadata and controls
510 lines (459 loc) · 14.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
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
506
507
508
509
510
//-------------------------------------------------------
// C++ script implementing Bethel algorithm (optimized)
// Author: Giulio Barcaroli (original), improvements by ...
// Comments in English
//-------------------------------------------------------
#include <Rcpp.h>
#include <iostream> // for possible debugging (use Rcpp::Rcout in production)
#include <cmath>
#include <algorithm>
using namespace Rcpp;
//-------------------------------------------------------
// 1) select_variables
// Extracts columns with names prefix + (1..nvar) from a DataFrame
//-------------------------------------------------------
// [[Rcpp::export]]
NumericMatrix select_variables(DataFrame dati,
std::string prefix,
int nvar) {
StringVector cols(nvar);
for (int i = 0; i < nvar; i++) {
// Build the column name: prefix + (i+1)
cols[i] = prefix + std::to_string(i + 1);
}
DataFrame subset = dati[cols];
// Convert the subset to a NumericMatrix
NumericMatrix mat = internal::convert_using_rfunction(subset, "as.matrix");
return mat;
}
//-------------------------------------------------------
// 2) disjoint
// Creates a disjoint (dummy) matrix based on 'dom' values:
// If dom[j] = k, then disj(j, k-1) = 1
//-------------------------------------------------------
// [[Rcpp::export]]
IntegerMatrix disjoint(const NumericVector& dom) {
// We assume 'dom' contains integer-like values >= 1
int nstrat = dom.size();
// maxdom is the maximum domain value
int maxdom = (int) Rcpp::max(dom);
IntegerMatrix disj(nstrat, maxdom);
// Fill the matrix: if dom[j] = k, we set disj(j, k-1) = 1
for (int j = 0; j < nstrat; j++) {
int val = (int)dom[j] - 1;
if (val >= 0 && val < maxdom) {
disj(j, val) = 1;
}
}
return disj;
}
//-------------------------------------------------------
// 3) m_s
// Multiplies each row of 'mat' by the corresponding
// indicator in 'disj' and combines them horizontally.
// If disj(i,k) = 1, columns for that domain are copied; otherwise 0
//-------------------------------------------------------
// [[Rcpp::export]]
NumericMatrix m_s(const IntegerMatrix& disj,
const NumericMatrix& mat) {
int nc = disj.ncol(); // number of domains
int nstrat = disj.nrow(); // number of strata
int nvar = mat.ncol(); // number of variables
// The output matrix will have size (nstrat) x (nc * nvar)
NumericMatrix out(nstrat, nc * nvar);
// For each domain (k), fill the block of columns [k*nvar : (k+1)*nvar - 1]
for (int k = 0; k < nc; k++) {
int offset = k * nvar;
for (int i = 0; i < nstrat; i++) {
double multiplier = (disj(i, k) == 1) ? 1.0 : 0.0;
for (int j = 0; j < nvar; j++) {
out(i, offset + j) = mat(i, j) * multiplier;
}
}
}
return out;
}
//-------------------------------------------------------
// 4) rowSums_Rcpp
// Calculates row sums of a NumericMatrix
//-------------------------------------------------------
// [[Rcpp::export]]
NumericVector rowSums_Rcpp(const NumericMatrix& x) {
int nr = x.nrow(), nc = x.ncol();
NumericVector ans(nr);
for (int i = 0; i < nr; i++) {
double sum = 0.0;
for (int j = 0; j < nc; j++) {
sum += x(i, j);
}
ans[i] = sum;
}
return ans;
}
//-------------------------------------------------------
// 5) colSums_Rcpp
// Calculates column sums of a NumericMatrix
//-------------------------------------------------------
// [[Rcpp::export]]
NumericVector colSums_Rcpp(const NumericMatrix& x) {
int nr = x.nrow(), nc = x.ncol();
NumericVector ans(nc);
for (int j = 0; j < nc; j++) {
double sum = 0.0;
for (int i = 0; i < nr; i++) {
sum += x(i, j);
}
ans[j] = sum;
}
return ans;
}
//-------------------------------------------------------
// 6) cv_Rcpp
// Example of building a 1 x (nvar*ndom) matrix of CVs.
// If you really want the same CV repeated for each domain,
// this approach replicates the same row n times horizontally.
//-------------------------------------------------------
// [[Rcpp::export]]
NumericMatrix cv_Rcpp(DataFrame errors,
int ndom,
int nvar) {
// Select columns "CV1", "CV2", ..., "CVnvar" from 'errors'
NumericMatrix cvx = select_variables(errors, "CV", nvar);
// cvx presumably has dimension 1 x nvar (depending on 'errors')
// We create a 1 x (nvar * ndom) output
NumericMatrix out(1, nvar * ndom);
// Replicate cvx horizontally ndom times
for (int d = 0; d < ndom; d++) {
int offset = d * nvar;
for (int j = 0; j < nvar; j++) {
out(0, offset + j) = cvx(0, j);
}
}
return out;
}
//-------------------------------------------------------
// 7) crea_a
// Constructs the matrix 'a' used by the Chromy algorithm
// based on m, s, nocens, N, cv, etc.
//-------------------------------------------------------
// [[Rcpp::export]]
NumericMatrix crea_a(NumericMatrix& m,
NumericMatrix& s,
NumericVector& nocens,
NumericVector& N,
NumericVector& cv,
double& epsilon) {
int nr = m.nrow(), nc = m.ncol();
NumericMatrix numA(nr, nc);
// numA(i,j) = N(i)^2 * s(i,j)^2 * nocens(i)
for (int i = 0; i < nr; i++) {
double Ni = N[i];
double Ni2 = Ni * Ni;
double noCi = nocens[i];
for (int j = 0; j < nc; j++) {
double sij = s(i, j);
numA(i, j) = Ni2 * sij * sij * noCi;
}
}
// Build y = N(i)*m(i,j)
NumericMatrix y(nr, nc);
for (int i = 0; i < nr; i++) {
double Ni = N[i];
for (int j = 0; j < nc; j++) {
y(i, j) = Ni * m(i, j);
}
}
// We'll transpose and multiply by cv
NumericMatrix yT(nc, nr);
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
yT(j, i) = y(i, j);
}
}
// Multiply each row j by cv[j]
for (int j = 0; j < nc; j++) {
double cvj = cv[j];
for (int i = 0; i < nr; i++) {
yT(j, i) *= cvj;
}
}
// Transpose back to y
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
y(i, j) = yT(j, i);
}
}
// denA1 = (colSums of y)^2
NumericVector denA1 = colSums_Rcpp(y);
for (int j = 0; j < nc; j++) {
denA1[j] = denA1[j] * denA1[j];
}
// denA2 = colSums( N(i)*s(i,j)^2 * nocens(i) )
NumericMatrix w(nr, nc);
for (int i = 0; i < nr; i++) {
double Ni = N[i];
double noCi = nocens[i];
for (int j = 0; j < nc; j++) {
double sij = s(i, j);
w(i, j) = Ni * sij * sij * noCi;
}
}
NumericVector denA2 = colSums_Rcpp(w);
// denA = denA1 + denA2 + epsilon
NumericVector denA(nc);
for (int j = 0; j < nc; j++) {
denA[j] = denA1[j] + denA2[j] + epsilon;
}
// a = transpose(numA) / denA => then transpose again
NumericMatrix z(nc, nr);
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
z(j, i) = numA(i, j);
}
}
for (int j = 0; j < nc; j++) {
double dA = denA[j];
for (int i = 0; i < nr; i++) {
z(j, i) /= dA;
}
}
// Transpose z back into 'a'
NumericMatrix a(nr, nc);
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
a(i, j) = z(j, i);
}
}
return a;
}
//-------------------------------------------------------
// 8) chromy_Rcpp
// Implements the Chromy algorithm to find allocations
//-------------------------------------------------------
// [[Rcpp::export]]
NumericVector chromy_Rcpp(NumericMatrix a,
double alfatot,
double diff,
int iter,
NumericVector alfa,
NumericVector alfanext,
NumericVector x,
NumericVector cost,
int nvar,
bool realAllocation
) {
int maxiter = 200;
double epsilon = 1e-11;
int nr = a.nrow();
int nc = nvar;
NumericVector n(nr); // final output
while (diff > epsilon && iter < maxiter) {
// 1) den1 = sqrt( rowSums( transpose( transpose(a)*alfa ) ) )
// We'll create a transposed version of a in 'b'
NumericMatrix b(nc, nr);
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
b(j, i) = a(i, j) * alfa[j];
}
}
// c = transpose(b)
NumericMatrix c(nr, nc);
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
c(i, j) = b(j, i);
}
}
NumericVector den1 = rowSums_Rcpp(c);
for (int i = 0; i < nr; i++) {
den1[i] = std::sqrt(den1[i]);
}
// 2) den2 = sum( sqrt( rowSums( t(t(a * cost) * alfa) ) ) )
for (int i = 0; i < nr; i++) {
double ci = cost[i];
for (int j = 0; j < nc; j++) {
c(i, j) = a(i, j) * ci;
}
}
// b = transpose(c), multiply by alfa
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
b(j, i) = c(i, j) * alfa[j];
}
}
// c = transpose(b) again
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
c(i, j) = b(j, i);
}
}
NumericVector d = rowSums_Rcpp(c);
for (int i = 0; i < nr; i++) {
d[i] = std::sqrt(d[i]);
}
double den2 = 0.0;
for (int i = 0; i < nr; i++) {
den2 += d[i];
}
// x(i) = sqrt(cost(i)) / (den1(i)*den2 + epsilon)
for (int i = 0; i < nr; i++) {
x[i] = std::sqrt(cost[i]) / (den1[i]*den2 + epsilon);
}
// alfatot = sum( alfa[j] * (t(a) %*% x)[j]^2 )
for (int i = 0; i < nr; i++) {
for (int j = 0; j < nc; j++) {
b(j, i) = a(i, j);
}
}
NumericVector e(nc, 0.0);
for (int j = 0; j < nc; j++) {
double sum_ = 0.0;
for (int i = 0; i < nr; i++) {
sum_ += b(j, i)* x[i];
}
e[j] = sum_;
}
double new_alfatot = 0.0;
for (int j = 0; j < nc; j++) {
new_alfatot += alfa[j] * (e[j]*e[j]);
}
if (new_alfatot == 0) new_alfatot = epsilon;
// alfanext = (alfa[j]*e[j]^2) / alfatot
for (int j = 0; j < nc; j++) {
alfanext[j] = (alfa[j]* e[j]*e[j]) / new_alfatot;
}
// diff = max( abs( alfanext - alfa ) )
double maxdiff = 0.0;
for (int j = 0; j < nc; j++) {
double tmp = std::fabs(alfanext[j] - alfa[j]);
if (tmp > maxdiff) maxdiff = tmp;
}
diff = maxdiff;
alfatot = new_alfatot;
// update alfa
for (int j = 0; j < nc; j++) {
alfa[j] = alfanext[j];
}
iter++;
}
// n = 1/x if realAllocation = true, or ceil(1/x) otherwise
if (realAllocation) {
for (int i = 0; i < nr; i++) {
n[i] = 1.0 / x[i];
}
} else {
for (int i = 0; i < nr; i++) {
n[i] = std::ceil(1.0 / x[i]);
}
}
return n;
}
//-------------------------------------------------------
// 9) check_n
// Ensures that n(i) respects [minnumstrat, N(i)] boundaries
//-------------------------------------------------------
// [[Rcpp::export]]
NumericVector check_n(NumericVector n,
NumericVector N,
int minnumstrat) {
int nstrat = n.size();
NumericVector n1(nstrat);
for (int i = 0; i < nstrat; i++) {
double ni = n[i];
double Ni = N[i];
if (ni > Ni) {
n1[i] = Ni;
} else if (ni < minnumstrat) {
n1[i] = (Ni >= minnumstrat) ? (double)minnumstrat : Ni;
} else {
n1[i] = ni;
}
}
return n1;
}
//-------------------------------------------------------
// 10) bethel
// Main function to compute Bethel allocation
//-------------------------------------------------------
// [[Rcpp::export]]
NumericVector bethel_cpp(DataFrame strata,
DataFrame errors,
int minnumstrat,
bool realAllocation = true) {
double epsilon = 1e-11;
int nstrat = strata.nrows();
// Determine number of CV variables (subtract domain columns)
StringVector errors_names = errors.names();
int nvar = errors_names.size() - 2;
bool isPresent = (std::find(errors_names.begin(), errors_names.end(), "domainvalue") != errors_names.end());
if (!isPresent) {
nvar = errors_names.size() - 1;
}
// Extract mean (M...) and standard deviation (S...) from 'strata'
NumericMatrix med = select_variables(strata, "M", nvar);
NumericMatrix esse = select_variables(strata, "S", nvar);
// Other vectors from 'strata'
NumericVector N = strata["N"];
NumericVector cens = strata["CENS"];
NumericVector nocens(nstrat);
for (int i = 0; i < nstrat; i++) {
nocens[i] = 1.0 - cens[i];
}
NumericVector cost = strata["COST"];
// 'DOM1' and creation of disjoint matrix
NumericVector dom = strata["DOM1"];
int ndom = (int) Rcpp::max(dom);
IntegerMatrix disj = disjoint(dom);
NumericMatrix m = m_s(disj, med);
NumericMatrix s = m_s(disj, esse);
// Retrieve CV matrix
NumericMatrix cv = cv_Rcpp(errors, ndom, nvar);
nvar = cv.ncol(); // should remain the same
// Build matrix a
NumericMatrix a = crea_a(m, s, nocens, N, cv, epsilon);
// Chromy parameters
double alfatot = 0.0;
double diff = 999.0;
int iter = 0;
NumericVector alfa(nvar);
NumericVector alfanext(nvar);
NumericVector x(nstrat, 0.1);
// Initialize alfa
for (int j = 0; j < nvar; j++) {
alfa[j] = 1.0 / nvar;
}
// First Chromy call
NumericVector n = chromy_Rcpp(a, alfatot, diff, iter, alfa, alfanext, x, cost, nvar, realAllocation);
// Check if n(i) > N(i)
int contx = 0;
for (int i = 0; i < nstrat; i++) {
if (n[i] > N[i]) {
contx++;
cens[i] = 1.0;
nocens[i] = 0.0;
}
}
n = check_n(n, N, minnumstrat);
// Iterate if overshoot occurs
int iter1 = 0;
int maxiter1 = 25;
while (contx > 0 && iter1 < maxiter1) {
iter1++;
a = crea_a(m, s, nocens, N, cv, epsilon);
n = chromy_Rcpp(a, alfatot, diff, iter, alfa, alfanext, x, cost, nvar, realAllocation);
contx = 0;
for (int i = 0; i < nstrat; i++) {
if (n[i] > N[i]) {
contx++;
cens[i] = 1.0;
nocens[i] = 0.0;
n[i] = N[i];
}
}
n = check_n(n, N, minnumstrat);
}
// Final step: n <- n * nocens + N * cens
for (int i = 0; i < nstrat; i++) {
n[i] = n[i]*nocens[i] + N[i]*cens[i];
}
return n;
}