SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TNIBs Class Reference

#include <cascdynetinf.h>

Collaboration diagram for TNIBs:

Public Member Functions

 TNIBs ()
 
 TNIBs (TSIn &SIn)
 
void Save (TSOut &SOut) const
 
void LoadCascadesTxt (TSIn &SIn)
 
void LoadGroundTruthTxt (TSIn &SIn)
 
void LoadGroundTruthNodesTxt (TSIn &SIn)
 
void LoadInferredTxt (TSIn &SIn)
 
void LoadInferredNodesTxt (TSIn &SIn)
 
void SetTotalTime (const float &tt)
 
void SetModel (const TModel &model)
 
void SetWindow (const double &window)
 
void SetDelta (const double &delta)
 
void SetK (const double &k)
 
void SetGamma (const double &gamma)
 
void SetAging (const double &aging)
 
void SetRegularizer (const TRegularizer &reg)
 
void SetMu (const double &mu)
 
void SetTolerance (const double &tol)
 
void SetMaxAlpha (const double &ma)
 
void SetMinAlpha (const double &ma)
 
void SetInitAlpha (const double &ia)
 
void AddCasc (const TStr &CascStr, const TModel &Model=EXP)
 
void AddCasc (const TCascade &Cascade)
 
void AddCasc (const TIntFltH &Cascade, const int &CId=-1, const TModel &Model=EXP)
 
void GenCascade (TCascade &C)
 
bool IsCascade (int c)
 
TCascadeGetCasc (int c)
 
int GetCascs ()
 
int GetCascadeId (const TStr &Cascade)
 
int GetNodes ()
 
void AddNodeNm (const int &NId, const TNodeInfo &Info)
 
TStr GetNodeNm (const int &NId) const
 
TNodeInfo GetNodeInfo (const int &NId) const
 
bool IsNodeNm (const int &NId) const
 
void SortNodeNmByVol (const bool &asc=false)
 
void AddDomainNm (const TStr &Domain, const int &DomainId=-1)
 
bool IsDomainNm (const TStr &Domain) const
 
int GetDomainId (const TStr &Domain)
 
void GetGroundTruthGraphAtT (const double &Step, PNGraph &GraphAtT)
 
void GetGroundTruthNetworkAtT (const double &Step, PStrFltNEDNet &NetworkAtT)
 
void GetInferredGraphAtT (const double &Step, PNGraph &GraphAtT)
 
void GetInferredNetworkAtT (const double &Step, PStrFltNEDNet &NetworkAtT)
 
void Reset ()
 
void Init (const TFltV &Steps)
 
void SG (const int &NId, const int &Iters, const TFltV &Steps, const TSampling &Sampling, const TStr &ParamSampling=TStr(""), const bool &PlotPerformance=false)
 
void BSG (const int &NId, const int &Iters, const TFltV &Steps, const int &BatchLen, const TSampling &Sampling, const TStr &ParamSampling=TStr(""), const bool &PlotPerformance=false)
 
void FG (const int &NId, const int &Iters, const TFltV &Steps)
 
void UpdateDiff (const TOptMethod &OptMethod, const int &NId, TCascade &Cascade, TIntPrV &AlphasToUpdate, const double &CurrentTime=TFlt::Mx)
 
void find_C (int t, TFltV &x, TFltVV &C, const int &k, const double &s, const double &gamma, const double &T)
 
void find_min_state (TFltVV &C, TIntV &states, const int &k, const double &s, const double &gamma, const double &T)
 
void LabelBurstAutomaton (const int &SrcId, const int &DstId, TIntV &state_labels, TFltV &state_times, const bool &inferred=false, const int &k=5, const double &s=2.0, const double &gamma=1.0, const TSecTm &MinTime=TSecTm(), const TSecTm &MaxTime=TSecTm())
 
void ComputePerformanceNId (const int &NId, const int &Step, const TFltV &Steps)
 
void SaveInferredPajek (const TStr &OutFNm, const double &Step, const TIntV &NIdV=TIntV())
 
void SaveInferred (const TStr &OutFNm, const TIntV &NIdV=TIntV())
 
void SaveInferred (const TStr &OutFNm, const double &Step, const TIntV &NIdV=TIntV())
 
void SaveInferredEdges (const TStr &OutFNm)
 
void SaveGroundTruthPajek (const TStr &OutFNm, const double &Step)
 
void SaveGroundTruth (const TStr &OutFNm)
 
void SaveSites (const TStr &OutFNm, const TIntFltVH &CascadesPerNode=TIntFltVH())
 
void SaveCascades (const TStr &OutFNm)
 

Public Attributes

THash< TInt, TCascadeCascH
 
THash< TInt, TNodeInfoNodeNmH
 
TStrIntH DomainsIdH
 
TStrIntH CascadeIdH
 
THash< TIntPr, TIntVCascPerEdge
 
TStrFltFltHNEDNet Network
 
TModel Model
 
TFlt Window
 
TFlt TotalTime
 
TFlt Delta
 
TFlt K
 
TFlt Gamma
 
TFlt Mu
 
TFlt Aging
 
TRegularizer Regularizer
 
TFlt Tol
 
TFlt MaxAlpha
 
TFlt MinAlpha
 
TFlt InitAlpha
 
TStrFltFltHNEDNet InferredNetwork
 
TIntFltH TotalCascadesAlpha
 
TIntFltH AveDiffAlphas
 
THash< TInt, TIntFltHDiffAlphas
 
TIntIntPrH SampledCascadesH
 
TFltPrV PrecisionRecall
 
TFltPrV Accuracy
 
TFltPrV MAE
 
TFltPrV MSE
 

Detailed Description

Definition at line 130 of file cascdynetinf.h.

Constructor & Destructor Documentation

TNIBs::TNIBs ( )
inline

Definition at line 173 of file cascdynetinf.h.

173 { }
TNIBs::TNIBs ( TSIn SIn)
inline

Definition at line 174 of file cascdynetinf.h.

References EXP, and Model.

174 : CascH(SIn), NodeNmH(SIn), CascPerEdge(SIn), InferredNetwork(SIn) { Model = EXP; }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
THash< TIntPr, TIntV > CascPerEdge
Definition: cascdynetinf.h:138
TModel Model
Definition: cascdynetinf.h:144

Member Function Documentation

void TNIBs::AddCasc ( const TStr CascStr,
const TModel Model = EXP 
)

Definition at line 147 of file cascdynetinf.cpp.

References TCascade::Add(), CascH, GetNodeInfo(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), TCascade::Sort(), TStr::SplitOnAllCh(), and TNodeInfo::Vol.

Referenced by AddCasc(), and LoadCascadesTxt().

147  {
148  int CId = CascH.Len();
149 
150  // support cascade id if any
151  TStrV FieldsV; CascStr.SplitOnAllCh(';', FieldsV);
152  if (FieldsV.Len()==2) { CId = FieldsV[0].GetInt(); }
153 
154  // read nodes
155  TStrV NIdV; FieldsV[FieldsV.Len()-1].SplitOnAllCh(',', NIdV);
156  TCascade C(CId, Model);
157  for (int i = 0; i < NIdV.Len(); i+=2) {
158  int NId;
159  double Tm;
160  NId = NIdV[i].GetInt();
161  Tm = NIdV[i+1].GetFlt();
162  GetNodeInfo(NId).Vol = GetNodeInfo(NId).Vol + 1;
163  C.Add(NId, Tm);
164  }
165  C.Sort();
166 
167  AddCasc(C);
168 }
TNodeInfo GetNodeInfo(const int &NId) const
Definition: cascdynetinf.h:217
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void AddCasc(const TStr &CascStr, const TModel &Model=EXP)
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
int Len() const
Definition: hash.h:228
TModel Model
Definition: cascdynetinf.h:144

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::AddCasc ( const TCascade Cascade)
inline

Definition at line 205 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::AddDat(), CascH, and TCascade::CId.

205 { CascH.AddDat(Cascade.CId) = Cascade; }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TInt CId
Definition: cascdynetinf.h:86
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

void TNIBs::AddCasc ( const TIntFltH Cascade,
const int &  CId = -1,
const TModel Model = EXP 
)

Definition at line 170 of file cascdynetinf.cpp.

References TCascade::Add(), AddCasc(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), GetNodeInfo(), and TCascade::Sort().

170  {
171  TCascade C(CId, Model);
172  for (TIntFltH::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
173  GetNodeInfo(NI.GetKey()).Vol = GetNodeInfo(NI.GetKey()).Vol + 1;
174  C.Add(NI.GetKey(), NI.GetDat());
175  }
176  C.Sort();
177  AddCasc(C);
178 }
TNodeInfo GetNodeInfo(const int &NId) const
Definition: cascdynetinf.h:217
TIter BegI() const
Definition: hash.h:213
void AddCasc(const TStr &CascStr, const TModel &Model=EXP)
TIter EndI() const
Definition: hash.h:218
TModel Model
Definition: cascdynetinf.h:144

Here is the call graph for this function:

void TNIBs::AddDomainNm ( const TStr Domain,
const int &  DomainId = -1 
)
inline

Definition at line 222 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::AddDat(), DomainsIdH, and THash< TKey, TDat, THashFunc >::Len().

222 { DomainsIdH.AddDat(Domain) = TInt(DomainId==-1? DomainsIdH.Len() : DomainId); }
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
Definition: dt.h:1134
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

void TNIBs::AddNodeNm ( const int &  NId,
const TNodeInfo Info 
)
inline

Definition at line 215 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::AddDat(), and NodeNmH.

Referenced by LoadCascadesTxt(), LoadGroundTruthNodesTxt(), LoadGroundTruthTxt(), LoadInferredNodesTxt(), and LoadInferredTxt().

215 { NodeNmH.AddDat(NId, Info); }
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TDat & AddDat(const TKey &Key)
Definition: hash.h:238

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::BSG ( const int &  NId,
const int &  Iters,
const TFltV Steps,
const int &  BatchLen,
const TSampling Sampling,
const TStr ParamSampling = TStr(""),
const bool &  PlotPerformance = false 
)

