SNAP Library , Developer Reference  2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TNIBs Class Reference

#include <cascdynetinf.h>

Collaboration diagram for TNIBs:

List of all members.

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.

{ }
TNIBs::TNIBs ( TSIn SIn) [inline]

Definition at line 174 of file cascdynetinf.h.

References EXP, and Model.

: CascH(SIn), NodeNmH(SIn), CascPerEdge(SIn), InferredNetwork(SIn) { Model = EXP; }

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 >::Len(), TCascade::Sort(), TStr::SplitOnAllCh(), and TNodeInfo::Vol.

Referenced by AddCasc(), and LoadCascadesTxt().

                                                            {
  int CId = CascH.Len();

  // support cascade id if any
  TStrV FieldsV; CascStr.SplitOnAllCh(';', FieldsV);
  if (FieldsV.Len()==2) { CId = FieldsV[0].GetInt(); }

  // read nodes
    TStrV NIdV; FieldsV[FieldsV.Len()-1].SplitOnAllCh(',', NIdV);
    TCascade C(CId, Model);
    for (int i = 0; i < NIdV.Len(); i+=2) {
      int NId;
      double Tm; 
      NId = NIdV[i].GetInt();
      Tm = NIdV[i+1].GetFlt();
      GetNodeInfo(NId).Vol = GetNodeInfo(NId).Vol + 1;
      C.Add(NId, Tm);
    }
    C.Sort();

    AddCasc(C);
}

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.

{ CascH.AddDat(Cascade.CId) = Cascade; }

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().

                                                                                {
  TCascade C(CId, Model);
  for (TIntFltH::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
    GetNodeInfo(NI.GetKey()).Vol = GetNodeInfo(NI.GetKey()).Vol + 1;
    C.Add(NI.GetKey(), NI.GetDat());
  }
  C.Sort();
  AddCasc(C);
}

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().

{ DomainsIdH.AddDat(Domain) = TInt(DomainId==-1? DomainsIdH.Len() : DomainId); }

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().

