10 #include "VectorMatrix.h" 14 #define DOXYGEN_SHOULD_SKIP_THIS 26 TVector<TVector<TVector<double> > > bins;
29 TVector<TMatrix<double> > binnedData;
32 TMatrix<double> avgBinnedData;
39 double totalPoints,totalAvgPoints;
40 int nDims, nReps, dataLen;
41 int dataInitedFlag, dataReadyFlag;
42 TVector<int> binningInitedFlag;
62 totalPoints = totalAvgPoints = 0;
64 bins.SetBounds(1,nReps);
65 for(
int r=1; r<=nReps; r++){
67 bins[r].SetBounds(1,nDims);
69 binnedData.SetBounds(1,nReps);
71 nBins.SetBounds(1,dims);
72 nBins.FillContents(0.);
79 binningInitedFlag.SetBounds(1,nDims);
80 binningInitedFlag.FillContents(0.);
93 cout <<
"************************ CONFIG ************************" << endl;
94 cout <<
"Total dimensionality = " << nDims << endl;
95 cout <<
"Number of shifted bins = " << nReps << endl;
96 cout <<
"Number of bins in each dimension = " << nBins << endl;
97 cout <<
"Bin boundaries in each dimension:" << endl;
98 for(
int d=1; d<=nDims;d++){
99 cout <<
"\tFor dimension #" << d << endl;
100 for(
int r=1; r<=nReps; r++){
101 cout <<
"\t\tFor rep #" << r <<
":" << bins[r][d] <<endl;
104 cout <<
"********************************************************" << endl;
109 cout <<
"************************ SNAPSHOT ************************" << endl;
110 cout <<
"Number of total datapoints added = " << totalPoints << endl;
111 cout <<
"Number of populated bins = " << dataLen << endl;
113 cout <<
"Size of populated bin list = " << binnedData[1].ColumnSize() << endl;
114 cout <<
"Has data been collapsed across multiple shifted binnings? " << (dataReadyFlag==0 ?
"No":
"Yes") << endl;
116 cout <<
"Size of average binned data = " << avgBinnedData.ColumnSize() << endl;
117 cout <<
"**********************************************************" << endl;
131 cerr <<
"ERROR: Cannot set bins after if at least one bin has been populated" << endl;
135 if(mins.Size() != nDims){
136 cerr <<
"ERROR: 'mins' should be a list of length = total dimensionality = " << nDims << endl;
139 if(maxs.Size() != nDims){
140 cerr <<
"ERROR: 'maxs' should be a list of length = total dimensionality = " << nDims << endl;
143 if(nbs.Size() != nDims){
144 cerr <<
"ERROR: Bin counts should be a list of length = total dimensionality = " << nDims << endl;
148 int b=nbs.LowerBound(), mi=mins.LowerBound(), ma=maxs.LowerBound();
150 for(
int d=1; d<=nDims; d++){
151 TVector<double> boundaries;
152 boundaries.SetBounds(1,nbs[b]-1);
153 for(
int bo=1; bo<nbs[b]; bo++){
154 double boundary = mins[mi] + bo*(maxs[ma]-mins[mi])/nbs[b];
155 boundaries[bo] = boundary;
168 cerr <<
"ERROR: Cannot set bins after if at least one bin has been populated" << endl;
171 int bLb = boundaries.LowerBound();
172 int bUb = boundaries.UpperBound();
174 nBins[dimIndex] = boundaries.Size()+1;
175 int bi = nBins[dimIndex]-1;
177 for(
int r=1; r<=nReps; r++){
179 bins[r][dimIndex].SetBounds(1,bi);
183 for(
int b=1,b1=bLb; b<=bi; b++,b1++){
184 bins[1][dimIndex][b] = boundaries[b1];
188 double offsetWidth, maxLimit;
189 TVector<double> unitOffset, minLimit;
190 unitOffset.SetBounds(1,bi);
191 minLimit.SetBounds(1,bi);
192 for(
int b=1,b1=bLb; b<bi; b++,b1++){
193 offsetWidth = (boundaries[b1+1]-boundaries[b1])/2;
194 minLimit[b] = boundaries[b1]-offsetWidth;
196 unitOffset[b] = (boundaries[b1] - minLimit[b])/(((nReps-1)/2)+1);
199 minLimit[bi] = boundaries[bUb] - offsetWidth;
200 unitOffset[bi] = (boundaries[bUb] - minLimit[bi])/(((nReps-1)/2)+1);
204 for(r=2; r<=((nReps-1)/2)+1; r++){
206 for(
int b=1,b1=bLb; b<=bi; b++,b1++){
207 bins[r][dimIndex][b] = boundaries[b1] - unitOffset[b]*(r-1);
212 for(r=((nReps-1)/2)+2; r<=nReps; r++){
214 for(
int b=1,b1=bLb; b<=bi; b++,b1++){
215 bins[r][dimIndex][b] = boundaries[b1] + unitOffset[b]*(r-1);
219 binningInitedFlag[dimIndex] = 1;
226 if(boundaries.Size() != nDims){
227 cerr <<
"ERROR: Boundaries should be a list of length = total dimensionality = " << nDims << endl;
231 for(
int d=boundaries.LowerBound(); d<=boundaries.UpperBound(); d++){
244 for(
int d=1; d<=nDims; d++){
245 if(binningInitedFlag[d] == 0){
246 cerr <<
"ERROR: binning has not been specified for dimension " << d-1 <<
" (0-indexing)" << endl;
251 cout <<
"WARNING: Datapoint is being added after existing collapsing binned data. Previous datapoints will be lost." << endl;
252 cout <<
"All datapoints need to be added first before using any information theoretic tools" << endl;
254 if(dp.Size() != nDims){
255 cerr <<
"ERROR: Each datapoint must be of size = total dimensionality = " << nDims <<
" *** ";
256 cerr <<
"Skipping this datapoint" << endl;
261 TVector<double> dataPoint;
262 normalizeBounds(dataPoint, dp);
272 for(
int r=1; r<=nReps; r++){
273 binnedData[r].SetBounds(1,BIN_LIMIT,1,nDims+1);
274 binnedData[r].FillContents(0);
280 TVector<double> this_bin;
281 this_bin.SetBounds(1,nDims);
282 this_bin.FillContents(0);
286 for(
int r=1; r<=nReps; r++){
288 this_bin.FillContents(0);
290 for(
int d=1; d<=nDims; d++){
292 for(
int b=1; b<=nBins[d]; b++){
297 if(dataPoint[d] < bins[r][d][1]){
305 else if(b == nBins[d]){
307 if(dataPoint[d] >= bins[r][d][b-1]){
317 if(bins[r][d][b-1] <= dataPoint[d] && dataPoint[d] < bins[r][d][b]){
328 if(valid_dBins == nDims){
332 added = addOrInsertCoords(this_bin,binnedData[r]);
348 void addData(TVector<TVector<double> >& data){
349 for(
int d=1; d<=data.Size(); d++)
363 if(vIDs.Size() != nDims){
364 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
365 cerr <<
"Skipping this call" << endl;
370 normalizeBounds(varIDs, vIDs);
374 computeIndProbs(p_x,varIDs);
377 for(
int xi=1; xi<=p_x.Size(); xi++){
379 entropy -= p_x[xi]*log2(p_x[xi]);
388 if(vIDs.Size() != nDims){
389 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
390 cerr <<
"Skipping this call" << endl;
395 normalizeBounds(varIDs, vIDs);
398 TMatrix<double> p_xy;
399 computeJointProbs(p_xy,varIDs);
405 for (
int i = 1; i <= p_xy.ColumnSize(); i++)
407 mi += p_xy[i][3]*log2(p_xy[i][3]/(p_xy[i][1]*p_xy[i][2]));
413 #ifndef DOXYGEN_SHOULD_SKIP_THIS 414 void specificInfo(TVector<double>& si, TVector<int>& varIDs){
418 TVector<TVector<TVector<double> > > px;
420 computeSpecProbs(px, varIDs);
423 si.SetBounds(1,px.Size());
425 for (
int x = 1; x <= px.Size(); x++)
428 for (
int y = 1; y <= px[x][2].Size(); y++)
430 si[x] += px[x][3][y] * log2(px[x][3][y] / (px[x][1][1] * px[x][2][y]));
442 if(vIDs.Size() != nDims){
443 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
444 cerr <<
"Skipping this call" << endl;
449 normalizeBounds(varIDs, vIDs);
451 TVector<double> siX1;
452 TVector<double> siX2;
455 TVector<int> varIDsX1, varIDsX2, varIDsY;
456 varIDsX1.SetBounds(1,varIDs.Size());
457 varIDsX2.SetBounds(1,varIDs.Size());
458 varIDsY.SetBounds(1,varIDs.Size());
460 for(
int d=1; d<=varIDs.Size(); d++){
482 computeIndProbs(p_y, varIDsY);
483 specificInfo(siX1,varIDsX1);
484 specificInfo(siX2,varIDsX2);
489 for (
int l = 1; l <= siX1.Size(); l++)
491 ri += (siX1[l]<siX2[l]?siX1[l]:siX2[l]);
500 if(vIDs.Size() != nDims){
501 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
502 cerr <<
"Skipping this call" << endl;
507 normalizeBounds(varIDs, vIDs);
509 TVector<int> varIDsX1;
510 varIDsX1.SetBounds(1,varIDs.Size());
512 for(
int d=1; d<=varIDs.Size(); d++){
533 return infoX1 - redun;
541 if(vIDs.Size() != nDims){
542 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
543 cerr <<
"Skipping this call" << endl;
548 normalizeBounds(varIDs, vIDs);
550 TVector<int> varIDsX1, varIDsX2, varCommon;
551 varIDsX1.SetBounds(1,varIDs.Size());
552 varIDsX2.SetBounds(1,varIDs.Size());
553 varCommon.SetBounds(1,varIDs.Size());
555 for(
int d=1; d<=varIDs.Size(); d++){
581 double synergy = mi - infoX1 - infoX2 + redun;
600 void pid(TVector<int>& vIDs, TVector<double>& infos){
601 if(vIDs.Size() != nDims){
602 cerr <<
"varIDs argument must be of size = total dimensionality = " << nDims << endl;
603 cerr <<
"Skipping this call" << endl;
608 normalizeBounds(varIDs, vIDs);
610 TVector<int> varIDsX1, varIDsX2, varCommon;
611 varIDsX1.SetBounds(1,varIDs.Size());
612 varIDsX2.SetBounds(1,varIDs.Size());
613 varCommon.SetBounds(1,varIDs.Size());
615 for(
int d=1; d<=varIDs.Size(); d++){
641 double synergy = mi - infoX1 - infoX2 + redun;
643 infos.SetBounds(1,5);
645 infos[2] = infoX1 - redun;
646 infos[3] = infoX2 - redun;
651 #ifndef DOXYGEN_SHOULD_SKIP_THIS 652 double transfer_entropy_1_delay(TVector<int> varIDs){
666 binnedData.SetSize(0);
667 avgBinnedData.SetSize(0,0);
670 #ifndef DOXYGEN_SHOULD_SKIP_THIS 671 void normalizeBounds(TVector<double>& normVec, TVector<double>& vec){
672 normVec.SetBounds(1,vec.Size());
674 for(
int d=vec.LowerBound();d<=vec.UpperBound();d++){
675 normVec[d1] = vec[d];
679 void normalizeBounds(TVector<int>& normVec, TVector<int>& vec){
680 normVec.SetBounds(1,vec.Size());
682 for(
int d=vec.LowerBound();d<=vec.UpperBound();d++){
683 normVec[d1] = vec[d];
688 int addOrInsertCoords(TVector<double>& coordinates, TMatrix<double>& intoMatrix){
695 int breakFlag, foundFlag, added=0;
698 int dims = intoMatrix.RowSize()-1;
699 int rowLim = intoMatrix.ColumnSize();
702 for(
int l=1; l<=intoMatrix.ColumnSize(); l++){
706 if(intoMatrix[l][1] == 0){
708 for(
int d=1; d<=dims; d++){
709 intoMatrix[l][d] = coordinates[d];
711 intoMatrix[l][dims+1] = 1;
721 for(
int d=1; d<=dims; d++){
723 if(intoMatrix[l][d] == coordinates[d]){
732 intoMatrix[l][dims+1] += 1;
745 TMatrix<double> _intoMatrix;
746 _intoMatrix.SetBounds(1,rowLim,1,dims+1);
747 for(
int i=1;i<=rowLim;i++){
748 for(
int d=1; d<=dims+1; d++){
749 _intoMatrix[i][d] = intoMatrix[i][d];
753 intoMatrix.SetBounds(1,
int(rowLim*1.5),1,dims+1);
754 intoMatrix.FillContents(0.);
755 for(
int i=1;i<=rowLim;i++){
756 for(
int d=1; d<=dims+1; d++){
757 intoMatrix[i][d] = _intoMatrix[i][d];
761 for(
int d=1; d<=dims; d++){
762 intoMatrix[l][d] = coordinates[d];
764 intoMatrix[l][dims+1] = 1;
778 int indexOf(TMatrix<double>& inWhat, TVector<double>& pattern, TVector<int>& atInds){
784 for(
int l=1; l<=inWhat.ColumnSize(); l++){
787 for(
int p=1; p<=pattern.Size(); p++){
788 if(inWhat[l][atInds[p]]!=pattern[p]){
793 if(matchCount == pattern.Size()){
801 double fetchTotalValue(TMatrix<double>& from, TVector<double>& pattern, TVector<int>& atInds,
int index){
804 double totalValue = 0.;
807 for(
int l=1; l<=from.ColumnSize(); l++){
810 for(
int p=1; p<=pattern.Size(); p++){
811 if(from[l][atInds[p]]!=pattern[p]){
816 if(matchCount == pattern.Size()){
818 totalValue += from[l][index];
825 void getXYInds(TVector<int>& xInds, TVector<int>& yInds, TVector<int>& xyInds, TVector<int>& xyDims, TVector<int>& varIDs){
833 xyDims.SetBounds(1,2);
836 for(
int d=1; d<=nDims; d++){
840 else if(varIDs[d] == 1){
846 xInds.SetBounds(1,xDim);
848 yInds.SetBounds(1,yDim);
850 xyInds.SetBounds(1,xDim+yDim);
853 for(
int d=1; d<=nDims; d++){
856 xyInds[xyi] = d; xyi++;
858 if(varIDs[d] == 1 && yDim>0){
860 xyInds[xyi] = d; xyi++;
865 void collapseBinnedData(){
871 TMatrix<double> _avgBinnedData;
872 _avgBinnedData.SetBounds(1,dataLen,1,nDims+1);
874 TVector<double> this_bin;
875 this_bin.SetBounds(1,nDims);
878 atInds.SetBounds(1,nDims);
882 for(
int r=1; r<=nReps; r++){
883 for(
int l=1; l<=binnedData[r].ColumnSize(); l++){
884 if(binnedData[r][l][1] > 0){
886 for(
int d=1; d<=nDims; d++){
887 this_bin[d] = binnedData[r][l][d];
890 int binExist = indexOf(_avgBinnedData,this_bin,atInds);
894 for(
int d=1; d<=nDims; d++){
896 _avgBinnedData[avgLen][d] = binnedData[r][l][d];
898 _avgBinnedData[avgLen][nDims+1] = 0;
899 for(
int r=1; r<=nReps; r++){
900 double counts = fetchTotalValue(binnedData[r],this_bin,atInds,nDims+1);
901 _avgBinnedData[avgLen][nDims+1] += counts;
904 _avgBinnedData[avgLen][nDims+1] /= nReps;
906 totalAvgPoints += _avgBinnedData[avgLen][nDims+1];
921 for(
int l=1;l<=dataLen;l++){
922 for(
int d=1; d<=nDims+1; d++){
923 _avgBinnedData[l][d] = binnedData[1][l][d];
925 totalAvgPoints += binnedData[1][l][nDims+1];
930 avgBinnedData.SetBounds(1,dataLen,1,nDims+1);
932 for(
int l=1;l<=dataLen;l++){
933 for(
int d=1; d<=nDims+1; d++){
936 avgBinnedData[l][d] = _avgBinnedData[l][d];
945 binnedData.SetSize(0);
954 #ifndef DOXYGEN_SHOULD_SKIP_THIS 955 void computeIndProbs(TVector<double>& p_x, TVector<int>& varIDs){
958 if(dataReadyFlag==0){
959 collapseBinnedData();
962 TVector<int> xInds, yInds, xyDims, xyInds;
963 getXYInds(xInds,yInds,xyInds,xyDims,varIDs);
964 int xDim = xyDims[1];
966 TVector<double> xpattern;
967 xpattern.SetBounds(1,xDim);
969 int uniqueXCounts=1,added;
970 TVector<double> _p_x;
971 _p_x.SetBounds(1,dataLen);
972 TMatrix<double> trackerVar;
973 trackerVar.SetBounds(1,dataLen,1,xDim+1);
974 trackerVar.FillContents(0.);
976 for(
int l=1; l<=dataLen; l++){
978 for(
int xi=1; xi<=xDim; xi++){
979 xpattern[xi] = avgBinnedData[l][xInds[xi]];
983 added = addOrInsertCoords(xpattern,trackerVar);
987 _p_x[uniqueXCounts] = fetchTotalValue(avgBinnedData,xpattern,xInds,nDims+1)/totalAvgPoints;
997 p_x.SetBounds(1,uniqueXCounts);
998 for(
int xi=1; xi<=uniqueXCounts; xi++){
1004 void computeJointProbs(TMatrix<double>& p_xy, TVector<int>& varIDs){
1007 if(dataReadyFlag==0){
1008 collapseBinnedData();
1013 TVector<int> xInds, yInds, xyDims, xyInds;
1014 getXYInds(xInds,yInds,xyInds,xyDims,varIDs);
1015 int xDim = xyDims[1];
1016 int yDim = xyDims[2];
1019 TVector<double> xypattern;
1020 xypattern.SetBounds(1,xDim+yDim);
1021 TVector<double> xpattern;
1022 xpattern.SetBounds(1,xDim);
1023 TVector<double> ypattern;
1024 ypattern.SetBounds(1,yDim);
1026 int uniqueXYcounts=1,added;
1027 TMatrix<double> _p_xy;
1028 _p_xy.SetBounds(1,dataLen,1,3);
1029 TMatrix<double> trackerVar;
1030 trackerVar.SetBounds(1,dataLen,1,xDim+yDim+1);
1031 trackerVar.FillContents(0.);
1035 for(
int l=1; l<=avgBinnedData.ColumnSize(); l++){
1040 for(
int i=1; i<=varIDs.Size(); i++){
1041 if(varIDs[i]==0 || varIDs[i]==1){
1042 xypattern[xyi] = avgBinnedData[l][i];
1048 added = addOrInsertCoords(xypattern,trackerVar);
1052 _p_xy[uniqueXYcounts][3] = fetchTotalValue(avgBinnedData,xypattern,xyInds,nDims+1)/totalAvgPoints;
1055 for(
int xi=1; xi<=xDim; xi++){
1056 xpattern[xi] = avgBinnedData[l][xInds[xi]];
1058 _p_xy[uniqueXYcounts][1] = fetchTotalValue(avgBinnedData,xpattern,xInds,nDims+1)/totalAvgPoints;
1061 for(
int yi=1; yi<=yDim; yi++){
1062 ypattern[yi] = avgBinnedData[l][yInds[yi]];
1064 _p_xy[uniqueXYcounts][2] = fetchTotalValue(avgBinnedData,ypattern,yInds,nDims+1)/totalAvgPoints;
1073 p_xy.SetBounds(1,uniqueXYcounts,1,3);
1074 for(
int l=1; l<=uniqueXYcounts; l++){
1075 for(
int d=1; d<=3; d++)
1076 p_xy[l][d] = _p_xy[l][d];
1082 void computeSpecProbs(TVector<TVector<TVector<double> > >& p_x, TVector<int>& varIDs){
1083 if(dataReadyFlag==0){
1084 collapseBinnedData();
1096 TVector<TVector<TVector<double> > > _p_x;
1097 _p_x.SetBounds(1,dataLen);
1098 for(
int l=1; l<=dataLen; l++){
1099 _p_x[l].SetBounds(1,3);
1100 _p_x[l][1].SetBounds(1,1);
1101 _p_x[l][2].SetBounds(1,dataLen);
1102 _p_x[l][3].SetBounds(1,dataLen);
1103 _p_x[l][1].FillContents(0.);
1104 _p_x[l][2].FillContents(0.);
1105 _p_x[l][3].FillContents(0.);;
1108 TVector<int> xInds, yInds, xyInds, xyDims;
1109 getXYInds(xInds,yInds,xyInds,xyDims,varIDs);
1110 int xDim = xyDims[1];
1111 int yDim = xyDims[2];
1114 int uniqueXcounts = 1;
1117 TMatrix<double> trackerVar;
1118 trackerVar.SetBounds(1,dataLen,1,xDim+1);
1119 trackerVar.FillContents(0.);
1121 TMatrix<double> xytrackerVar;
1122 xytrackerVar.SetBounds(1,dataLen,1,xDim+yDim+1);
1123 xytrackerVar.FillContents(0.);
1125 TVector<int> uniqueXYcounts;
1126 uniqueXYcounts.SetBounds(1,dataLen);
1127 uniqueXYcounts.FillContents(2.);
1129 TVector<double> this_xbin,this_ybin,this_xybin;
1130 TVector<int> trackDim;
1131 this_xbin.SetBounds(1,xDim);
1132 trackDim.SetBounds(1,xDim);
1133 this_ybin.SetBounds(1,yDim);
1134 this_xybin.SetBounds(1,xDim+yDim);
1138 for(
int l=1; l<=dataLen; l++){
1141 for(
int xi=1; xi<=xDim; xi++){
1142 this_xbin[xi] = avgBinnedData[l][xInds[xi]];
1147 for(
int yi=1; yi<=yDim; yi++){
1148 this_ybin[yi] = avgBinnedData[l][yInds[yi]];
1152 for(
int i=1; i<=varIDs.Size(); i++){
1153 if(varIDs[i]==0 || varIDs[i]==1){
1154 this_xybin[index] = avgBinnedData[l][i];
1162 added = addOrInsertCoords(this_xbin,trackerVar);
1171 _p_x[uniqueXcounts][1][1] = fetchTotalValue(avgBinnedData,this_xbin,xInds,nDims+1)/totalAvgPoints;
1175 _p_x[uniqueXcounts][2][1] = fetchTotalValue(avgBinnedData,this_ybin,yInds,nDims+1)/totalAvgPoints;
1179 _p_x[uniqueXcounts][3][1] = fetchTotalValue(avgBinnedData,this_xybin,xyInds,nDims+1)/totalAvgPoints;
1182 xyAdded = addOrInsertCoords(this_xybin,xytrackerVar);
1193 xyAdded = addOrInsertCoords(this_xybin,xytrackerVar);
1195 int ind = indexOf(trackerVar,this_xbin,trackDim);
1200 _p_x[ind][2][uniqueXYcounts[ind]] = fetchTotalValue(avgBinnedData,this_ybin,yInds,nDims+1)/totalAvgPoints;
1201 _p_x[ind][3][uniqueXYcounts[ind]] = fetchTotalValue(avgBinnedData,this_xybin,xyInds,nDims+1)/totalAvgPoints;
1202 uniqueXYcounts[ind]++;
1216 p_x.SetBounds(1,uniqueXcounts);
1218 for(
int l=1; l<=uniqueXcounts; l++){
1219 p_x[l].SetBounds(1,3);
1220 p_x[l][1].SetBounds(1,1);
1221 p_x[l][1][1] = _p_x[l][1][1];
1223 for(
int d=2; d<=3; d++){
1224 p_x[l][d].SetBounds(1,uniqueXYcounts[l]-1);
1225 for(
int i=1; i<=uniqueXYcounts[l]-1; i++){
1226 p_x[l][d][i] =_p_x[l][d][i];