[go: up one dir, main page]

Menu

[2a2a8a]: / src / mclist.cc  Maximize  Restore  History

Download this file

437 lines (331 with data), 9.3 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
/*
* See COPYING file distributed along with the psignifit package for
* the copyright and license terms
*/
#include "mclist.h"
void newsample ( const PsiData * data, const std::vector<double>& p, std::vector<int> * sample ) {
/* Draw a new sample from the psychometric function */
BinomialRandom binomial ( 10, 0.5 ); // Initialize with nonsense parameters
unsigned int k; // Block index
for ( k=0; k<data->getNblocks(); k++ ) {
binomial.setprm ( data->getNtrials(k), p[k] );
(*sample)[k] = binomial.draw ();
}
}
/************************************************************
* PsiMClist methods
*/
std::vector<double> PsiMClist::getEst ( unsigned int i ) const
{
// Check that the call does not ask for something we don't have
if ( i>=getNsamples() )
throw BadIndexError();
unsigned int k;
std::vector<double> out ( getNparams() );
for (k=0; k<getNparams(); k++)
out[k] = mcestimates[k][i];
return out;
}
double PsiMClist::getEst ( unsigned int i, unsigned int prm ) const
{
// check that the call does not ask for something we don't have
if ( i>=getNsamples() )
throw BadIndexError();
if ( prm>=getNparams() )
throw BadIndexError();
return mcestimates[prm][i];
}
void PsiMClist::setEst ( unsigned int i, const std::vector<double> est, double deviance )
{
// Check that the call does not ask for something we can't do
if ( i>=getNsamples() )
throw BadIndexError();
unsigned int k;
for ( k=0; k<getNparams(); k++ )
mcestimates[k][i] = est[k];
deviances[i] = deviance;
}
double PsiMClist::getPercentile ( double p, unsigned int prm ) {
if ( prm>=getNparams() )
throw BadIndexError();
if ( p>1 || p<0 )
throw BadArgumentError();
int position;
sort( mcestimates[prm].begin(),mcestimates[prm].end() );
position = getNsamples()*p;
return mcestimates[prm][position];
}
void PsiMClist::setdeviance ( unsigned int i, double deviance ) {
if ( i>=getNsamples() )
throw BadIndexError();
deviances[i] = deviance;
}
double PsiMClist::getdeviance ( unsigned int i ) const {
if ( i>=getNsamples() )
throw BadIndexError();
return deviances[i];
}
double PsiMClist::getDeviancePercentile ( double p ) {
if ( p<=0 || p>= 1 )
throw BadArgumentError();
int ind ( p*deviances.size() );
sort( deviances.begin(), deviances.end() );
return deviances[ind];
}
double PsiMClist::getMean ( unsigned int prm ) const {
double m(0);
unsigned int i,Nsamples(getNsamples());
if ( prm>=getNparams() )
throw BadIndexError();
for (i=0; i<Nsamples; i++)
m += getEst ( i, prm );
m /= Nsamples;
return m;
}
double PsiMClist::getStd ( unsigned int prm ) const {
double m ( getMean ( prm ) ), s(0), ss;
unsigned int i, Nsamples(getNsamples());
if ( prm>=getNparams() )
throw BadIndexError();
for (i=0; i<Nsamples; i++) {
ss = getEst ( i, prm ) - m;
s += ss*ss;
}
s /= Nsamples-1;
return sqrt(s);
}
/************************************************************
* BootstrapList methods
*/
void BootstrapList::setData ( unsigned int i, const std::vector<int>& newdata )
{
if ( i>=getNsamples() || i<0 )
throw BadIndexError();
unsigned int k;
for ( k=0; k<getNblocks(); k++ )
data[i][k] = newdata[k];
}
std::vector<int> BootstrapList::getData ( unsigned int i ) const
{
if ( i>=getNsamples() || i<0 )
throw BadIndexError();
return data[i];
}
double BootstrapList::getThres ( double p, unsigned int cut ) {
if ( cut>=cuts.size() )
throw BadIndexError();
if ( p>1 || p<0 )
throw BadArgumentError();
int position;
sort( thresholds[cut].begin(), thresholds[cut].end() );
// Bias correction of p
if (BCa)
p = Phi(bias_t[cut] + (invPhi(p) + bias_t[cut])/(1-acceleration_t[cut]*(invPhi(p) + bias_t[cut])));
position = int(getNsamples()*p);
return thresholds[cut][position];
}
double BootstrapList::getThres_byPos ( unsigned int i, unsigned int cut ) {
if ( cut>=cuts.size() )
throw BadIndexError();
if (i>getNsamples())
throw BadIndexError();
return thresholds[cut][i];
}
void BootstrapList::setThres ( double thres, unsigned int i, unsigned int cut )
{
if ( i>=getNsamples() )
throw BadIndexError();
if (cut>=cuts.size() )
throw BadIndexError();
thresholds[cut][i] = thres;
}
double BootstrapList::getSlope ( double p, unsigned int cut ) {
if ( cut>=cuts.size() )
throw BadIndexError();
if ( p>1 || p<0 )
throw BadArgumentError();
int position;
sort( slopes[cut].begin(), slopes[cut].end() );
// Bias correction of p
if (BCa)
p = Phi(bias_s[cut] + (invPhi(p) + bias_s[cut])/(1-acceleration_s[cut]*(invPhi(p) + bias_s[cut])));
position = int(getNsamples()*p);
return slopes[cut][position];
}
double BootstrapList::getSlope_byPos ( unsigned int i, unsigned int cut ) {
if ( cut>=cuts.size() )
throw BadIndexError();
if (i>getNsamples())
throw BadIndexError();
return slopes[cut][i];
}
void BootstrapList::setSlope ( double slope, unsigned int i, unsigned int cut )
{
if ( i>=getNsamples() )
throw BadIndexError();
if (cut>=cuts.size() )
throw BadIndexError();
slopes[cut][i] = slope;
}
double BootstrapList::getCut ( unsigned int i ) const
{
if ( i>=cuts.size() || i<0 )
throw BadIndexError();
return cuts[i];
}
void BootstrapList::setRpd ( unsigned int i, double r_pd ) {
if ( i>=getNsamples() )
throw BadIndexError();
Rpd[i] = r_pd;
}
double BootstrapList::getRpd ( unsigned int i ) const {
if ( i>=getNsamples() )
throw BadIndexError();
return Rpd[i];
}
double BootstrapList::percRpd ( double p ) {
if ( p<0 || p>1 )
throw BadArgumentError();
int index ( p*(getNsamples()-1));
sort ( Rpd.begin(), Rpd.end() );
return Rpd[index];
}
void BootstrapList::setRkd ( unsigned int i, double r_kd ) {
if ( i>=getNsamples() )
throw BadIndexError();
Rkd[i] = r_kd;
}
double BootstrapList::getRkd ( unsigned int i ) const {
if ( i>=getNsamples() )
throw BadIndexError();
return Rkd[i];
}
double BootstrapList::percRkd ( double p ) {
if ( p<0 || p>1 )
throw BadIndexError();
int index ( p*(getNsamples()-1) );
sort ( Rkd.begin(), Rkd.end() );
return Rkd[index];
}
/************************************************************
* JackKnifeList methods
*/
double JackKnifeList::influential ( unsigned int block, const std::vector<double>& ci_lower, const std::vector<double>& ci_upper ) const {
unsigned int prm;
double est;
double infl(0),x;
for ( prm=0; prm<getNparams(); prm++ ) {
est = getEst(block,prm);
if ( est<mlestimate[prm] ) {
x = ( mlestimate[prm]-est ) / ( mlestimate[prm]-ci_lower[prm] );
} else {
x = ( est-mlestimate[prm] ) / ( ci_upper[prm]-mlestimate[prm] );
}
if ( x>infl )
infl = x;
}
return infl;
}
bool JackKnifeList::outlier ( unsigned int block ) const {
if ( block>=getNblocks() )
throw BadIndexError();
if ( maxdeviance-getdeviance(block) > 6.63 )
return true;
return false;
}
/************************************************************
* MCMCList methods
*/
void MCMCList::setppData ( unsigned int i, const std::vector<int>& ppdata, double ppdeviance )
{
if ( i>=getNsamples() || i<0 )
throw BadIndexError ();
unsigned int k;
for ( k=0; k<getNblocks(); k++ )
posterior_predictive_data[i][k] = ppdata[k];
posterior_predictive_deviances[i] = ppdeviance;
}
std::vector<int> MCMCList::getppData ( unsigned int i ) const
{
if ( i>=getNsamples() || i<0 )
throw BadIndexError();
return posterior_predictive_data[i];
}
int MCMCList::getppData ( unsigned int i, unsigned int j ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
if ( j>=getNblocks() )
throw BadIndexError();
return posterior_predictive_data[i][j];
}
double MCMCList::getppDeviance ( unsigned int i ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
return posterior_predictive_deviances[i];
}
void MCMCList::setppRpd ( unsigned int i, double Rpd )
{
if ( i>=getNsamples() )
throw BadIndexError();
posterior_predictive_Rpd[i] = Rpd;
}
double MCMCList::getppRpd ( unsigned int i ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
return posterior_predictive_Rpd[i];
}
void MCMCList::setppRkd ( unsigned int i, double Rkd )
{
if ( i>=getNsamples() )
throw BadIndexError();
posterior_predictive_Rkd[i] = Rkd;
}
double MCMCList::getppRkd ( unsigned int i ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
return posterior_predictive_Rkd[i];
}
void MCMCList::setRpd ( unsigned int i, double Rpd )
{
if ( i>=getNsamples() )
throw BadIndexError();
posterior_Rpd[i] = Rpd;
}
double MCMCList::getRpd ( unsigned int i ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
return posterior_Rpd[i];
}
void MCMCList::setRkd ( unsigned int i, double Rkd )
{
if ( i>=getNsamples() )
throw BadIndexError();
posterior_Rkd[i] = Rkd;
}
double MCMCList::getRkd ( unsigned int i ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
return posterior_Rkd[i];
}
void MCMCList::setlogratio ( unsigned int i, unsigned int j, double logratio )
{
if ( i>=getNsamples() )
throw BadIndexError();
if ( j>=getNblocks() )
throw BadIndexError();
logratios[i][j] = logratio;
}
double MCMCList::getlogratio ( unsigned int i, unsigned int j ) const
{
if ( i>=getNsamples() )
throw BadIndexError();
if ( j>=getNblocks() )
throw BadIndexError();
return logratios[i][j];
}