{ NodeNmH.AddDat(NId, Info); }

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 >::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.

                                                                                                                                                                            {
  bool verbose = false;
  int currentCascade = -1;
  TIntIntH SampledCascades;
  TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV);

  Reset();

  printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes());

  // traverse through all times (except 0.0; we use 0.0 to compute mse/mae since the inference is delayed)
  for (int t=1; t<Steps.Len(); t++) {
    // find cascades whose two first infections are earlier than Steps[t]
    TIntFltH CascadesIdx;
    int num_infections = 0;
    for (int i = 0; i < CascH.Len(); i++) {
      if (CascH[i].LenBeforeT(Steps[t]) > 1 &&
            ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) ||
          (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) ||
          (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) {
        num_infections += CascH[i].LenBeforeT(Steps[t]);
        CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
      }
    }

    // if there are not recorded cascades by Steps[t], continue
    if (CascadesIdx.Len() == 0) {
      printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
      if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
      continue;
    }

    printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val,
        CascadesIdx.Len(), num_infections);

    // sort cascade from most recent to least recent
    CascadesIdx.SortByDat(false);

    // sampling cascades with no replacement
    for (int i=0; i < Iters; i++) {
      // alphas to update
      TIntPrV AlphasToUpdate;

      // delete average gradients
      AveDiffAlphas.Clr();

      // use all cascades for the average gradient
      for (int c=0; c<BatchLen; c++) {
        switch (Sampling) {
          case UNIF_SAMPLING:
            currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
            break;

          case WIN_SAMPLING:
            currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
            break;

          case EXP_SAMPLING:
            do {
              currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt());
            } while (currentCascade > CascadesIdx.Len()-1);
            break;

          case WIN_EXP_SAMPLING:
            do {
              currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt());
            } while (currentCascade > CascadesIdx.Len()-1);
            break;

          case RAY_SAMPLING:
            do {
              currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt());
            } while (currentCascade > CascadesIdx.Len()-1);
            break;
        }

        // sampled cascade
        TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)];

        if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; }
        SampledCascades.GetDat(currentCascade)++;

        // update gradient and alpha's
        UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]);
      }

      // update alpha's
      for (int j=0; j<AlphasToUpdate.Len(); j++) {
        if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) {
          switch (Regularizer) {
            case 0:
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
                Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
            case 1:
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) =
                InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)BatchLen)
                - Gamma * (1.0/(double)BatchLen) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
          }

          // project into alpha >= 0
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
          }

          // project into alpha <= MaxAlpha
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
          }
        }
      }

      // for alphas that did not get updated, copy last alpha value
      int unchanged = 0;
      for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
        if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }

        EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]);
        unchanged++;
      }
      if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); }
    }

    printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val);

    // compute performance on-the-fly for each time step
    if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
  }
}

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().

                                                                                  {
  double CurrentMAE = 0.0;
  double CurrentMSE = 0.0;
  TFltPr CurrentPrecisionRecall(0.0, 0.0);
  double CurrentAccD = 0.0;

  TStrFltFltHNEDNet::TNodeI NI = InferredNetwork.GetNI(NId);
  for (int i=0; i<NI.GetInDeg(); i++) {
    double inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]);
    // we get the true alpha in the previous step (the inferred network contains delayed estimates)
    double trueAlpha = 0.0;
    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]); }

    // accuracy (denominator)
    CurrentAccD += (double) (inferredAlpha > MinAlpha);

    // precision
    CurrentPrecisionRecall.Val2 += (double) (inferredAlpha > MinAlpha && trueAlpha<MinAlpha);
  }

  NI = Network.GetNI(NId);
  int NumEdges = 0;
  for (int i=0; i<NI.GetInDeg(); i++) {
    TIntPr Pair(NI.GetInNId(i), NId);

    // we get the inferred Alpha if any
    double inferredAlpha = 0.0;
    if (InferredNetwork.IsEdge(NI.GetInNId(i), NId) && InferredNetwork.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t])) {
      inferredAlpha = InferredNetwork.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t]).Val;
    }

    // we get the true alpha in the previous step (the inferred network contains delayed estimates)
    double trueAlpha = 0.0;
    if (Network.GetEDat(NI.GetInNId(i), NId).IsKey(Steps[t-1])) { trueAlpha = Network.GetEDat(NI.GetInNId(i), NId).GetDat(Steps[t-1]); }

    // mae
    if (trueAlpha > MinAlpha) {
      NumEdges++;
      CurrentMAE += fabs(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha))/trueAlpha;
    }

    // mse
    CurrentMSE += pow(trueAlpha - TFlt::GetMn(inferredAlpha, MaxAlpha), 2.0);

    // recall
    CurrentPrecisionRecall.Val1 += (double) (inferredAlpha > MinAlpha && trueAlpha > MinAlpha);
  }

  if (NumEdges > 0) {
    MAE[t-1].Val2 += CurrentMAE / ((double)(NumEdges*Network.GetNodes()));
    MSE[t-1].Val2 += CurrentMSE / ((double)(NumEdges*Network.GetNodes()));
    PrecisionRecall[t-1].Val1 += CurrentPrecisionRecall.Val1/(double)(NumEdges*Network.GetNodes());
  }

  if (CurrentAccD > 0) {
    PrecisionRecall[t-1].Val2 += (1.0 - CurrentPrecisionRecall.Val2 / CurrentAccD)/(double)Network.GetNodes();
  } else {
    PrecisionRecall[t-1].Val2 += 1.0/(double)Network.GetNodes();
  }

  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));
}

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 >::Len(), MaxAlpha, Mu, OBSG, Regularizer, Reset(), THash< TKey, TDat, THashFunc >::SortByDat(), Tol, and UpdateDiff().

                                                                   {
  bool verbose = false;
  
  Reset();

  printf("Node %d (|A|: %d)\n", NId, InferredNetwork.GetNodes());

  // traverse through all times
  for (int t=1; t<Steps.Len(); t++) {
    // find cascades whose two first infections are earlier than Steps[t]
    TIntFltH CascadesIdx;
    int num_infections = 0;
    for (int i=0; i<CascH.Len(); i++) {
      if (CascH[i].LenBeforeT(Steps[t]) > 1) {
        num_infections += CascH[i].LenBeforeT(Steps[t]);
        CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
      }
    }

    // if there are not recorded cascades by Steps[t], continue
    if (CascadesIdx.Len()==0) {
      printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
//      ComputePerformance(NId, Tol, t, Steps);
      continue;
    }

    printf("Solving step %f (%d cascades, %d infections)\n", Steps[t].Val, CascadesIdx.Len(), num_infections);

    // sort cascade from most recent to least recent
    CascadesIdx.SortByDat(false);

    // sampling cascades with no replacement
    for (int i=0; i < Iters; i++) {
      // alphas to update
      TIntPrV AlphasToUpdate;

      // delete average gradients
      AveDiffAlphas.Clr();

      // use all cascades for the average gradient
      for (int c=0; c<CascadesIdx.Len(); c++) {
        // each cascade
        TCascade &Cascade = CascH[CascadesIdx.GetKey(c)];

        // update gradient and alpha's
        UpdateDiff(OBSG, NId, Cascade, AlphasToUpdate, Steps[t]);
      }

      // update alpha's
      for (int j=0; j<AlphasToUpdate.Len(); j++) {
        if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])) {
          switch (Regularizer) {
            case 0:
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
                Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
            case 1:
              InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) =
                InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t])*(1.0-Mu*Gamma/(double)CascadesIdx.Len())
                - Gamma * (1.0/(double)CascadesIdx.Len()) * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1);
          }

          // project into alpha >= 0
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
          }

          // project into alpha <= MaxAlpha
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
          }
        }
      }

      // for alphas that did not get updated, copy last alpha value
      int unchanged = 0;
      for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
        if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }

        EI().AddDat(Steps[t]) = EI().GetDat(Steps[t-1]);
        unchanged++;
      }
      if (verbose) { printf("%d unchanged transmission rates updated!\n", unchanged); }
    }

    // compute performance on-the-fly for each time step
    ComputePerformanceNId(NId, t, Steps);
  }
}

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 >::Len().