Definition at line 501 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), AveDiffAlphas, TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), CascH, THash< TKey, TDat, THashFunc >::Clr(), ComputePerformanceNId(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), EXP_SAMPLING, Gamma, THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), THash< TKey, TDat, THashFunc >::GetKey(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), TRnd::GetUniDevInt(), InferredNetwork, TNodeEDatNet< TNodeData, TEdgeData >::IsEdge(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, Mu, OBSG, RAY_SAMPLING, Regularizer, Reset(), TInt::Rnd, TFlt::Rnd, THash< TKey, TDat, THashFunc >::SortByDat(), TStr::SplitOnAllCh(), Tol, UNIF_SAMPLING, UpdateDiff(), WIN_EXP_SAMPLING, and WIN_SAMPLING.

501  {
502  bool verbose = false;
503  int currentCascade = -1;
504  TIntIntH SampledCascades;
505  TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV);
506 
507  Reset();
508 
509  printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes());
510 
511  // traverse through all times (except 0.0; we use 0.0 to compute mse/mae since the inference is delayed)
512  for (int t=1; t<Steps.Len(); t++) {
513  // find cascades whose two first infections are earlier than Steps[t]
514  TIntFltH CascadesIdx;
515  int num_infections = 0;
516  for (int i = 0; i < CascH.Len(); i++) {
517  if (CascH[i].LenBeforeT(Steps[t]) > 1 &&
518  ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) ||
519  (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) ||
520  (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) {
521  num_infections += CascH[i].LenBeforeT(Steps[t]);
522  CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
523  }
524  }
525 
526  // if there are not recorded cascades by Steps[t], continue
527  if (CascadesIdx.Len() == 0) {
528  printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
529  if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
530  continue;
531  }
532 
533  printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val,
534  CascadesIdx.Len(), num_infections);
535 
536  // sort cascade from most recent to least recent
537  CascadesIdx.SortByDat(false);
538 
539  // sampling cascades with no replacement
540  for (int i=0; i < Iters; i++) {
541  // alphas to update
542  TIntPrV AlphasToUpdate;
543 
544  // delete average gradients
545  AveDiffAlphas.Clr();
546 
547  // use all cascades for the average gradient
548  for (int c=0; c<BatchLen; c++) {
549  switch (Sampling) {
550  case UNIF_SAMPLING:
551  currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
552  break;
553 
554  case WIN_SAMPLING:
555  currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
556  break;
557 
558  case EXP_SAMPLING:
559  do {
560  currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt());
561  } while (currentCascade > CascadesIdx.Len()-1);
562  break;
563 
564  case WIN_EXP_SAMPLING:
565  do {
566  currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt());
567  } while (currentCascade > CascadesIdx.Len()-1);
568  break;
569 
570  case RAY_SAMPLING:
571  do {
572  currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt());
573  } while (currentCascade > CascadesIdx.Len()-1);
574  break;
575  }
576 
577  // sampled cascade
578  TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)];
579 
580  if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; }
581  SampledCascades.GetDat(currentCascade)++;
582 
583  // update gradient and alpha's
584  UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]);
585  }
586 
587  // update alpha's
588  for (int j=0; j<AlphasToUpdate.Len(); j++) {
589  if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
590  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) {
591  switch (Regularizer) {
592  case 0:
593  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
594  Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
595  case 1:
596  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) =
597  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)BatchLen)
598  - Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
599  }
600 
601  // project into alpha >= 0
602  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
603  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
604  }
605 
606  // project into alpha <= MaxAlpha
607  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
608  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
609  }
610  }
611  }
612 
613  // for alphas that did not get updated, copy last alpha value
614  int unchanged = 0;
615  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
616  if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }
617 
618  EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]);
619  unchanged++;
620  }
621  if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); }
622  }
623 
624  printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val);
625 
626  // compute performance on-the-fly for each time step
627  if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
628  }
629 }
TFlt Mu
Definition: cascdynetinf.h:153
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
static TRnd Rnd
Definition: dt.h:1143
TIntFltH AveDiffAlphas
Definition: cascdynetinf.h:162
TFlt Gamma
Definition: cascdynetinf.h:153
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
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 network.
Definition: network.h:939
void ComputePerformanceNId(const int &NId, const int &Step, const TFltV &Steps)
void Reset()
void UpdateDiff(const TOptMethod &OptMethod, const int &NId, TCascade &Cascade, TIntPrV &AlphasToUpdate, const double &CurrentTime=TFlt::Mx)
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
Definition: hash.h:97
TRegularizer Regularizer
Definition: cascdynetinf.h:154
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
static TRnd Rnd
Definition: dt.h:1393
TFlt Tol
Definition: cascdynetinf.h:155
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

void TNIBs::ComputePerformanceNId ( const int &  NId,
const int &  Step,
const TFltV Steps 
)

Definition at line 918 of file cascdynetinf.cpp.

References Accuracy, TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), TFlt::GetMn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNI(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), InferredNetwork, TNodeEDatNet< TNodeData, TEdgeData >::IsEdge(), MAE, MaxAlpha, MinAlpha, MSE, Network, PrecisionRecall, TPair< TVal1, TVal2 >::Val1, and TPair< TVal1, TVal2 >::Val2.

Referenced by BSG(), FG(), and SG().

918  {
919  double CurrentMAE = 0.0;
920  double CurrentMSE = 0.0;
921  TFltPr CurrentPrecisionRecall(0.0, 0.0);
922  double CurrentAccD = 0.0;
923 
924  TStrFltFltHNEDNet::TNodeI NI = InferredNetwork.GetNI(NId);
925  for (int i=0; i<NI.GetInDeg(); i++) {
926  double inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]);
927  // we get the true alpha in the previous step (the inferred network contains delayed estimates)
928  double trueAlpha = 0.0;
929  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]); }
930 
931  // accuracy (denominator)
932  CurrentAccD += (double) (inferredAlpha > MinAlpha);
933 
934  // precision
935  CurrentPrecisionRecall.Val2 += (double) (inferredAlpha > MinAlpha && trueAlpha<MinAlpha);
936  }
937 
938  NI = Network.GetNI(NId);
939  int NumEdges = 0;
940  for (int i=0; i<NI.GetInDeg(); i++) {
941  TIntPr Pair(NI.GetInNId(i), NId);
942 
943  // we get the inferred Alpha if any
944  double inferredAlpha = 0.0;
945  if (InferredNetwork.IsEdge(NI.GetInNId(i), NId) && InferredNetwork.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t])) {
946  inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]).Val;
947  }
948 
949  // we get the true alpha in the previous step (the inferred network contains delayed estimates)
950  double trueAlpha = 0.0;
951  if (Network.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t-1])) { trueAlpha = Network.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t-1]); }
952 
953  // mae
954  if (trueAlpha > MinAlpha) {
955  NumEdges++;
956  CurrentMAE += fabs(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha))/trueAlpha;
957  }
958 
959  // mse
960  CurrentMSE += pow(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha), 2.0);
961 
962  // recall
963  CurrentPrecisionRecall.Val1 += (double) (inferredAlpha > MinAlpha && trueAlpha > MinAlpha);
964  }
965 
966  if (NumEdges > 0) {
967  MAE[t-1].Val2 += CurrentMAE / ((double)(NumEdges*Network.GetNodes()));
968  MSE[t-1].Val2 += CurrentMSE / ((double)(NumEdges*Network.GetNodes()));
969  PrecisionRecall[t-1].Val1 += CurrentPrecisionRecall.Val1/(double)(NumEdges*Network.GetNodes());
970  }
971 
972  if (CurrentAccD > 0) {
973  PrecisionRecall[t-1].Val2 += (1.0 - CurrentPrecisionRecall.Val2 / CurrentAccD)/(double)Network.GetNodes();
974  } else {
975  PrecisionRecall[t-1].Val2 += 1.0/(double)Network.GetNodes();
976  }
977 
978  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));
979 }
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
TFltPrV PrecisionRecall
Definition: cascdynetinf.h:169
TFlt MinAlpha
Definition: cascdynetinf.h:155
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:714
TFltPrV MSE
Definition: cascdynetinf.h:170
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 network.
Definition: network.h:939
TFltPrV Accuracy
Definition: cascdynetinf.h:170
Definition: ds.h:32
static double GetMn(const double &Flt1, const double &Flt2)
Definition: dt.h:1434
TFltPrV MAE
Definition: cascdynetinf.h:170
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::FG ( const int &  NId,
const int &  Iters,
const TFltV Steps 
)

Definition at line 631 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), AveDiffAlphas, TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), CascH, THash< TKey, TDat, THashFunc >::Clr(), ComputePerformanceNId(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), Gamma, THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), THash< TKey, TDat, THashFunc >::GetKey(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), InferredNetwork, TNodeEDatNet< TNodeData, TEdgeData >::IsEdge(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, Mu, OBSG, Regularizer, Reset(), THash< TKey, TDat, THashFunc >::SortByDat(), Tol, and UpdateDiff().

631  {
632  bool verbose = false;
633 
634  Reset();
635 
636  printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes());
637 
638  // traverse through all times
639  for (int t=1; t<Steps.Len(); t++) {
640  // find cascades whose two first infections are earlier than Steps[t]
641  TIntFltH CascadesIdx;
642  int num_infections = 0;
643  for (int i=0; i<CascH.Len(); i++) {
644  if (CascH[i].LenBeforeT(Steps[t]) > 1) {
645  num_infections += CascH[i].LenBeforeT(Steps[t]);
646  CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
647  }
648  }
649 
650  // if there are not recorded cascades by Steps[t], continue
651  if (CascadesIdx.Len()==0) {
652  printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
653 // ComputePerformance(NId, Tol, t, Steps);
654  continue;
655  }
656 
657  printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val, CascadesIdx.Len(), num_infections);
658 
659  // sort cascade from most recent to least recent
660  CascadesIdx.SortByDat(false);
661 
662  // sampling cascades with no replacement
663  for (int i=0; i < Iters; i++) {
664  // alphas to update
665  TIntPrV AlphasToUpdate;
666 
667  // delete average gradients
668  AveDiffAlphas.Clr();
669 
670  // use all cascades for the average gradient
671  for (int c=0; c<CascadesIdx.Len(); c++) {
672  // each cascade
673  TCascade &Cascade = CascH[CascadesIdx.GetKey(c)];
674 
675  // update gradient and alpha's
676  UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]);
677  }
678 
679  // update alpha's
680  for (int j=0; j<AlphasToUpdate.Len(); j++) {
681  if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
682  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) {
683  switch (Regularizer) {
684  case 0:
685  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
686  Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
687  case 1:
688  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) =
689  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)CascadesIdx.Len())
690  - Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
691  }
692 
693  // project into alpha >= 0
694  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
695  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
696  }
697 
698  // project into alpha <= MaxAlpha
699  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
700  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
701  }
702  }
703  }
704 
705  // for alphas that did not get updated, copy last alpha value
706  int unchanged = 0;
707  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
708  if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }
709 
710  EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]);
711  unchanged++;
712  }
713  if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); }
714  }
715 
716  // compute performance on-the-fly for each time step
717  ComputePerformanceNId(NId, t, Steps);
718  }
719 }
TFlt Mu
Definition: cascdynetinf.h:153
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
TIntFltH AveDiffAlphas
Definition: cascdynetinf.h:162
TFlt Gamma
Definition: cascdynetinf.h:153
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
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 network.
Definition: network.h:939
void ComputePerformanceNId(const int &NId, const int &Step, const TFltV &Steps)
void Reset()
void UpdateDiff(const TOptMethod &OptMethod, const int &NId, TCascade &Cascade, TIntPrV &AlphasToUpdate, const double &CurrentTime=TFlt::Mx)
TRegularizer Regularizer
Definition: cascdynetinf.h:154
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
TFlt Tol
Definition: cascdynetinf.h:155
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

