[fbf06f]: / partyMod / src / LinearStatistic.c

Download this file

427 lines (315 with data), 12.0 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
/**
Linear statistics for conditional inference based on Strasser & Weber (1999)
*\file LinearStatistic.c
*\author $Author$
*\date $Date$
*/
#include "party.h"
/**
Computes the linear statistic, formula (1) in the paper\n
*\param x values of the transformation
*\param p dimension of the transformation
*\param y values of the influence function
*\param q dimension of the influence function
*\param weights case weights
*\param n number of observations
*\param ans return value; a pointer to a REALSXP-vector of length pq
*/
void C_LinearStatistic (const double *x, const int p,
const double *y, const int q,
const double *weights, const int n,
double *ans) {
int i, j, k, kp, kn;
double tmp;
for (k = 0; k < q; k++) {
kn = k * n;
kp = k * p;
for (j = 0; j < p; j++) ans[kp + j] = 0.0;
for (i = 0; i < n; i++) {
/* optimization: weights are often zero */
if (weights[i] == 0.0) continue;
tmp = y[kn + i] * weights[i];
for (j = 0; j < p; j++)
ans[kp + j] += x[j*n + i] * tmp;
}
}
}
/**
R-interface to C_LinearStatistic \n
*\param x values of the transformation
*\param y values of the influence function
*\param weights case weights
*/
SEXP R_LinearStatistic(SEXP x, SEXP y, SEXP weights) {
/* the return value; a vector of type REALSXP */
SEXP ans;
/* dimensions */
int n, p, q;
/*
* only a basic check: we do not coerce objects since this
* function is for internal use only
*/
if (!isReal(x) || !isReal(y) || !isReal(weights))
error("LinStat: arguments are not of type REALSXP");
n = nrow(y);
if (nrow(x) != n || LENGTH(weights) != n)
error("LinStat: dimensions don't match");
q = ncol(y);
p = ncol(x);
PROTECT(ans = allocVector(REALSXP, p*q));
C_LinearStatistic(REAL(x), p, REAL(y), q, REAL(weights), n,
REAL(ans));
UNPROTECT(1);
return(ans);
}
/**
Conditional expectation and covariance of the influence function\n
*\param y values of the influence function
*\param q dimension of the influence function
*\param weights case weights
*\param n number of observations
*\param ans return value; an object of class `ExpectCovarInfluence'
*/
void C_ExpectCovarInfluence(const double* y, const int q,
const double* weights, const int n,
SEXP ans) {
int i, j, k, jq;
/* pointers to the slots of object ans */
double *dExp_y, *dCov_y, *dsweights, tmp;
/* return values: set to zero initially */
dExp_y = REAL(GET_SLOT(ans, PL2_expectationSym));
for (j = 0; j < q; j++) dExp_y[j] = 0.0;
dCov_y = REAL(GET_SLOT(ans, PL2_covarianceSym));
for (j = 0; j < q*q; j++) dCov_y[j] = 0.0;
dsweights = REAL(GET_SLOT(ans, PL2_sumweightsSym));
/* compute the sum of the weights */
dsweights[0] = 0;
for (i = 0; i < n; i++) dsweights[0] += weights[i];
if (dsweights[0] <= 1)
error("C_ExpectCovarInfluence: sum of weights is less than one");
/*
* Expectation of the influence function
*/
for (i = 0; i < n; i++) {
/* observations with zero case weights do not contribute */
if (weights[i] == 0.0) continue;
for (j = 0; j < q; j++)
dExp_y[j] += weights[i] * y[j * n + i];
}
for (j = 0; j < q; j++)
dExp_y[j] = dExp_y[j] / dsweights[0];
/*
* Covariance of the influence function
*/
for (i = 0; i < n; i++) {
if (weights[i] == 0.0) continue;
for (j = 0; j < q; j++) {
tmp = weights[i] * (y[j * n + i] - dExp_y[j]);
jq = j * q;
for (k = 0; k < q; k++)
dCov_y[jq + k] += tmp * (y[k * n + i] - dExp_y[k]);
}
}
for (j = 0; j < q*q; j++)
dCov_y[j] = dCov_y[j] / dsweights[0];
}
/**
R-interface to C_ExpectCovarInfluence\n
*\param y values of the influence function
*\param weights case weights
*/
SEXP R_ExpectCovarInfluence(SEXP y, SEXP weights) {
SEXP ans;
int q, n;
if (!isReal(y) || !isReal(weights))
error("R_ExpectCovarInfluence: arguments are not of type REALSXP");
n = nrow(y);
q = ncol(y);
if (LENGTH(weights) != n)
error("R_ExpectCovarInfluence: vector of case weights does not have %d elements", n);
/* allocate storage for return values */
PROTECT(ans = NEW_OBJECT(MAKE_CLASS("ExpectCovarInfluence")));
SET_SLOT(ans, PL2_expectationSym,
PROTECT(allocVector(REALSXP, q)));
SET_SLOT(ans, PL2_covarianceSym,
PROTECT(allocMatrix(REALSXP, q, q)));
SET_SLOT(ans, PL2_sumweightsSym,
PROTECT(allocVector(REALSXP, 1)));
C_ExpectCovarInfluence(REAL(y), q, REAL(weights), n, ans);
UNPROTECT(4);
return(ans);
}
/**
Conditional expectation and covariance of the a linear statistic\n
*\param x values of the transformation
*\param p dimension of the transformation
*\param y values of the influence function
*\param q dimension of the influence function
*\param weights case weights
*\param n number of observations
*\param expcovinf an object of class `ExpectCovarInfluence'
*\param ans return value; an object of class `ExpectCovar'
*/
void C_ExpectCovarLinearStatistic(const double* x, const int p,
const double* y, const int q,
const double* weights, const int n,
const SEXP expcovinf, SEXP ans) {
int i, j, k, pq;
double sweights = 0.0, f1, f2, tmp;
double *swx, *CT1, *CT2, *Covy_x_swx,
*dExp_y, *dCov_y, *dExp_T, *dCov_T;
pq = p * q;
/* the expectation and covariance of the influence function */
dExp_y = REAL(GET_SLOT(expcovinf, PL2_expectationSym));
dCov_y = REAL(GET_SLOT(expcovinf, PL2_covarianceSym));
sweights = REAL(GET_SLOT(expcovinf, PL2_sumweightsSym))[0];
if (sweights <= 1.0)
error("C_ExpectCovarLinearStatistic: sum of weights is less than one");
/* prepare for storing the results */
dExp_T = REAL(GET_SLOT(ans, PL2_expectationSym));
dCov_T = REAL(GET_SLOT(ans, PL2_covarianceSym));
/* allocate storage: all helpers, initially zero */
swx = Calloc(p, double); /* p x 1 */
CT1 = Calloc(p * p, double); /* p x p */
for (i = 0; i < n; i++) {
/* observations with zero case weights do not contribute */
if (weights[i] == 0.0) continue;
for (k = 0; k < p; k++) {
tmp = weights[i] * x[k * n + i];
swx[k] += tmp;
/* covariance part */
for (j = 0; j < p; j++) {
CT1[j * p + k] += tmp * x[j * n + i];
}
}
}
/*
* dExp_T: expectation of the linear statistic T
*/
for (k = 0; k < p; k++) {
for (j = 0; j < q; j++)
dExp_T[j * p + k] = swx[k] * dExp_y[j];
}
/*
* dCov_T: covariance of the linear statistic T
*/
f1 = sweights/(sweights - 1);
f2 = (1/(sweights - 1));
if (pq == 1) {
dCov_T[0] = f1 * dCov_y[0] * CT1[0];
dCov_T[0] -= f2 * dCov_y[0] * swx[0] * swx[0];
} else {
/* two more helpers needed */
CT2 = Calloc(pq * pq, double); /* pq x pq */
Covy_x_swx = Calloc(pq * q, double); /* pq x q */
C_kronecker(dCov_y, q, q, CT1, p, p, dCov_T);
C_kronecker(dCov_y, q, q, swx, p, 1, Covy_x_swx);
C_kronecker(Covy_x_swx, pq, q, swx, 1, p, CT2);
for (k = 0; k < (pq * pq); k++)
dCov_T[k] = f1 * dCov_T[k] - f2 * CT2[k];
/* clean up */
Free(CT2); Free(Covy_x_swx);
}
/* clean up */
Free(swx); Free(CT1);
}
/**
R-interface to C_ExpectCovarLinearStatistic\n
*\param x values of the transformation
*\param y values of the influence function
*\param weights case weights
*\param expcovinf an object of class `ExpectCovarInfluence'
*/
SEXP R_ExpectCovarLinearStatistic(SEXP x, SEXP y, SEXP weights,
SEXP expcovinf) {
SEXP ans;
int n, p, q, pq;
/* determine the dimensions and some checks */
n = nrow(x);
p = ncol(x);
q = ncol(y);
pq = p * q;
if (nrow(y) != n)
error("y does not have %d rows", n);
if (LENGTH(weights) != n)
error("vector of case weights does not have %d elements", n);
PROTECT(ans = NEW_OBJECT(MAKE_CLASS("ExpectCovar")));
SET_SLOT(ans, PL2_expectationSym,
PROTECT(allocVector(REALSXP, pq)));
SET_SLOT(ans, PL2_covarianceSym,
PROTECT(allocMatrix(REALSXP, pq, pq)));
C_ExpectCovarLinearStatistic(REAL(x), p, REAL(y), q,
REAL(weights), n, expcovinf, ans);
UNPROTECT(3);
return(ans);
}
/**
Linear Statistic with permuted indices\n
*\param x values of the transformation
*\param p dimension of the transformation
*\param y values of the influence function
*\param q dimension of the influence function
*\param n number of observations
*\param nperm number of permutations
*\param indx indices for the x-part
*\param perm (permuted) indices for the y-part
*\param ans return value; a pointer to a REALSXP-vector of length pq
*/
void C_PermutedLinearStatistic(const double *x, const int p,
const double *y, const int q,
const int n, const int nperm,
const int *indx, const int *perm,
double *ans) {
int i, j, k, kp, kn, knpi;
for (k = 0; k < q; k++) {
kn = k * n;
kp = k * p;
for (j = 0; j < p; j++) ans[kp + j] = 0.0;
for (i = 0; i < nperm; i++) {
knpi = kn + perm[i];
for (j = 0; j < p; j++)
ans[kp + j] += x[j*n + indx[i]] * y[knpi];
}
}
}
/**
Linear Statistic with permuted indices\n
*\param x values of the transformation
*\param y values of the influence function
*\param indx indices for the x-part
*\param perm (permuted) indices for the y-part
*/
SEXP R_PermutedLinearStatistic(SEXP x, SEXP y, SEXP indx, SEXP perm) {
SEXP ans;
int n, nperm, p, q, i, *iperm, *iindx;
/*
only a basic check
*/
if (!isReal(x) || !isReal(y))
error("R_PermutedLinearStatistic: arguments are not of type REALSXP");
if (!isInteger(perm))
error("R_PermutedLinearStatistic: perm is not of type INTSXP");
if (!isInteger(indx))
error("R_PermutedLinearStatistic: indx is not of type INTSXP");
n = nrow(y);
nperm = LENGTH(perm);
iperm = INTEGER(perm);
if (LENGTH(indx) != nperm)
error("R_PermutedLinearStatistic: dimensions don't match");
iindx = INTEGER(indx);
if (nrow(x) != n)
error("R_PermutedLinearStatistic: dimensions don't match");
for (i = 0; i < nperm; i++) {
if (iperm[i] < 0 || iperm[i] > (n - 1) )
error("R_PermutedLinearStatistic: perm is not between 1 and nobs");
if (iindx[i] < 0 || iindx[i] > (n - 1) )
error("R_PermutedLinearStatistic: indx is not between 1 and nobs");
}
q = ncol(y);
p = ncol(x);
PROTECT(ans = allocVector(REALSXP, p*q));
C_PermutedLinearStatistic(REAL(x), p, REAL(y), q, n, nperm,
iindx, iperm, REAL(ans));
UNPROTECT(1);
return(ans);
}