Referenced by LabelBurstAutomaton().

                                                                                                                   {
  if ( t >= x.Len() ) return;
  if ( t == 0 ){
    C = TFltVV( x.Len(), k );
  }else{
    int n = x.Len() - 1;
    for (int j = 0; j < k; j++){
      double alpha = ( (x.Len() ) / T ) * pow( s, j );
      double term_1 = -log(alpha) + alpha * x[t];
      double term_2 = 0;
      if ( t == 1 ){
        term_2 = j * log(n) * gamma;
      }
      else{
        bool first = false;
        for (int l = 0; l < k; l++){
          double my_val = C(t-1, l);
          if ( j > l ) my_val += (j - l) * log(n) * gamma;
          if ( !first || my_val < term_2 ){
            term_2 = my_val;
            first = true;
          }
        }
      }
      C( t, j ) = term_1 + term_2;
    }
  }
  find_C( t + 1, x, C, k, s, gamma, 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 >::GetCols(), and TVVec< TVal >::GetRows().

Referenced by LabelBurstAutomaton().

                                                                                                                         {
  states = TIntV( C.GetRows() );
  states[0] = 0;
  int n = C.GetRows() - 1;
  for (int t = C.GetRows() - 1; t > 0; t --){
    double best_val = 0;
    int best_state = -1;
    for (int j = 0; j < C.GetCols(); j++){
      double c_state = C( t, j );
      if ( t < C.GetRows() - 2 && states[t+1] > j ){
        c_state += ( states[t+1] - j ) * gamma * log(n);
      }
      if ( best_state == -1 || best_val > c_state ){
        best_state = j;
        best_val = c_state;
      }
    }
    states[t] = best_state;
  }
}

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.

                                  {
  bool verbose = false;
  TIntFltH InfectedNIdH; TIntH InfectedBy;
  double GlobalTime, InitTime;
  double alpha;
  int StartNId;

  if (Network.GetNodes() == 0)
    return;

        // set random seed
        TInt::Rnd.Randomize();

  while (C.Len() < 2) {
    C.Clr();
    InfectedNIdH.Clr();
    InfectedBy.Clr();

    InitTime = TFlt::Rnd.GetUniDev() * TotalTime; // random starting point <TotalTime
    GlobalTime = InitTime;

    StartNId = Network.GetRndNId();
    InfectedNIdH.AddDat(StartNId) = GlobalTime;

    while (true) {
      // sort by time & get the oldest node that did not run infection
      InfectedNIdH.SortByDat(true);
      const int& NId = InfectedNIdH.BegI().GetKey();
      GlobalTime = InfectedNIdH.BegI().GetDat();

      // all the nodes has run infection
      if ( GlobalTime >= TFlt::GetMn(TotalTime, InitTime+Window) )
        break;

      // add current oldest node to the network and set its time
      C.Add(NId, GlobalTime);

      if (verbose) { printf("GlobalTime:%f, infected node:%d\n", GlobalTime, NId); }

      // run infection from the current oldest node
      TStrFltFltHNEDNet::TNodeI NI = Network.GetNI(NId);
      for (int e = 0; e < NI.GetOutDeg(); e++) {
        const int DstNId = NI.GetOutNId(e);

        // choose the current tx rate (we assume the most recent tx rate)
        TFltFltH& Alphas = Network.GetEDat(NId, DstNId);
        for (int j=0; j<Alphas.Len() && Alphas.GetKey(j)<GlobalTime; j++) { alpha = Alphas[j]; }
        if (verbose) { printf("GlobalTime:%f, nodes:%d->%d, alpha:%f\n", GlobalTime, NId, DstNId, alpha); }

        if (alpha<1e-9) { continue; }

        // not infecting the parent
        if (InfectedBy.IsKey(NId) && InfectedBy.GetDat(NId).Val == DstNId)
          continue;

        double sigmaT;
        switch (Model) {
        case EXP:
          // exponential with alpha parameter
          sigmaT = TInt::Rnd.GetExpDev(alpha);
          break;
        case POW:
          // power-law with alpha parameter
          sigmaT = TInt::Rnd.GetPowerDev(1+alpha);
          while (sigmaT < Delta) { sigmaT = Delta*TInt::Rnd.GetPowerDev(1+alpha); }
          break;
        case RAY:
          // rayleigh with alpha parameter
          sigmaT = TInt::Rnd.GetRayleigh(1/sqrt(alpha));
          break;
        default:
          sigmaT = 1;
          break;
        }

        IAssert(sigmaT >= 0);

        double t1 = TFlt::GetMn(GlobalTime + sigmaT, TFlt::GetMn(InitTime+Window, TotalTime));

        if (InfectedNIdH.IsKey(DstNId)) {
          double t2 = InfectedNIdH.GetDat(DstNId);
          if ( t2 > t1 && t2 < TFlt::GetMn(InitTime+Window, TotalTime)) {
            InfectedNIdH.GetDat(DstNId) = t1;
            InfectedBy.GetDat(DstNId) = NId;
          }
        } else {
          InfectedNIdH.AddDat(DstNId) = t1;
          InfectedBy.AddDat(DstNId) = NId;
        }
      }

      // we cannot delete key (otherwise, we cannot sort), so we assign a big time (InitTime + window cut-off)
      InfectedNIdH.GetDat(NId) = TFlt::GetMn(InitTime+Window, TotalTime);
    }
    }

  C.Sort();
}

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().

{ return CascH.GetDat(c); }

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().

{ return CascadeIdH.GetDat(Cascade); }

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().

{ return CascH.Len(); }

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().

{ return DomainsIdH.GetDat(Domain); }

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.

                                                                        {
  GraphAtT = TNGraph::New();

  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); }

  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
    double Alpha = 0.0;
    if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); }

    if (Alpha > MinAlpha) {
      GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
    }
  }
}

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.

                                                                                  {
  NetworkAtT = TStrFltNEDNet::New();

  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name); }

  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
    double Alpha = 0.0;
    if (EI().IsKey(Step)) { Alpha = EI().GetDat(Step); }

    if (Alpha > MinAlpha) {
      NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), Alpha);
    }
  }
}

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.

                                                                     {
  GraphAtT = TNGraph::New();

  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) { GraphAtT->AddNode(NI.GetKey()); }

  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    double inferredAlpha = 0.0;
    if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }

    if (inferredAlpha > MinAlpha) {
      GraphAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId());
    }
  }
}

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.

                                                                               {
  NetworkAtT = TStrFltNEDNet::New();

  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    NetworkAtT->AddNode(NI.GetKey(), NI.GetDat().Name);
  }

  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    double inferredAlpha = 0.0;
    if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }

    if (inferredAlpha > MinAlpha) {
      NetworkAtT->AddEdge(EI.GetSrcNId(), EI.GetDstNId(), inferredAlpha);
    }
  }
}

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().

{ return NodeNmH.GetDat(NId); }

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.

{ return NodeNmH.GetDat(NId).Name; }

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.