void TNIBs::find_C ( int  t,
TFltV x,
TFltVV C,
const int &  k,
const double &  s,
const double &  gamma,
const double &  T 
)

Definition at line 835 of file cascdynetinf.cpp.

References TVec< TVal, TSizeTy >::Len().

Referenced by LabelBurstAutomaton().

835  {
836  if ( t >= x.Len() ) return;
837  if ( t == 0 ){
838  C = TFltVV( x.Len(), k );
839  }else{
840  int n = x.Len() - 1;
841  for (int j = 0; j < k; j++){
842  double alpha = ( (x.Len() ) / T ) * pow( s, j );
843  double term_1 = -log(alpha) + alpha * x[t];
844  double term_2 = 0;
845  if ( t == 1 ){
846  term_2 = j * log((double) n) * gamma;
847  }
848  else{
849  bool first = false;
850  for (int l = 0; l < k; l++){
851  double my_val = C(t-1, l);
852  if ( j > l ) my_val += (j - l) * log((double) n) * gamma;
853  if ( !first || my_val < term_2 ){
854  term_2 = my_val;
855  first = true;
856  }
857  }
858  }
859  C( t, j ) = term_1 + term_2;
860  }
861  }
862  find_C( t + 1, x, C, k, s, gamma, T );
863 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TVVec< TFlt > TFltVV
Definition: ds.h:2402
void find_C(int t, TFltV &x, TFltVV &C, const int &k, const double &s, const double &gamma, const double &T)

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::find_min_state ( TFltVV C,
TIntV states,
const int &  k,
const double &  s,
const double &  gamma,
const double &  T 
)

Definition at line 865 of file cascdynetinf.cpp.

References TVVec< TVal, TSizeTy >::GetCols(), and TVVec< TVal, TSizeTy >::GetRows().

Referenced by LabelBurstAutomaton().

865  {
866  states = TIntV( C.GetRows() );
867  states[0] = 0;
868  int n = C.GetRows() - 1;
869  for (int t = C.GetRows() - 1; t > 0; t --){
870  double best_val = 0;
871  int best_state = -1;
872  for (int j = 0; j < C.GetCols(); j++){
873  double c_state = C( t, j );
874  if ( t < C.GetRows() - 2 && states[t+1] > j ){
875  c_state += ( states[t+1] - j ) * gamma * log((double) n);
876  }
877  if ( best_state == -1 || best_val > c_state ){
878  best_state = j;
879  best_val = c_state;
880  }
881  }
882  states[t] = best_state;
883  }
884 }
TSizeTy GetRows() const
Definition: ds.h:2251
TVec< TInt > TIntV
Definition: ds.h:1594
TSizeTy GetCols() const
Definition: ds.h:2252

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::GenCascade ( TCascade C)

Definition at line 180 of file cascdynetinf.cpp.

References TCascade::Add(), THash< TKey, TDat, THashFunc >::AddDat(), THash< TKey, TDat, THashFunc >::BegI(), TCascade::Clr(), THash< TKey, TDat, THashFunc >::Clr(), Delta, EXP, THashKeyDatI< TKey, TDat >::GetDat(), THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), TRnd::GetExpDev(), THashKeyDatI< TKey, TDat >::GetKey(), THash< TKey, TDat, THashFunc >::GetKey(), TFlt::GetMn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNI(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), TRnd::GetPowerDev(), TRnd::GetRayleigh(), TNodeEDatNet< TNodeData, TEdgeData >::GetRndNId(), TRnd::GetUniDev(), IAssert, THash< TKey, TDat, THashFunc >::IsKey(), TCascade::Len(), THash< TKey, TDat, THashFunc >::Len(), Model, Network, POW, TRnd::Randomize(), RAY, TInt::Rnd, TFlt::Rnd, TCascade::Sort(), THash< TKey, TDat, THashFunc >::SortByDat(), TotalTime, TInt::Val, and Window.

180  {
181  bool verbose = false;
182  TIntFltH InfectedNIdH; TIntH InfectedBy;
183  double GlobalTime, InitTime;
184  double alpha;
185  int StartNId;
186 
187  if (Network.GetNodes() == 0)
188  return;
189 
190  // set random seed
192 
193  while (C.Len() < 2) {
194  C.Clr();
195  InfectedNIdH.Clr();
196  InfectedBy.Clr();
197 
198  InitTime = TFlt::Rnd.GetUniDev() * TotalTime; // random starting point <TotalTime
199  GlobalTime = InitTime;
200 
201  StartNId = Network.GetRndNId();
202  InfectedNIdH.AddDat(StartNId) = GlobalTime;
203 
204  while (true) {
205  // sort by time & get the oldest node that did not run infection
206  InfectedNIdH.SortByDat(true);
207  const int& NId = InfectedNIdH.BegI().GetKey();
208  GlobalTime = InfectedNIdH.BegI().GetDat();
209 
210  // all the nodes has run infection
211  if ( GlobalTime >= TFlt::GetMn(TotalTime, InitTime+Window) )
212  break;
213 
214  // add current oldest node to the network and set its time
215  C.Add(NId, GlobalTime);
216 
217  if (verbose) { printf("GlobalTime:%f, infected node:%d\n", GlobalTime, NId); }
218 
219  // run infection from the current oldest node
220  TStrFltFltHNEDNet::TNodeI NI = Network.GetNI(NId);
221  for (int e = 0; e < NI.GetOutDeg(); e++) {
222  const int DstNId = NI.GetOutNId(e);
223 
224  // choose the current tx rate (we assume the most recent tx rate)
225  TFltFltH& Alphas = Network.GetEDat(NId, DstNId);
226  for (int j=0; j<Alphas.Len() && Alphas.GetKey(j)<GlobalTime; j++) { alpha = Alphas[j]; }
227  if (verbose) { printf("GlobalTime:%f, nodes:%d->%d, alpha:%f\n", GlobalTime, NId, DstNId, alpha); }
228 
229  if (alpha<1e-9) { continue; }
230 
231  // not infecting the parent
232  if (InfectedBy.IsKey(NId) && InfectedBy.GetDat(NId).Val == DstNId)
233  continue;
234 
235  double sigmaT;
236  switch (Model) {
237  case EXP:
238  // exponential with alpha parameter
239  sigmaT = TInt::Rnd.GetExpDev(alpha);
240  break;
241  case POW:
242  // power-law with alpha parameter
243  sigmaT = TInt::Rnd.GetPowerDev(1+alpha);
244  while (sigmaT < Delta) { sigmaT = Delta*TInt::Rnd.GetPowerDev(1+alpha); }
245  break;
246  case RAY:
247  // rayleigh with alpha parameter
248  sigmaT = TInt::Rnd.GetRayleigh(1/sqrt(alpha));
249  break;
250  default:
251  sigmaT = 1;
252  break;
253  }
254 
255  IAssert(sigmaT >= 0);
256 
257  double t1 = TFlt::GetMn(GlobalTime + sigmaT, TFlt::GetMn(InitTime+Window, TotalTime));
258 
259  if (InfectedNIdH.IsKey(DstNId)) {
260  double t2 = InfectedNIdH.GetDat(DstNId);
261  if ( t2 > t1 && t2 < TFlt::GetMn(InitTime+Window, TotalTime)) {
262  InfectedNIdH.GetDat(DstNId) = t1;
263  InfectedBy.GetDat(DstNId) = NId;
264  }
265  } else {
266  InfectedNIdH.AddDat(DstNId) = t1;
267  InfectedBy.AddDat(DstNId) = NId;
268  }
269  }
270 
271  // we cannot delete key (otherwise, we cannot sort), so we assign a big time (InitTime + window cut-off)
272  InfectedNIdH.GetDat(NId) = TFlt::GetMn(InitTime+Window, TotalTime);
273  }
274  }
275 
276  C.Sort();
277 }
#define IAssert(Cond)
Definition: bd.h:262
void Randomize()
Definition: dt.h:60
int Val
Definition: dt.h:1136
TFlt TotalTime
Definition: cascdynetinf.h:147
int Len() const
Definition: cascdynetinf.h:97
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TIter BegI() const
Definition: hash.h:213
TFlt Window
Definition: cascdynetinf.h:147
double GetRayleigh(const double &Sigma)
Definition: dt.h:50
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
static TRnd Rnd
Definition: dt.h:1143
const TKey & GetKey() const
Definition: hash.h:80
TFlt Delta
Definition: cascdynetinf.h:150
const TDat & GetDat() const
Definition: hash.h:81
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the network.
Definition: network.h:714
double GetExpDev()
Definition: dt.cpp:83
int GetRndNId(TRnd &Rnd=TInt::Rnd)
Returns an ID of a random node in the network.
Definition: network.h:777
void Clr()
Definition: cascdynetinf.h:95
static double GetMn(const double &Flt1, const double &Flt2)
Definition: dt.h:1434
void Add(const int &NId, const double &HitTm)
Definition: cascdynetinf.h:107
double GetUniDev()
Definition: dt.h:30
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228
void Sort()
Definition: cascdynetinf.h:110
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
double GetPowerDev(const double &AlphaSlope)
Definition: dt.h:47
static TRnd Rnd
Definition: dt.h:1393
TModel Model
Definition: cascdynetinf.h:144
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

