-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHist.h
1570 lines (1318 loc) · 34.9 KB
/
Hist.h
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// $Id: Hist.h,v 1.25 2009/06/25 04:04:14 samn Exp $
#pragma once
#include "Cluster.h"
#include "WCMath.h"
#include "KDTree.h"
#include "ScopedTimer.h"
#include "Log.h"
#include <map>
#include <set>
//#define DO_TIMING
//binary logarithm
inline prob_t log2(prob_t d)
{
static prob_t dl2 = log(2.0);
return log(d) / dl2;
}
inline double log2(double d)
{
static double dl2 = log(2.0);
return log(d) / dl2;
}
prob_t Prob(int,int);
void InitProbs(int iMaxNumElems);
struct ProbInitFree
{
ProbInitFree(int i);
~ProbInitFree();
};
inline bool GZeroMinElem(float f1,float f2)
{
return f1 > 0.0 && (f1 < f2 || f2 <= 0.0);
}
inline float GZeroMinElem(vector<float>& v)
{
float fm = *std::max_element(v.begin(),v.end());
int i=0,sz=v.size();
for(;i<sz;i++)
if(v[i]<fm && v[i]>0.0)
fm=v[i];
return fm;
}
inline float MinElem(vector<float>& v,bool bAllowZero)
{
float fm = *std::max_element(v.begin(),v.end());
int i=0,sz=v.size();
for(;i<sz;i++)
if(v[i]<fm && (bAllowZero || v[i]>0.0))
fm=v[i];
return fm;
}
inline int MinIdx(vector<float>& v,bool bAllowZero)
{
float fm = *std::max_element(v.begin(),v.end());
int i=0,sz=v.size(), minID = 0;
bool bFound = false;
for(;i<sz;i++)
{
if(v[i]<fm && (bAllowZero || v[i]>0.0))
{
bFound = true;
fm=v[i];
minID=i;
}
}
if(!bFound) return -1;
return minID;
}
struct Neighbor
{
prob_t m_dist;
int m_id;
Neighbor(int id,prob_t dist)
:m_id(id),m_dist(dist){}
Neighbor()
:m_id(0),m_dist(0){}
};
inline bool operator<(Neighbor& n1,Neighbor& n2)
{
return n1.m_dist<n2.m_dist;
}
//kd tree from biopython used for continuous probability distribution estimates
class KDTreeHist
{
NSKDTree::KDTree* m_pTree;
int m_iDims;
int m_iNumElems;
prob_t m_dPiPow;
prob_t m_dTop;
prob_t m_dGamma;
vector<float> m_vData;
vector<float> m_vProbs;
public:
KDTreeHist()
:m_iDims(0),
m_iNumElems(0),
m_pTree(0)
{
}
virtual ~KDTreeHist()
{
if(m_pTree) delete m_pTree;
}
prob_t Top(){ return m_dTop; }
prob_t PiPow(){ return m_dPiPow; }
prob_t Gam(){ return m_dGamma; }
bool SetData(vector<float>& vFloat,vector<int>& vIDs,int iNumPoints,int iDims,int iCID,bool bNot=false)
{
if(!vFloat.size() || iNumPoints<1) return false;
int iV = 0 , iC = 0;
int iTotalVs = vFloat.size() / iDims;
m_vData = vector<float>(iNumPoints*iDims);
int j = 0 , k = 0;
if(bNot)
{
for(iV=0;iV<vIDs.size();iV++)
{
if(vIDs[iV] != iCID && vIDs[iV]<1000)
{
int iD = 0;
for(iD=0;iD<iDims;iD++)
{
m_vData[j++]=vFloat[iV*iDims+iD];
}
}
}
}
else
{
for(iV=0;iV<vIDs.size();iV++)
{
if(vIDs[iV] == iCID)
{
int iD = 0;
for(iD=0;iD<iDims;iD++)
{
m_vData[j++]=vFloat[iV*iDims+iD];
}
}
}
}
m_iDims = iDims;
if(m_pTree) delete m_pTree;
m_pTree = new NSKDTree::KDTree(m_iDims,8,false);
m_iNumElems = iNumPoints;
const prob_t PI=3.14159265358979323846;
m_dPiPow = pow((prob_t)PI,(prob_t)m_iDims/two);
m_dTop = (one/(m_iNumElems-one));
m_dGamma = Gamma(m_iDims/two+one);
m_pTree->set_data(&m_vData[0],iNumPoints);
return true;
}
bool SetData(int iDims,float* pData,int iNumPoints)
{
if(!pData || iNumPoints<1) return false;
m_iDims = iDims;
if(m_pTree) delete m_pTree;
//m_pTree = new NSKDTree::KDTree(m_iDims,16,false);
m_pTree = new NSKDTree::KDTree(m_iDims,8,false);
m_iNumElems = iNumPoints;
m_vData = vector<float>(iNumPoints*m_iDims);
memcpy(&m_vData[0],pData,iNumPoints*m_iDims*sizeof(float));
const prob_t PI=3.14159265358979323846;
m_dPiPow = pow((prob_t)PI,(prob_t)m_iDims/two);
m_dTop = (one/(m_iNumElems-one));
m_dGamma = Gamma(m_iDims/two+one);
m_pTree->set_data(&m_vData[0],iNumPoints);
// m_vProbs = vector<float>(iNumPoints);
return true;
}
int NumElems()
{
return m_iNumElems;
}
int NumDims()
{
return m_iDims;
}
float* operator[](int i)
{
if(i >= m_iNumElems) return 0;
return &m_vData[i*m_iDims];
}
float GetKNN(float* p,vector<Neighbor>& vnn,int iNNToFind,double fRadStart=8.0,double fRadFctr=2.0)
{
if(m_iNumElems == 1) return 0.0;
int iIter = 0;
float fRad = fRadStart;
while(true)
{
m_pTree->search_center_radius_sq(p,fRad,iNNToFind);
int iCount = m_pTree->get_count();
if(iCount>iNNToFind)// || (iCount&&iNNToFind==1))
{
vector<float> vRadii(iCount);
m_pTree->copy_radii_sq(&vRadii[0]);
vector<long> vID(iCount);
m_pTree->copy_indices(&vID[0]);
vnn=vector<Neighbor>(iCount);
#ifdef _DEBUG
Write2Log("found %d neighbors want %d",iCount,iNNToFind);
#endif
int i = 0, j = 0;
for(i=0;i<iCount;i++)
if(vRadii[i]>0.0)
vnn[j++] = Neighbor(vID[i],vRadii[i]);
vnn.resize(j);
std::sort(vnn.begin(),vnn.end());
if(vnn.size()>iNNToFind)
{
vector<Neighbor> vNNTmp(iNNToFind);
std::copy(vnn.begin(),vnn.begin()+iNNToFind,vNNTmp.begin());
vnn=vNNTmp;
}
#ifdef _DEBUG
prob_t ttt = vnn[0].m_dist;
#endif
return fRad;
}
//increase search radius
fRad *= fRadFctr;
}
}
float GetKNN(float* p,Neighbor* vnn,int iNNToFind,double fRadStart,double fRadFctr)
{
if(m_iNumElems == 1) return 0.0;
int iIter = 0;
float fRad = fRadStart;
while(true)
{
m_pTree->search_center_radius_sq(p,fRad,iNNToFind);
int iCount = m_pTree->get_count();
if(iCount>iNNToFind)// || (iCount&&iNNToFind==1))
{
vector<float> vRadii(iCount);
m_pTree->copy_radii_sq(&vRadii[0]);
vector<long> vID(iCount);
m_pTree->copy_indices(&vID[0]);
vector<Neighbor> vnnTMP(iCount);
int i = 0, j = 0;
for(i=0;i<iCount;i++)
if(vRadii[i]>0.0)
vnnTMP[j++] = Neighbor(vID[i],vRadii[i]);
vnnTMP.resize(j);
std::sort(vnnTMP.begin(),vnnTMP.end());
for(i=0;i<iNNToFind;i++) vnn[i]=vnnTMP[i];
return fRad;
}
//increase search radius
fRad *= fRadFctr;
}
}
float GetAllKNN(A2D<Neighbor>& vnn,int iNNToFind,double fRadStart,double fRadFctr,vector<int>& vFound)
{
if(m_iNumElems == 1) return 0.0;
int iIter = 0;
float fRad = fRadStart;
vnn.Fill(Neighbor(0,INF));
vFound.resize(m_iNumElems);
while(true)
{
m_pTree->neighbor_search_sq(fRadStart);
int iCount = m_pTree->neighbor_get_count();
if(iCount/2>m_iNumElems*iNNToFind)
{
Neighbor** pnn = vnn.GetP();
vector<float> vRadii(iCount);
m_pTree->neighbor_copy_radii_sq(&vRadii[0]);
vector<long> vID(iCount*2);
m_pTree->neighbor_copy_indices(&vID[0]);
int i = 0 , j = 0, k = 0;
for(i=0;i<iCount;i+=2)
{
int iN1 = vID[i], iN2 = vID[i+1], iPos = 0;
prob_t distSQ = vRadii[i/2];
if(distSQ <= 0.0f) continue;
//find sorted position in iN1 neighbor array to place element in
for(j=0;j<vFound[iN1];j++)
{
if(distSQ < pnn[iN1][j].m_dist)
{
iPos = j;
break;
}
}
//do we need to shift elements after this one?
if(iPos < iNNToFind - 1)
{
for(j=vFound[iN1]-1;j>iPos;j--)
pnn[iN1][j]=pnn[iN1][j-1];
pnn[iN1][iPos]=Neighbor(iN2,distSQ);
if(vFound[iN1]+1<iNNToFind)vFound[iN1]++;
}
else if(iPos<iNNToFind)//no shift needed, just store
{
pnn[iN1][iPos]=Neighbor(iN2,distSQ);
if(vFound[iN1]+1<iNNToFind)vFound[iN1]++;
}
iPos = 0;
for(j=0;j<vFound[iN2];j++)
{
if(distSQ < pnn[iN2][j].m_dist)
{
iPos = j;
break;
}
}
if(iPos < iNNToFind - 1)
{
for(j=vFound[iN2]-1;j>iPos;j--)
pnn[iN2][j]=pnn[iN2][j-1];
pnn[iN2][iPos]=Neighbor(iN1,distSQ);
if(vFound[iN2]+1<iNNToFind)vFound[iN2]++;
}
else if(iPos<iNNToFind)
{
pnn[iN2][iPos]=Neighbor(iN1,distSQ);
if(vFound[iN2]+1<iNNToFind)vFound[iN2]++;
}
}
return fRad;
}
//increase search radius
fRad *= fRadFctr;
}
}
//get nearest neighbor as float*
float* GetNearestNeighbor(float* p,bool bAllowZeroDist)
{
if(m_iNumElems == 1) return 0;
const int iNumRads = 7;
float pRads[7] = {3.0f,30.0f,150.0f,300.0f,600.0f,900.0f,1000.0f};
int iIter = 0;
float fRad = pRads[0];
while(true)
{
m_pTree->search_center_radius_sq(p,fRad,1);
int iCount = m_pTree->get_count();
if(iCount)
{
vector<float> vRadii(iCount);
m_pTree->copy_radii_sq(&vRadii[0]);
int id = MinIdx(vRadii,bAllowZeroDist);
if(id != -1)
{
vector<long> vID(iCount);
m_pTree->copy_indices(&vID[0]);
return &m_vData[vID[id]*m_iDims];
}
}
//increase search radius
if(iIter+1 >= iNumRads)
fRad *= two;
else
fRad = pRads[++iIter];
}
}
float GetNearestRadiusSQ(float* p,vector<int>& vMap,int iID)
{
if(m_iNumElems == 1) return 0.0;
int iIter = 0;
extern prob_t gstartrad;
float fRad = gstartrad;
while(true)
{
m_pTree->search_center_radius_sq(p,fRad,1);
int iCount = m_pTree->get_count();
if(iCount)
{
vector<float> vRadii(iCount);
m_pTree->copy_radii_sq(&vRadii[0]);
float fm = FLT_MAX;
vector<long> vIndices(iCount);
m_pTree->copy_indices(&vIndices[0]);
bool bFound = false;
int i = 0;
for(i=0;i<iCount;i++)
{
if(vMap[vIndices[i]]==iID && vRadii[i]<= fm && vRadii[i]>0.0)
{ bFound = true;
fm = vRadii[i];
}
}
if(bFound)
return fm;
}
//increase search radius
fRad *= two;
}
}
bool GetNearestNeighbor(float* p,bool bAllowZeroDist,Neighbor& n)
{
if(m_iNumElems == 1) return false;
m_pTree->search_nn(p,bAllowZeroDist);
float fm = 0.0;
m_pTree->copy_radii_sq(&fm);
long id;
m_pTree->copy_indices(&id);
n.m_dist = fm;
n.m_id = id;
return true;
}
float GetNearestRadiusSQ(float* p,bool bAllowZeroDist,bool bTest)
{
if(m_iNumElems == 1) return 0.0;
int iIter = 0;
extern prob_t gstartrad;
float fRad = gstartrad;
if(bTest)
{
#ifdef DO_TIMING
extern MTimer oMT; TimerInc oT(oMT);
#endif
m_pTree->search_nn(p,bAllowZeroDist);
float fm = 0.0;
m_pTree->copy_radii_sq(&fm);
return fm;
}
else
{
#ifdef DO_TIMING
extern MTimer oMF; TimerInc oT(oMF);
#endif
while(true)
{
m_pTree->search_center_radius_sq(p,fRad,1);
int iCount = m_pTree->get_count();
if(iCount)
{
vector<float> vRadii(iCount);
m_pTree->copy_radii_sq(&vRadii[0]);
float fm = 0.0;
if(bAllowZeroDist)
return *std::min_element(vRadii.begin(),vRadii.end());
else
fm=GZeroMinElem(vRadii);
if(fm>0.0)
return fm;
}
//increase search radius
fRad *= two;
}
}
}
float GetNearestRadiusSQ(int i,bool bTest)
{
return GetNearestRadiusSQ(&m_vData[i*m_iDims],false,bTest);
}
//returns probability based on distance
//of an arbitrary element in THIS distribution
//to it's nearest neighbor in THIS distribution
prob_t RProb(prob_t dRad)
{
return m_dTop / (m_dPiPow*dRad*m_dGamma);
}
//returns probability based on distance
//of an arbitrary element in a DIFFERENT distribution
//to it's nearest neighbor in THIS distribution
prob_t RProbOther(prob_t dRad)
{
return (one/m_iNumElems) / (m_dPiPow*dRad*m_dGamma);
}
//returns probability of element i
prob_t IProb(int i)
{
if(!m_pTree || i<0 || i>=m_iNumElems) return 0.0;
if(1==m_iNumElems)return 1.0;
return VProb(&m_vData[i*m_iDims]);
}
//returns probability of vector p
//vector p must be in THIS distribution
prob_t VProb(float* p)
{
if(!p || !m_pTree) return 0.0;
if(1==m_iNumElems)return 1.0;
prob_t dRad = sqrt(GetNearestRadiusSQ(p,false,false));
return RProb(dRad);
}
//returns probability of vector p
//vector p must be in DIFFERENT distribution
prob_t VProbOther(float* p)
{
if(!p || !m_pTree) return 0.0;
if(1==m_iNumElems)return 1.0;
prob_t dRad = sqrt(GetNearestRadiusSQ(p,true,false));
if(dRad == 0.0) return 0.0;
return RProbOther(dRad);
}
char m_strMsg[1024];
//entropy of distribution
prob_t Entropy()
{ //sprintf(m_strMsg,"Entropy sz=%d",m_iNumElems);
//ScopedTimer S(m_strMsg);
if(m_iNumElems<2) return 0.0;
prob_t dEntrop = 0.0;
prob_t dPiPowGamma = m_dPiPow*m_dGamma;
int isz = m_iNumElems , i=0, iOffset = 0;
for(i=0;i<isz;i++)
{
prob_t dDist = GetNearestRadiusSQ(&m_vData[iOffset],false,false);
if(dDist<=0.0)continue;
dDist = sqrt(dDist);
prob_t dProb = m_dTop / (dDist*dPiPowGamma);
if(dProb<=0.0)continue;
dEntrop += dProb * log2(dProb);
iOffset += m_iDims;
}
return -dEntrop;
}
//vIDs specifies which cluster each element
//belongs to. iClust specifies which cluster
//to get entropy for
prob_t Entropy(vector<int>& vIDs,int iClust)
{
prob_t dEntrop = 0.0;
int isz = m_iNumElems , i=0;
for(i=0;i<isz;i++)
{
if(vIDs[i]==iClust)
{
prob_t dProb = IProb(i);
if(dProb==0.0) continue;
dEntrop += dProb * log2(dProb);
}
}
return -dEntrop;
}
//vIDs specifies which cluster each element
//belongs to. iClust specifies which cluster
//to get entropy for
prob_t Entropy(vector<int>& vIDs,int iClust,int iNumElems)
{ sprintf(m_strMsg,"Entropy c%d sz=%d totsz=%d",iClust,iNumElems,m_iNumElems);
ScopedTimer S(m_strMsg);
if(iNumElems<2) return 0.0;
const prob_t PI=3.14159265358979323846;
prob_t dPiPow = pow((prob_t)PI,(prob_t)m_iDims/two);
prob_t dTop = (one/(iNumElems-one));
prob_t dGamma = Gamma(m_iDims/two+one);
prob_t dPiPowGamma = dPiPow*dGamma;
prob_t dEntrop = 0.0;
int isz = m_iNumElems , i=0;
for(i=0;i<isz;i++)
{
if(vIDs[i]==iClust)
{
prob_t dDist = GetNearestRadiusSQ(&m_vData[i*m_iDims],vIDs,iClust);
if(dDist<=0.0)continue;
dDist=sqrt(dDist);
prob_t dProb = dTop / (dDist*dPiPowGamma);
if(dProb<=0.0)continue;
dEntrop += dProb * log2(dProb);
}
}
return -dEntrop;
}
};
template< class T >
struct vpwrap
{
vector<T>* p_;
int iMax_, iMin_;
vpwrap(vector<T>* p,int iMin=-1,int iMax=-1)
:p_(p),
iMax_(iMax),
iMin_(iMin){}
//global operator== is used, not this one
//this one doesn't even get compiled
bool operator==(const vpwrap<T>& vr) const
{
if(iMax_ == -1 || iMin_ == -1)
return *p_ == *vr.p_;
dont
vector<T>& v1 = *p_;
vector<T>& v2 = *vr.p_;
int i = iMin_;
for(;i<iMax_;i++)
if(v1[i] != v2[i])
return false;
return true;
}
bool operator<(const vpwrap<T>& vr) const
{
if(iMax_ == -1 || iMin_ == -1)
return *p_ < *vr.p_;
vector<T>& v1 = *p_;
vector<T>& v2 = *vr.p_;
return lexicographical_compare(&v1[iMin_],&v1[iMax_],&v2[iMin_],&v2[iMax_]);
}
T Dist(const vpwrap<T>& vr, T tMinSoFar) const
{
vector<T>& v1 = *p_;
vector<T>& v2 = *vr.p_;
int iMinT = iMin_ == -1 ? 0 : iMin_,
iMaxT = iMax_ == -1 ? v1.size() : iMax_;
T tDist(0);
int i = iMinT;
for(;i<iMaxT;i++)
{
T val = v1[i] - v2[i];
val *= val;
tDist += val;
if(tDist > tMinSoFar) return tDist;
}
return tDist;
}
void Print(FILE* fp=stdout,bool bNewLine=false) const
{
vector<T>& v = *p_;
int iMinT = iMin_ == -1 ? 0 : iMin_;
int iMaxT = iMax_ == -1 ? v.size() : iMax_;
int i = iMinT;
if(sizeof(T)==sizeof(int))
{
for(;i<iMaxT;i++)
{
fprintf(fp," %d ",v[i]);
}
}
else// if(sizeof(T)==sizeof(float))
{
for(;i<iMaxT;i++)
{
fprintf(fp," %f ",v[i]);
}
}
if(bNewLine) fprintf(fp,"\n");
}
};
inline bool operator==(const vpwrap<float>& vl,const vpwrap<float>& vr)
{
if(vl.iMax_ == -1 || vl.iMin_ == -1)
return *vl.p_ == *vr.p_;
vector<float>& v1 = *vl.p_;
vector<float>& v2 = *vr.p_;
int i = vl.iMin_;
for(;i<vl.iMax_;i++)
if(v1[i] != v2[i])
return false;
return true;
}
inline bool operator==(const vpwrap<int>& vl,const vpwrap<int>& vr)
{
if(vl.iMax_ == -1 || vl.iMin_ == -1)
return *vl.p_ == *vr.p_;
vector<int>& v1 = *vl.p_;
vector<int>& v2 = *vr.p_;
int i = vl.iMin_;
for(;i<vl.iMax_;i++)
if(v1[i] != v2[i])
return false;
return true;
}
typedef std::map< vpwrap<int> , int> THMapI;
typedef THMapI::iterator THMapITI;
typedef std::map< vpwrap<float> , int> THMapF;
typedef THMapF::iterator THMapITF;
template< class T >
class TreeHist
{
typename std::map< vpwrap<T> , int> m_hist;
int m_iNumDims;
int m_iNumElems;
bool m_bBinless;
int m_iMinD;
int m_iMaxD;
public:
void SetDRange(int iMinD,int iMaxD){ m_iMinD=iMinD; m_iMaxD=iMaxD; }
bool GetBinless(){ return m_bBinless; }
void SetBinless(bool b){ m_bBinless=b; }
typename std::map< vpwrap<T> , int>::iterator Begin(){ return m_hist.begin(); }
typename std::map< vpwrap<T> , int>::iterator End(){ return m_hist.end(); }
int Size(){ return m_hist.size(); }
void Print(FILE* fp=stdout)
{
int i = 0;
std::map< vpwrap<T> , int>::iterator IT = m_hist.begin();
for(;IT!=m_hist.end();IT++)
{
fprintf(fp,"vec%d ",i++);
IT->first.Print(fp,true);
fprintf(fp,"count = %d, probability = %.4f\n\n",IT->second,GetITProb(IT));
}
}
prob_t SumProb()
{
if(m_iNumElems == 0) return 0.0;
prob_t dSum = 0.0;
std::map< vpwrap<T> , int>::iterator IT = Begin();
for(;IT!=End();IT++)
{
dSum += IT->second;
}
dSum /= (prob_t) m_iNumElems;
return dSum;
}
bool UpdateProb( vector<T>* v,int iCount)
{
vpwrap<T> vr(v,m_iMinD,m_iMaxD);
std::map< vpwrap<T> , int>::iterator IT = m_hist.find(vr);
if(IT==m_hist.end())
{
m_hist.insert( make_pair(vr,iCount) );
m_iNumElems += iCount;
}
else
{
#ifdef _DEBUG
vector<T>& v1 = *v;
vector<T>& v2 = *IT->first.p_;
int moo=0;
#endif
IT->second+=iCount;
m_iNumElems += iCount;
if(IT->second == 0)
m_hist.erase(IT);
}
return true;
}
//get count of bin that dVal is in
inline int GetVCount( vector<int>* v)
{
typename std::map< vpwrap<T> , int>::iterator IT = m_hist.find(vpwrap<T>(v,m_iMinD,m_iMaxD));
if(IT==m_hist.end())
return 0;
return IT->second;
}
//get probability of variable being bin i
inline prob_t GetVProb(vector<int>* v)
{
if(m_iNumElems == 0) return 0.0;
return GetVCount(v) / (float) m_iNumElems; // Prob(m_iNumElems,GetVCount(v));
}
inline prob_t GetITProb(typename std::map< vpwrap<T> , int>::iterator& IT)
{
return IT->second / (float) m_iNumElems; // Prob(m_iNumElems,IT->second);
}
inline prob_t Entropy()
{
int i = 0;
typename std::map< vpwrap<T> , int>::iterator IT = m_hist.begin();
prob_t dEntropy = 0.0;
if(m_bBinless)
{
if(1==m_hist.size())return 0.0;
prob_t dSz = m_hist.size();
prob_t dDim = Begin()->first.iMax_ - Begin()->first.iMin_ + 1.0;
prob_t dPiPow = pow(PI,(prob_t)dDim/2.0);
prob_t dTop = (1.0/(dSz-1.0));
prob_t dGamma = Gamma(dDim/2.0+1.0);
for(;IT!=m_hist.end();IT++)
{
T tDistC = sqrt( (prob_t) ClosestDist(IT));
if(tDistC == 0.0) continue;
prob_t dProb = (IT->second*dTop) / (dPiPow*tDistC*dGamma);
dEntropy += dProb * log2(dProb);
}
}
else
{
for(;IT!=m_hist.end();IT++)
{
prob_t dProb = IT->second / (prob_t) m_iNumElems; // Prob(m_iNumElems,IT->second);
if(dProb == 0.0) continue;
dEntropy += dProb * log2(dProb);
}
}
return -dEntropy;
}
TreeHist(){ m_iNumElems = 0; m_bBinless = false; m_iMinD=-1;m_iMaxD=-1;};
~TreeHist(){};
//add h to this
void Add(TreeHist& h)
{
typename std::map< vpwrap<T> , int>::iterator IT = h.m_hist.begin();
for(;IT!=h.m_hist.end();IT++)
{
UpdateProb(IT->first.p_,IT->second);
}
}
T ClosestDist(typename const std::map< vpwrap<T>, int>::iterator ITOther)
{
T tMinSoFar = 9e10;
typename std::map< vpwrap<T>, int>::iterator IT = Begin();
for(;IT!=End();IT++)
{
if(IT == ITOther) continue;
T tDistTmp = IT->first.Dist(ITOther->first,tMinSoFar);
if(tDistTmp < tMinSoFar)
tMinSoFar = tDistTmp;
}
return tMinSoFar;
}
};
//histogram with variable bin width
class VHist
{
float m_fMin;
float m_fMax;
float m_fBinWidth;
int m_iBins;
float m_fNumElems;
vector<int> m_counts;
bool ChangeBin(float val,int iInc)
{
int ibin = Bin(val);
if(ibin<0 || ibin>=m_iBins)
return false;
m_counts[ibin]+=iInc;
m_fNumElems+=iInc;
return true;
}
public:
float BinWidth()
{
return m_fBinWidth;
}
float Elems()
{
return m_fNumElems;
}
int Bins()
{
return m_iBins;
}
int operator[](int i)