{ return InferredNetwork.GetNodes(); }

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 >::Add(), TNodeEDatNet< TNodeData, TEdgeData >::AddNode(), THash< TKey, TDat, THashFunc >::BegI(), THash< TKey, TDat, THashFunc >::Clr(), TVec< TVal >::Clr(), TNodeEDatNet< TNodeData, TEdgeData >::Clr(), THash< TKey, TDat, THashFunc >::EndI(), InferredNetwork, TVec< TVal >::Len(), MAE, MSE, NodeNmH, PrecisionRecall, and TotalCascadesAlpha.

                                   {
  // inferred network
  InferredNetwork.Clr();

  // copy nodes from NodeNmH (it will be filled by loading cascades or loading groundtruth)
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    InferredNetwork.AddNode(NI.GetKey(), NI.GetDat().Name);
  }

  // performance measures
  PrecisionRecall.Clr();
  Accuracy.Clr();
  MAE.Clr();
  TotalCascadesAlpha.Clr();

  for (int i=0; i<Steps.Len()-1; i++) {
    MAE.Add(TFltPr(Steps[i], 0.0));
    MSE.Add(TFltPr(Steps[i], 0.0));
    Accuracy.Add(TFltPr(Steps[i], 0.0));
    PrecisionRecall.Add(TFltPr(0.0,0.0));
  }
}

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().

{ return CascH.IsKey(c); }

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().

{ return DomainsIdH.IsKey(Domain); }

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().

{ return NodeNmH.IsKey(NId); }

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 >::Add(), find_C(), find_min_state(), TSecTm::GetAbsSecs(), TNodeEDatNet< TNodeData, TEdgeData >::GetEDat(), THash< TKey, TDat, THashFunc >::GetKey(), InferredNetwork, TVec< TVal >::Last(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal >::Len(), MinAlpha, Network, and TVec< TVal >::Sort().

                                                                                                                                                                                                                                    {
  TVec<TSecTm> arrival_times;
  TFltFltH &LinksEdge = (inferred? InferredNetwork.GetEDat(SrcId, DstId) : Network.GetEDat(SrcId, DstId));

  for (int i=0; i<LinksEdge.Len(); i++) {
    if (LinksEdge[i]>MinAlpha) {
      TSecTm tsecs;
      tsecs = (uint)LinksEdge.GetKey(i); // link rates is in seconds
      if (tsecs > MaxTime || tsecs < MinTime) { continue; }
      arrival_times.Add(tsecs);
    }
  }

  if ( arrival_times.Len() < 2 ) return;

  TFltV x;
  x.Add( 0 );
  arrival_times.Sort(true);
  double T = ((double)arrival_times.Last().GetAbsSecs()) - ((double)arrival_times[0].GetAbsSecs());
  for (int i = 1; i < arrival_times.Len(); i++){
    x.Add( ((double)arrival_times[i].GetAbsSecs()) - ((double)arrival_times[i-1].GetAbsSecs()) );
  }
  TFltVV Cost_matrix;
  //printf("arrivals = %d\n", x.Len() );
  find_C( 0, x, Cost_matrix, k, s, gamma, T);

  find_min_state( Cost_matrix, state_labels, k, s, gamma, T );

  for (int i=0; i<state_labels.Len(); i++) { state_times.Add((double)arrival_times[i].GetAbsSecs()); }
}

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().

                                     {
  TStr Line;
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    if (Line=="") { break; }
    TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
    AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); 
  }
  printf("All nodes read!\n");
  while (!SIn.Eof()) { SIn.GetNextLn(Line); AddCasc(Line, Model); }

  printf("All cascades read!\n");
}

Here is the call graph for this function:

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().

                                             {
  TStr Line;

  Network.Clr(); // clear network (if any)

  // add nodes
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    if (Line=="") { break; }
    TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
    Network.AddNode(NIdV[0].GetInt(), NIdV[1]);
    if (!IsNodeNm(NIdV[0].GetInt())) {
      AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0));
      DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt();
    }
  }

  printf("groundtruth nodes:%d\n", Network.GetNodes());
}

Here is the call graph for this function:

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 >::Len(), Network, and TStr::SplitOnAllCh().

                                        {
  bool verbose = false;
  TStr Line;

  Network.Clr(); // clear network (if any)

  // add nodes
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    if (Line=="") { break; }
    TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
    Network.AddNode(NIdV[0].GetInt(), NIdV[1]);
    if (!IsNodeNm(NIdV[0].GetInt())) {
      AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0));
      DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt();
    }
  }

  // add edges
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV);

    TFltFltH Alphas;
    if (FieldsV.Len() == 3) { 
    Alphas.AddDat(0.0) = FieldsV[2].GetFlt(); 
    } else {
      for (int i=2; i<FieldsV.Len()-1; i+=2) {
        Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt();
      }
    }

    Network.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas);

    if (verbose) {
      printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt());
      TFltFltH &AlphasE = Network.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt());
      for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); }
      printf("\n");
    }
  }

  printf("groundtruth nodes:%d edges:%d\n", Network.GetNodes(), Network.GetEdges());
}

Here is the call graph for this function:

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().

                                          {
  TStr Line;

  InferredNetwork.Clr(); // clear network (if any)

  // add nodes
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    if (Line=="") { break; }
    TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
    if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); }
    InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]);
    if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); }
    if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); }
  }

  printf("Nodes:%d\n", InferredNetwork.GetNodes());
}

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 >::Len(), and TStr::SplitOnAllCh().

                                     {
  bool verbose = false;
  TStr Line;

  InferredNetwork.Clr(); // clear network (if any)

  // add nodes
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    if (Line=="") { break; }
    TStrV NIdV; Line.SplitOnAllCh(',', NIdV);
    if (DomainsIdH.IsKey(NIdV[1])) { IAssert(NIdV[0].GetInt()==DomainsIdH.GetDat(NIdV[1])); }
    InferredNetwork.AddNode(NIdV[0].GetInt(), NIdV[1]);
    if (!IsNodeNm(NIdV[0].GetInt())) { AddNodeNm(NIdV[0].GetInt(), TNodeInfo(NIdV[1], 0)); }
    if (!DomainsIdH.IsKey(NIdV[1])) { DomainsIdH.AddDat(NIdV[1]) = NIdV[0].GetInt(); }
    if (verbose) { printf("Node:%s\n", NIdV[1].CStr()); }
  }

  // add edges
  while (!SIn.Eof()) {
    SIn.GetNextLn(Line);
    TStrV FieldsV; Line.SplitOnAllCh(',', FieldsV);

    TFltFltH Alphas;
    if (FieldsV.Len() == 3) { 
      Alphas.AddDat(0.0) = FieldsV[2].GetFlt(); 
    } else {
      for (int i=2; i<FieldsV.Len()-1; i+=2) {
        Alphas.AddDat(FieldsV[i].GetFlt()) = FieldsV[i+1].GetFlt();
      }
    }

    InferredNetwork.AddEdge(FieldsV[0].GetInt(), FieldsV[1].GetInt(), Alphas);

    if (verbose) {
      printf("Edge %d -> %d: ", FieldsV[0].GetInt(), FieldsV[1].GetInt());
      TFltFltH &AlphasE = InferredNetwork.GetEDat(FieldsV[0].GetInt(), FieldsV[1].GetInt());
      for (int i=0; i<AlphasE.Len(); i+=2) { printf("(%f, %f)", AlphasE.GetKey(i).Val, AlphasE[i].Val); }
      printf("\n");
    }
  }

  printf("inferred nodes:%d edges:%d\n", InferredNetwork.GetNodes(), InferredNetwork.GetEdges());
}

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, THash< TKey, TDat, THashFunc >::Len(), SampledCascadesH, and TotalCascadesAlpha.

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

                  {
    // reset vectors
  for (int i=0; i<DiffAlphas.Len(); i++) {
    DiffAlphas[i].Clr();
  }
    DiffAlphas.Clr();
    AveDiffAlphas.Clr();
    SampledCascadesH.Clr();
    TotalCascadesAlpha.Clr();
}

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().

{ CascH.Save(SOut); NodeNmH.Save(SOut); CascPerEdge.Save(SOut); InferredNetwork.Save(SOut); }

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().

                                           {
  TFOut FOut(OutFNm);

  // write nodes to file
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
  }

  FOut.PutStr("\r\n");

  // write cascades to file
  for (THash<TInt, TCascade>::TIter CI = CascH.BegI(); CI < CascH.EndI(); CI++) {
    TCascade &C = CI.GetDat();
    int j = 0;
    for (THash<TInt, THitInfo>::TIter NI = C.NIdHitH.BegI(); NI < C.NIdHitH.EndI(); NI++) {
      if (!NodeNmH.IsKey(NI.GetDat().NId)) { continue; }
      if (j > 0) { FOut.PutStr(TStr::Fmt(",%d,%f", NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); }
      else { FOut.PutStr(TStr::Fmt("%d;%d,%f", CI.GetKey().Val, NI.GetDat().NId.Val, NI.GetDat().Tm.Val)); }
      j++;
    }

    if (j >= 1)
      FOut.PutStr(TStr::Fmt("\r\n"));
  }
}

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().

                                              {
  TFOut FOut(OutFNm);

  // write nodes to file
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
  }

  FOut.PutStr("\r\n");

  // write edges to file (not allowing self loops in the network)
  for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    // not allowing self loops in the Kronecker network
    if (EI.GetSrcNId() != EI.GetDstNId()) {
      if (EI().Len() > 0) {
        FOut.PutStr(TStr::Fmt("%d,%d,", EI.GetSrcNId(), EI.GetDstNId()));
        for (int i=0; i<EI().Len()-1; i++) { FOut.PutStr(TStr::Fmt("%f,%f,", EI().GetKey(i).Val, EI()[i].Val)); }
        FOut.PutStr(TStr::Fmt("%f,%f", EI().GetKey(EI().Len()-1).Val, EI()[EI().Len()-1].Val));
        FOut.PutStr("\r\n");
      }
      else
        FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
    }
  }
}

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.

                                                                       {
    TFOut FOut(OutFNm);

    FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len()));
    for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
      FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr()));
    }
    FOut.PutStr("*Arcs\r\n");
    for (TStrFltFltHNEDNet::TEdgeI EI = Network.BegEI(); EI < Network.EndEI(); EI++) {
      if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }
      double trueAlpha = 0.0;
      if (EI().IsKey(Step)) { trueAlpha = EI().GetDat(Step); }
      else { for (int j=0; j<EI().Len() && EI().GetKey(j)<=Step; j++) { trueAlpha = EI()[j]; } }

      if (trueAlpha > MinAlpha) {
        FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (trueAlpha > MaxAlpha? MaxAlpha.Val : trueAlpha)));
      }
    }
}

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 >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

                                                              {
  TFOut FOut(OutFNm);

  // write nodes to file
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }

    FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
  }

  FOut.PutStr("\r\n");

  // write edges to file (not allowing self loops in the network)
  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
    if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    // not allowing self loops in the Kronecker network
    if (EI.GetSrcNId() != EI.GetDstNId()) {
      if (EI().Len() > 0) {
        TStr Line; bool IsEdge = false;
        for (int i=0; i<EI().Len(); i++) {
          if (EI()[i]>MinAlpha) {
            Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) );
            IsEdge = true;
          } else { // we write 0 explicitly
            Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val);
          }
        }
        // if none of the alphas is bigger than 0, no edge is written
        if (IsEdge) {
          FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId()));
          FOut.PutStr(Line);
          FOut.PutStr("\r\n");
        }
      }
      else
        FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
    }
  }
}

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 >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), TVec< TVal >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

                                                                                  {
  TFOut FOut(OutFNm);

  // write nodes to file
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }

    FOut.PutStr(TStr::Fmt("%d,%s\r\n", NI.GetKey().Val, NI.GetDat().Name.CStr()));
  }
  FOut.PutStr("\r\n");

  // write edges to file (not allowing self loops in the network)
  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
    if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    // not allowing self loops in the Kronecker network
    if (EI.GetSrcNId() != EI.GetDstNId()) {
      double inferredAlpha = 0.0;
      if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }

      if (inferredAlpha > MinAlpha) {
        FOut.PutStr(TStr::Fmt("%d,%d,%f\r\n", EI.GetSrcNId(), EI.GetDstNId(), (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha)));
      }
    }
  }
}

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.

                                                {
  TFOut FOut(OutFNm);

  // write edges to file (not allowing self loops in the network)
  for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
    if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

    // not allowing self loops in the Kronecker network
    if (EI.GetSrcNId() != EI.GetDstNId()) {
      if (EI().Len() > 0) {
        TStr Line; bool IsEdge = false;
        for (int i=0; i<EI().Len(); i++) {
          if (EI()[i]>MinAlpha) {
            Line += TStr::Fmt(",%f,%f", EI().GetKey(i).Val, (EI()[i] > MaxAlpha? MaxAlpha.Val : EI()[i].Val) );
            IsEdge = true;
          } else { // we write 0 explicitly
            Line += TStr::Fmt(",%f,0.0", EI().GetKey(i).Val);
          }
        }
        // if none of the alphas is bigger than 0, no edge is written
        if (IsEdge) {
          FOut.PutStr(TStr::Fmt("%d,%d", EI.GetSrcNId(), EI.GetDstNId()));
          FOut.PutStr(Line);
          FOut.PutStr("\r\n");
        }
      }
      else
        FOut.PutStr(TStr::Fmt("%d,%d,1\r\n", EI.GetSrcNId(), EI.GetDstNId()));
    }
  }
}

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 >::IsIn(), THash< TKey, TDat, THashFunc >::IsKey(), THash< TKey, TDat, THashFunc >::Len(), TVec< TVal >::Len(), MaxAlpha, MinAlpha, NodeNmH, TSOut::PutStr(), and TFlt::Val.

                                                                                       {
    TFOut FOut(OutFNm);
    FOut.PutStr(TStr::Fmt("*Vertices %d\r\n", NodeNmH.Len()));
    for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
      if (NIdV.Len() > 0 && !NIdV.IsIn(NI.GetKey())) { continue; }

      FOut.PutStr(TStr::Fmt("%d \"%s\" ic Blue\r\n", NI.GetKey().Val+1, NI.GetDat().Name.CStr()));
    }
    FOut.PutStr("*Arcs\r\n");
    for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
      if (NIdV.Len() > 0 && (!NIdV.IsIn(EI.GetSrcNId()) || !NIdV.IsIn(EI.GetDstNId()))) { continue; }
      if (!NodeNmH.IsKey(EI.GetSrcNId()) || !NodeNmH.IsKey(EI.GetDstNId())) { continue; }

      double inferredAlpha = 0.0;
      if (EI().IsKey(Step)) { inferredAlpha = EI().GetDat(Step); }

      if (inferredAlpha > MinAlpha) {
        FOut.PutStr(TStr::Fmt("%d %d %f\r\n", EI.GetSrcNId()+1, EI.GetDstNId()+1, (inferredAlpha > MaxAlpha? MaxAlpha.Val : inferredAlpha)));
      }
    }
}

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().

                                                                          {
  TFOut FOut(OutFNm);

  // write nodes to file
  for (THash<TInt, TNodeInfo>::TIter NI = NodeNmH.BegI(); NI < NodeNmH.EndI(); NI++) {
    FOut.PutStr(TStr::Fmt("%d,%s", NI.GetKey().Val, NI.GetDat().Name.CStr()));
    if (CascadesPerNode.IsKey(NI.GetKey().Val)) {
      for (int i=0; i<CascadesPerNode.GetDat(NI.GetKey().Val).Len(); i++) {
        FOut.PutStr(TStr::Fmt(",%f", CascadesPerNode.GetDat(NI.GetKey().Val)[i].Val));
      }
    }
    FOut.PutStr("\r\n");
  }
}

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.

{ Aging = aging; }
void TNIBs::SetDelta ( const double &  delta) [inline]

Definition at line 190 of file cascdynetinf.h.

References Delta.

{ Delta = delta; }
void TNIBs::SetGamma ( const double &  gamma) [inline]

Definition at line 194 of file cascdynetinf.h.

References Gamma.

{ Gamma = gamma; }
void TNIBs::SetInitAlpha ( const double &  ia) [inline]

Definition at line 201 of file cascdynetinf.h.

References InitAlpha.

{ InitAlpha = ia; }
void TNIBs::SetK ( const double &  k) [inline]

Definition at line 191 of file cascdynetinf.h.

References K.

{ K = k; }
void TNIBs::SetMaxAlpha ( const double &  ma) [inline]

Definition at line 199 of file cascdynetinf.h.

References MaxAlpha.

{ MaxAlpha = ma; }
void TNIBs::SetMinAlpha ( const double &  ma) [inline]

Definition at line 200 of file cascdynetinf.h.

References MinAlpha.

{ MinAlpha = ma; }
void TNIBs::SetModel ( const TModel model) [inline]

Definition at line 186 of file cascdynetinf.h.

References Model.

{ Model = model; }
void TNIBs::SetMu ( const double &  mu) [inline]

Definition at line 197 of file cascdynetinf.h.

References Mu.

{ Mu = mu; }
void TNIBs::SetRegularizer ( const TRegularizer reg) [inline]

Definition at line 196 of file cascdynetinf.h.

References Regularizer.

{ Regularizer = reg; }
void TNIBs::SetTolerance ( const double &  tol) [inline]

Definition at line 198 of file cascdynetinf.h.

References Tol.

{ Tol = tol; }
void TNIBs::SetTotalTime ( const float &  tt) [inline]

Definition at line 185 of file cascdynetinf.h.

References TotalTime.

{ TotalTime = tt; }
void TNIBs::SetWindow ( const double &  window) [inline]

Definition at line 187 of file cascdynetinf.h.

References Window.

{ Window = window; }
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 >::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.

                                                                                                                                                      {
  bool verbose = false;
  int currentCascade = -1;
  TIntIntH SampledCascades;
  TStrV ParamSamplingV; ParamSampling.SplitOnAllCh(';', ParamSamplingV);

  Reset();

  printf("Node %d\n", NId);

  // traverse through all times
  for (int t=1; t<Steps.Len(); t++) {
    // find cascades whose two first infections are earlier than Steps[t]
    TIntFltH CascadesIdx;
    int num_infections = 0;
    for (int i=0; i<CascH.Len(); i++) {
      if (CascH[i].LenBeforeT(Steps[t]) > 1 &&
        ( (Sampling!=WIN_SAMPLING && Sampling!=WIN_EXP_SAMPLING) ||
          (Sampling==WIN_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) ||
          (Sampling==WIN_EXP_SAMPLING && (Steps[t]-CascH[i].GetMinTm()) <= ParamSamplingV[0].GetFlt()) )) {
        num_infections += CascH[i].LenBeforeT(Steps[t]);
        CascadesIdx.AddDat(i) = CascH[i].GetMinTm();
      }
    }

    // if there are not recorded cascades by Steps[t], continue
    if (CascadesIdx.Len()==0) {
      printf("WARNING: No cascades recorded by %f!\n", Steps[t].Val);
      if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
      continue;
    }

    // later cascades first
    CascadesIdx.SortByDat(false);

    printf("Solving step %f: %d cascades, %d infections\n", Steps[t].Val, CascadesIdx.Len(), num_infections);
    SampledCascades.Clr();

    // sampling cascades with no replacement
    for (int i=0; i < Iters; i++) {
      switch (Sampling) {
        case UNIF_SAMPLING:
          currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
          break;

        case WIN_SAMPLING:
          currentCascade = TInt::Rnd.GetUniDevInt(CascadesIdx.Len());
          break;

        case EXP_SAMPLING:
          do {
            currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[0].GetFlt());
          } while (currentCascade > CascadesIdx.Len()-1);
          break;

        case WIN_EXP_SAMPLING:
          do {
            currentCascade = (int)TFlt::Rnd.GetExpDev(ParamSamplingV[1].GetFlt());
          } while (currentCascade > CascadesIdx.Len()-1);
          break;

        case RAY_SAMPLING:
          do {
            currentCascade = (int)TFlt::Rnd.GetRayleigh(ParamSamplingV[0].GetFlt());
          } while (currentCascade > CascadesIdx.Len()-1);
          break;
      }

      if (!SampledCascades.IsKey(currentCascade)) { SampledCascades.AddDat(currentCascade) = 0; }
      SampledCascades.GetDat(currentCascade)++;

      if (verbose) { printf("Cascade %d sampled!\n", currentCascade); }

      // sampled cascade
      TCascade &Cascade = CascH[CascadesIdx.GetKey(currentCascade)];

      // update gradient and alpha's
      TIntPrV AlphasToUpdate;
      UpdateDiff(OSG, NId, Cascade, AlphasToUpdate, Steps[t]);

      // update alpha's
      for (int j=0; j<AlphasToUpdate.Len(); j++) {
        if (InferredNetwork.IsEdge(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2) &&
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).IsKey(Steps[t])
          ) {
          InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) -=
              (Gamma * AveDiffAlphas.GetDat(AlphasToUpdate[j].Val1)
                  - (Regularizer==1? Mu*InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) : 0.0));

          // project into alpha >= 0
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) < Tol) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = Tol;
          }

          // project into alpha <= MaxAlpha
          if (InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) > MaxAlpha) {
            InferredNetwork.GetEDat(AlphasToUpdate[j].Val1, AlphasToUpdate[j].Val2).GetDat(Steps[t]) = MaxAlpha;
          }
        }
      }
      if (verbose) { printf("%d transmission rates updated!\n", AlphasToUpdate.Len()); }
    }

    printf("%d different cascades have been sampled for step %f!\n", SampledCascades.Len(), Steps[t].Val);

    // For alphas that did not get updated, copy last alpha value * aging factor
    int unchanged = 0;
    for (TStrFltFltHNEDNet::TEdgeI EI = InferredNetwork.BegEI(); EI < InferredNetwork.EndEI(); EI++) {
      if (EI().IsKey(Steps[t]) || t == 0 || !EI().IsKey(Steps[t-1])) { continue; }

      EI().AddDat(Steps[t]) = Aging*EI().GetDat(Steps[t-1]);
      unchanged++;
    }
    if (verbose) { printf("%d transmission rates that did not changed were 'aged' by %f!\n", unchanged, Aging.Val); }

    // compute performance on-the-fly
    if (PlotPerformance) { ComputePerformanceNId(NId, t, Steps); }
  }
}

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().

{ NodeNmH.SortByDat(asc); }

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 >::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().

                                                                                                                                         {
  IAssert(InferredNetwork.IsNode(NId));

  double sum = 0.0;

  // we assume cascade is sorted & iterator returns in sorted order
  if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) {
    for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
      // consider only nodes that are earlier in time
      if ( (Cascade.GetTm(NId)<=NI.GetDat().Tm) ||
           (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW)
         ) { break; }

      TIntPr Pair(NI.GetKey(), NId);

      // if edge/alpha doesn't exist, create
      if (!InferredNetwork.IsEdge(Pair.Val1, Pair.Val2)) { InferredNetwork.AddEdge(Pair.Val1, Pair.Val2, TFltFltH()); }
      if (!InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).IsKey(CurrentTime)) {
        InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).AddDat(CurrentTime) = InitAlpha;
      }

      switch(Model) {
        case EXP: // exponential
          sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val;
          break;
        case POW: // powerlaw
          sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val/(Cascade.GetTm(NId)-NI.GetDat().Tm);
          break;
        case RAY: // rayleigh
          sum += InferredNetwork.GetEDat(Pair.Val1, Pair.Val2).GetDat(CurrentTime).Val*(Cascade.GetTm(NId)-NI.GetDat().Tm);
          break;
        default:
          sum = 0.0;
      }
    }
  }

  // we assume cascade is sorted & iterator returns in sorted order
  for (THash<TInt, THitInfo>::TIter NI = Cascade.BegI(); NI < Cascade.EndI(); NI++) {
    // only consider nodes that are earlier in time if node belongs to the cascade
    if ( Cascade.IsNode(NId) && (Cascade.GetTm(NId)<=NI.GetDat().Tm ||
        (Cascade.GetTm(NId)-Delta<=NI.GetDat().Tm && Model==POW))
       ) { break; }

    // consider infected nodes up to CurrentTime
    if (NI.GetDat().Tm > CurrentTime) { break; }

    TIntPr Pair(NI.GetKey(), NId); // potential edge

    double val = 0.0;

    if (Cascade.IsNode(NId) && Cascade.GetTm(NId) <= CurrentTime) {
      IAssert((Cascade.GetTm(NId) - NI.GetDat().Tm) > 0.0);

      switch(Model) { // compute gradients for infected
        case EXP: // exponential
          val = (Cascade.GetTm(NId) - NI.GetDat().Tm) - 1.0/sum;
          break;
        case POW: // powerlaw
          val = log((Cascade.GetTm(NId) - NI.GetDat().Tm)/Delta) - 1.0/((Cascade.GetTm(NId)-NI.GetDat().Tm)*sum);
          break;
        case RAY: // rayleigh
          val = TMath::Power(Cascade.GetTm(NId)-NI.GetDat().Tm, 2.0)/2.0 - (Cascade.GetTm(NId)-NI.GetDat().Tm)/sum;
          break;
        default:
          val = 0.0;
      }
    } else { // compute gradients for non infected
      IAssert((CurrentTime - NI.GetDat().Tm) >= 0.0);

      switch(Model) {
        case EXP: // exponential
          val = (CurrentTime-NI.GetDat().Tm);
          // if every cascade was recorded up to a maximum Window cut-off
          if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = (Cascade.GetMinTm()+Window-NI.GetDat().Tm); }
          break;
        case POW: // power-law
          val = TMath::Mx(log((CurrentTime-NI.GetDat().Tm)/Delta), 0.0);
          // if every cascade was recorded up to a maximum Window cut-off
          if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Mx(log((Cascade.GetMinTm()+Window-NI.GetDat().Tm)/Delta), 0.0); }
          break;
        case RAY: // rayleigh
          val = TMath::Power(CurrentTime-NI.GetDat().Tm,2.0)/2.0;
          // if every cascade was recorded up to a maximum Window cut-off
          if ( (Window > -1) && (CurrentTime-Cascade.GetMinTm() > Window) ) { val = TMath::Power(Cascade.GetMinTm()+Window-NI.GetDat().Tm,2.0)/2.0; }
          break;
        default:
          val = 0.0;
      }
    }

    if (!AveDiffAlphas.IsKey(Pair.Val1)) { AveDiffAlphas.AddDat(Pair.Val1) = 0.0; }

    switch (OptMethod) {
      case OBSG:
      case OEBSG:
      case OFG:
        // update stochastic average gradient (based on batch for OBSG and OEBSG and based on all cascades for FG)
        AveDiffAlphas.GetDat(Pair.Val1) += val;
        break;
      case OSG:
      case OESG:
        // update stochastic gradient (we use a single gradient due to current cascade)
        AveDiffAlphas.GetDat(Pair.Val1) = val;
      default:
        break;
    }

    AlphasToUpdate.Add(Pair);
  }

  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

Definition at line 153 of file cascdynetinf.h.

Referenced by SetAging(), and SG().

Definition at line 162 of file cascdynetinf.h.

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

Definition at line 135 of file cascdynetinf.h.

Referenced by GetCascadeId().

Definition at line 138 of file cascdynetinf.h.

Referenced by Save().

Definition at line 150 of file cascdynetinf.h.

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

Definition at line 163 of file cascdynetinf.h.

Referenced by Reset().

Definition at line 153 of file cascdynetinf.h.

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

Definition at line 155 of file cascdynetinf.h.

Referenced by SetInitAlpha(), and UpdateDiff().

Definition at line 150 of file cascdynetinf.h.

Referenced by SetK().

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

Definition at line 170 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

Definition at line 153 of file cascdynetinf.h.

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

Definition at line 169 of file cascdynetinf.h.

Referenced by ComputePerformanceNId(), and Init().

Definition at line 154 of file cascdynetinf.h.

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

Definition at line 166 of file cascdynetinf.h.

Referenced by Reset().

Definition at line 155 of file cascdynetinf.h.

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

Definition at line 159 of file cascdynetinf.h.

Referenced by Init(), and Reset().

Definition at line 147 of file cascdynetinf.h.

Referenced by GenCascade(), and SetTotalTime().

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: