-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFuns.cpp
executable file
·620 lines (467 loc) · 10.6 KB
/
Funs.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
extern "C" {
#include <R.h>
#include <Rmath.h>
};
#include "global.h"
#include "List.h"
#include "Node.h"
#include "Rlob.h"
#include "Funs.h"
#include "Likelihood.h"
#include "Prior.h"
#include "BirthDeath.h"
#include "ChangeRule.h"
#include "Swap.h"
typedef Node *NodeP;
void MakeBotVec(Node *top,NodeP **botvec,int *NBot)
// allocates and defines array of bot nodes for tree top, sets NBot
{
int i;
List *bots;
top->GetBotList(&bots);
*NBot = bots->length;
*botvec = new NodeP [*NBot+1];
Cell *cell = bots->first;
(*botvec)[1]=(Node *)cell->contents;
for(i=2;i<=(*NBot);i++) {
cell = cell->after;
(*botvec)[i]=(Node *)cell->contents;
}
bots->deall();
delete bots;
}
void MakeNogVec(Node *top,NodeP **nogvec,int *NNog)
// allocates and defines list of nog nodes for tree top, sets NNog
{
int i;
List *bots;
top->GetNogList(&bots);
*NNog = bots->length;
*nogvec = new NodeP [*NNog+1];
if (*NNog){
Cell *cell = bots->first;
(*nogvec)[1]=(Node *)cell->contents;
for(i=2;i<=(*NNog);i++) {
cell = cell->after;
(*nogvec)[i]=(Node *)cell->contents;
}
}
bots->deall();
delete bots;
}
void MakeSwapVec(Node *top,NodeP **swapvec,int *Nswap)
//
{
int i;
List *swaps;
top->GetSwapsList(&swaps);
*Nswap = swaps->length;
*swapvec = new NodeP [*Nswap+1];
if (*Nswap){
Cell *cell = swaps->first;
(*swapvec)[1]=(Node *)cell->contents;
for(i=2;i<=(*Nswap);i++) {
cell = cell->after;
(*swapvec)[i]=(Node *)cell->contents;
}
}
swaps->deall();
delete swaps;
}
void MakeNotBotVec(Node *top,NodeP **notbotvec,int *Nnotbot)
// allocates and defines list of nog nodes for tree top, sets NNog
{
int i;
List *bots;
top->GetNotBotList(&bots);
*Nnotbot = bots->length;
*notbotvec = new NodeP [*Nnotbot+1];
if (*Nnotbot){
Cell *cell = bots->first;
(*notbotvec)[1]=(Node *)cell->contents;
for(i=2;i<=(*Nnotbot);i++) {
cell = cell->after;
(*notbotvec)[i]=(Node *)cell->contents;
}
}
bots->deall();
delete bots;
}
void MakeIntVec(List *intlist, int **ivec, int *n)
//allocate and define vec of integers corresponding to list of int pointers
{
int i;
*n = intlist->length;
*ivec = new int [*n + 1];
Cell *cell = intlist->first;
if(*n>0) (*ivec)[1]=*((int *)(cell->contents));
for(i=2;i<=(*n);i++) {
cell = cell->after;
(*ivec)[i]=*((int *)(cell->contents));
}
}
void GetDataInd(Node *top,int *ind)
// sets ind[i] to be index of nodes corresponding to obs i
// ind is already allocated of length NumObs
{
int i,j;
int nbot;
NodeP *botvec;
MakeBotVec(top,&botvec,&nbot);
Node *nn;
for(i=1;i<=NumObs;i++) {
top->FindNode(XDat[i],&nn);
for(j=1;nn!=botvec[j];j++);
ind[i] = j;
}
delete [] botvec;
}
void GetDataInd(Node *top,int *ind, int NumObsPred, double** data)
// sets ind[i] to be index of nodes corresponding to obs i
// ind is already allocated of length NumObsPred
{
int i,j;
int nbot;
NodeP *botvec;
MakeBotVec(top,&botvec,&nbot);
Node *nn;
for(i=1;i<=NumObsPred;i++) {
top->FindNode(data[i],&nn);
for(j=1;nn!=botvec[j];j++);
ind[i] = j;
}
delete [] botvec;
}
int getMaxDepth(Node *top)
{
int nbot;
NodeP *botvec;
MakeBotVec(top,&botvec,&nbot);
int i;
int max=0;
int tDepth;
for(i=1;i<=nbot;i++)
{
tDepth = Depth(botvec[i]);
if(tDepth>max) max = tDepth;
}
delete [] botvec;
return max;
}
Rule *GetRulePointer(int index, int curindex, int depth, int curdepth, Node* n)
// gets the pointer to the rule of the node having index value index
// index: index of node whose rule we want
// curindex: index of node n
// depth: number of levels below the top
// curdepth: depth of node n
//
// when you call at the top you should have
// curindex = 1
// depth = top->DepthBelow()
// curdepth = 0
// node = top
{
//return &(n->rule);
int nn;
// if you are a bottom node then you have no rule so return 0 for the pointer value
if(n->Bot) return 0;
// if you are at the right node return the address of the rule
if(index==curindex)
{
return &(n->rule);
}
// let d= depth-curdepth = # of levels below n
// the number of nodes in each branch below n is 2^d -1
// the index of the left child is the curindex + 1
// the index of the left is current + #below to the left + 1 = curindex + 2^d
// either go left or right
else
{
nn = (int)pow(2.0,(double)(depth-curdepth));
if(index >= (curindex + nn))
{
return GetRulePointer(index,curindex+nn,depth,curdepth+1,n->RightC);
}
else
{
return GetRulePointer(index,curindex+1,depth,curdepth+1,n->LeftC);
}
}
}
int RulesDifferent(Rule *r1,Rule *r2)
{
if((r1==0) && (r2==0)) return 0;
if((r1==0) && (r2!=0)) return 1;
if((r1!=0) && (r2==0)) return 1;
int v1 = r1->Var;
int v2 = r2->Var;
if(v1!=v2)
{
return 1;
}
else
{
return 0;
}
}
int ShannonBanksMetric(Node *top1,Node *top2)
{
int d1 = top1->DepthBelow();
int d2 = top2->DepthBelow();
int depth = (int)max(d1,d2);
int numnodes = (int)pow(2.0,depth+1.0) - 1;
int index;
Rule *r1;
Rule *r2;
int numdiff=0;
for(index=1;index<=numnodes;index++)
{
r1 = GetRulePointer(index, 1, depth, 0, top1);
r2 = GetRulePointer(index, 1, depth, 0, top2);
numdiff += RulesDifferent(r1,r2);
}
return numdiff;
}
/*double FitMetric(Node* top1,Node* top2)
//gets the fits for the two trees and sums the squared differences between the fitted values
{
double* f1 = Fits(top1);
double* f2 = Fits(top2);
int i;
double met=0;
for(i=1;i<=NumObs;i++)
{
met += (f1[i]-f2[i])*(f1[i]-f2[i]);
}
delete [] f1;
delete [] f2;
return met;
}
int MisCMetric(Node* top1,Node* top2)
//gets the fits for the two trees and sums the squared differences between the fitted values
{
double* f1 = Fits(top1);
double* f2 = Fits(top2);
int i;
int met=0;
int r1,r2;
for(i=1;i<=NumObs;i++)
{
r1=0;
r2=0;
if(f1[i]>.5) r1=1;
if(f2[i]>.5) r2=1;
if(!(r1==r2)) met += 1;
}
delete [] f1;
delete [] f2;
return met;
}*/
int AndrewsMetric(Node *top1,Node *top2)
// for each pair of observations count the number of times they are not "partioned the same"
// in the two trees.
// they are partioned the same if whether or not the two observations are in the same
// partition is the same for both trees
{
int *ind1 = new int [NumObs+1];
int *ind2 = new int [NumObs+1];
GetDataInd(top1,ind1);
GetDataInd(top2,ind2);
int count=0;
int Isame1,Isame2;
int i,j;
for(i=1;i<NumObs;i++)
{
for(j=(i+1);j<=NumObs;j++)
{
Isame1 = (ind1[i]==ind1[j])?1: 0;
Isame2 = (ind2[i]==ind2[j])?1: 0;
count += abs(Isame1-Isame2);
}
}
delete [] ind1;
delete [] ind2;
return count;
}
void AddDatChildren(Node *n)
{
if(!(n->rule).Var)
printf("error in AddDatChildren: rule not set\n");
if(((n->LeftC)->DataList.length!=0) || ((n->RightC)->DataList.length!=0))
printf("error in AddDatChildren: data already set\n");
int *divec;
int nob;
MakeIntVec(&(n->DataList),&divec,&nob);
int i;
for(i=1;i<=nob;i++) {
if((n->rule).Right(XDat[divec[i]])) {
(n->RightC)->SetData(divec[i]);
}
else {
(n->LeftC)->SetData(divec[i]);
}
}
delete [] divec;
}
void FixDataBelow(Node *cnode)
{
int *divec;
int nobs;
int i;
(cnode->LeftC)->ClearData();
(cnode->RightC)->ClearData();
MakeIntVec(&(cnode->DataList), &divec, &nobs);
for(i=1;i<=nobs;i++) {
if ((cnode->rule).Right(XDat[divec[i]])) {
(cnode->RightC)->SetData(divec[i]);
} else {
(cnode->LeftC)->SetData(divec[i]);
}
}
delete [] divec;
}
void UpDateOrdVarAvail(Node *n, int VarI, int left, int right)
{
int numsplit = right-left+1;
if(numsplit<1) {
(n->VarAvail)[VarI]=0;
} else {
(n->VarAvail)[VarI]=1;
}
if(!(n->Bot)) {
int lleft,lright,rleft,rright;
lleft=left;
rleft=left;
lright=right;
rright=right;
if(((n->rule).Var)==VarI) {
lright = (n->rule).OrdRule - 1;
rleft = (n->rule).OrdRule + 1;
}
UpDateOrdVarAvail(n->LeftC,VarI,lleft,lright);
UpDateOrdVarAvail(n->RightC,VarI,rleft,rright);
}
}
void UpDateCatVarAvail(Node *n, int VarI, int *cats)
{
int i;
int RN = RuleNum[VarI];
if(ISum(RN,cats)<2) {
(n->VarAvail)[VarI]=0;
} else {
(n->VarAvail)[VarI]=1;
}
if(!(n->Bot)) {
int *catsl = new int [RN+1];
int *catsr = new int [RN+1];
for(i=1;i<=RN;i++) {
catsl[i]=cats[i];
catsr[i]=cats[i];
}
if(((n->rule).Var)==VarI) {
for(i=1;i<=RN;i++) {
if(cats[i]) {
if((n->rule).CatRule[i]) {
catsl[i]=0;
} else {
catsr[i]=0;
}
}
}
}
UpDateCatVarAvail(n->LeftC,VarI,catsl);
UpDateCatVarAvail(n->RightC,VarI,catsr);
}
delete [] cats;
}
void UpDateVarAvail(Node *n,int VarI)
{
if(VarType[VarI]==CAT) {
int *cats = new int [RuleNum[VarI]+1];
GetSetCats(n,VarI,cats);
UpDateCatVarAvail(n,VarI,cats);// note cats is deleted in here
} else {
int LeftI,RightI;
GetSplitInterval(&LeftI,&RightI, n,VarI);
UpDateOrdVarAvail(n,VarI,LeftI,RightI);
}
}
void CheckTree(Node *top)
{
}
void RestrictSize(Node **n,int min)
// this isn't done yet, but I want to modify so it will trim back a tree
// if the tree has fewer than min obs in a node
{
/* if ( (!(*n)->Bot) & (((*n)->Datalist).length>min)){
RestrictSize(&((*n)->LeftC),min);
RestrictSize(&((*n)->RightC),min);
}
printf("%d\n",(*n)->DataList.length);
else {
RestrictSize(&((*n)->LeftC),min);
RestrictSize(&((*n)->RightC),min);
printf("%d\n",(*n)->DataList.length);
}
*/
}
double Metrop(Node **top,int *Done,int *step)
{
double alpha;
int BD;
//disable backout----------------
////Node *copy = new Node;
//(*top)->CopyTree(copy);
//++++++++++++++++++++++++++++++++
//double u = ran1(&idum);
double u = unif_rand();
if(u<pBD) {
alpha = BirthDeath(*top,&BD,Done);
if(BD) {
*step = BIRTH;
} else {
*step = DEATH;
}
} else if(u<pBD+pSwap) {
alpha = SwapRule(*top,Done);
*step=SWAP;
} else {
alpha = ChangeRule(*top,Done);
*step = CHANGE;
}
//disable backout
// back out if any nodes of the resultant tree have less than 5 obs---------------------------
/*
NodeP *botvec;
int NBot;
MakeBotVec(*top,&botvec,&NBot);
int i;
int minN = NumObs;
for (i=1;i<=NBot;i++){
if ((((botvec[i])->DataList).length) < minN)
minN = (((botvec[i])->DataList).length);
}
delete [] botvec;
if (minN < 10){
(*top)->deall();
delete (*top);
(*top) = copy;
alpha = -1;
} else {
copy->deall();
delete copy;
}
*/
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
return alpha;
}
void countVarUsage(std::vector<Node*>& trees, std::vector<int>& cnt)
{
std::vector<VarUsage> vu;
cnt.clear(); cnt.resize(NumX+1);
for(std::vector<Node*>::size_type i=1;i<trees.size();i++) {
vu.clear();
getVarUsage(trees[i],0,0,vu);
for(std::vector<VarUsage>::size_type j=0;j<vu.size();j++) cnt[vu[j].varIndex]++;
}
}