SNAP Library 2.2, Developer Reference
2014-03-11 19:15:55
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
00001 #include "stdafx.h" 00002 #include "cascdynetinf.h" 00003 00004 void TNIBs::LoadCascadesTxt(TSIn& SIn) { 00005 TStr Line; 00006 while (!SIn.Eof()) { 00007 SIn.GetNextLn(Line); 00008 if (Line=="") { break; } 00009 TStrV NIdV; Line.SplitOnAllCh(',', NIdV); 00010 AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); 00011 } 00012 printf("All nodes read!\n"); 00013 while (!SIn.Eof()) { SIn.GetNextLn(Line); AddCasc(Line, Model); } 00014 00015 printf("All cascades read!\n"); 00016 } 00017 00018 void TNIBs::LoadGroundTruthTxt(TSIn& SIn) { 00019 bool verbose = false; 00020 TStr Line; 00021 00022 Network.Clr(); // clear network (if any) 00023 00024 // add nodes 00025 while (!SIn.Eof()) { 00026 SIn.GetNextLn(Line); 00027 if (Line=="") { break; } 00028 TStrV NIdV; Line.SplitOnAllCh(',', NIdV); 00029 Network.AddNode(NIdV[0].GetInt(), NIdV[1]); 00030 if (!IsNodeNm(NIdV[0].GetInt())) { 00031 AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); 00032 DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); 00033 } 00034 } 00035 00036 // add edges 00037 while (!SIn.Eof()) { 00038 SIn.GetNextLn(Line); 00039 TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV); 00040 00041 TFltFltH Alphas; 00042 if (FieldsV.Len() == 3) { 00043 Alphas.AddDat(0.0) = FieldsV[2].GetFlt(); 00044 } else { 00045 for (int i=2; i<FieldsV.Len()-1; i+=2) { 00046 Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt(); 00047 } 00048 } 00049 00050 Network.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas); 00051 00052 if (verbose) { 00053 printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt()); 00054 TFltFltH &AlphasE = Network.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt()); 00055 for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); } 00056 printf("\n"); 00057 } 00058 } 00059 00060 printf("groundtruth nodes:%d edges:%d\n", Network.GetNodes(), Network.GetEdges()); 00061 } 00062 00063 void TNIBs::LoadGroundTruthNodesTxt(TSIn& SIn) { 00064 TStr Line; 00065 00066 Network.Clr(); // clear network (if any) 00067 00068 // add nodes 00069 while (!SIn.Eof()) { 00070 SIn.GetNextLn(Line); 00071 if (Line=="") { break; } 00072 TStrV NIdV; Line.SplitOnAllCh(',', NIdV); 00073 Network.AddNode(NIdV[0].GetInt(), NIdV[1]); 00074 if (!IsNodeNm(NIdV[0].GetInt())) { 00075 AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); 00076 DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); 00077 } 00078 } 00079 00080 printf("groundtruth nodes:%d\n", Network.GetNodes()); 00081 } 00082 00083 void TNIBs::LoadInferredTxt(TSIn& SIn) { 00084 bool verbose = false; 00085 TStr Line; 00086 00087 InferredNetwork.Clr(); // clear network (if any) 00088 00089 // add nodes 00090 while (!SIn.Eof()) { 00091 SIn.GetNextLn(Line); 00092 if (Line=="") { break; } 00093 TStrV NIdV; Line.SplitOnAllCh(',', NIdV); 00094 if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); } 00095 InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]); 00096 if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); } 00097 if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); } 00098 if (verbose) { printf("Node:%s\n", NIdV[1].CStr()); } 00099 } 00100 00101 // add edges 00102 while (!SIn.Eof()) { 00103 SIn.GetNextLn(Line); 00104 TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV); 00105 00106 TFltFltH Alphas; 00107 if (FieldsV.Len() == 3) { 00108 Alphas.AddDat(0.0) = FieldsV[2].GetFlt(); 00109 } else { 00110 for (int i=2; i<FieldsV.Len()-1; i+=2) { 00111 Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt(); 00112 } 00113 } 00114 00115 InferredNetwork.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas); 00116 00117 if (verbose) { 00118 printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt()); 00119 TFltFltH &AlphasE = InferredNetwork.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt()); 00120 for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); } 00121 printf("\n"); 00122 } 00123 } 00124 00125 printf("inferred nodes:%d edges:%d\n", InferredNetwork.GetNodes(), InferredNetwork.GetEdges()); 00126 } 00127 00128 void TNIBs::LoadInferredNodesTxt(TSIn& SIn) { 00129 TStr Line; 00130 00131 InferredNetwork.Clr(); // clear network (if any) 00132 00133 // add nodes 00134 while (!SIn.Eof()) { 00135 SIn.GetNextLn(Line); 00136 if (Line=="") { break; } 00137 TStrV NIdV; Line.SplitOnAllCh(',', NIdV); 00138 if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); } 00139 InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]); 00140 if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); } 00141 if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); } 00142 } 00143 00144 printf("Nodes:%d\n", InferredNetwork.GetNodes()); 00145 } 00146 00147 void TNIBs::AddCasc(const TStr& CascStr, const TModel& Model) { 00148 int CId = CascH.Len(); 00149 00150 // support cascade id if any 00151 TStrV FieldsV; CascStr.SplitOnAllCh(';', FieldsV); 00152 if (FieldsV.Len()==2) { CId = FieldsV[0].GetInt(); } 00153 00154 // read nodes 00155 TStrV NIdV; FieldsV[FieldsV.Len()-1].SplitOnAllCh(',', NIdV); 00156 TCascade C(CId, Model); 00157 for (int i = 0; i < NIdV.Len(); i+=2) { 00158 int NId; 00159 double Tm; 00160 NId = NIdV[i].GetInt(); 00161 Tm = NIdV[i+1].GetFlt(); 00162 GetNodeInfo(NId).Vol = GetNodeInfo(NId).Vol + 1; 00163 C.Add(NId, Tm); 00164 } 00165 C.Sort(); 00166 00167 AddCasc(C); 00168 } 00169 00170 void TNIBs::AddCasc(const TIntFltH& Cascade, const int& CId, const TModel& Model) { 00171 TCascade C(CId, Model); 00172 for (TIntFltH::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) { 00173 GetNodeInfo(NI.GetKey()).Vol = GetNodeInfo(NI.GetKey()).Vol + 1; 00174 C.Add(NI.GetKey(), NI.GetDat()); 00175 } 00176 C.Sort(); 00177 AddCasc(C); 00178 } 00179 00180 void TNIBs::GenCascade(TCascade& C) { 00181 bool verbose = false; 00182 TIntFltH InfectedNIdH; TIntH InfectedBy; 00183 double GlobalTime, InitTime; 00184 double alpha; 00185 int StartNId; 00186 00187 if (Network.GetNodes() == 0) 00188 return; 00189 00190 // set random seed 00191 TInt::Rnd.Randomize(); 00192 00193 while (C.Len() < 2) { 00194 C.Clr(); 00195 InfectedNIdH.Clr(); 00196 InfectedBy.Clr(); 00197 00198 InitTime = TFlt::Rnd.GetUniDev() * TotalTime; // random starting point <TotalTime 00199 GlobalTime = InitTime; 00200 00201 StartNId = Network.GetRndNId(); 00202 InfectedNIdH.AddDat(StartNId) = GlobalTime; 00203 00204 while (true) { 00205 // sort by time & get the oldest node that did not run infection 00206 InfectedNIdH.SortByDat(true); 00207 const int& NId = InfectedNIdH.BegI().GetKey(); 00208 GlobalTime = InfectedNIdH.BegI().GetDat(); 00209 00210 // all the nodes has run infection 00211 if ( GlobalTime >= TFlt::GetMn(TotalTime, InitTime+Window) ) 00212 break; 00213 00214 // add current oldest node to the network and set its time 00215 C.Add(NId, GlobalTime); 00216 00217 if (verbose) { printf("GlobalTime:%f, infected node:%d\n", GlobalTime, NId); } 00218 00219 // run infection from the current oldest node 00220 TStrFltFltHNEDNet::TNodeI NI = Network.GetNI(NId); 00221 for (int e = 0; e < NI.GetOutDeg(); e++) { 00222 const int DstNId = NI.GetOutNId(e); 00223 00224 // choose the current tx rate (we assume the most recent tx rate) 00225 TFltFltH& Alphas = Network.GetEDat(NId, DstNId); 00226 for (int j=0; j<Alphas.Len() && Alphas.GetKey(j)<GlobalTime; j++) { alpha = Alphas[j]; } 00227 if (verbose) { printf("GlobalTime:%f, nodes:%d->%d, alpha:%f\n", GlobalTime, NId, DstNId, alpha); } 00228 00229 if (alpha<1e-9) { continue; } 00230 00231 // not infecting the parent 00232 if (InfectedBy.IsKey(NId) && InfectedBy.GetDat(NId).Val == DstNId) 00233 continue; 00234 00235 double sigmaT; 00236 switch (Model) { 00237 case EXP: 00238 // exponential with alpha parameter 00239 sigmaT = TInt::Rnd.GetExpDev(alpha); 00240 break; 00241 case POW: 00242 // power-law with alpha parameter 00243 sigmaT = TInt::Rnd.GetPowerDev(1+alpha); 00244 while (sigmaT < Delta) { sigmaT = Delta*TInt::Rnd.GetPowerDev(1+alpha); } 00245 break; 00246 case RAY: 00247 // rayleigh with alpha parameter 00248 sigmaT = TInt::Rnd.GetRayleigh(1/sqrt(alpha)); 00249 break; 00250 default: 00251 sigmaT = 1; 00252 break; 00253 } 00254 00255 IAssert(sigmaT >= 0); 00256 00257 double t1 = TFlt::GetMn(GlobalTime + sigmaT, TFlt::GetMn(InitTime+Window, TotalTime)); 00258 00259 if (InfectedNIdH.IsKey(DstNId)) { 00260 double t2 = InfectedNIdH.GetDat(DstNId); 00261 if ( t2 > t1 && t2 < TFlt::GetMn(InitTime+Window, TotalTime)) { 00262 InfectedNIdH.GetDat(DstNId) = t1; 00263 InfectedBy.GetDat(DstNId) = NId; 00264 } 00265 } else { 00266 InfectedNIdH.AddDat(DstNId) = t1; 00267 InfectedBy.AddDat(DstNId) = NId; 00268 } 00269 } 00270 00271 // we cannot delete key (otherwise, we cannot sort), so we assign a big time (InitTime + window cut-off) 00272 InfectedNIdH.GetDat(NId) = TFlt::GetMn(InitTime+Window, TotalTime); 00273 } 00274 } 00275 00276 C.Sort(); 00277 } 00278 00279 void TNIBs::GetGroundTruthGraphAtT(const double& Step, PNGraph &GraphAtT) { 00280 GraphAtT = TNGraph::New(); 00281 00282 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); } 00283 00284 for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) { 00285 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 00286 double Alpha = 0.0; 00287 if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); } 00288 00289 if (Alpha > MinAlpha) { 00290 GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId()); 00291 } 00292 } 00293 } 00294 00295 void TNIBs::GetGroundTruthNetworkAtT(const double& Step, PStrFltNEDNet& NetworkAtT) { 00296 NetworkAtT = TStrFltNEDNet::New(); 00297 00298 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name); } 00299 00300 for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) { 00301 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 00302 double Alpha = 0.0; 00303 if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); } 00304 00305 if (Alpha > MinAlpha) { 00306 NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), Alpha); 00307 } 00308 } 00309 } 00310 00311 void TNIBs::GetInferredGraphAtT(const double& Step, PNGraph &GraphAtT) { 00312 GraphAtT = TNGraph::New(); 00313 00314 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); } 00315 00316 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00317 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 00318 00319 double inferredAlpha = 0.0; 00320 if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); } 00321 00322 if (inferredAlpha > MinAlpha) { 00323 GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId()); 00324 } 00325 } 00326 } 00327 00328 void TNIBs::GetInferredNetworkAtT(const double& Step, PStrFltNEDNet& NetworkAtT) { 00329 NetworkAtT = TStrFltNEDNet::New(); 00330 00331 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 00332 NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name); 00333 } 00334 00335 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00336 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 00337 00338 double inferredAlpha = 0.0; 00339 if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); } 00340 00341 if (inferredAlpha > MinAlpha) { 00342 NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), inferredAlpha); 00343 } 00344 } 00345 } 00346 00347 void TNIBs::Init(const TFltV& Steps) { 00348 // inferred network 00349 InferredNetwork.Clr(); 00350 00351 // copy nodes from NodeNmH (it will be filled by loading cascades or loading groundtruth) 00352 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 00353 InferredNetwork.AddNode(NI.GetKey(), NI.GetDat().Name); 00354 } 00355 00356 // performance measures 00357 PrecisionRecall.Clr(); 00358 Accuracy.Clr(); 00359 MAE.Clr(); 00360 TotalCascadesAlpha.Clr(); 00361 00362 for (int i=0; i<Steps.Len()-1; i++) { 00363 MAE.Add(TFltPr(Steps[i], 0.0)); 00364 MSE.Add(TFltPr(Steps[i], 0.0)); 00365 Accuracy.Add(TFltPr(Steps[i], 0.0)); 00366 PrecisionRecall.Add(TFltPr(0.0,0.0)); 00367 } 00368 } 00369 00370 void TNIBs::Reset() { 00371 // reset vectors 00372 for (int i=0; i<DiffAlphas.Len(); i++) { 00373 DiffAlphas[i].Clr(); 00374 } 00375 DiffAlphas.Clr(); 00376 AveDiffAlphas.Clr(); 00377 SampledCascadesH.Clr(); 00378 TotalCascadesAlpha.Clr(); 00379 } 00380 00381 void TNIBs::SG(const int& NId, const int& Iters, const TFltV& Steps, const TSampling& Sampling, const TStr& ParamSampling, const bool& PlotPerformance) { 00382 bool verbose = false; 00383 int currentCascade = -1; 00384 TIntIntH SampledCascades; 00385 TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV); 00386 00387 Reset(); 00388 00389 printf("Node %d\n", NId); 00390 00391 // traverse through all times 00392 for (int t=1; t<Steps.Len(); t++) { 00393 // find cascades whose two first infections are earlier than Steps[t] 00394 TIntFltH CascadesIdx; 00395 int num_infections = 0; 00396 for (int i=0; i<CascH.Len(); i++) { 00397 if (CascH[i].LenBeforeT(Steps[t]) > 1 && 00398 ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) || 00399 (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) || 00400 (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) { 00401 num_infections += CascH[i].LenBeforeT(Steps[t]); 00402 CascadesIdx.AddDat(i) = CascH[i].GetMinTm(); 00403 } 00404 } 00405 00406 // if there are not recorded cascades by Steps[t], continue 00407 if (CascadesIdx.Len()==0) { 00408 printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val); 00409 if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); } 00410 continue; 00411 } 00412 00413 // later cascades first 00414 CascadesIdx.SortByDat(false); 00415 00416 printf("Solving step %f: %d cascades, %d infections\n", Steps[t].Val, CascadesIdx.Len(), num_infections); 00417 SampledCascades.Clr(); 00418 00419 // sampling cascades with no replacement 00420 for (int i=0; i < Iters; i++) { 00421 switch (Sampling) { 00422 case UNIF_SAMPLING: 00423 currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len()); 00424 break; 00425 00426 case WIN_SAMPLING: 00427 currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len()); 00428 break; 00429 00430 case EXP_SAMPLING: 00431 do { 00432 currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt()); 00433 } while (currentCascade > CascadesIdx.Len()-1); 00434 break; 00435 00436 case WIN_EXP_SAMPLING: 00437 do { 00438 currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt()); 00439 } while (currentCascade > CascadesIdx.Len()-1); 00440 break; 00441 00442 case RAY_SAMPLING: 00443 do { 00444 currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt()); 00445 } while (currentCascade > CascadesIdx.Len()-1); 00446 break; 00447 } 00448 00449 if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; } 00450 SampledCascades.GetDat(currentCascade)++; 00451 00452 if (verbose) { printf("Cascade %d sampled!\n", currentCascade); } 00453 00454 // sampled cascade 00455 TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)]; 00456 00457 // update gradient and alpha's 00458 TIntPrV AlphasToUpdate; 00459 UpdateDiff(OSG, NId, Cascade, AlphasToUpdate, Steps[t]); 00460 00461 // update alpha's 00462 for (int j=0; j<AlphasToUpdate.Len(); j++) { 00463 if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) && 00464 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t]) 00465 ) { 00466 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -= 00467 (Gamma * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1) 00468 - (Regularizer==1? Mu*InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) : 0.0)); 00469 00470 // project into alpha >= 0 00471 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) { 00472 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol; 00473 } 00474 00475 // project into alpha <= MaxAlpha 00476 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) { 00477 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha; 00478 } 00479 } 00480 } 00481 if (verbose) { printf("%d transmission rates updated!\n", AlphasToUpdate.Len()); } 00482 } 00483 00484 printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val); 00485 00486 // For alphas that did not get updated, copy last alpha value * aging factor 00487 int unchanged = 0; 00488 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00489 if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; } 00490 00491 EI().AddDat(Steps[t]) = Aging*EI().GetDat(Steps[t-1]); 00492 unchanged++; 00493 } 00494 if (verbose) { printf("%d transmission rates that did not changed were 'aged' by %f!\n", unchanged, Aging.Val); } 00495 00496 // compute performance on-the-fly 00497 if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); } 00498 } 00499 } 00500 00501 void TNIBs::BSG(const int& NId, const int& Iters, const TFltV& Steps, const int& BatchLen, const TSampling& Sampling, const TStr& ParamSampling, const bool& PlotPerformance) { 00502 bool verbose = false; 00503 int currentCascade = -1; 00504 TIntIntH SampledCascades; 00505 TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV); 00506 00507 Reset(); 00508 00509 printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes()); 00510 00511 // traverse through all times (except 0.0; we use 0.0 to compute mse/mae since the inference is delayed) 00512 for (int t=1; t<Steps.Len(); t++) { 00513 // find cascades whose two first infections are earlier than Steps[t] 00514 TIntFltH CascadesIdx; 00515 int num_infections = 0; 00516 for (int i = 0; i < CascH.Len(); i++) { 00517 if (CascH[i].LenBeforeT(Steps[t]) > 1 && 00518 ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) || 00519 (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) || 00520 (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) { 00521 num_infections += CascH[i].LenBeforeT(Steps[t]); 00522 CascadesIdx.AddDat(i) = CascH[i].GetMinTm(); 00523 } 00524 } 00525 00526 // if there are not recorded cascades by Steps[t], continue 00527 if (CascadesIdx.Len() == 0) { 00528 printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val); 00529 if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); } 00530 continue; 00531 } 00532 00533 printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val, 00534 CascadesIdx.Len(), num_infections); 00535 00536 // sort cascade from most recent to least recent 00537 CascadesIdx.SortByDat(false); 00538 00539 // sampling cascades with no replacement 00540 for (int i=0; i < Iters; i++) { 00541 // alphas to update 00542 TIntPrV AlphasToUpdate; 00543 00544 // delete average gradients 00545 AveDiffAlphas.Clr(); 00546 00547 // use all cascades for the average gradient 00548 for (int c=0; c<BatchLen; c++) { 00549 switch (Sampling) { 00550 case UNIF_SAMPLING: 00551 currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len()); 00552 break; 00553 00554 case WIN_SAMPLING: 00555 currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len()); 00556 break; 00557 00558 case EXP_SAMPLING: 00559 do { 00560 currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt()); 00561 } while (currentCascade > CascadesIdx.Len()-1); 00562 break; 00563 00564 case WIN_EXP_SAMPLING: 00565 do { 00566 currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt()); 00567 } while (currentCascade > CascadesIdx.Len()-1); 00568 break; 00569 00570 case RAY_SAMPLING: 00571 do { 00572 currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt()); 00573 } while (currentCascade > CascadesIdx.Len()-1); 00574 break; 00575 } 00576 00577 // sampled cascade 00578 TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)]; 00579 00580 if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; } 00581 SampledCascades.GetDat(currentCascade)++; 00582 00583 // update gradient and alpha's 00584 UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]); 00585 } 00586 00587 // update alpha's 00588 for (int j=0; j<AlphasToUpdate.Len(); j++) { 00589 if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) && 00590 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) { 00591 switch (Regularizer) { 00592 case 0: 00593 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -= 00594 Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1); 00595 case 1: 00596 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = 00597 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)BatchLen) 00598 - Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1); 00599 } 00600 00601 // project into alpha >= 0 00602 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) { 00603 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol; 00604 } 00605 00606 // project into alpha <= MaxAlpha 00607 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) { 00608 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha; 00609 } 00610 } 00611 } 00612 00613 // for alphas that did not get updated, copy last alpha value 00614 int unchanged = 0; 00615 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00616 if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; } 00617 00618 EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]); 00619 unchanged++; 00620 } 00621 if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); } 00622 } 00623 00624 printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val); 00625 00626 // compute performance on-the-fly for each time step 00627 if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); } 00628 } 00629 } 00630 00631 void TNIBs::FG(const int& NId, const int& Iters, const TFltV& Steps) { 00632 bool verbose = false; 00633 00634 Reset(); 00635 00636 printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes()); 00637 00638 // traverse through all times 00639 for (int t=1; t<Steps.Len(); t++) { 00640 // find cascades whose two first infections are earlier than Steps[t] 00641 TIntFltH CascadesIdx; 00642 int num_infections = 0; 00643 for (int i=0; i<CascH.Len(); i++) { 00644 if (CascH[i].LenBeforeT(Steps[t]) > 1) { 00645 num_infections += CascH[i].LenBeforeT(Steps[t]); 00646 CascadesIdx.AddDat(i) = CascH[i].GetMinTm(); 00647 } 00648 } 00649 00650 // if there are not recorded cascades by Steps[t], continue 00651 if (CascadesIdx.Len()==0) { 00652 printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val); 00653 // ComputePerformance(NId, Tol, t, Steps); 00654 continue; 00655 } 00656 00657 printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val, CascadesIdx.Len(), num_infections); 00658 00659 // sort cascade from most recent to least recent 00660 CascadesIdx.SortByDat(false); 00661 00662 // sampling cascades with no replacement 00663 for (int i=0; i < Iters; i++) { 00664 // alphas to update 00665 TIntPrV AlphasToUpdate; 00666 00667 // delete average gradients 00668 AveDiffAlphas.Clr(); 00669 00670 // use all cascades for the average gradient 00671 for (int c=0; c<CascadesIdx.Len(); c++) { 00672 // each cascade 00673 TCascade &Cascade = CascH[CascadesIdx.GetKey(c)]; 00674 00675 // update gradient and alpha's 00676 UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]); 00677 } 00678 00679 // update alpha's 00680 for (int j=0; j<AlphasToUpdate.Len(); j++) { 00681 if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) && 00682 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) { 00683 switch (Regularizer) { 00684 case 0: 00685 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -= 00686 Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1); 00687 case 1: 00688 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = 00689 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)CascadesIdx.Len()) 00690 - Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1); 00691 } 00692 00693 // project into alpha >= 0 00694 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) { 00695 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol; 00696 } 00697 00698 // project into alpha <= MaxAlpha 00699 if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) { 00700 InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha; 00701 } 00702 } 00703 } 00704 00705 // for alphas that did not get updated, copy last alpha value 00706 int unchanged = 0; 00707 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00708 if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; } 00709 00710 EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]); 00711 unchanged++; 00712 } 00713 if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); } 00714 } 00715 00716 // compute performance on-the-fly for each time step 00717 ComputePerformanceNId(NId, t, Steps); 00718 } 00719 } 00720 00721 void TNIBs::UpdateDiff(const TOptMethod& OptMethod, const int& NId, TCascade& Cascade, TIntPrV& AlphasToUpdate, const double& CurrentTime) { 00722 IAssert(InferredNetwork.IsNode(NId)); 00723 00724 double sum = 0.0; 00725 00726 // we assume cascade is sorted & iterator returns in sorted order 00727 if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) { 00728 for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) { 00729 // consider only nodes that are earlier in time 00730 if ( (Cascade.GetTm(NId)<=NI.GetDat().Tm) || 00731 (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW) 00732 ) { break; } 00733 00734 TIntPr Pair(NI.GetKey(), NId); 00735 00736 // if edge/alpha doesn't exist, create 00737 if (!InferredNetwork.IsEdge(Pair.Val1, Pair.Val2)) { InferredNetwork.AddEdge(Pair.Val1, Pair.Val2, TFltFltH()); } 00738 if (!InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).IsKey(CurrentTime)) { 00739 InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).AddDat(CurrentTime) = InitAlpha; 00740 } 00741 00742 switch(Model) { 00743 case EXP: // exponential 00744 sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val; 00745 break; 00746 case POW: // powerlaw 00747 sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val/(Cascade.GetTm(NId)-NI.GetDat().Tm); 00748 break; 00749 case RAY: // rayleigh 00750 sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val*(Cascade.GetTm(NId)-NI.GetDat().Tm); 00751 break; 00752 default: 00753 sum = 0.0; 00754 } 00755 } 00756 } 00757 00758 // we assume cascade is sorted & iterator returns in sorted order 00759 for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) { 00760 // only consider nodes that are earlier in time if node belongs to the cascade 00761 if ( Cascade.IsNode(NId) && (Cascade.GetTm(NId)<=NI.GetDat().Tm || 00762 (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW)) 00763 ) { break; } 00764 00765 // consider infected nodes up to CurrentTime 00766 if (NI.GetDat().Tm > CurrentTime) { break; } 00767 00768 TIntPr Pair(NI.GetKey(), NId); // potential edge 00769 00770 double val = 0.0; 00771 00772 if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) { 00773 IAssert((Cascade.GetTm(NId) - NI.GetDat().Tm) > 0.0); 00774 00775 switch(Model) { // compute gradients for infected 00776 case EXP: // exponential 00777 val = (Cascade.GetTm(NId) - NI.GetDat().Tm) - 1.0/sum; 00778 break; 00779 case POW: // powerlaw 00780 val = log((Cascade.GetTm(NId) - NI.GetDat().Tm)/Delta) - 1.0/((Cascade.GetTm(NId)-NI.GetDat().Tm)*sum); 00781 break; 00782 case RAY: // rayleigh 00783 val = TMath::Power(Cascade.GetTm(NId)-NI.GetDat().Tm, 2.0)/2.0 - (Cascade.GetTm(NId)-NI.GetDat().Tm)/sum; 00784 break; 00785 default: 00786 val = 0.0; 00787 } 00788 } else { // compute gradients for non infected 00789 IAssert((CurrentTime - NI.GetDat().Tm) >= 0.0); 00790 00791 switch(Model) { 00792 case EXP: // exponential 00793 val = (CurrentTime-NI.GetDat().Tm); 00794 // if every cascade was recorded up to a maximum Window cut-off 00795 if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = (Cascade.GetMinTm()+Window-NI.GetDat().Tm); } 00796 break; 00797 case POW: // power-law 00798 val = TMath::Mx(log((CurrentTime-NI.GetDat().Tm)/Delta), 0.0); 00799 // if every cascade was recorded up to a maximum Window cut-off 00800 if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Mx(log((Cascade.GetMinTm()+Window-NI.GetDat().Tm)/Delta), 0.0); } 00801 break; 00802 case RAY: // rayleigh 00803 val = TMath::Power(CurrentTime-NI.GetDat().Tm,2.0)/2.0; 00804 // if every cascade was recorded up to a maximum Window cut-off 00805 if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Power(Cascade.GetMinTm()+Window-NI.GetDat().Tm,2.0)/2.0; } 00806 break; 00807 default: 00808 val = 0.0; 00809 } 00810 } 00811 00812 if (!AveDiffAlphas.IsKey(Pair.Val1)) { AveDiffAlphas.AddDat(Pair.Val1) = 0.0; } 00813 00814 switch (OptMethod) { 00815 case OBSG: 00816 case OEBSG: 00817 case OFG: 00818 // update stochastic average gradient (based on batch for OBSG and OEBSG and based on all cascades for FG) 00819 AveDiffAlphas.GetDat(Pair.Val1) += val; 00820 break; 00821 case OSG: 00822 case OESG: 00823 // update stochastic gradient (we use a single gradient due to current cascade) 00824 AveDiffAlphas.GetDat(Pair.Val1) = val; 00825 default: 00826 break; 00827 } 00828 00829 AlphasToUpdate.Add(Pair); 00830 } 00831 00832 return; 00833 } 00834 00835 void TNIBs::find_C( int t, TFltV &x, TFltVV &C, const int& k, const double& s, const double& gamma, const double& T ){ 00836 if ( t >= x.Len() ) return; 00837 if ( t == 0 ){ 00838 C = TFltVV( x.Len(), k ); 00839 }else{ 00840 int n = x.Len() - 1; 00841 for (int j = 0; j < k; j++){ 00842 double alpha = ( (x.Len() ) / T ) * pow( s, j ); 00843 double term_1 = -log(alpha) + alpha * x[t]; 00844 double term_2 = 0; 00845 if ( t == 1 ){ 00846 term_2 = j * log(n) * gamma; 00847 } 00848 else{ 00849 bool first = false; 00850 for (int l = 0; l < k; l++){ 00851 double my_val = C(t-1, l); 00852 if ( j > l ) my_val += (j - l) * log(n) * gamma; 00853 if ( !first || my_val < term_2 ){ 00854 term_2 = my_val; 00855 first = true; 00856 } 00857 } 00858 } 00859 C( t, j ) = term_1 + term_2; 00860 } 00861 } 00862 find_C( t + 1, x, C, k, s, gamma, T ); 00863 } 00864 00865 void TNIBs::find_min_state( TFltVV &C, TIntV &states, const int& k, const double& s, const double& gamma, const double& T ){ 00866 states = TIntV( C.GetRows() ); 00867 states[0] = 0; 00868 int n = C.GetRows() - 1; 00869 for (int t = C.GetRows() - 1; t > 0; t --){ 00870 double best_val = 0; 00871 int best_state = -1; 00872 for (int j = 0; j < C.GetCols(); j++){ 00873 double c_state = C( t, j ); 00874 if ( t < C.GetRows() - 2 && states[t+1] > j ){ 00875 c_state += ( states[t+1] - j ) * gamma * log(n); 00876 } 00877 if ( best_state == -1 || best_val > c_state ){ 00878 best_state = j; 00879 best_val = c_state; 00880 } 00881 } 00882 states[t] = best_state; 00883 } 00884 } 00885 00886 void TNIBs::LabelBurstAutomaton( const int& SrcId, const int& DstId, TIntV &state_labels, TFltV &state_times, const bool& inferred, const int& k, const double& s, const double& gamma, const TSecTm& MinTime, const TSecTm& MaxTime ){ 00887 TVec<TSecTm> arrival_times; 00888 TFltFltH &LinksEdge = (inferred? InferredNetwork.GetEDat(SrcId, DstId) : Network.GetEDat(SrcId, DstId)); 00889 00890 for (int i=0; i<LinksEdge.Len(); i++) { 00891 if (LinksEdge[i]>MinAlpha) { 00892 TSecTm tsecs; 00893 tsecs = (uint)LinksEdge.GetKey(i); // link rates is in seconds 00894 if (tsecs > MaxTime || tsecs < MinTime) { continue; } 00895 arrival_times.Add(tsecs); 00896 } 00897 } 00898 00899 if ( arrival_times.Len() < 2 ) return; 00900 00901 TFltV x; 00902 x.Add( 0 ); 00903 arrival_times.Sort(true); 00904 double T = ((double)arrival_times.Last().GetAbsSecs()) - ((double)arrival_times[0].GetAbsSecs()); 00905 for (int i = 1; i < arrival_times.Len(); i++){ 00906 x.Add( ((double)arrival_times[i].GetAbsSecs()) - ((double)arrival_times[i-1].GetAbsSecs()) ); 00907 } 00908 TFltVV Cost_matrix; 00909 //printf("arrivals = %d\n", x.Len() ); 00910 find_C( 0, x, Cost_matrix, k, s, gamma, T); 00911 00912 find_min_state( Cost_matrix, state_labels, k, s, gamma, T ); 00913 00914 for (int i=0; i<state_labels.Len(); i++) { state_times.Add((double)arrival_times[i].GetAbsSecs()); } 00915 } 00916 00917 00918 void TNIBs::ComputePerformanceNId(const int& NId, const int& t, const TFltV& Steps) { 00919 double CurrentMAE = 0.0; 00920 double CurrentMSE = 0.0; 00921 TFltPr CurrentPrecisionRecall(0.0, 0.0); 00922 double CurrentAccD = 0.0; 00923 00924 TStrFltFltHNEDNet::TNodeI NI = InferredNetwork.GetNI(NId); 00925 for (int i=0; i<NI.GetInDeg(); i++) { 00926 double inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]); 00927 // we get the true alpha in the previous step (the inferred network contains delayed estimates) 00928 double trueAlpha = 0.0; 00929 if (Network.IsEdge(NI.GetInNId(i), NId) && Network.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t-1])) { trueAlpha = Network.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t-1]); } 00930 00931 // accuracy (denominator) 00932 CurrentAccD += (double) (inferredAlpha > MinAlpha); 00933 00934 // precision 00935 CurrentPrecisionRecall.Val2 += (double) (inferredAlpha > MinAlpha && trueAlpha<MinAlpha); 00936 } 00937 00938 NI = Network.GetNI(NId); 00939 int NumEdges = 0; 00940 for (int i=0; i<NI.GetInDeg(); i++) { 00941 TIntPr Pair(NI.GetInNId(i), NId); 00942 00943 // we get the inferred Alpha if any 00944 double inferredAlpha = 0.0; 00945 if (InferredNetwork.IsEdge(NI.GetInNId(i), NId) && InferredNetwork.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t])) { 00946 inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]).Val; 00947 } 00948 00949 // we get the true alpha in the previous step (the inferred network contains delayed estimates) 00950 double trueAlpha = 0.0; 00951 if (Network.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t-1])) { trueAlpha = Network.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t-1]); } 00952 00953 // mae 00954 if (trueAlpha > MinAlpha) { 00955 NumEdges++; 00956 CurrentMAE += fabs(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha))/trueAlpha; 00957 } 00958 00959 // mse 00960 CurrentMSE += pow(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha), 2.0); 00961 00962 // recall 00963 CurrentPrecisionRecall.Val1 += (double) (inferredAlpha > MinAlpha && trueAlpha > MinAlpha); 00964 } 00965 00966 if (NumEdges > 0) { 00967 MAE[t-1].Val2 += CurrentMAE / ((double)(NumEdges*Network.GetNodes())); 00968 MSE[t-1].Val2 += CurrentMSE / ((double)(NumEdges*Network.GetNodes())); 00969 PrecisionRecall[t-1].Val1 += CurrentPrecisionRecall.Val1/(double)(NumEdges*Network.GetNodes()); 00970 } 00971 00972 if (CurrentAccD > 0) { 00973 PrecisionRecall[t-1].Val2 += (1.0 - CurrentPrecisionRecall.Val2 / CurrentAccD)/(double)Network.GetNodes(); 00974 } else { 00975 PrecisionRecall[t-1].Val2 += 1.0/(double)Network.GetNodes(); 00976 } 00977 00978 Accuracy[t-1].Val2 = 1.0 - (1.0-PrecisionRecall[t-1].Val2)/(PrecisionRecall[t-1].Val2 * (1.0/PrecisionRecall[t-1].Val2 + 1.0/PrecisionRecall[t-1].Val1)) - (1.0-PrecisionRecall[t-1].Val1)/(PrecisionRecall[t-1].Val1* (1.0/PrecisionRecall[t-1].Val2 + 1.0/PrecisionRecall[t-1].Val1)); 00979 } 00980 00981 void TNIBs::SaveInferredPajek(const TStr& OutFNm, const double& Step, const TIntV& NIdV) { 00982 TFOut FOut(OutFNm); 00983 FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len())); 00984 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 00985 if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; } 00986 00987 FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr())); 00988 } 00989 FOut.PutStr("*Arcs\r\n"); 00990 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 00991 if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; } 00992 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 00993 00994 double inferredAlpha = 0.0; 00995 if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); } 00996 00997 if (inferredAlpha > MinAlpha) { 00998 FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha))); 00999 } 01000 } 01001 } 01002 01003 void TNIBs::SaveInferred(const TStr& OutFNm, const TIntV& NIdV) { 01004 TFOut FOut(OutFNm); 01005 01006 // write nodes to file 01007 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01008 if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; } 01009 01010 FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr())); 01011 } 01012 01013 FOut.PutStr("\r\n"); 01014 01015 // write edges to file (not allowing self loops in the network) 01016 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 01017 if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; } 01018 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 01019 01020 // not allowing self loops in the Kronecker network 01021 if (EI.GetSrcNId() != EI.GetDstNId()) { 01022 if (EI().Len() > 0) { 01023 TStr Line; bool IsEdge = false; 01024 for (int i=0; i<EI().Len(); i++) { 01025 if (EI()[i]>MinAlpha) { 01026 Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) ); 01027 IsEdge = true; 01028 } else { // we write 0 explicitly 01029 Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val); 01030 } 01031 } 01032 // if none of the alphas is bigger than 0, no edge is written 01033 if (IsEdge) { 01034 FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId())); 01035 FOut.PutStr(Line); 01036 FOut.PutStr("\r\n"); 01037 } 01038 } 01039 else 01040 FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId())); 01041 } 01042 } 01043 } 01044 01045 void TNIBs::SaveInferred(const TStr& OutFNm, const double& Step, const TIntV& NIdV) { 01046 TFOut FOut(OutFNm); 01047 01048 // write nodes to file 01049 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01050 if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; } 01051 01052 FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr())); 01053 } 01054 FOut.PutStr("\r\n"); 01055 01056 // write edges to file (not allowing self loops in the network) 01057 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 01058 if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; } 01059 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 01060 01061 // not allowing self loops in the Kronecker network 01062 if (EI.GetSrcNId() != EI.GetDstNId()) { 01063 double inferredAlpha = 0.0; 01064 if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); } 01065 01066 if (inferredAlpha > MinAlpha) { 01067 FOut.PutStr(TStr::Fmt("%d,%d,%f\r\n", EI.GetSrcNId(), EI.GetDstNId(), (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha))); 01068 } 01069 } 01070 } 01071 } 01072 01073 void TNIBs::SaveInferredEdges(const TStr& OutFNm) { 01074 TFOut FOut(OutFNm); 01075 01076 // write edges to file (not allowing self loops in the network) 01077 for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) { 01078 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 01079 01080 // not allowing self loops in the Kronecker network 01081 if (EI.GetSrcNId() != EI.GetDstNId()) { 01082 if (EI().Len() > 0) { 01083 TStr Line; bool IsEdge = false; 01084 for (int i=0; i<EI().Len(); i++) { 01085 if (EI()[i]>MinAlpha) { 01086 Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) ); 01087 IsEdge = true; 01088 } else { // we write 0 explicitly 01089 Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val); 01090 } 01091 } 01092 // if none of the alphas is bigger than 0, no edge is written 01093 if (IsEdge) { 01094 FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId())); 01095 FOut.PutStr(Line); 01096 FOut.PutStr("\r\n"); 01097 } 01098 } 01099 else 01100 FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId())); 01101 } 01102 } 01103 } 01104 01105 void TNIBs::SaveGroundTruth(const TStr& OutFNm) { 01106 TFOut FOut(OutFNm); 01107 01108 // write nodes to file 01109 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01110 FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr())); 01111 } 01112 01113 FOut.PutStr("\r\n"); 01114 01115 // write edges to file (not allowing self loops in the network) 01116 for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) { 01117 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 01118 01119 // not allowing self loops in the Kronecker network 01120 if (EI.GetSrcNId() != EI.GetDstNId()) { 01121 if (EI().Len() > 0) { 01122 FOut.PutStr(TStr::Fmt("%d,%d,", EI.GetSrcNId(), EI.GetDstNId())); 01123 for (int i=0; i<EI().Len()-1; i++) { FOut.PutStr(TStr::Fmt("%f,%f,", EI().GetKey(i).Val, EI()[i].Val)); } 01124 FOut.PutStr(TStr::Fmt("%f,%f", EI().GetKey(EI().Len()-1).Val, EI()[EI().Len()-1].Val)); 01125 FOut.PutStr("\r\n"); 01126 } 01127 else 01128 FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId())); 01129 } 01130 } 01131 } 01132 01133 void TNIBs::SaveGroundTruthPajek(const TStr& OutFNm, const double& Step) { 01134 TFOut FOut(OutFNm); 01135 01136 FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len())); 01137 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01138 FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr())); 01139 } 01140 FOut.PutStr("*Arcs\r\n"); 01141 for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) { 01142 if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; } 01143 double trueAlpha = 0.0; 01144 if (EI().IsKey(Step)) { trueAlpha = EI().GetDat(Step); } 01145 else { for (int j=0; j<EI().Len() && EI().GetKey(j)<=Step; j++) { trueAlpha = EI()[j]; } } 01146 01147 if (trueAlpha > MinAlpha) { 01148 FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (trueAlpha > MaxAlpha? MaxAlpha.Val : trueAlpha))); 01149 } 01150 } 01151 } 01152 01153 void TNIBs::SaveSites(const TStr& OutFNm, const TIntFltVH& CascadesPerNode) { 01154 TFOut FOut(OutFNm); 01155 01156 // write nodes to file 01157 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01158 FOut.PutStr(TStr::Fmt("%d,%s", NI.GetKey().Val, NI.GetDat().Name.CStr())); 01159 if (CascadesPerNode.IsKey(NI.GetKey().Val)) { 01160 for (int i=0; i<CascadesPerNode.GetDat(NI.GetKey().Val).Len(); i++) { 01161 FOut.PutStr(TStr::Fmt(",%f", CascadesPerNode.GetDat(NI.GetKey().Val)[i].Val)); 01162 } 01163 } 01164 FOut.PutStr("\r\n"); 01165 } 01166 } 01167 01168 void TNIBs::SaveCascades(const TStr& OutFNm) { 01169 TFOut FOut(OutFNm); 01170 01171 // write nodes to file 01172 for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { 01173 FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr())); 01174 } 01175 01176 FOut.PutStr("\r\n"); 01177 01178 // write cascades to file 01179 for (THash<TInt, TCascade>::TIter CI = CascH.BegI(); CI < CascH.EndI(); CI++) { 01180 TCascade &C = CI.GetDat(); 01181 int j = 0; 01182 for (THash<TInt, THitInfo>::TIter NI = C.NIdHitH.BegI(); NI < C.NIdHitH.EndI(); NI++) { 01183 if (!NodeNmH.IsKey(NI.GetDat().NId)) { continue; } 01184 if (j > 0) { FOut.PutStr(TStr::Fmt(",%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); } 01185 else { FOut.PutStr(TStr::Fmt("%d;%d,%f", CI.GetKey().Val, NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); } 01186 j++; 01187 } 01188 01189 if (j >= 1) 01190 FOut.PutStr(TStr::Fmt("\r\n")); 01191 } 01192 }