TCascade& TNIBs::GetCasc ( int  c)
inline

Definition at line 209 of file cascdynetinf.h.

References CascH, and THash< TKey, TDat, THashFunc >::GetDat().

209 { return CascH.GetDat(c); }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262

Here is the call graph for this function:

int TNIBs::GetCascadeId ( const TStr Cascade)
inline

Definition at line 211 of file cascdynetinf.h.

References CascadeIdH, and THash< TKey, TDat, THashFunc >::GetDat().

211 { return CascadeIdH.GetDat(Cascade); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TStrIntH CascadeIdH
Definition: cascdynetinf.h:135

Here is the call graph for this function:

int TNIBs::GetCascs ( )
inline

Definition at line 210 of file cascdynetinf.h.

References CascH, and THash< TKey, TDat, THashFunc >::Len().

210 { return CascH.Len(); }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
int Len() const
Definition: hash.h:228

Here is the call graph for this function:

int TNIBs::GetDomainId ( const TStr Domain)
inline

Definition at line 224 of file cascdynetinf.h.

References DomainsIdH, and THash< TKey, TDat, THashFunc >::GetDat().

224 { return DomainsIdH.GetDat(Domain); }
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134

Here is the call graph for this function:

void TNIBs::GetGroundTruthGraphAtT ( const double &  Step,
PNGraph GraphAtT 
)

Definition at line 279 of file cascdynetinf.cpp.

References TNGraph::AddEdge(), TNGraph::AddNode(), TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), THash< TKey, TDat, THashFunc >::IsKey(), MinAlpha, Network, TNGraph::New(), and NodeNmH.

279  {
280  GraphAtT = TNGraph::New();
281 
282  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); }
283 
284  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
285  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
286  double Alpha = 0.0;
287  if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); }
288 
289  if (Alpha > MinAlpha) {
290  GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
291  }
292  }
293 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TIter BegI() const
Definition: hash.h:213
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::GetGroundTruthNetworkAtT ( const double &  Step,
PStrFltNEDNet NetworkAtT 
)

Definition at line 295 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), THash< TKey, TDat, THashFunc >::IsKey(), MinAlpha, Network, TNodeEDatNet< TNodeData, TEdgeData >::New(), and NodeNmH.

295  {
296  NetworkAtT = TStrFltNEDNet::New();
297 
298  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name); }
299 
300  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
301  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
302  double Alpha = 0.0;
303  if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); }
304 
305  if (Alpha > MinAlpha) {
306  NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), Alpha);
307  }
308  }
309 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TIter BegI() const
Definition: hash.h:213
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
static PNet New()
Static constructor that returns a pointer to the network. Call: TPt > Net = TNodeEDatNet::New().
Definition: network.h:661
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::GetInferredGraphAtT ( const double &  Step,
PNGraph GraphAtT 
)

Definition at line 311 of file cascdynetinf.cpp.

References TNGraph::AddEdge(), TNGraph::AddNode(), TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), InferredNetwork, THash< TKey, TDat, THashFunc >::IsKey(), MinAlpha, TNGraph::New(), and NodeNmH.

311  {
312  GraphAtT = TNGraph::New();
313 
314  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); }
315 
316  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
317  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
318 
319  double inferredAlpha = 0.0;
320  if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }
321 
322  if (inferredAlpha > MinAlpha) {
323  GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
324  }
325  }
326 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:477
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TIter BegI() const
Definition: hash.h:213
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::GetInferredNetworkAtT ( const double &  Step,
PStrFltNEDNet NetworkAtT 
)

Definition at line 328 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), InferredNetwork, THash< TKey, TDat, THashFunc >::IsKey(), MinAlpha, TNodeEDatNet< TNodeData, TEdgeData >::New(), and NodeNmH.

328  {
329  NetworkAtT = TStrFltNEDNet::New();
330 
331  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
332  NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name);
333  }
334 
335  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
336  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
337 
338  double inferredAlpha = 0.0;
339  if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }
340 
341  if (inferredAlpha > MinAlpha) {
342  NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), inferredAlpha);
343  }
344  }
345 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TIter BegI() const
Definition: hash.h:213
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
static PNet New()
Static constructor that returns a pointer to the network. Call: TPt > Net = TNodeEDatNet::New().
Definition: network.h:661
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

TNodeInfo TNIBs::GetNodeInfo ( const int &  NId) const
inline

Definition at line 217 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::GetDat(), and NodeNmH.

Referenced by AddCasc().

217 { return NodeNmH.GetDat(NId); }
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262

Here is the call graph for this function:

Here is the caller graph for this function:

TStr TNIBs::GetNodeNm ( const int &  NId) const
inline

Definition at line 216 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::GetDat(), TNodeInfo::Name, and NodeNmH.

216 { return NodeNmH.GetDat(NId).Name; }
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262

Here is the call graph for this function:

int TNIBs::GetNodes ( )
inline

Definition at line 214 of file cascdynetinf.h.

References TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), and InferredNetwork.

214 { return InferredNetwork.GetNodes(); }
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680

Here is the call graph for this function:

void TNIBs::Init ( const TFltV Steps)

Definition at line 347 of file cascdynetinf.cpp.

References Accuracy, TVec< TVal, TSizeTy >::Add(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::Clr(), TVec< TVal, TSizeTy >::Clr(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), THash< TKey, TDat, THashFunc >::EndI(), InferredNetwork, TVec< TVal, TSizeTy >::Len(), MAE, MSE, NodeNmH, PrecisionRecall, and TotalCascadesAlpha.

347  {
348  // inferred network
350 
351  // copy nodes from NodeNmH (it will be filled by loading cascades or loading groundtruth)
352  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
353  InferredNetwork.AddNode(NI.GetKey(), NI.GetDat().Name);
354  }
355 
356  // performance measures
358  Accuracy.Clr();
359  MAE.Clr();
361 
362  for (int i=0; i<Steps.Len()-1; i++) {
363  MAE.Add(TFltPr(Steps[i], 0.0));
364  MSE.Add(TFltPr(Steps[i], 0.0));
365  Accuracy.Add(TFltPr(Steps[i], 0.0));
366  PrecisionRecall.Add(TFltPr(0.0,0.0));
367  }
368 }
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:836
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TIter BegI() const
Definition: hash.h:213
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TFltPrV PrecisionRecall
Definition: cascdynetinf.h:169
void Clr(const bool &DoDel=true, const bool &ResetDat=true)
Deletes all nodes and edges from the network.
Definition: network.h:786
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
TFltPrV MSE
Definition: cascdynetinf.h:170
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
TFltPrV Accuracy
Definition: cascdynetinf.h:170
TFltPrV MAE
Definition: cascdynetinf.h:170
Definition: hash.h:97
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TIntFltH TotalCascadesAlpha
Definition: cascdynetinf.h:159

Here is the call graph for this function:

bool TNIBs::IsCascade ( int  c)
inline

Definition at line 208 of file cascdynetinf.h.

References CascH, and THash< TKey, TDat, THashFunc >::IsKey().

208 { return CascH.IsKey(c); }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

bool TNIBs::IsDomainNm ( const TStr Domain) const
inline

Definition at line 223 of file cascdynetinf.h.

References DomainsIdH, and THash< TKey, TDat, THashFunc >::IsKey().

223 { return DomainsIdH.IsKey(Domain); }
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

bool TNIBs::IsNodeNm ( const int &  NId) const
inline

Definition at line 218 of file cascdynetinf.h.

References THash< TKey, TDat, THashFunc >::IsKey(), and NodeNmH.

Referenced by LoadGroundTruthNodesTxt(), LoadGroundTruthTxt(), LoadInferredNodesTxt(), and LoadInferredTxt().

218 { return NodeNmH.IsKey(NId); }
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::LabelBurstAutomaton ( const int &  SrcId,
const int &  DstId,
TIntV state_labels,
TFltV state_times,
const bool &  inferred = false,
const int &  k = 5,
const double &  s = 2.0,
const double &  gamma = 1.0,
const TSecTm MinTime = TSecTm(),
const TSecTm MaxTime = TSecTm() 
)

Definition at line 886 of file cascdynetinf.cpp.

References TVec< TVal, TSizeTy >::Add(), find_C(), find_min_state(), TSecTm::GetAbsSecs(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), THash< TKey, TDat, THashFunc >::GetKey(), InferredNetwork, TVec< TVal, TSizeTy >::Last(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), MinAlpha, Network, and TVec< TVal, TSizeTy >::Sort().

886  {
887  TVec<TSecTm> arrival_times;
888  TFltFltH &LinksEdge = (inferred? InferredNetwork.GetEDat(SrcId, DstId) : Network.GetEDat(SrcId, DstId));
889 
890  for (int i=0; i<LinksEdge.Len(); i++) {
891  if (LinksEdge[i]>MinAlpha) {
892  TSecTm tsecs;
893  tsecs = (uint)LinksEdge.GetKey(i); // link rates is in seconds
894  if (tsecs > MaxTime || tsecs < MinTime) { continue; }
895  arrival_times.Add(tsecs);
896  }
897  }
898 
899  if ( arrival_times.Len() < 2 ) return;
900 
901  TFltV x;
902  x.Add( 0 );
903  arrival_times.Sort(true);
904  double T = ((double)arrival_times.Last().GetAbsSecs()) - ((double)arrival_times[0].GetAbsSecs());
905  for (int i = 1; i < arrival_times.Len(); i++){
906  x.Add( ((double)arrival_times[i].GetAbsSecs()) - ((double)arrival_times[i-1].GetAbsSecs()) );
907  }
908  TFltVV Cost_matrix;
909  //printf("arrivals = %d\n", x.Len() );
910  find_C( 0, x, Cost_matrix, k, s, gamma, T);
911 
912  find_min_state( Cost_matrix, state_labels, k, s, gamma, T );
913 
914  for (int i=0; i<state_labels.Len(); i++) { state_times.Add((double)arrival_times[i].GetAbsSecs()); }
915 }
unsigned int uint
Definition: bd.h:11
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
TFlt MinAlpha
Definition: cascdynetinf.h:155
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
uint GetAbsSecs() const
Definition: tm.h:150
const TVal & Last() const
Returns a reference to the last element of the vector.
Definition: ds.h:579
Definition: tm.h:81
void find_C(int t, TFltV &x, TFltVV &C, const int &k, const double &s, const double &gamma, const double &T)
Definition: hash.h:97
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
int Len() const
Definition: hash.h:228
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
void find_min_state(TFltVV &C, TIntV &states, const int &k, const double &s, const double &gamma, const double &T)
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

void TNIBs::LoadCascadesTxt ( TSIn SIn)

Definition at line 4 of file cascdynetinf.cpp.

References AddCasc(), AddNodeNm(), TSIn::Eof(), TSIn::GetNextLn(), Model, and TStr::SplitOnAllCh().

4  {
5  TStr Line;
6  while (!SIn.Eof()) {
7  SIn.GetNextLn(Line);
8  if (Line=="") { break; }
9  TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
10  AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0));
11  }
12  printf("All nodes read!\n");
13  while (!SIn.Eof()) { SIn.GetNextLn(Line); AddCasc(Line, Model); }
14 
15  printf("All cascades read!\n");
16 }
void AddCasc(const TStr &CascStr, const TModel &Model=EXP)
virtual bool Eof()=0
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascdynetinf.h:215
TModel Model
Definition: cascdynetinf.h:144
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43

Here is the call graph for this function:

void TNIBs::LoadGroundTruthNodesTxt ( TSIn SIn)

Definition at line 63 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), AddNodeNm(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), DomainsIdH, TSIn::Eof(), TSIn::GetNextLn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), IsNodeNm(), Network, and TStr::SplitOnAllCh().

63  {
64  TStr Line;
65 
66  Network.Clr(); // clear network (if any)
67 
68  // add nodes
69  while (!SIn.Eof()) {
70  SIn.GetNextLn(Line);
71  if (Line=="") { break; }
72  TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
73  Network.AddNode(NIdV[0].GetInt(), NIdV[1]);
74  if (!IsNodeNm(NIdV[0].GetInt())) {
75  AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0));
76  DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt();
77  }
78  }
79 
80  printf("groundtruth nodes:%d\n", Network.GetNodes());
81 }
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:836
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
void Clr(const bool &DoDel=true, const bool &ResetDat=true)
Deletes all nodes and edges from the network.
Definition: network.h:786
virtual bool Eof()=0
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
bool IsNodeNm(const int &NId) const
Definition: cascdynetinf.h:218
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascdynetinf.h:215
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43

Here is the call graph for this function:

void TNIBs::LoadGroundTruthTxt ( TSIn SIn)

Definition at line 18 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TNodeEDatNet< TNodeData, TEdgeData >::AddEdge(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), AddNodeNm(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), DomainsIdH, TSIn::Eof(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEdges(), THash< TKey, TDat, THashFunc >::GetKey(), TSIn::GetNextLn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), IsNodeNm(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), Network, and TStr::SplitOnAllCh().

18  {
19  bool verbose = false;
20  TStr Line;
21 
22  Network.Clr(); // clear network (if any)
23 
24  // add nodes
25  while (!SIn.Eof()) {
26  SIn.GetNextLn(Line);
27  if (Line=="") { break; }
28  TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
29  Network.AddNode(NIdV[0].GetInt(), NIdV[1]);
30  if (!IsNodeNm(NIdV[0].GetInt())) {
31  AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0));
32  DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt();
33  }
34  }
35 
36  // add edges
37  while (!SIn.Eof()) {
38  SIn.GetNextLn(Line);
39  TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV);
40 
41  TFltFltH Alphas;
42  if (FieldsV.Len() == 3) {
43  Alphas.AddDat(0.0) = FieldsV[2].GetFlt();
44  } else {
45  for (int i=2; i<FieldsV.Len()-1; i+=2) {
46  Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt();
47  }
48  }
49 
50  Network.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas);
51 
52  if (verbose) {
53  printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt());
54  TFltFltH &AlphasE = Network.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt());
55  for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); }
56  printf("\n");
57  }
58  }
59 
60  printf("groundtruth nodes:%d edges:%d\n", Network.GetNodes(), Network.GetEdges());
61 }
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:898
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:836
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the network.
Definition: network.h:906
void Clr(const bool &DoDel=true, const bool &ResetDat=true)
Deletes all nodes and edges from the network.
Definition: network.h:786
virtual bool Eof()=0
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
bool IsNodeNm(const int &NId) const
Definition: cascdynetinf.h:218
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
Definition: hash.h:97
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascdynetinf.h:215
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43

Here is the call graph for this function:

void TNIBs::LoadInferredNodesTxt ( TSIn SIn)

Definition at line 128 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), AddNodeNm(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), DomainsIdH, TSIn::Eof(), THash< TKey, TDat, THashFunc >::GetDat(), TSIn::GetNextLn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), IAssert, InferredNetwork, THash< TKey, TDat, THashFunc >::IsKey(), IsNodeNm(), and TStr::SplitOnAllCh().

128  {
129  TStr Line;
130 
131  InferredNetwork.Clr(); // clear network (if any)
132 
133  // add nodes
134  while (!SIn.Eof()) {
135  SIn.GetNextLn(Line);
136  if (Line=="") { break; }
137  TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
138  if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); }
139  InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]);
140  if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); }
141  if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); }
142  }
143 
144  printf("Nodes:%d\n", InferredNetwork.GetNodes());
145 }
#define IAssert(Cond)
Definition: bd.h:262
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:836
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
void Clr(const bool &DoDel=true, const bool &ResetDat=true)
Deletes all nodes and edges from the network.
Definition: network.h:786
virtual bool Eof()=0
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
bool IsNodeNm(const int &NId) const
Definition: cascdynetinf.h:218
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascdynetinf.h:215
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43

Here is the call graph for this function:

void TNIBs::LoadInferredTxt ( TSIn SIn)

Definition at line 83 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), TNodeEDatNet< TNodeData, TEdgeData >::AddEdge(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), AddNodeNm(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), DomainsIdH, TSIn::Eof(), THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEdges(), THash< TKey, TDat, THashFunc >::GetKey(), TSIn::GetNextLn(), TNodeEDatNet< TNodeData, TEdgeData >::GetNodes(), IAssert, InferredNetwork, THash< TKey, TDat, THashFunc >::IsKey(), IsNodeNm(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), and TStr::SplitOnAllCh().

83  {
84  bool verbose = false;
85  TStr Line;
86 
87  InferredNetwork.Clr(); // clear network (if any)
88 
89  // add nodes
90  while (!SIn.Eof()) {
91  SIn.GetNextLn(Line);
92  if (Line=="") { break; }
93  TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
94  if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); }
95  InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]);
96  if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); }
97  if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); }
98  if (verbose) { printf("Node:%s\n", NIdV[1].CStr()); }
99  }
100 
101  // add edges
102  while (!SIn.Eof()) {
103  SIn.GetNextLn(Line);
104  TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV);
105 
106  TFltFltH Alphas;
107  if (FieldsV.Len() == 3) {
108  Alphas.AddDat(0.0) = FieldsV[2].GetFlt();
109  } else {
110  for (int i=2; i<FieldsV.Len()-1; i+=2) {
111  Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt();
112  }
113  }
114 
115  InferredNetwork.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas);
116 
117  if (verbose) {
118  printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt());
119  TFltFltH &AlphasE = InferredNetwork.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt());
120  for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); }
121  printf("\n");
122  }
123  }
124 
125  printf("inferred nodes:%d edges:%d\n", InferredNetwork.GetNodes(), InferredNetwork.GetEdges());
126 }
#define IAssert(Cond)
Definition: bd.h:262
int GetEdges() const
Returns the number of edges in the network.
Definition: network.h:898
int AddNode(int NId=-1)
Adds a node of ID NId to the network.
Definition: network.h:836
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the network.
Definition: network.h:906
void Clr(const bool &DoDel=true, const bool &ResetDat=true)
Deletes all nodes and edges from the network.
Definition: network.h:786
virtual bool Eof()=0
TStrIntH DomainsIdH
Definition: cascdynetinf.h:134
bool IsNodeNm(const int &NId) const
Definition: cascdynetinf.h:218
Definition: dt.h:412
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void AddNodeNm(const int &NId, const TNodeInfo &Info)
Definition: cascdynetinf.h:215
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
int GetNodes() const
Returns the number of nodes in the network.
Definition: network.h:680
bool GetNextLn(TStr &LnStr)
Definition: fl.cpp:43

Here is the call graph for this function:

void TNIBs::Reset ( )

Definition at line 370 of file cascdynetinf.cpp.

References AveDiffAlphas, THash< TKey, TDat, THashFunc >::Clr(), DiffAlphas, SampledCascadesH, and TotalCascadesAlpha.

Referenced by BSG(), FG(), and SG().

370  {
371  // reset vectors
372  for (int i=0; i<DiffAlphas.Len(); i++) {
373  DiffAlphas[i].Clr();
374  }
375  DiffAlphas.Clr();
376  AveDiffAlphas.Clr();
379 }
TIntIntPrH SampledCascadesH
Definition: cascdynetinf.h:166
TIntFltH AveDiffAlphas
Definition: cascdynetinf.h:162
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
THash< TInt, TIntFltH > DiffAlphas
Definition: cascdynetinf.h:163
TIntFltH TotalCascadesAlpha
Definition: cascdynetinf.h:159

Here is the call graph for this function:

Here is the caller graph for this function:

void TNIBs::Save ( TSOut SOut) const
inline

Definition at line 175 of file cascdynetinf.h.

References CascH, CascPerEdge, InferredNetwork, NodeNmH, THash< TKey, TDat, THashFunc >::Save(), and TNodeEDatNet< TNodeData, TEdgeData >::Save().

175 { CascH.Save(SOut); NodeNmH.Save(SOut); CascPerEdge.Save(SOut); InferredNetwork.Save(SOut); }
virtual void Save(TSOut &SOut) const
Saves the network to a (binary) stream SOut.
Definition: network.h:659
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
void Save(TSOut &SOut) const
Definition: hash.h:183
THash< TIntPr, TIntV > CascPerEdge
Definition: cascdynetinf.h:138

Here is the call graph for this function:

void TNIBs::SaveCascades ( const TStr OutFNm)

Definition at line 1168 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::BegI(), CascH, THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::IsKey(), TCascade::NIdHitH, NodeNmH, and TSOut::PutStr().

1168  {
1169  TFOut FOut(OutFNm);
1170 
1171  // write nodes to file
1172  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1173  FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
1174  }
1175 
1176  FOut.PutStr("\r\n");
1177 
1178  // write cascades to file
1179  for (THash<TInt, TCascade>::TIter CI = CascH.BegI(); CI < CascH.EndI(); CI++) {
1180  TCascade &C = CI.GetDat();
1181  int j = 0;
1182  for (THash<TInt, THitInfo>::TIter NI = C.NIdHitH.BegI(); NI < C.NIdHitH.EndI(); NI++) {
1183  if (!NodeNmH.IsKey(NI.GetDat().NId)) { continue; }
1184  if (j > 0) { FOut.PutStr(TStr::Fmt(",%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); }
1185  else { FOut.PutStr(TStr::Fmt("%d;%d,%f", CI.GetKey().Val, NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); }
1186  j++;
1187  }
1188 
1189  if (j >= 1)
1190  FOut.PutStr(TStr::Fmt("\r\n"));
1191  }
1192 }
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TIter BegI() const
Definition: hash.h:213
Definition: fl.h:319
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
THash< TInt, THitInfo > NIdHitH
Definition: cascdynetinf.h:87
TIter EndI() const
Definition: hash.h:218
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SaveGroundTruth ( const TStr OutFNm)

Definition at line 1105 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::IsKey(), Network, NodeNmH, and TSOut::PutStr().

1105  {
1106  TFOut FOut(OutFNm);
1107 
1108  // write nodes to file
1109  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1110  FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
1111  }
1112 
1113  FOut.PutStr("\r\n");
1114 
1115  // write edges to file (not allowing self loops in the network)
1116  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
1117  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
1118 
1119  // not allowing self loops in the Kronecker network
1120  if (EI.GetSrcNId() != EI.GetDstNId()) {
1121  if (EI().Len() > 0) {
1122  FOut.PutStr(TStr::Fmt("%d,%d,", EI.GetSrcNId(), EI.GetDstNId()));
1123  for (int i=0; i<EI().Len()-1; i++) { FOut.PutStr(TStr::Fmt("%f,%f,", EI().GetKey(i).Val, EI()[i].Val)); }
1124  FOut.PutStr(TStr::Fmt("%f,%f", EI().GetKey(EI().Len()-1).Val, EI()[EI().Len()-1].Val));
1125  FOut.PutStr("\r\n");
1126  }
1127  else
1128  FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
1129  }
1130  }
1131 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TIter BegI() const
Definition: hash.h:213
Definition: fl.h:319
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SaveGroundTruthPajek ( const TStr OutFNm,
const double &  Step 
)

Definition at line 1133 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), MaxAlpha, MinAlpha, Network, NodeNmH, TSOut::PutStr(), and TFlt::Val.

1133  {
1134  TFOut FOut(OutFNm);
1135 
1136  FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len()));
1137  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1138  FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr()));
1139  }
1140  FOut.PutStr("*Arcs\r\n");
1141  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
1142  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
1143  double trueAlpha = 0.0;
1144  if (EI().IsKey(Step)) { trueAlpha = EI().GetDat(Step); }
1145  else { for (int j=0; j<EI().Len() && EI().GetKey(j)<=Step; j++) { trueAlpha = EI()[j]; } }
1146 
1147  if (trueAlpha > MinAlpha) {
1148  FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (trueAlpha > MaxAlpha? MaxAlpha.Val : trueAlpha)));
1149  }
1150  }
1151 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
TStrFltFltHNEDNet Network
Definition: cascdynetinf.h:141
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TIter BegI() const
Definition: hash.h:213
double Val
Definition: dt.h:1385
Definition: fl.h:319
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228

Here is the call graph for this function:

void TNIBs::SaveInferred ( const TStr OutFNm,
const TIntV NIdV = TIntV() 
)

Definition at line 1003 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), InferredNetwork, TVec< TVal, TSizeTy >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

1003  {
1004  TFOut FOut(OutFNm);
1005 
1006  // write nodes to file
1007  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1008  if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }
1009 
1010  FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
1011  }
1012 
1013  FOut.PutStr("\r\n");
1014 
1015  // write edges to file (not allowing self loops in the network)
1016  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
1017  if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
1018  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
1019 
1020  // not allowing self loops in the Kronecker network
1021  if (EI.GetSrcNId() != EI.GetDstNId()) {
1022  if (EI().Len() > 0) {
1023  TStr Line; bool IsEdge = false;
1024  for (int i=0; i<EI().Len(); i++) {
1025  if (EI()[i]>MinAlpha) {
1026  Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) );
1027  IsEdge = true;
1028  } else { // we write 0 explicitly
1029  Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val);
1030  }
1031  }
1032  // if none of the alphas is bigger than 0, no edge is written
1033  if (IsEdge) {
1034  FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId()));
1035  FOut.PutStr(Line);
1036  FOut.PutStr("\r\n");
1037  }
1038  }
1039  else
1040  FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
1041  }
1042  }
1043 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TIter BegI() const
Definition: hash.h:213
double Val
Definition: dt.h:1385
Definition: fl.h:319
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SaveInferred ( const TStr OutFNm,
const double &  Step,
const TIntV NIdV = TIntV() 
)

Definition at line 1045 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), InferredNetwork, TVec< TVal, TSizeTy >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

1045  {
1046  TFOut FOut(OutFNm);
1047 
1048  // write nodes to file
1049  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1050  if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }
1051 
1052  FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
1053  }
1054  FOut.PutStr("\r\n");
1055 
1056  // write edges to file (not allowing self loops in the network)
1057  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
1058  if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
1059  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
1060 
1061  // not allowing self loops in the Kronecker network
1062  if (EI.GetSrcNId() != EI.GetDstNId()) {
1063  double inferredAlpha = 0.0;
1064  if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }
1065 
1066  if (inferredAlpha > MinAlpha) {
1067  FOut.PutStr(TStr::Fmt("%d,%d,%f\r\n", EI.GetSrcNId(), EI.GetDstNId(), (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha)));
1068  }
1069  }
1070  }
1071 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TIter BegI() const
Definition: hash.h:213
double Val
Definition: dt.h:1385
Definition: fl.h:319
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SaveInferredEdges ( const TStr OutFNm)

Definition at line 1073 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), TStr::Fmt(), InferredNetwork, THash< TKey, TDat, THashFunc >::IsKey(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

1073  {
1074  TFOut FOut(OutFNm);
1075 
1076  // write edges to file (not allowing self loops in the network)
1077  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
1078  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
1079 
1080  // not allowing self loops in the Kronecker network
1081  if (EI.GetSrcNId() != EI.GetDstNId()) {
1082  if (EI().Len() > 0) {
1083  TStr Line; bool IsEdge = false;
1084  for (int i=0; i<EI().Len(); i++) {
1085  if (EI()[i]>MinAlpha) {
1086  Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) );
1087  IsEdge = true;
1088  } else { // we write 0 explicitly
1089  Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val);
1090  }
1091  }
1092  // if none of the alphas is bigger than 0, no edge is written
1093  if (IsEdge) {
1094  FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId()));
1095  FOut.PutStr(Line);
1096  FOut.PutStr("\r\n");
1097  }
1098  }
1099  else
1100  FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
1101  }
1102  }
1103 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
double Val
Definition: dt.h:1385
Definition: fl.h:319
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
Definition: dt.h:412
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SaveInferredPajek ( const TStr OutFNm,
const double &  Step,
const TIntV NIdV = TIntV() 
)

Definition at line 981 of file cascdynetinf.cpp.

References TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), THash< TKey, TDat, THashFunc >::BegI(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), InferredNetwork, TVec< TVal, TSizeTy >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

981  {
982  TFOut FOut(OutFNm);
983  FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len()));
984  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
985  if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }
986 
987  FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr()));
988  }
989  FOut.PutStr("*Arcs\r\n");
990  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
991  if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
992  if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
993 
994  double inferredAlpha = 0.0;
995  if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }
996 
997  if (inferredAlpha > MinAlpha) {
998  FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha)));
999  }
1000  }
1001 }
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
bool IsIn(const TVal &Val) const
Checks whether element Val is a member of the vector.
Definition: ds.h:828
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
TIter BegI() const
Definition: hash.h:213
double Val
Definition: dt.h:1385
Definition: fl.h:319
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
TIter EndI() const
Definition: hash.h:218
TFlt MinAlpha
Definition: cascdynetinf.h:155
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228

Here is the call graph for this function:

void TNIBs::SaveSites ( const TStr OutFNm,
const TIntFltVH CascadesPerNode = TIntFltVH() 
)

Definition at line 1153 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::EndI(), TStr::Fmt(), THash< TKey, TDat, THashFunc >::GetDat(), THash< TKey, TDat, THashFunc >::IsKey(), NodeNmH, and TSOut::PutStr().

1153  {
1154  TFOut FOut(OutFNm);
1155 
1156  // write nodes to file
1157  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
1158  FOut.PutStr(TStr::Fmt("%d,%s", NI.GetKey().Val, NI.GetDat().Name.CStr()));
1159  if (CascadesPerNode.IsKey(NI.GetKey().Val)) {
1160  for (int i=0; i<CascadesPerNode.GetDat(NI.GetKey().Val).Len(); i++) {
1161  FOut.PutStr(TStr::Fmt(",%f", CascadesPerNode.GetDat(NI.GetKey().Val)[i].Val));
1162  }
1163  }
1164  FOut.PutStr("\r\n");
1165  }
1166 }
TIter BegI() const
Definition: hash.h:213
Definition: fl.h:319
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
TIter EndI() const
Definition: hash.h:218
static TStr Fmt(const char *FmtStr,...)
Definition: dt.cpp:1599
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258

Here is the call graph for this function:

void TNIBs::SetAging ( const double &  aging)
inline

Definition at line 195 of file cascdynetinf.h.

References Aging.

195 { Aging = aging; }
TFlt Aging
Definition: cascdynetinf.h:153
void TNIBs::SetDelta ( const double &  delta)
inline

Definition at line 190 of file cascdynetinf.h.

References Delta.

190 { Delta = delta; }
TFlt Delta
Definition: cascdynetinf.h:150
void TNIBs::SetGamma ( const double &  gamma)
inline

Definition at line 194 of file cascdynetinf.h.

References Gamma.

194 { Gamma = gamma; }
TFlt Gamma
Definition: cascdynetinf.h:153
void TNIBs::SetInitAlpha ( const double &  ia)
inline

Definition at line 201 of file cascdynetinf.h.

References InitAlpha.

201 { InitAlpha = ia; }
TFlt InitAlpha
Definition: cascdynetinf.h:155
void TNIBs::SetK ( const double &  k)
inline

Definition at line 191 of file cascdynetinf.h.

References K.

191 { K = k; }
TFlt K
Definition: cascdynetinf.h:150
void TNIBs::SetMaxAlpha ( const double &  ma)
inline

Definition at line 199 of file cascdynetinf.h.

References MaxAlpha.

199 { MaxAlpha = ma; }
TFlt MaxAlpha
Definition: cascdynetinf.h:155
void TNIBs::SetMinAlpha ( const double &  ma)
inline

Definition at line 200 of file cascdynetinf.h.

References MinAlpha.

200 { MinAlpha = ma; }
TFlt MinAlpha
Definition: cascdynetinf.h:155
void TNIBs::SetModel ( const TModel model)
inline

Definition at line 186 of file cascdynetinf.h.

References Model.

186 { Model = model; }
TModel Model
Definition: cascdynetinf.h:144
void TNIBs::SetMu ( const double &  mu)
inline

Definition at line 197 of file cascdynetinf.h.

References Mu.

197 { Mu = mu; }
TFlt Mu
Definition: cascdynetinf.h:153
void TNIBs::SetRegularizer ( const TRegularizer reg)
inline

Definition at line 196 of file cascdynetinf.h.

References Regularizer.

196 { Regularizer = reg; }
TRegularizer Regularizer
Definition: cascdynetinf.h:154
void TNIBs::SetTolerance ( const double &  tol)
inline

Definition at line 198 of file cascdynetinf.h.

References Tol.

198 { Tol = tol; }
TFlt Tol
Definition: cascdynetinf.h:155
void TNIBs::SetTotalTime ( const float &  tt)
inline

Definition at line 185 of file cascdynetinf.h.

References TotalTime.

185 { TotalTime = tt; }
TFlt TotalTime
Definition: cascdynetinf.h:147
void TNIBs::SetWindow ( const double &  window)
inline

Definition at line 187 of file cascdynetinf.h.

References Window.

187 { Window = window; }
TFlt Window
Definition: cascdynetinf.h:147
void TNIBs::SG ( const int &  NId,
const int &  Iters,
const TFltV Steps,
const TSampling Sampling,
const TStr ParamSampling = TStr(""),
const bool &  PlotPerformance = false 
)

Definition at line 381 of file cascdynetinf.cpp.

References THash< TKey, TDat, THashFunc >::AddDat(), Aging, AveDiffAlphas, TNodeEDatNet< TNodeData, TEdgeData >::BegEI(), CascH, THash< TKey, TDat, THashFunc >::Clr(), ComputePerformanceNId(), TNodeEDatNet< TNodeData, TEdgeData >::EndEI(), EXP_SAMPLING, Gamma, THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), THash< TKey, TDat, THashFunc >::GetKey(), TRnd::GetUniDevInt(), InferredNetwork, TNodeEDatNet< TNodeData, TEdgeData >::IsEdge(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal, TSizeTy >::Len(), MaxAlpha, Mu, OSG, RAY_SAMPLING, Regularizer, Reset(), TInt::Rnd, TFlt::Rnd, THash< TKey, TDat, THashFunc >::SortByDat(), TStr::SplitOnAllCh(), Tol, UNIF_SAMPLING, UpdateDiff(), TFlt::Val, WIN_EXP_SAMPLING, and WIN_SAMPLING.

381  {
382  bool verbose = false;
383  int currentCascade = -1;
384  TIntIntH SampledCascades;
385  TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV);
386 
387  Reset();
388 
389  printf("Node %d\n", NId);
390 
391  // traverse through all times
392  for (int t=1; t<Steps.Len(); t++) {
393  // find cascades whose two first infections are earlier than Steps[t]
394  TIntFltH CascadesIdx;
395  int num_infections = 0;
396  for (int i=0; i<CascH.Len(); i++) {
397  if (CascH[i].LenBeforeT(Steps[t]) > 1 &&
398  ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) ||
399  (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) ||
400  (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) {
401  num_infections += CascH[i].LenBeforeT(Steps[t]);
402  CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
403  }
404  }
405 
406  // if there are not recorded cascades by Steps[t], continue
407  if (CascadesIdx.Len()==0) {
408  printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
409  if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
410  continue;
411  }
412 
413  // later cascades first
414  CascadesIdx.SortByDat(false);
415 
416  printf("Solving step %f: %d cascades, %d infections\n", Steps[t].Val, CascadesIdx.Len(), num_infections);
417  SampledCascades.Clr();
418 
419  // sampling cascades with no replacement
420  for (int i=0; i < Iters; i++) {
421  switch (Sampling) {
422  case UNIF_SAMPLING:
423  currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
424  break;
425 
426  case WIN_SAMPLING:
427  currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
428  break;
429 
430  case EXP_SAMPLING:
431  do {
432  currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt());
433  } while (currentCascade > CascadesIdx.Len()-1);
434  break;
435 
436  case WIN_EXP_SAMPLING:
437  do {
438  currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt());
439  } while (currentCascade > CascadesIdx.Len()-1);
440  break;
441 
442  case RAY_SAMPLING:
443  do {
444  currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt());
445  } while (currentCascade > CascadesIdx.Len()-1);
446  break;
447  }
448 
449  if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; }
450  SampledCascades.GetDat(currentCascade)++;
451 
452  if (verbose) { printf("Cascade %d sampled!\n", currentCascade); }
453 
454  // sampled cascade
455  TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)];
456 
457  // update gradient and alpha's
458  TIntPrV AlphasToUpdate;
459  UpdateDiff(OSG, NId, Cascade, AlphasToUpdate, Steps[t]);
460 
461  // update alpha's
462  for (int j=0; j<AlphasToUpdate.Len(); j++) {
463  if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
464  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])
465  ) {
466  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
467  (Gamma * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1)
468  - (Regularizer==1? Mu*InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) : 0.0));
469 
470  // project into alpha >= 0
471  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
472  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
473  }
474 
475  // project into alpha <= MaxAlpha
476  if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
477  InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
478  }
479  }
480  }
481  if (verbose) { printf("%d transmission rates updated!\n", AlphasToUpdate.Len()); }
482  }
483 
484  printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val);
485 
486  // For alphas that did not get updated, copy last alpha value * aging factor
487  int unchanged = 0;
488  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
489  if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }
490 
491  EI().AddDat(Steps[t]) = Aging*EI().GetDat(Steps[t-1]);
492  unchanged++;
493  }
494  if (verbose) { printf("%d transmission rates that did not changed were 'aged' by %f!\n", unchanged, Aging.Val); }
495 
496  // compute performance on-the-fly
497  if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
498  }
499 }
TFlt Mu
Definition: cascdynetinf.h:153
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the network.
Definition: network.h:758
THash< TInt, TCascade > CascH
Definition: cascdynetinf.h:132
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt MaxAlpha
Definition: cascdynetinf.h:155
double Val
Definition: dt.h:1385
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
static TRnd Rnd
Definition: dt.h:1143
TIntFltH AveDiffAlphas
Definition: cascdynetinf.h:162
TFlt Gamma
Definition: cascdynetinf.h:153
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the network.
Definition: network.h:756
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 network.
Definition: network.h:939
void ComputePerformanceNId(const int &NId, const int &Step, const TFltV &Steps)
void Reset()
TFlt Aging
Definition: cascdynetinf.h:153
void UpdateDiff(const TOptMethod &OptMethod, const int &NId, TCascade &Cascade, TIntPrV &AlphasToUpdate, const double &CurrentTime=TFlt::Mx)
void SplitOnAllCh(const char &SplitCh, TStrV &StrV, const bool &SkipEmpty=true) const
Definition: dt.cpp:926
Definition: hash.h:97
TRegularizer Regularizer
Definition: cascdynetinf.h:154
void Clr(const bool &DoDel=true, const int &NoDelLim=-1, const bool &ResetDat=true)
Definition: hash.h:361
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
bool IsKey(const TKey &Key) const
Definition: hash.h:258
int Len() const
Definition: hash.h:228
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
const TKey & GetKey(const int &KeyId) const
Definition: hash.h:252
static TRnd Rnd
Definition: dt.h:1393
TFlt Tol
Definition: cascdynetinf.h:155
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

void TNIBs::SortNodeNmByVol ( const bool &  asc = false)
inline

Definition at line 219 of file cascdynetinf.h.

References NodeNmH, and THash< TKey, TDat, THashFunc >::SortByDat().

219 { NodeNmH.SortByDat(asc); }
THash< TInt, TNodeInfo > NodeNmH
Definition: cascdynetinf.h:133
void SortByDat(const bool &Asc=true)
Definition: hash.h:292

Here is the call graph for this function:

void TNIBs::UpdateDiff ( const TOptMethod OptMethod,
const int &  NId,
TCascade Cascade,
TIntPrV AlphasToUpdate,
const double &  CurrentTime = TFlt::Mx 
)

Definition at line 721 of file cascdynetinf.cpp.

References TVec< TVal, TSizeTy >::Add(), THash< TKey, TDat, THashFunc >::AddDat(), TNodeEDatNet< TNodeData, TEdgeData >::AddEdge(), AveDiffAlphas, TCascade::BegI(), Delta, TCascade::EndI(), EXP, THash< TKey, TDat, THashFunc >::GetDat(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), TCascade::GetMinTm(), TCascade::GetTm(), IAssert, InferredNetwork, InitAlpha, TNodeEDatNet< TNodeData, TEdgeData >::IsEdge(), THash< TKey, TDat, THashFunc >::IsKey(), TCascade::IsNode(), TNodeEDatNet< TNodeData, TEdgeData >::IsNode(), Model, TMath::Mx(), OBSG, OEBSG, OESG, OFG, OSG, POW, TMath::Power(), RAY, and Window.

Referenced by BSG(), FG(), and SG().

721  {
723 
724  double sum = 0.0;
725 
726  // we assume cascade is sorted & iterator returns in sorted order
727  if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) {
728  for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
729  // consider only nodes that are earlier in time
730  if ( (Cascade.GetTm(NId)<=NI.GetDat().Tm) ||
731  (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW)
732  ) { break; }
733 
734  TIntPr Pair(NI.GetKey(), NId);
735 
736  // if edge/alpha doesn't exist, create
737  if (!InferredNetwork.IsEdge(Pair.Val1, Pair.Val2)) { InferredNetwork.AddEdge(Pair.Val1, Pair.Val2, TFltFltH()); }
738  if (!InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).IsKey(CurrentTime)) {
739  InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).AddDat(CurrentTime) = InitAlpha;
740  }
741 
742  switch(Model) {
743  case EXP: // exponential
744  sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val;
745  break;
746  case POW: // powerlaw
747  sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val/(Cascade.GetTm(NId)-NI.GetDat().Tm);
748  break;
749  case RAY: // rayleigh
750  sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val*(Cascade.GetTm(NId)-NI.GetDat().Tm);
751  break;
752  default:
753  sum = 0.0;
754  }
755  }
756  }
757 
758  // we assume cascade is sorted & iterator returns in sorted order
759  for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
760  // only consider nodes that are earlier in time if node belongs to the cascade
761  if ( Cascade.IsNode(NId) && (Cascade.GetTm(NId)<=NI.GetDat().Tm ||
762  (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW))
763  ) { break; }
764 
765  // consider infected nodes up to CurrentTime
766  if (NI.GetDat().Tm > CurrentTime) { break; }
767 
768  TIntPr Pair(NI.GetKey(), NId); // potential edge
769 
770  double val = 0.0;
771 
772  if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) {
773  IAssert((Cascade.GetTm(NId) - NI.GetDat().Tm) > 0.0);
774 
775  switch(Model) { // compute gradients for infected
776  case EXP: // exponential
777  val = (Cascade.GetTm(NId) - NI.GetDat().Tm) - 1.0/sum;
778  break;
779  case POW: // powerlaw
780  val = log((Cascade.GetTm(NId) - NI.GetDat().Tm)/Delta) - 1.0/((Cascade.GetTm(NId)-NI.GetDat().Tm)*sum);
781  break;
782  case RAY: // rayleigh
783  val = TMath::Power(Cascade.GetTm(NId)-NI.GetDat().Tm, 2.0)/2.0 - (Cascade.GetTm(NId)-NI.GetDat().Tm)/sum;
784  break;
785  default:
786  val = 0.0;
787  }
788  } else { // compute gradients for non infected
789  IAssert((CurrentTime - NI.GetDat().Tm) >= 0.0);
790 
791  switch(Model) {
792  case EXP: // exponential
793  val = (CurrentTime-NI.GetDat().Tm);
794  // if every cascade was recorded up to a maximum Window cut-off
795  if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = (Cascade.GetMinTm()+Window-NI.GetDat().Tm); }
796  break;
797  case POW: // power-law
798  val = TMath::Mx(log((CurrentTime-NI.GetDat().Tm)/Delta), 0.0);
799  // if every cascade was recorded up to a maximum Window cut-off
800  if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Mx(log((Cascade.GetMinTm()+Window-NI.GetDat().Tm)/Delta), 0.0); }
801  break;
802  case RAY: // rayleigh
803  val = TMath::Power(CurrentTime-NI.GetDat().Tm,2.0)/2.0;
804  // if every cascade was recorded up to a maximum Window cut-off
805  if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Power(Cascade.GetMinTm()+Window-NI.GetDat().Tm,2.0)/2.0; }
806  break;
807  default:
808  val = 0.0;
809  }
810  }
811 
812  if (!AveDiffAlphas.IsKey(Pair.Val1)) { AveDiffAlphas.AddDat(Pair.Val1) = 0.0; }
813 
814  switch (OptMethod) {
815  case OBSG:
816  case OEBSG:
817  case OFG:
818  // update stochastic average gradient (based on batch for OBSG and OEBSG and based on all cascades for FG)
819  AveDiffAlphas.GetDat(Pair.Val1) += val;
820  break;
821  case OSG:
822  case OESG:
823  // update stochastic gradient (we use a single gradient due to current cascade)
824  AveDiffAlphas.GetDat(Pair.Val1) = val;
825  default:
826  break;
827  }
828 
829  AlphasToUpdate.Add(Pair);
830  }
831 
832  return;
833 }
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: network.h:708
#define IAssert(Cond)
Definition: bd.h:262
static const T & Mx(const T &LVal, const T &RVal)
Definition: xmath.h:32
THash< TInt, THitInfo >::TIter EndI() const
Definition: cascdynetinf.h:102
TStrFltFltHNEDNet InferredNetwork
Definition: cascdynetinf.h:158
TFlt Window
Definition: cascdynetinf.h:147
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
bool GetEDat(const int &SrcNId, const int &DstNId, TEdgeData &EdgeDat) const
Returns edge data in Data for the edge from node IDs SrcNId to DstNId.
Definition: network.h:953
TIntFltH AveDiffAlphas
Definition: cascdynetinf.h:162
double GetMinTm() const
Definition: cascdynetinf.h:106
TFlt Delta
Definition: cascdynetinf.h:150
double GetTm(const int &NId) const
Definition: cascdynetinf.h:104
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the network.
Definition: network.h:906
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 network.
Definition: network.h:939
static double Power(const double &Base, const double &Exponent)
Definition: xmath.h:25
TFlt InitAlpha
Definition: cascdynetinf.h:155
Definition: ds.h:32
THash< TInt, THitInfo >::TIter BegI() const
Definition: cascdynetinf.h:101
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
bool IsNode(const int &NId) const
Definition: cascdynetinf.h:109
TModel Model
Definition: cascdynetinf.h:144

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

TFltPrV TNIBs::Accuracy

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

TFlt TNIBs::Aging

Definition at line 153 of file cascdynetinf.h.

Referenced by SetAging(), and SG().

TIntFltH TNIBs::AveDiffAlphas

Definition at line 162 of file cascdynetinf.h.

Referenced by BSG(), FG(), Reset(), SG(), and UpdateDiff().

TStrIntH TNIBs::CascadeIdH

Definition at line 135 of file cascdynetinf.h.

Referenced by GetCascadeId().

THash<TInt, TCascade> TNIBs::CascH

Definition at line 132 of file cascdynetinf.h.

Referenced by AddCasc(), BSG(), FG(), GetCasc(), GetCascs(), IsCascade(), Save(), SaveCascades(), and SG().

THash<TIntPr, TIntV> TNIBs::CascPerEdge

Definition at line 138 of file cascdynetinf.h.

Referenced by Save().

TFlt TNIBs::Delta

Definition at line 150 of file cascdynetinf.h.

Referenced by GenCascade(), SetDelta(), and UpdateDiff().

THash<TInt, TIntFltH> TNIBs::DiffAlphas

Definition at line 163 of file cascdynetinf.h.

Referenced by Reset().

TFlt TNIBs::Gamma

Definition at line 153 of file cascdynetinf.h.

Referenced by BSG(), FG(), SetGamma(), and SG().

TFlt TNIBs::InitAlpha

Definition at line 155 of file cascdynetinf.h.

Referenced by SetInitAlpha(), and UpdateDiff().

TFlt TNIBs::K

Definition at line 150 of file cascdynetinf.h.

Referenced by SetK().

TFltPrV TNIBs::MAE

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

TModel TNIBs::Model

Definition at line 144 of file cascdynetinf.h.

Referenced by GenCascade(), LoadCascadesTxt(), SetModel(), TNIBs(), and UpdateDiff().

TFltPrV TNIBs::MSE

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

TFlt TNIBs::Mu

Definition at line 153 of file cascdynetinf.h.

Referenced by BSG(), FG(), SetMu(), and SG().

TFltPrV TNIBs::PrecisionRecall

Definition at line 169 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

TRegularizer TNIBs::Regularizer

Definition at line 154 of file cascdynetinf.h.

Referenced by BSG(), FG(), SetRegularizer(), and SG().

TIntIntPrH TNIBs::SampledCascadesH

Definition at line 166 of file cascdynetinf.h.

Referenced by Reset().

TFlt TNIBs::Tol

Definition at line 155 of file cascdynetinf.h.

Referenced by BSG(), FG(), SetTolerance(), and SG().

TIntFltH TNIBs::TotalCascadesAlpha

Definition at line 159 of file cascdynetinf.h.

Referenced by Init(), and Reset().

TFlt TNIBs::TotalTime

Definition at line 147 of file cascdynetinf.h.

Referenced by GenCascade(), and SetTotalTime().

TFlt TNIBs::Window

Definition at line 147 of file cascdynetinf.h.

Referenced by GenCascade(), SetWindow(), and UpdateDiff().


The documentation for this class was generated from the following files: