10 MtxDim = (int) sqrt((
double)SeedMatrix.
Len());
15 FILE *F = fopen(OutFNm.
CStr(),
"wt");
16 for (
int i = 0; i <
GetDim(); i++) {
17 for (
int j = 0; j <
GetDim(); j++) {
18 if (j > 0) fprintf(F,
"\t");
19 fprintf(F,
"%f",
At(i,j)); }
26 if (
this != &Kronecker){
34 for (
int i = 0; i <
Len(); i++) {
35 if (
At(i) < 0.0 ||
At(i) > 1.0)
return false;
50 void TKronMtx::SetEpsMtx(
const double& Eps1,
const double& Eps0,
const int& Eps1Val,
const int& Eps0Val) {
51 for (
int i = 0; i <
Len(); i++) {
53 if (Val == Eps1Val) Val = double(Eps1);
54 else if (Val == Eps0Val) Val = double(Eps0);
61 const double EZero = pow((
double) Edges, 1.0/
double(KronIter));
62 const double Factor = EZero /
GetMtxSum();
63 for (
int i = 0; i <
Len(); i++) {
65 if (
At(i) > 1) {
At(i) = 1; }
73 for (
int i = 0; i <
Len(); i++) {
74 for(c = 0; ((NewVal =
At(i)*
Rnd.
GetNrmDev(1, SDev, 0.8, 1.2)) < 0.01 || NewVal>0.99) && c <1000; c++) { }
75 if (c < 999) {
At(i) = NewVal; }
else { printf(
"XXXXX\n"); }
82 for (
int i = 0; i <
Len(); i++) {
84 if ((i+1)%
GetDim()==0 && (i+1<
Len())) { ChA +=
"; "; }
85 else if (i+1<
Len()) { ChA +=
", "; }
92 for (
int i = 0; i <
Len(); i++) {
100 for (
int i = 0; i <
Len(); i++) {
101 if (
At(i) != 0.0) { LLMtx.
At(i) = log(
At(i)); }
102 else { LLMtx.
At(i) =
NInf; }
108 for (
int i = 0; i <
Len(); i++) {
109 if (
At(i) !=
NInf) { ProbMtx.
At(i) = exp(
At(i)); }
110 else { ProbMtx.
At(i) = 0.0; }
120 return (
int) pow(
double(
GetDim()),
double(NIter));
124 return (
int) pow(
double(
GetMtxSum()),
double(NIter));
128 return (
int) ceil(log(
double(Nodes)) / log(
double(
GetDim())));
137 return pow((
double) Edges, 1.0/
double(KronIters));
142 for (
int i = 0; i <
Len(); i++) {
149 for (
int c = 0; c <
GetDim(); c++) {
150 Sum +=
At(RowId, c); }
156 for (
int r = 0; r <
GetDim(); r++) {
157 Sum +=
At(r, ColId); }
163 for (
int level = 0; level < NKronIters; level++) {
164 Prob *=
At(NId1 %
MtxDim, NId2 % MtxDim);
165 if (Prob == 0.0) {
return 0.0; }
177 for (
int level = 0; level < NKronIters; level++) {
178 const double& LLVal =
At(NId1 %
MtxDim, NId2 % MtxDim);
187 return log(1.0 - exp(
GetEdgeLL(NId1, NId2, NKronIters)));
192 const double EdgeLL =
GetEdgeLL(NId1, NId2, NKronIters);
193 return -exp(EdgeLL) - 0.5*exp(2*EdgeLL);
198 for (
int level = 0; level < NKronIters; level++) {
199 Prob *=
At(NId1 %
MtxDim, NId2 % MtxDim);
200 if (ProbTresh > Prob) {
return false; }
208 const int ThetaX = ParamId %
GetDim();
209 const int ThetaY = ParamId /
GetDim();
211 for (
int level = 0; level < NKronIters; level++) {
212 if ((NId1 %
MtxDim) == ThetaX && (NId2 % MtxDim) == ThetaY) {
216 return double(ThetaCnt) / exp(
At(ParamId));
221 const int& ThetaX = ParamId %
GetDim();
222 const int& ThetaY = ParamId /
GetDim();
224 double DLL = 0, LL = 0;
225 for (
int level = 0; level < NKronIters; level++) {
226 const int X = NId1 %
MtxDim;
227 const int Y = NId2 %
MtxDim;
228 const double LVal =
At(X, Y);
229 if (X == ThetaX && Y == ThetaY) {
230 if (ThetaCnt != 0) { DLL += LVal; }
232 }
else { DLL += LVal; }
236 return -ThetaCnt*exp(DLL) / (1.0 - exp(LL));
241 const int& ThetaX = ParamId %
GetDim();
242 const int& ThetaY = ParamId /
GetDim();
245 for (
int level = 0; level < NKronIters; level++) {
246 const int X = NId1 %
MtxDim;
247 const int Y = NId2 %
MtxDim;
249 if (X == ThetaX && Y == ThetaY) {
250 if (ThetaCnt != 0) { DLL += LVal; }
252 }
else { DLL += LVal; }
259 return -ThetaCnt*exp(DLL) - ThetaCnt*exp(
At(ThetaX, ThetaY)+2*DLL);
264 for (
int i = 0; i < (int)(8*
sizeof(
uint)); i++) {
265 if (TKronMtx::Rnd.GetUniDev() < OneProb) {
274 for (
int i = 0; i < NIter; i++) {
275 const uint Mask = (1u<<i);
276 const uint Bit1 = NId1Sig & Mask;
277 const uint Bit2 = NId2Sig & Mask;
278 Prob *=
At(
int(Bit1!=0),
int(Bit2!=0));
285 for (
int i = 0; i <
GetDim(); i++) {
287 for (
int r = 0; r <
GetDim(); r++) {
288 for (
int c = 0; c <
GetDim(); c++) {
289 if (
At(r, c) >= Thresh) { Graph->
AddEdge(r, c); }
297 for (
int i = 0; i <
GetDim(); i++) {
299 for (
int r = 0; r <
GetDim(); r++) {
300 for (
int c = 0; c <
GetDim(); c++) {
308 return (
int) ceil(log(
double(GNodes)) / log(
double(SeedMtxSz)));
314 const int NNodes = SeedGraph.
GetNodes(NIter);
315 printf(
" Kronecker: %d nodes, %s...\n", NNodes, IsDir ?
"Directed":
"UnDirected");
320 for (
int node1 = 0; node1 < NNodes; node1++) {
323 for (
int node1 = 0; node1 < NNodes; node1++) {
324 for (
int node2 = 0; node2 < NNodes; node2++) {
330 if (node1 % 1000 == 0) printf(
"\r...%dk, %dk", node1/1000, edges/1000);
333 for (
int node1 = 0; node1 < NNodes; node1++) {
334 for (
int node2 = node1; node2 < NNodes; node2++) {
341 if (node1 % 1000 == 0) printf(
"\r...%dk, %dk", node1/1000, edges/1000);
352 const double MtxSum = SeedGraph.
GetMtxSum();
353 const int NNodes = SeedGraph.
GetNodes(NIter);
354 const int NEdges = SeedGraph.
GetEdges(NIter);
357 printf(
" FastKronecker: %d nodes, %d edges, %s...\n", NNodes, NEdges, IsDir ?
"Directed":
"UnDirected");
363 double CumProb = 0.0;
364 for (
int r = 0; r <
MtxDim; r++) {
365 for (
int c = 0; c <
MtxDim; c++) {
366 const double Prob = SeedGraph.
At(r, c);
374 for (
int i = 0; i < NNodes; i++) {
377 int Rng, Row, Col, Collision=0, n = 0;
378 for (
int edges = 0; edges < NEdges; ) {
379 Rng=NNodes; Row=0; Col=0;
380 for (
int iter = 0; iter < NIter; iter++) {
382 n = 0;
while(Prob > ProbToRCPosV[n].Val1) { n++; }
383 const int MtxRow = ProbToRCPosV[n].Val2;
384 const int MtxCol = ProbToRCPosV[n].Val3;
389 if (! Graph->
IsEdge(Row, Col)) {
390 Graph->
AddEdge(Row, Col); edges++;
392 if (Row != Col) Graph->
AddEdge(Col, Row);
395 }
else { Collision++; }
399 printf(
" collisions: %d (%.4f)\n", Collision, Collision/(
double)Graph->
GetEdges());
407 const double MtxSum = SeedGraph.
GetMtxSum();
408 const int NNodes = SeedGraph.
GetNodes(NIter);
409 const int NEdges = Edges;
412 printf(
" RMat Kronecker: %d nodes, %d edges, %s...\n", NNodes, NEdges, IsDir ?
"Directed":
"UnDirected");
418 double CumProb = 0.0;
419 for (
int r = 0; r <
MtxDim; r++) {
420 for (
int c = 0; c <
MtxDim; c++) {
421 const double Prob = SeedGraph.
At(r, c);
429 for (
int i = 0; i < NNodes; i++) {
432 int Rng, Row, Col, Collision=0, n = 0;
433 for (
int edges = 0; edges < NEdges; ) {
434 Rng=NNodes; Row=0; Col=0;
435 for (
int iter = 0; iter < NIter; iter++) {
437 n = 0;
while(Prob > ProbToRCPosV[n].Val1) { n++; }
438 const int MtxRow = ProbToRCPosV[n].Val2;
439 const int MtxCol = ProbToRCPosV[n].Val3;
444 if (! Graph->
IsEdge(Row, Col)) {
445 Graph->
AddEdge(Row, Col); edges++;
447 if (Row != Col) Graph->
AddEdge(Col, Row);
450 }
else { Collision++; }
454 printf(
" collisions: %d (%.4f)\n", Collision, Collision/(
double)Graph->
GetEdges());
460 const int NNodes = SeedGraph.
GetNodes(NIter);
461 printf(
" Deterministic Kronecker: %d nodes, %s...\n", NNodes, IsDir ?
"Directed":
"UnDirected");
465 for (
int node1 = 0; node1 < NNodes; node1++) { Graph->
AddNode(node1); }
467 for (
int node1 = 0; node1 < NNodes; node1++) {
468 for (
int node2 = 0; node2 < NNodes; node2++) {
474 if (node1 % 1000 == 0) printf(
"\r...%dk, %dk", node1/1000, edges/1000);
482 const bool FastGen =
true;
494 const TStr Style =
"linewidth 1 pointtype 6 pointsize 1";
518 const bool FastGen =
true;
534 const TStr Style =
"linewidth 1 pointtype 6 pointsize 1";
562 for (
int m = 0; m < SeedMtxV.
Len(); m++) {
563 const int KronIters = SeedMtxV[m].GetKronIter(Graph->
GetNodes());
565 printf(
"*** K(%d, %d) n0=%d\n", KronG1->
GetNodes(), KronG1->
GetEdges(), SeedMtxV[m].GetDim());
567 printf(
" del zero deg K(%d, %d) n0=%d\n", KronG1->
GetNodes(), KronG1->
GetEdges(), m);
568 GS.Add(KronG1,
TSecTm(m+2),
TStr::Fmt(
"K(%d, %d) n0^k=%d n0=%d", KronG1->
GetNodes(), KronG1->
GetEdges(), SeedMtxV[m].GetNZeroK(Graph), SeedMtxV[m].GetDim()));
570 const TStr Style =
"linewidth 1 pointtype 6 pointsize 1";
572 GS.ImposeDistr(
gsdInDeg, FNmPref+
"-B", Desc1,
true,
false,
gpwLines, Style);
575 GS.ImposeDistr(
gsdHops, FNmPref, Desc1,
false,
false,
gpwLines, Style);
582 GS.ImposeDistr(
gsdWcc, FNmPref, Desc1,
false,
false,
gpwLines, Style);
583 GS.ImposeDistr(
gsdWcc, FNmPref+
"-B", Desc1,
true,
false,
gpwLines, Style);
584 GS.ImposeDistr(
gsdScc, FNmPref, Desc1,
false,
false,
gpwLines, Style);
585 GS.ImposeDistr(
gsdScc, FNmPref+
"-B", Desc1,
true,
false,
gpwLines, Style);
592 const int LDim = Left.
GetDim();
593 const int RDim = Right.
GetDim();
594 Result.
GenMtx(LDim * RDim);
595 for (
int r1 = 0; r1 < LDim; r1++) {
596 for (
int c1 = 0; c1 < LDim; c1++) {
597 const double& Val = Left.
At(r1, c1);
598 for (
int r2 = 0; r2 < RDim; r2++) {
599 for (
int c2 = 0; c2 < RDim; c2++) {
600 Result.
At(r1*RDim+r2, c1*RDim+c2) = Val * Right.
At(r2, c2);
608 const int LDim = Left.
GetDim();
609 const int RDim = Right.
GetDim();
610 Result.
GenMtx(LDim * RDim);
611 for (
int r1 = 0; r1 < LDim; r1++) {
612 for (
int c1 = 0; c1 < LDim; c1++) {
613 const double& Val = Left.
At(r1, c1);
614 for (
int r2 = 0; r2 < RDim; r2++) {
615 for (
int c2 = 0; c2 < RDim; c2++) {
616 if (Val ==
NInf || Right.
At(r2, c2) ==
NInf) {
617 Result.
At(r1*RDim+r2, c1*RDim+c2) =
NInf; }
619 Result.
At(r1*RDim+r2, c1*RDim+c2) = Val + Right.
At(r2, c2); }
629 for (
int iter = 0; iter < NIter; iter++) {
630 KronMul(OutMtx, KronMtx, NewOutMtx);
631 NewOutMtx.
Swap(OutMtx);
642 if (! MtxNm.
Empty()) printf(
"%s\n", MtxNm.
CStr());
645 if (Sort) { ValV.
Sort(
false); }
646 for (
int i = 0; i < ValV.
Len(); i++) {
647 printf(
" %10.4g", ValV[i]());
649 if ((i+1) %
GetDim() == 0) { printf(
"\n"); }
651 printf(
" (sum:%.4f)\n", Sum);
661 for (
int i = 0; i < P1.
Len(); i++) {
662 delta += fabs(P1[i] - P2[i]);
664 return delta/P1.
Len();
674 for (
int i = 0; i < P1.
Len(); i++) {
675 delta += pow(P1[i] - P2[i], 2);
677 return sqrt(delta/P1.
Len());
682 TStrV RowStrV, ColStrV;
685 RowStrV[0].SplitOnWs(ColStrV);
IAssert(! ColStrV.
Empty());
686 const int Rows = RowStrV.
Len();
687 const int Cols = ColStrV.
Len();
690 for (
int r = 0; r < Rows; r++) {
691 RowStrV[r].SplitOnWs(ColStrV);
693 for (
int c = 0; c < Cols; c++) {
694 Mtx.
At(r, c) = (double) ColStrV[c].GetFlt(); }
706 const double MxParam = 0.8+TKronMtx::Rnd.
GetUniDev()/5.0;
707 const double MnParam = 0.2-TKronMtx::Rnd.
GetUniDev()/5.0;
708 const double Step = (MxParam-MnParam) / (Dim*Dim-1);
709 TFltV ParamV(Dim*Dim);
710 if (Dim == 1) { ParamV.
PutAll(0.5); }
712 for (
int p = 0; p < ParamV.
Len(); p++) {
713 ParamV[p] = MxParam - p*Step; }
725 else if (MtxStr[0] ==
'a') {
726 const double Prob = TKronMtx::Rnd.
GetUniDev();
730 const double Max = 0.9+TKronMtx::Rnd.
GetUniDev()/10.0;
731 const double Min = 0.1-TKronMtx::Rnd.
GetUniDev()/10.0;
732 const double Med = (Max-Min)/2.0;
733 Mtx.
At(0,0) = Max; Mtx.
At(0,Dim-1) = Med;
734 Mtx.
At(Dim-1, 0) = Med; Mtx.
At(Dim-1, Dim-1) = Min;
735 for (
int i = 1; i < Dim-1; i++) {
736 Mtx.
At(i,i) = Max - double(i)*(Max-Min)/
double(Dim-1);
737 Mtx.
At(i, 0) = Mtx.
At(0, i) = Max - double(i)*(Max-Med)/
double(Dim-1);
738 Mtx.
At(i, Dim-1) = Mtx.
At(Dim-1, i) = Med - double(i)*(Med-Min)/
double(Dim-1);
740 for (
int i = 1; i < Dim-1; i++) {
741 for (
int j = 1; j < Dim-1; j++) {
742 if (i >= j) {
continue; }
743 Mtx.
At(i,j) = Mtx.
At(j,i) = Mtx.
At(i,i) - (j-i)*(Mtx.
At(i,i)-Mtx.
At(i,Dim-1))/(Dim-i-1);
748 }
else {
FailR(
"Wrong mtx: matlab str, or random (r), or all (a)"); }
755 else if (MtxNm ==
"4star")
return TKronMtx::GetMtx(
"1 1 1 1; 1 1 0 0 ; 1 0 1 0; 1 0 0 1");
756 else if (MtxNm ==
"4chain")
return TKronMtx::GetMtx(
"1 1 0 0; 1 1 1 0 ; 0 1 1 1; 0 0 1 1");
757 else if (MtxNm ==
"4square")
return TKronMtx::GetMtx(
"1 1 0 1; 1 1 1 0 ; 0 1 1 1; 1 0 1 1");
758 else if (MtxNm ==
"5star")
return TKronMtx::GetMtx(
"1 1 1 1 1; 1 1 0 0 0; 1 0 1 0 0; 1 0 0 1 0; 1 0 0 0 1");
759 else if (MtxNm ==
"6star")
return TKronMtx::GetMtx(
"1 1 1 1 1 1; 1 1 0 0 0 0; 1 0 1 0 0 0; 1 0 0 1 0 0; 1 0 0 0 1 0; 1 0 0 0 0 1");
760 else if (MtxNm ==
"7star")
return TKronMtx::GetMtx(
"1 1 1 1 1 1 1; 1 1 0 0 0 0 0; 1 0 1 0 0 0 0; 1 0 0 1 0 0 0; 1 0 0 0 1 0 0; 1 0 0 0 0 1 0; 1 0 0 0 0 0 1");
761 else if (MtxNm ==
"5burst")
return TKronMtx::GetMtx(
"1 1 1 1 0; 1 1 0 0 0; 1 0 1 0 0; 1 0 0 1 1; 0 0 0 1 1");
762 else if (MtxNm ==
"7burst")
return TKronMtx::GetMtx(
"1 0 0 1 0 0 0; 0 1 0 1 0 0 0; 0 0 1 1 0 0 0; 1 1 1 1 1 0 0; 0 0 0 1 1 1 1; 0 0 0 0 1 1 0; 0 0 0 0 1 0 1");
763 else if (MtxNm ==
"7cross")
return TKronMtx::GetMtx(
"1 0 0 1 0 0 0; 0 1 0 1 0 0 0; 0 0 1 1 0 0 0; 1 1 1 1 1 0 0; 0 0 0 1 1 1 0; 0 0 0 0 1 1 1; 0 0 0 0 0 1 1");
770 IAssertR(Ss->GetXLen() == Ss->GetYLen(),
"Not a square matrix");
771 IAssert(Ss->GetYLen() == Ss->GetXLen());
773 for (
int r = 0; r < Ss->GetYLen(); r++) {
774 for (
int c = 0; c < Ss->GetXLen(); c++) {
775 Mtx.
At(r, c) = (double) Ss->At(c, r).GetFlt(); }
788 InitLL(GraphPt, ParamMtx);
792 InitLL(GraphPt, ParamMtx);
798 return new TKroneckerLL(GraphPt, ParamMtx, PermPSwapNd);
802 return new TKroneckerLL(GraphPt, ParamMtx, NodeIdPermV, PermPSwapNd);
810 else FailR(
"Unknown permutation type (o,d,r)");
831 DegNIdV.
Add(
TIntPr(NI.GetDeg(), NI.GetId()));
835 for (
int i = 0; i < DegNIdV.
Len(); i++) {
848 for(
int i = 0; i < NZero; i++) {
849 for(
int j = 0; j < NZero; j++) {
855 for(
int i = 0; i <
Nodes; i++) {
858 double RowP = 1.0, ColP = 1.0;
860 int Bit = NId % NZero;
861 RowP *= Row[Bit]; ColP *= Col[Bit];
867 DegV.Sort(
false); CDegV.
Sort(
false);
868 for(
int i = 0; i <
Nodes; i++) {
869 NodePerm[DegV[i].Val2] = CDegV[i].Val2;
877 for (
int i = 0; i < Perm.
Len(); i++) {
887 if (!
Graph->
IsNode(nid)) { NodesOk=
false;
break; } }
901 if (EI.GetSrcNId() != EI.GetDstNId()) {
959 for (
int level = 0; level <
KronIters; level++) {
969 for (
int level = 0; level <
KronIters; level++) {
988 double Sum=0.0, SumSq=0.0;
1005 if (
GradV.
Len() != ProbMtx.Len()) {
1016 if (
GradV.
Len() != ProbMtx.Len()) {
1024 for (
int nid = 0; nid <
Nodes; nid++) {
1027 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
1039 for (
int nid = 0; nid <
Nodes; nid++) {
1042 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
1061 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
1068 for (
int e = 0; e < Node.
GetInDeg(); e++) {
1122 const double U = TKronMtx::Rnd.
GetUniDev();
1125 const double LogU = log(U);
1126 if (LogU > NewLL - OldLL) {
1138 for (
int NId1 = 0; NId1 <
Nodes; NId1++) {
1139 for (
int NId2 = 0; NId2 <
Nodes; NId2++) {
1148 double Sum=0.0, SumSq=0.0;
1159 for (
int ParamId = 0; ParamId <
LLMtx.
Len(); ParamId++) {
1161 for (
int NId1 = 0; NId1 <
Nodes; NId1++) {
1162 for (
int NId2 = 0; NId2 <
Nodes; NId2++) {
1170 GradV[ParamId] = DLL;
1177 for (
int ParamId = 0; ParamId <
LLMtx.
Len(); ParamId++) {
1179 for (
int NId1 = 0; NId1 <
Nodes; NId1++) {
1180 for (
int NId2 = 0; NId2 <
Nodes; NId2++) {
1188 GradV[ParamId] = DLL;
1195 for (
int ParamId = 0; ParamId <
LLMtx.
Len(); ParamId++) {
1197 for (
int nid = 0; nid <
Nodes; nid++) {
1200 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
1206 GradV[ParamId] = DLL;
1219 for (
int e = 0; e < Node.
GetOutDeg(); e++) {
1225 for (
int e = 0; e < Node.
GetInDeg(); e++) {
1242 for (
int ParamId = 0; ParamId <
LLMtx.
Len(); ParamId++) {
1273 int NId1=0, NId2=0, NAccept=0;
1278 printf(
" warm-up:%s,", ExeTm1.
GetTmStr()); ExeTm1.
Tick();
1285 for (
int s = 0; s < NSamples; s++) {
1288 for (
int m = 0; m <
LLMtx.
Len(); m++) { AvgGradV[m] +=
GradV[m]; }
1292 AvgLL = AvgLL / double(NSamples);
1293 for (
int m = 0; m <
LLMtx.
Len(); m++) {
1294 AvgGradV[m] = AvgGradV[m] / double(NSamples); }
1295 printf(
":%s (%.0f/s), accept %.1f%%\n", ExeTm1.
GetTmStr(), double(NSamples)/ExeTm1.
GetSecs(),
1296 double(100*NAccept)/double(NSamples));
1299 double TKroneckerLL::GradDescent(
const int& NIter,
const double& LrnRate,
double MnStep,
double MxStep,
const int& WarmUp,
const int& NSamples) {
1300 printf(
"\n----------------------------------------------------------------------\n");
1304 double OldLL=-1e10, CurLL=0;
1307 LearnRateV.PutAll(LrnRate);
1315 for (
int Iter = 0; Iter < NIter; Iter++) {
1316 printf(
"%03d] ", Iter);
1319 LearnRateV[p] *= 0.95;
1321 while (fabs(LearnRateV[p]*CurGradV[p]) > MxStep) { LearnRateV[p] *= 0.95; }
1322 while (fabs(LearnRateV[p]*CurGradV[p]) < 0.02) { LearnRateV[p] *= (1.0/0.95); }
1325 while (fabs(LearnRateV[p]*CurGradV[p]) > MxStep) { LearnRateV[p] *= 0.95; printf(
".");}
1326 while (fabs(LearnRateV[p]*CurGradV[p]) < MnStep) { LearnRateV[p] *= (1.0/0.95); printf(
"*");}
1327 if (MxStep > 3*MnStep) { MxStep *= 0.95; }
1329 NewProbMtx.
At(p) =
ProbMtx.
At(p) + LearnRateV[p]*CurGradV[p];
1330 if (NewProbMtx.
At(p) > 0.9999) { NewProbMtx.
At(p)=0.9999; }
1331 if (NewProbMtx.
At(p) < 0.0001) { NewProbMtx.
At(p)=0.0001; }
1333 printf(
" trueE0: %.2f (%d), estE0: %.2f (%d), ERR: %f\n", EZero,
Graph->
GetEdges(),
1335 printf(
" currLL: %.4f, deltaLL: %.4f\n", CurLL, CurLL-OldLL);
1337 printf(
" %d] %f <-- %f + %9f Grad: %9.1f Rate: %g\n", p, NewProbMtx.
At(p),
1338 ProbMtx.
At(p), (double)(LearnRateV[p]*CurGradV[p]), CurGradV[p](), LearnRateV[p]());
1340 if (Iter+1 < NIter) {
1343 printf(
"\n"); fflush(stdout);
1350 printf(
"TotalExeTm: %s %g\n", TotalTm.GetStr(), TotalTm.GetSecs());
1355 double TKroneckerLL::GradDescent2(
const int& NIter,
const double& LrnRate,
double MnStep,
double MxStep,
const int& WarmUp,
const int& NSamples) {
1356 printf(
"\n----------------------------------------------------------------------\n");
1357 printf(
"GradDescent2\n");
1359 printf(
"Skip moves that make likelihood smaller\n");
1362 double CurLL=0, NewLL=0;
1365 LearnRateV.PutAll(LrnRate);
1367 bool GoodMove =
false;
1369 for (
int Iter = 0; Iter < NIter; Iter++) {
1370 printf(
"%03d] ", Iter);
1371 if (! GoodMove) {
SampleGradient(WarmUp, NSamples, CurLL, CurGradV); }
1375 while (fabs(LearnRateV[p]*CurGradV[p]) > MxStep) { LearnRateV[p] *= 0.95; printf(
".");}
1376 while (fabs(LearnRateV[p]*CurGradV[p]) < MnStep) { LearnRateV[p] *= (1.0/0.95); printf(
"*");}
1377 NewProbMtx.
At(p) = CurProbMtx.At(p) + LearnRateV[p]*CurGradV[p];
1378 if (NewProbMtx.
At(p) > 0.9999) { NewProbMtx.
At(p)=0.9999; }
1379 if (NewProbMtx.
At(p) < 0.0001) { NewProbMtx.
At(p)=0.0001; }
1380 LearnRateV[p] *= 0.95;
1385 if (NewLL > CurLL) {
1386 printf(
"== Good move:\n");
1387 printf(
" trueE0: %.2f (%d), estE0: %.2f (%d), ERR: %f\n", EZero,
Graph->
GetEdges(),
1389 printf(
" currLL: %.4f deltaLL: %.4f\n", CurLL, NewLL-CurLL);
1391 printf(
" %d] %f <-- %f + %9f Grad: %9.1f Rate: %g\n", p, NewProbMtx.
At(p),
1392 CurProbMtx.At(p), (double)(LearnRateV[p]*CurGradV[p]), CurGradV[p](), LearnRateV[p]()); }
1394 CurGradV = NewGradV;
1397 printf(
"** BAD move:\n");
1398 printf(
" *trueE0: %.2f (%d), estE0: %.2f (%d), ERR: %f\n", EZero,
Graph->
GetEdges(),
1400 printf(
" *curLL: %.4f deltaLL: %.4f\n", CurLL, NewLL-CurLL);
1402 printf(
" b%d] %f <-- %f + %9f Grad: %9.1f Rate: %g\n", p, NewProbMtx.
At(p),
1403 CurProbMtx.At(p), (double)(LearnRateV[p]*CurGradV[p]), CurGradV[p](), LearnRateV[p]()); }
1408 printf(
"\n"); fflush(stdout);
1410 printf(
"TotalExeTm: %s %g\n", TotalTm.GetStr(), TotalTm.GetSecs());
1418 int count = 0, added = 0, collision = 0;
1422 double CumProb = 0.0;
1424 for(
int r = 0; r < MtxDim; r++) {
1425 for(
int c = 0; c < MtxDim; c++) {
1434 int Rng, Row, Col, n, NId1, NId2;
1435 while(added < NEdges) {
1436 Rng =
Nodes; Row = 0; Col = 0;
1437 for (
int iter = 0; iter <
KronIters; iter++) {
1438 const double& Prob = TKronMtx::Rnd.
GetUniDev();
1439 n = 0;
while(Prob > ProbToRCPosV[n].Val1) { n++; }
1440 const int MtxRow = ProbToRCPosV[n].Val2;
1441 const int MtxCol = ProbToRCPosV[n].Val3;
1443 Row += MtxRow * Rng;
1444 Col += MtxCol * Rng;
1470 }
else { collision ++; }
1478 int NId1 = 0, NId2 = 0;
1494 NMissing = (NMissing < 1) ? 1 : NMissing;
1504 int NId1 = 0, NId2 = 0, hit = 0, GId = 0;
1505 TIntTr EdgeToRemove, NewEdge;
1509 for(
int i = 0; i < WarmUp; i++) {
1518 RndAccept = (RndAccept > 1.0) ? 1.0 : RndAccept;
1520 if(TKronMtx::Rnd.GetUniDev() > RndAccept) {
1539 GEdgeV[GId].
Val3 = hit;
1541 LEdgeV[GEdgeV[GId].Val3].Val3 = GId;
1549 for (
int p = 0; p <
LLMtx.
Len(); p++) {
1559 for (
int s = 0; s < WarmUp; s++) {
1569 LLV.
Gen(NSamples, 0);
1570 DLLV.
Gen(NSamples, 0);
1574 printf(
" Warm-Up [%u] : %s\n", WarmUp, ExeTm.
GetTmStr());
1577 printf(
" Gibbs Warm-Up [%u] : %s\n", GibbsWarmUp, ExeTm.
GetTmStr());
1582 for(
int i = 0; i < NSamples; i++) {
1588 int OnePercent = (i+1) % (NSamples / 10);
1589 if(OnePercent == 0) {
1590 int TenPercent = ((i+1) / (NSamples / 10)) * 10;
1591 printf(
" %3u%% done : %s\n", TenPercent, ExeTm.
GetTmStr());
1599 double OldLL=
LogLike, CurLL=0;
1604 LearnRateV.PutAll(LrnRate);
1607 const int NSamples = LLV.
Len();
1608 const int ReCalcLen = NSamples / 10;
1610 for (
int s = 0; s < LLV.
Len(); s++) {
1612 for(
int p = 0; p <
GetParams(); p++) { CurGradV[p] += DLLV[s][p]; }
1615 for(
int p = 0; p <
GetParams(); p++) { CurGradV[p] /= NSamples; }
1617 double MaxLL = CurLL;
1621 for (
int Iter = 0; Iter < GradIter; Iter++) {
1622 printf(
" %03d] ", Iter+1);
1627 while (fabs(LearnRateV[p]*CurGradV[p]) > MxStep) { LearnRateV[p] *= 0.95; }
1628 while (fabs(LearnRateV[p]*CurGradV[p]) < 5 * MnStep) { LearnRateV[p] *= (1.0/0.95); }
1631 while (fabs(LearnRateV[p]*CurGradV[p]) > MxStep) { LearnRateV[p] *= 0.95; printf(
".");}
1632 while (fabs(LearnRateV[p]*CurGradV[p]) < MnStep) { LearnRateV[p] *= (1.0/0.95); printf(
"*");}
1633 if (MxStep > 3*MnStep) { MxStep *= 0.95; }
1635 NewProbMtx.
At(p) =
ProbMtx.
At(p) + LearnRateV[p]*CurGradV[p];
1636 if (NewProbMtx.
At(p) > 0.9999) { NewProbMtx.
At(p)=0.9999; }
1637 if (NewProbMtx.
At(p) < 0.0001) { NewProbMtx.
At(p)=0.0001; }
1638 LearnRateV[p] *= 0.95;
1641 printf(
" currLL: %.4f, deltaLL: %.4f\n", CurLL, CurLL-OldLL);
1643 printf(
" %d] %f <-- %f + %9f Grad: %9.1f Rate: %g\n", p, NewProbMtx.
At(p),
1644 ProbMtx.
At(p), (double)(LearnRateV[p]*CurGradV[p]), CurGradV[p](), LearnRateV[p]());
1648 if(Iter == GradIter - 1) {
1653 CurGradV.PutAll(0.0);
1659 for(
int s = 0; s < NSamples; s++) {
1663 if(s % ReCalcLen == ReCalcLen/2) {
1671 CurGradV[p] += OneDLL[p];
1677 MaxLL = CurLL; MaxProbMtx =
ProbMtx;
1680 printf(
" Time: %s\n", IterTm.
GetTmStr());
1681 printf(
"\n"); fflush(stdout);
1685 printf(
" FinalLL : %f, TotalExeTm: %s\n", MaxLL, TotalTm.
GetTmStr());
1692 void TKroneckerLL::RunKronEM(
const int& EMIter,
const int& GradIter,
double LrnRate,
double MnStep,
double MxStep,
const int& GibbsWarmUp,
const int& WarmUp,
const int& NSamples,
const TKronEMType& Type,
const int& NMissing) {
1693 printf(
"\n----------------------------------------------------------------------\n");
1711 for(
int i = 0; i < EMIter; i++) {
1712 printf(
"\n----------------------------------------------------------------------\n");
1713 printf(
"%03d EM-iter] E-Step\n", i+1);
1714 RunEStep(GibbsWarmUp, WarmUp, NSamples, LLV, DLLV);
1717 printf(
"%03d EM-iter] M-Step\n", i+1);
1718 double CurLL =
RunMStep(LLV, DLLV, GradIter, LrnRate, MnStep, MxStep);
1734 for (
int i = 0; i < XYValV.
Len(); i++) {
1735 Min =
TMath::Mn(Min, XYValV[i].Val2.Val);
1736 Max =
TMath::Mx(Max, XYValV[i].Val2.Val);
1741 double Min, Max, Min1, Max1;
1748 GP.
SetXYLabel(
"Sample Index (time)",
"Log-likelihood");
1751 {
TGnuPlot GP(
"sAcc-"+OutFNm,
TStr::Fmt(
"Pct. accepted rnd moves (over 1k samples). %s", Desc.
CStr()),
true);
1753 GP.
SetXYLabel(
"Sample Index (time)",
"Pct accept permutation swaps");
1758 for (
int g = 0; g < GradVV.
Len(); g++) {
1760 GetMinMax(GradVV[g], Min1, Max1,
false);
1764 GP.
SetYRange((
int)floor(Min-1), (
int)ceil(Max+1));
1765 GP.
SetXYLabel(
"Sample Index (time)",
"Gradient");
1768 GPAll.
SetYRange((
int)floor(Min1-1), (
int)ceil(Max1+1));
1769 GPAll.
SetXYLabel(
"Sample Index (time)",
"Gradient");
1774 double Avg=0.0, Var=0.0;
1775 for (
int i = 0; i < ValV.
Len(); i++) { Avg += ValV[i]; }
1776 Avg /= (double) ValV.
Len();
1777 for (
int i = 0; i < ValV.
Len(); i++) { Var +=
TMath::Sqr(ValV[i]-Avg); }
1779 for (
int k = 0; k <
TMath::Mn(ValV.
Len(), MaxK); k++) {
1781 for (
int i = 0; i < ValV.
Len() - k; i++) {
1782 corr += (ValV[i]-Avg)*(ValV[i+k]-Avg);
1789 GP.
SetXYLabel(
"Lag, k",
"Autocorrelation, r_k");
1797 int NId1=0, NId2=0, NAccept=0;
1799 const int PlotLen = NSamples/1000+1;
1800 double TrueLL=-1, AvgLL=0.0;
1805 TFltV SampleLLV(NSamples, 0);
1808 GradVV[g].
Gen(PlotLen, 0); }
1809 if (! TrueMtx.
Empty()) {
1815 printf(
"LogLike at start: %f\n",
LogLike());
1818 if (TrueLL != -1) { TrueLLV.
Add(
TFltPr(0, TrueLL)); }
1820 printf(
" warm-up:%s,", ExeTm.
GetTmStr()); ExeTm.
Tick();
1822 printf(
"LogLike afterm warm-up: %f\n",
LogLike());
1826 if (TrueLL != -1) { TrueLLV.
Add(
TFltPr(WarmUp, TrueLL)); }
1827 printf(
" recalculated: %f\n",
LogLike());
1829 printf(
" sampling (average per 1000 samples)\n");
1831 for (
int s = 0; s < NSamples; s++) {
1834 for (
int m = 0; m < AvgGradV.
Len(); m++) { AvgGradV[m] +=
GradV[m]; }
1842 if (s > 0 && s % 1000 == 0) {
1844 for (
int g = 0; g < AvgGradV.
Len(); g++) {
1845 GradVV[g].
Add(
TFltPr(WarmUp+s, AvgGradV[g] / 1000.0)); }
1846 EstLLV.
Add(
TFltPr(WarmUp+s, AvgLL / 1000.0));
1847 if (TrueLL != -1) { TrueLLV.
Add(
TFltPr(WarmUp+s, TrueLL)); }
1848 AcceptV.
Add(
TFltPr(WarmUp+s, NAccept/1000.0));
1855 if (s % 100000 == 0 && DoPlot) {
1858 PlotGrad(EstLLV, TrueLLV, GradVV, AcceptV, OutFNm, Desc);
1859 for (
int n = 0; n < SamplVV.
Len(); n++) {
1861 printf(
" samples: %d, time: %s, samples/s: %.1f\n", s, ExeTm.
GetTmStr(), double(s+1)/ExeTm.
GetSecs());
1863 AvgLL = 0; AvgGradV.
PutAll(0); NAccept=0;
1869 PlotGrad(EstLLV, TrueLLV, GradVV, AcceptV, OutFNm, Desc);
1870 for (
int n = 0; n < SamplVV.
Len(); n++) {
1878 for (
int j = 0; j < AvgJV.
Len(); j++) {
1879 AvgAvg += AvgJV[j]; }
1880 AvgAvg /= AvgJV.
Len();
1884 for (
int j = 0; j < ChainLLV.
Len(); j++) {
1885 const TFltV& ChainV = ChainLLV[j];
1887 for (
int i = 0; i < ChainV.
Len(); i++) {
1890 AvgJV.
Add(Avg/ChainV.
Len());
1896 const double J = ChainLLV.
Len();
1897 const double K = ChainLLV[0].
Len();
1901 double InChainVar=0, OutChainVar=0;
1903 for (
int j = 0; j < AvgJV.
Len(); j++) {
1904 OutChainVar +=
TMath::Sqr(AvgJV[j] - AvgAvg); }
1905 OutChainVar = OutChainVar * (K/double(J-1));
1906 printf(
"*** %g chains of len %g\n", J, K);
1907 printf(
" ** between chain var: %f\n", OutChainVar);
1909 for (
int j = 0; j < AvgJV.
Len(); j++) {
1910 const TFltV& ChainV = ChainLLV[j];
1911 for (
int k = 0; k < ChainV.
Len(); k++) {
1912 InChainVar +=
TMath::Sqr(ChainV[k] - AvgJV[j]); }
1914 InChainVar = InChainVar * 1.0/double(J*(K-1));
1915 printf(
" ** within chain var: %f\n", InChainVar);
1916 const double PostVar = (K-1)/K * InChainVar + 1.0/K * OutChainVar;
1917 printf(
" ** posterior var: %f\n", PostVar);
1918 const double ScaleRed = sqrt(PostVar/InChainVar);
1919 printf(
" ** scale reduction (< 1.2): %f\n\n", ScaleRed);
1927 const int K = ChainLLV[0].
Len();
1928 const int Buckets=1000;
1929 const int BucketSz = K/Buckets;
1930 for (
int b = 1; b < Buckets; b++) {
1931 const int End =
TMath::Mn(BucketSz*b, K-1);
1932 for (
int c = 0; c < ChainLLV.
Len(); c++) {
1933 ChainLLV[c].
GetSubValV(0, End, SmallLLV[c]); }
1938 Desc.
CStr(), ChainLLV.
Len(), ChainLLV[0].
Len(), BucketSz),
"Chain length",
"Potential scale reduction");
1943 printf(
"Test gradient descent on a synthetic kronecker graphs:\n");
1944 if (DoExact) { printf(
" -- Exact gradient calculations\n"); }
1945 else { printf(
" -- Approximate gradient calculations\n"); }
1946 if (TruePerm) { printf(
" -- No permutation sampling (use true permutation)\n"); }
1947 else { printf(
" -- Sample permutations (start with degree permutation)\n"); }
1950 double OldLL=0, MyLL=0, AvgAbsErr, AbsSumErr;
1951 TFltV MyGradV, SDevV;
1966 printf(
"DEGREE PERMUTATION\n");
SetDegPerm();
1968 for (Iter = 0; Iter < 100; Iter++) {
1979 printf(
"%d] LL: %g, ", Iter, MyLL);
1982 printf(
" avgAbsErr: %.4f, absSumErr: %.4f, newLL: %.2f, deltaLL: %.2f\n", AvgAbsErr, AbsSumErr, MyLL, OldLL-MyLL);
1985 LearnRateV[p] *= 0.9;
1987 while (fabs(LearnRateV[p]*MyGradV[p]) > 0.1) { LearnRateV[p] *= 0.9; }
1989 while (fabs(LearnRateV[p]*MyGradV[p]) < 0.001) { LearnRateV[p] *= (1.0/0.9); }
1991 printf(
" %d] %f <-- %f + %f lrnRate:%g\n", p,
ProbMtx.
At(p) + LearnRateV[p]*MyGradV[p],
1992 ProbMtx.
At(p), (double)(LearnRateV[p]*MyGradV[p]), LearnRateV[p]());
1999 if (AvgAbsErr < 0.01) { printf(
"***CONVERGED!\n");
break; }
2000 printf(
"\n"); fflush(stdout);
2002 TrueParam.
Dump(
"True Thetas",
true);
2004 printf(
" AvgAbsErr: %f\n AbsSumErr: %f\n Iterations: %d\n", AvgAbsErr, AbsSumErr, Iter);
2005 printf(
"Iteration run time: %s, sec: %g\n\n", IterTm.
GetTmStr(), IterTm.
GetSecs());
2006 return TFltQu(AvgAbsErr, AbsSumErr, Iter, IterTm.
GetSecs());
2012 if (! TrueV.
Empty()) { GP.AddPlot(TrueV,
gpwLines,
"TRUE"); }
2013 GP.SetXYLabel(
"Gradient descent iterations", YLabel);
2018 double LearnRate,
const int& WarmUp,
const int& NSamples,
const int& AvgKGraphs,
const TKronMtx& TrueParam) {
2021 double OldLL=0, MyLL=0, AvgAbsErr=0, AbsSumErr=0;
2022 TFltV MyGradV, SDevV;
2024 TFltPrV EZeroV, DiamV, Lambda1V, Lambda2V, AvgAbsErrV, AvgLLV;
2025 TFltPrV TrueEZeroV, TrueDiamV, TrueLambda1V, TrueLambda2V, TrueLLV;
2029 const double TrueLambda1 = SngValV[0];
2030 const double TrueLambda2 = SngValV[1];
2031 if (! TrueParam.
Empty()) {
2036 const double TrueLL = OldLL;
2038 for (Iter = 0; Iter < NIters; Iter++) {
2047 double SumDiam=0, SumSngVal1=0, SumSngVal2=0;
2048 for (
int trial = 0; trial < AvgKGraphs; trial++) {
2054 SumSngVal1 += SngValV[0]; SumSngVal2 += SngValV[1];
2059 DiamV.
Add(
TFltPr(Iter, SumDiam/
double(AvgKGraphs)));
2060 Lambda1V.
Add(
TFltPr(Iter, SumSngVal1/
double(AvgKGraphs)));
2061 Lambda2V.
Add(
TFltPr(Iter, SumSngVal2/
double(AvgKGraphs)));
2063 TrueEZeroV.
Add(
TFltPr(Iter, TrueEZero));
2064 TrueDiamV.
Add(
TFltPr(Iter, TrueEffDiam));
2065 TrueLambda1V.
Add(
TFltPr(Iter, TrueLambda1));
2066 TrueLambda2V.
Add(
TFltPr(Iter, TrueLambda2));
2067 if (Iter % 10 == 0) {
2070 PlotTrueAndEst(
"LL."+OutFNm, Desc,
"Average LL", AvgLLV, TrueLLV);
2071 PlotTrueAndEst(
"E0."+OutFNm, Desc,
"E0 (expected number of edges)", EZeroV, TrueEZeroV);
2072 PlotTrueAndEst(
"Diam."+OutFNm+
"-Diam", Desc,
"Effective diameter", DiamV, TrueDiamV);
2073 PlotTrueAndEst(
"Lambda1."+OutFNm, Desc,
"Lambda 1", Lambda1V, TrueLambda1V);
2074 PlotTrueAndEst(
"Lambda2."+OutFNm, Desc,
"Lambda 2", Lambda2V, TrueLambda2V);
2075 if (! TrueParam.
Empty()) {
2078 if (! TrueParam.
Empty()) {
2080 AvgAbsErrV.
Add(
TFltPr(Iter, AvgAbsErr));
2081 }
else { AvgAbsErr = 1.0; }
2086 LearnRateV[p] *= 0.99;
2087 while (fabs(LearnRateV[p]*MyGradV[p]) > 0.1) { LearnRateV[p] *= 0.99; printf(
".");}
2088 while (fabs(LearnRateV[p]*MyGradV[p]) < 0.002) { LearnRateV[p] *= (1.0/0.95); printf(
"*");}
2089 printf(
" %d] %f <-- %f + %9f Grad: %9.1f, Rate:%g\n", p,
ProbMtx.
At(p) + LearnRateV[p]*MyGradV[p],
2090 ProbMtx.
At(p), (double)(LearnRateV[p]*MyGradV[p]), MyGradV[p](), LearnRateV[p]());
2096 printf(
"%d] LL: %g, ", Iter, MyLL);
2097 printf(
" avgAbsErr: %.4f, absSumErr: %.4f, newLL: %.2f, deltaLL: %.2f\n", AvgAbsErr, AbsSumErr, MyLL, OldLL-MyLL);
2098 if (AvgAbsErr < 0.001) { printf(
"***CONVERGED!\n");
break; }
2099 printf(
"\n"); fflush(stdout);
2102 TrueParam.
Dump(
"True Thetas",
true);
2104 printf(
" AvgAbsErr: %f\n AbsSumErr: %f\n Iterations: %d\n", AvgAbsErr, AbsSumErr, Iter);
2105 printf(
"Iteration run time: %s, sec: %g\n\n", IterTm.
GetTmStr(), IterTm.
GetSecs());
2110 double LearnRate,
const int& WarmUp,
const int& NSamples,
const int& TrueN0) {
2117 for (
int NZero = 2; NZero < 10; NZero++) {
2119 InitKronMtx.
Dump(
"INIT PARAM",
true);
2122 const double LastLL = KronLL.
GradDescent(GradIters, LearnRate, 0.001, 0.01, WarmUp, NSamples);
2126 {
TGnuPlot GP(
"LL-"+OutFNm, Desc1);
2129 {
TGnuPlot GP(
"BIC-"+OutFNm, Desc1);
2132 {
TGnuPlot GP(
"MDL-"+OutFNm, Desc1);
2144 int NId1 = 0, NId2 = 0, NAccept = 0;
2148 else if (Permutation ==
"d") KronLL.
SetDegPerm();
2150 else FailR(
"Unknown permutation (r,d,o)");
2153 for (
int s = 0; s < 1000*KiloSamples; s++) {
2157 for (
int m = 0; m < 4; m++) { GradVV[m].Add(KronLL.
GradV[m]); }
2158 if ((s+1) % 1000 == 0) {
2160 for (
int m = 0; m < 4; m++) { GradVV[m].Add(KronLL.
GradV[m]); }
2162 if ((s+1) % 100000 == 0) {
2164 for (
int g = 0; g < GradVV.Len(); g++) {
2166 GP.
SetXYLabel(
"sample index",
"log Gradient");
2173 for (
int m = 0; m < 4; m++) {
2175 printf(
"grad %d: mean: %12f sDev: %12f median: %12f\n", m,
2176 GradMomV[m].GetMean(), GradMomV[m].GetSDev(), GradMomV[m].GetMedian());
2211 ShufflePerm.
Shuffle(TKronMtx::Rnd);
2212 for(i = 0; i < NNodes; i++) {
2213 Graph->
DelNode(
int(ShufflePerm[i]));
2216 for(i = 0; i < NNodes; i++) {
2217 Graph->
DelNode(
int(ShufflePerm[ShufflePerm.
Len() - 1 - i]));
2225 IAssert(Rate > 0 && Rate < 0.5);
2230 IAssert(NEdges > 0 && NEdges < Graph->GetEdges());
2232 const int Nodes = Graph->
GetNodes();
2233 const int Edges = Graph->
GetEdges();
2240 ToAdd.
Gen(NEdges / 2, 0);
2241 for(
int i = 0; i < NEdges / 2; i++) {
2244 if(Graph->
IsEdge(Src, Dst)) { i--;
continue; }
2249 ToDel.
Gen(Edges, 0);
2251 ToDel.
Add(
TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
2255 for(
int i = 0; i < NEdges / 2; i++) {
2256 Graph->
DelEdge(ToDel[i].Val1, ToDel[i].Val2);
2257 Graph->
AddEdge(ToAdd[i].Val1, ToAdd[i].Val2);
2264 IAssert(Rate > 0 && Rate < 0.5);
2269 IAssert(NEdges > 0 && NEdges < Graph->GetEdges());
2275 if(EI.GetSrcNId() != EI.GetDstNId()) {
2276 ToDel.
Add(
TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
2281 for(
int i = 0; i < NEdges; i++) {
2282 Graph->
DelEdge(ToDel[i].Val1, ToDel[i].Val2);
2289 IAssert(Rate > 0 && Rate < 0.5);
2301 else FailR(
"Unknown permutation type (o,d,r)");
2355 NewThetaV.
Gen(ThetaV.
Len());
2356 for (
int i = 0; i < ThetaV.
Len(); i++) {
2362 Kronecker.
GenMtx((
int)sqrt((
double)ThetaV.
Len()));
2363 for (
int i = 0; i < ThetaV.
Len(); i++) {
static const T & Mn(const T &LVal, const T &RVal)
void SetRndMtx(const int &MtxDim, const double &MinProb=0.0)
TPair< TInt, TInt > TIntPr
TKronMtx & operator=(const TKronMtx &Kronecker)
static int RemoveNodeNoise(PNGraph &Graph, const int &NNodes, const bool Random=true)
!!!!! MYUNGHWAN, CHECK!
static PNGraph GenKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir, const int &Seed=0)
static PKroneckerLL New()
double GetEdgeProb(int NId1, int NId2, const int &NKronIters) const
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
#define IAssertR(Cond, Reason)
TPair< TFlt, TInt > TFltIntPr
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
int GetEdges(const int &NIter) const
static void TestBicCriterion(const TStr &OutFNm, const TStr &Desc1, const PNGraph &G, const int &GradIters, double LearnRate, const int &WarmUp, const int &NSamples, const int &TrueN0)
static TStr GetMegaStr(const int &Val)
static void KronMul(const TKronMtx &LeftPt, const TKronMtx &RightPt, TKronMtx &OutMtx)
TFltV TestSamplePerm(const TStr &OutFNm, const int &WarmUp, const int &NSamples, const TKronMtx &TrueMtx, const bool &DoPlot=true)
static TKronMtx LoadTxt(const TStr &MtxFNm)
static PSs LoadTxt(const TSsFmt &SsFmt, const TStr &FNm, const PNotify &Notify=NULL, const bool &IsExcelEoln=true, const int &MxY=-1, const TIntV &AllowedColNV=TIntV(), const bool &IsQStr=true)
static bool IsNum(const char &Ch)
void SetRandomEdges(const int &NEdges, const bool isDir=true)
!!!!! MYUNGHWAN, CHECK!
void Del(const TSizeTy &ValN)
Removes the element at position ValN.
int GetKronIter(const int &Nodes) const
void SavePng(const int &SizeX=1000, const int &SizeY=800, const TStr &Comment=TStr())
PGraph GetMxWcc(const PGraph &Graph)
Returns a graph representing the largest weakly connected component on an input Graph.
static const T & Mx(const T &LVal, const T &RVal)
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
void RunEStep(const int &GibbsWarmUp, const int &WarmUp, const int &NSamples, TFltV &LLV, TVec< TFltV > &DLLV)
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
void UpdateGraphDLL(const int &SwapNId1, const int &SwapNId2)
static double CalcChainR2(const TVec< TFltV > &ChainLLV)
double GetApxNoEdgeLL(int NId1, int NId2, const int &NKronIters) const
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
double GetFullGraphLL() const
int GetEdges() const
Returns the number of edges in the graph.
bool IsEdgePlace(int NId1, int NId2, const int &NKronIters, const double &ProbTresh) const
void Swap(TKronMtx &KronMtx)
TSizeTy Len() const
Returns the number of elements in the vector.
void GetMinMax(const TFltPrV &XYValV, double &Min, double &Max, const bool &ResetMinMax)
void Dump(const TStr &MtxNm=TStr(), const bool &Sort=false) const
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
double GetAnfEffDiam(const PGraph &Graph, const bool &IsDir, const double &Percentile, const int &NApprox)
double NodeDLLDelta(const int ParamId, const int &NId) const
double GetFullRowLL(int RowId) const
void SetPerm(const char &PermId)
int GetNodes() const
Returns the number of nodes in the graph.
double GetEdgeLL(int NId1, int NId2, const int &NKronIters) const
void SetXYLabel(const TStr &XLabel, const TStr &YLabel)
double GetEmptyGraphDLL(const int &ParamId) const
double GetEmptyGraphLL() const
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
static uint GetNodeSig(const double &OneProb=0.5)
bool SampleNextPerm(int &NId1, int &NId2)
static TKronMtx GetInitMtx(const int &Dim, const int &Nodes, const int &Edges)
void RunKronEM(const int &EMIter, const int &GradIter, double LrnRate, double MnStep, double MxStep, const int &GibbsWarmUp, const int &WarmUp, const int &NSamples, const TKronEMType &Type=kronNodeMiss, const int &NMissing=-1)
static double Sqr(const double &x)
void SetIPerm(const TIntV &Perm)
!!!!! MYUNGHWAN, CHECK!
void SetYRange(const double &Min, const double &Max)
void SetForEdges(const int &Nodes, const int &Edges)
static void KronPwr(const TKronMtx &KronPt, const int &NIter, TKronMtx &OutMtx)
int ChangeChAll(const char &SrcCh, const char &DstCh)
const double & At(const int &Row, const int &Col) const
void SetEpsMtx(const double &Eps1, const double &Eps0, const int &Eps1Val=1, const int &Eps0Val=0)
double GetEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
void RestoreGraph(const bool RestoreNodes=true)
!!!!! MYUNGHWAN, CHECK!
bool Empty() const
Tests whether the vector is empty.
static void TestGradDescent(const int &KronIters, const int &KiloSamples, const TStr &Permutation)
bool IsObsEdge(const int &NId1, const int &NId2) const
Graph Statistics Sequence.
void DelNode(const int &NId)
Deletes node of ID NId from the graph.
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
void GetProbMtx(TKronMtx &ProbMtx)
const char * GetTmStr() const
void SetBestDegPerm()
!!!!! MYUNGHWAN, CHECK!
void DelZeroDegNodes(PGraph &Graph)
Removes all the zero-degree nodes, that isolated nodes, from the graph.
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void SetPerm(const char &PermId)
static int FlipEdgeNoise(PNGraph &Graph, const int &NEdges, const bool Random=true)
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
double GetApxEmptyGraphLL() const
Edge iterator. Only forward iteration (operator++) is supported.
static void PutRndSeed(const int &Seed)
double GetApxNoEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
static TKronMtx GetMtxFromNm(const TStr &MtxNm)
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
void PlotGrad(const TFltPrV &EstLLV, const TFltPrV &TrueLLV, const TVec< TFltPrV > &GradVV, const TFltPrV &AcceptV, const TStr &OutFNm, const TStr &Desc)
void SampleGradient(const int &WarmUp, const int &NSamples, double &AvgLL, TFltV &GradV)
PNGraph GenRndGraph(const double &RndFact=1.0) const
double NodeLLDelta(const int &NId) const
void DelEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true)
Deletes an edge from node IDs SrcNId to DstNId from the graph.
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
double GetApxEmptyGraphDLL(const int &ParamId) const
void PlotAutoCorrelation(const TFltV &ValV, const int &MaxK, const TStr &OutFNm, const TStr &Desc)
double RunMStep(const TFltV &LLV, const TVec< TFltV > &DLLV, const int &GradIter, const double &LrnRate, double MnStep, double MxStep)
static double Round(const double &Val)
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
static double Power(const double &Base, const double &Exponent)
void AddRndNoise(const double &SDev)
const TVal & Last() const
Returns a reference to the last element of the vector.
static double GetAvgFroErr(const TKronMtx &Kron1, const TKronMtx &Kron2)
void GradDescentConvergence(const TStr &OutFNm, const TStr &Desc1, const bool &SamplePerm, const int &NIters, double LearnRate, const int &WarmUp, const int &NSamples, const int &AvgKGraphs, const TKronMtx &TrueParam)
static void ChainGelmapRubinPlot(const TVec< TFltV > &ChainLLV, const TStr &OutFNm, const TStr &Desc)
TQuad< TFlt, TFlt, TFlt, TFlt > TFltQu
double GetFullColLL(int ColId) const
void InitLL(const TFltV &ParamV)
TPair< TFlt, TFlt > TFltPr
int GetDeg() const
Returns degree of the current node, the sum of in-degree and out-degree.
double GetEZero(const int &Edges, const int &KronIter) const
double GetNoEdgeProb(int NId1, int NId2, const int &NKronIters) const
int GetNZeroK(const PNGraph &Graph) const
void GetLLMtx(TKronMtx &LLMtx)
const TFltV & CalcGraphDLL()
void MetroGibbsSampleSetup(const int &WarmUp)
const TFltV & CalcFullApxGraphDLL()
void MetroGibbsSampleNext(const int &WarmUp, const bool DLLUpdate=false)
void GetSngVals(const PNGraph &Graph, const int &SngVals, TFltV &SngValV)
Computes largest SngVals singular values of the adjacency matrix representing a directed Graph...
double GradDescent2(const int &NIter, const double &LrnRate, double MnStep, double MxStep, const int &WarmUp, const int &NSamples)
static PNGraph GenDetKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir)
void GenMtx(const int &Dim)
double GetColSum(const int &ColId) const
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
void PlotTrueAndEst(const TStr &OutFNm, const TStr &Desc, const TStr &YLabel, const TFltPrV &EstV, const TFltPrV &TrueV)
int GetOutDeg() const
Returns out-degree of the current node.
const TFltV & CalcApxGraphDLL()
double GetNoEdgeLL(int NId1, int NId2, const int &NKronIters) const
static TKronMtx GetRndMtx(const int &Dim, const double &MinProb)
void SaveTxt(const TStr &OutFNm) const
double SwapNodesLL(const int &NId1, const int &NId2)
static TStr Fmt(const char *FmtStr,...)
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
void Shuffle(TRnd &Rnd)
Randomly shuffles the elements of the vector.
Node iterator. Only forward iteration (operator++) is supported.
void McMcGetAvgAvg(const TFltV &AvgJV, double &AvgAvg)
int AddPlot(const TIntV &YValV, const TGpSeriesTy &SeriesTy=gpwLinesPoints, const TStr &Label=TStr(), const TStr &Style=TStr())
static void KronSum(const TKronMtx &LeftPt, const TKronMtx &RightPt, TKronMtx &OutMtx)
double GetRowSum(const int &RowId) const
TTriple< TInt, TInt, TInt > TIntTr
int GetNodes(const int &NIter) const
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
int GetUniDevInt(const int &Range=0)
PNGraph GenThreshGraph(const double &Thresh) const
double GetNoEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
static TVec< TFlt, TSizeTy > GetV(const TFlt &Val1)
Returns a vector on element Val1.
void SetGraph(const PNGraph &GraphPt)
int GetInDeg() const
Returns in-degree of the current node.
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
TFltQu TestKronDescent(const bool &DoExact, const bool &TruePerm, double LearnRate, const int &WarmUp, const int &NSamples, const TKronMtx &TrueParam)
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
void DelLast()
Removes the last element of the vector.
static double GetAvgAbsErr(const TKronMtx &Kron1, const TKronMtx &Kron2)
static void PlotCmpGraphs(const TKronMtx &SeedMtx, const PNGraph &Graph, const TStr &OutFNm, const TStr &Desc)
static void RoundTheta(const TFltV &ThetaV, TFltV &NewThetaV)
static void PlotValV(const TVec< TVal1 > &ValV, const TStr &OutFNmPref, const TStr &Desc="", const TStr &XLabel="", const TStr &YLabel="", const TGpScaleTy &ScaleTy=gpsAuto, const bool &PowerFit=false, const TGpSeriesTy &SeriesTy=gpwLinesPoints)
double GradDescent(const int &NIter, const double &LrnRate, double MnStep, double MxStep, const int &WarmUp, const int &NSamples)
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
TTriple< TFlt, TInt, TInt > TFltIntIntTr
void McMcGetAvgJ(const TVec< TFltV > &ChainLLV, TFltV &AvgJV)
static int RemoveEdgeNoise(PNGraph &Graph, const int &NEdges)
static PNGraph GenFastKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir, const int &Seed=0)
void GetSubValV(const TSizeTy &BValN, const TSizeTy &EValN, TVec< TVal, TSizeTy > &ValV) const
Fills ValV with elements at positions BValN...EValN.