Exemplo n.º 1
0
void TSirModel::GetParam(TFltV& ParamV) const { 
  ParamV.Clr(false); 
  ParamV.Add(N0);
  ParamV.Add(I0);
  ParamV.Add(Beta); 
  ParamV.Add(Gamma); 
  ParamV.Add(T0);
}
Exemplo n.º 2
0
void TEpidemModel::RungeKutta(const TFltV& y, const TFltV& dydx, double x, double h, TFltV& SirOutV) {
  const int n = y.Len();
  IAssert(y.Len() == n && dydx.Len() == n);
  TFltV dym(n), dyt(n), yt(n);
  int i;
  double hh=h*0.5;
  double h6=h/6.0;
  double xh=x+hh;
  for (i=0; i < n; i++) {
    yt[i]=y[i]+hh*dydx[i];
  }
  GetDerivs(xh, yt, dyt);
  for (i=0; i<n; i++) {
    yt[i]=y[i]+hh*dyt[i];
  }
  GetDerivs(xh,yt,dym);
  for (i=0; i<n; i++) {
	  yt[i]=y[i]+h*dym[i];
	  dym[i] += dyt[i];
  }
  GetDerivs(x+h,yt,dyt);
  SirOutV.Clr(false);  
  for (i=0; i<n; i++) {
    SirOutV.Add(y[i]+h6 * (dydx[i]+dyt[i]+2.0*dym[i]));
  }
}
Exemplo n.º 3
0
int main() {
  TLSHash LSH(7, 7, DIM, TLSHash::EUCLIDEAN);
  LSH.Init();

  TRnd Gen;
  Gen.Randomize();

  TVec<TFltV> DataV;
  for (int i=0; i<1000000; i++) {
    TFltV Datum;
    for (int j=0; j<3; j++) {
      Datum.Add(Gen.GetUniDev()*2100);
    }
    DataV.Add(Datum);
  }
  LSH.AddV(DataV);
  
  TVec<TPair<TFltV, TFltV> > NeighborsV = LSH.GetAllCandidatePairs();
  printf("Number of Candidates: %d\n", NeighborsV.Len());

  NeighborsV = LSH.GetAllNearPairs();
  printf("Number of Close Pairs: %d\n", NeighborsV.Len());
  for (int i=0; i<NeighborsV.Len(); i++) {
    outputPoint(NeighborsV[i].GetVal1());
    printf(" ");
    outputPoint(NeighborsV[i].GetVal2());
    printf("\n");
  }
  return 0;
}
Exemplo n.º 4
0
void TNetInfBs::SaveObjInfo(const TStr& OutFNm) {
  TGnuPlot GnuPlot(OutFNm);

  TFltV Objective;

  for (THash<TIntPr, TEdgeInfo>::TIter EI = EdgeInfoH.BegI(); EI < EdgeInfoH.EndI(); EI++) {
    if (Objective.Len()==0) { Objective.Add(EI.GetDat().MarginalGain); 
    } else {
      Objective.Add(Objective[Objective.Len()-1]+EI.GetDat().MarginalGain);
    }
  }

  GnuPlot.AddPlot(Objective, gpwLinesPoints);
  
  GnuPlot.SavePng();
}
Exemplo n.º 5
0
void TNNet::GetResults(TFltV& ResultV) const{
    ResultV.Clr(true, -1);

    for(int NeuronN = 0; NeuronN < LayerV.Last().GetNeuronN() - 1; ++NeuronN){
        ResultV.Add(LayerV.Last().GetOutVal(NeuronN));
    }
}
Exemplo n.º 6
0
void TJsonVal::GetArrNumV(TFltV& FltV) const {
    EAssert(IsArr());
    for (int FltN = 0; FltN < GetArrVals(); FltN++) {
        PJsonVal ArrVal = GetArrVal(FltN);
        EAssert(ArrVal->IsNum());
        FltV.Add(ArrVal->GetNum());
    }
}
Exemplo n.º 7
0
void TBowLinAlg::GetDual(const PBowDocWgtBs& X,
        const TFltV& x, TFltV& y, const int& _Docs) {

    const int Docs = (_Docs == -1) ? X->GetDocs() : _Docs;
    y.Gen(Docs, 0); // prepare space
    for (int DId = 0; DId < Docs; DId++) {
        y.Add(TBowLinAlg::DotProduct(x, X->GetSpV(DId)));
    }   
}
Exemplo n.º 8
0
void TNEANet::FltAttrValueEI(const TInt& EId, TStrIntPrH::TIter EdgeHI, TFltV& Values) const {
  Values = TVec<TFlt>();
  while (!EdgeHI.IsEnd()) {
    if (EdgeHI.GetDat().Val1 == FltType && !EdgeAttrIsFltDeleted(EId, EdgeHI)) {
      TFlt val = (this->VecOfFltVecsE.GetVal(EdgeHI.GetDat().Val2).GetVal(EId));
      Values.Add(val);
    }
    EdgeHI++;
  }  
}
Exemplo n.º 9
0
void TNEANet::FltAttrValueNI(const TInt& NId, TStrIntPrH::TIter NodeHI, TFltV& Values) const {
  Values = TVec<TFlt>();
  while (!NodeHI.IsEnd()) {
    if (NodeHI.GetDat().Val1 == FltType && !NodeAttrIsFltDeleted(NId, NodeHI)) {
      TFlt val = (this->VecOfFltVecsN.GetVal(NodeHI.GetDat().Val2).GetVal(NId));
      Values.Add(val);
    }
    NodeHI++;
  }  
}
Exemplo n.º 10
0
void TGUtil::MakeExpBins(const TFltV& YValV, TFltV& ExpYValV, const double& BinFactor) {
  ExpYValV.Clr(true);
  int prevI=0;
  for (int i = 0; i < YValV.Len(); ) {
    ExpYValV.Add(YValV[i]);
    i = int(i*BinFactor);
    if (i==prevI) { i++; }
    prevI = i;
  }
}
Exemplo n.º 11
0
void TSirSR2Model::GetParam(TFltV& ParamV) const { 
  ParamV.Clr(false); 
  ParamV.Add(N0M);
  ParamV.Add(I0M);
  ParamV.Add(N0B);
  ParamV.Add(I0B);
  ParamV.Add(T0);
  ParamV.Add(BetaM);
  ParamV.Add(GammaM);
  ParamV.Add(BetaB);
  ParamV.Add(GammaB);
  ParamV.Add(BetaMB);
  ParamV.Add(BetaBM);
  ParamV.Add(DeltaM);
  ParamV.Add(DeltaB);
}
Exemplo n.º 12
0
void TEpidemModel::LoadTxt(const TStr& InFNm, const int& ColId, TFltV& ValV) {
  ValV.Clr();
  if (! TFile::Exists(InFNm)) { 
    printf("*** %s not found!\n", InFNm.CStr());
    return; 
  }
  TSsParser Ss(InFNm, ssfTabSep);
  while (Ss.Next()) {
    ValV.Add(Ss.GetFlt(ColId));
  }
}
Exemplo n.º 13
0
PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const double& DensityCoef, const double& ScaleCoef, TRnd& Rnd) {
    TFltV CProbV;
    double Prob;
    for (int i = 0; i < CmtyVV.Len(); i++) {
        Prob = ScaleCoef*pow( double( CmtyVV[i].Len()), - DensityCoef);
        if (Prob > 1.0) {
            Prob = 1;
        }
        CProbV.Add(Prob);
    }
    return TAGM::GenAGM(CmtyVV, CProbV, Rnd);
}
Exemplo n.º 14
0
void TWgtNet::PermOutEdgeWgt() {
  TFltV WgtV;
  for (TNodeI NI = BegNI(); NI < EndNI(); NI++) {
    WgtV.Clr(false);
    for (int e = 0; e < NI.GetOutDeg(); e++) {
      WgtV.Add(NI.GetOutEDat(e)); 
    }
    WgtV.Shuffle(TInt::Rnd);
    for (int e = 0; e < NI.GetOutDeg(); e++) {
      NI.GetOutEDat(e) = WgtV[e]; 
    }
  }
}
Exemplo n.º 15
0
TBowMatrix::TBowMatrix(PBowDocBs BowDocBs, PBowDocWgtBs BowDocWgtBs,
        const TStr& CatNm,  const TIntV& DIdV, TFltV& ClsV): TMatrix() {

    RowN = BowDocBs->GetWords();
    ClsV.Gen(DIdV.Len(), 0);
    ColSpVV.Gen(DIdV.Len(), 0);
    IAssert(BowDocBs->IsCatNm(CatNm));
    int CatId = BowDocBs->GetCId(CatNm);
    for (int i = 0; i < DIdV.Len(); i++) {
        ColSpVV.Add(BowDocWgtBs->GetSpV(DIdV[i]));
        ClsV.Add(BowDocBs->IsCatInDoc(DIdV[i], CatId) ? 0.99 : -0.99);
    }
}
Exemplo n.º 16
0
void TGnuPlot::MakeExpBins(const TFltKdV& XYValV, TFltKdV& ExpXYValV, const double& BinFactor, const double& MinYVal) {
  if (XYValV.Empty()) { ExpXYValV.Clr(false); return; }
  IAssert(! XYValV.Empty());
  IAssert(XYValV.IsSorted());
  const TFlt MxX = XYValV.Last().Key;
  // find buckets
  TFltV BucketEndV; BucketEndV.Add(1);
  double PrevBPos = 1, BPos = 1;
  while (BPos <= MxX) {
    PrevBPos = (uint) floor(BPos);
    BPos *= BinFactor;
    if (floor(BPos) == PrevBPos) {
      BPos = PrevBPos + 1; }
    BucketEndV.Add(floor(BPos));
  }
  //printf("buckets:\n"); for (int i = 0; i < BucketEndV.Len(); i++) { printf("\t%g\n", BucketEndV[i]);}
  ExpXYValV.Gen(BucketEndV.Len(), 0);
  int CurB = 0;
  double AvgPos=0, Cnt=0, AvgVal=0;
  for (int v = 0; v < XYValV.Len(); v++) {
    if (XYValV[v].Key() == 0.0) { continue; }
    AvgPos += XYValV[v].Key ;//* XYValV[v].Dat;  // x
    AvgVal += XYValV[v].Dat;  // y
    Cnt++;
    if (v+1 == XYValV.Len() || XYValV[v+1].Key > BucketEndV[CurB]) {
      if (Cnt != 0) {
        //AvgPos /= AvgVal;
        //AvgVal /= (BucketEndV[CurB]-BucketEndV[CurB-1]);
        AvgPos /= (double) Cnt;
        AvgVal /= (double) Cnt;
        if (AvgVal < MinYVal) { AvgVal = MinYVal; }
        ExpXYValV.Add(TFltKd(AvgPos, AvgVal));
        //printf("b: %6.2f\t%6.2f\n", AvgPos, AvgVal);
        AvgPos = 0;  AvgVal = 0;  Cnt = 0;
      }
      CurB++;
    }
  }
}
Exemplo n.º 17
0
void TWgtNet::PermEdgeWgt() {
  TFltV WgtV;
  for (TNodeI NI = BegNI(); NI < EndNI(); NI++) {
    for (int e = 0; e < NI.GetOutDeg(); e++) {
      WgtV.Add(NI.GetOutEDat(e)); 
    }
  }
  WgtV.Shuffle(TInt::Rnd);
  int w = 0;
  for (TNodeI NI = BegNI(); NI < EndNI(); NI++) {
    for (int e = 0; e < NI.GetOutDeg(); e++) {
      NI.GetOutEDat(e) = WgtV[w++]; 
    }
  }
}
Exemplo n.º 18
0
double TNetInfBs::GetBound(const TIntPr& Edge, double& CurProb) {
	double Bound = 0;
	TFltV Bounds;

	// bound could be computed faster (using lazy evaluation, as in the optimization procedure)
	for (int e=0; e < EdgeGainV.Len(); e++) {
		const TIntPr& EE = EdgeGainV[e].Val2;
		if (EE != Edge && !Graph->IsEdge(EE.Val1, EE.Val2)) {
			const double EProb = GetAllCascProb(EE.Val1, EE.Val2);
			if (EProb > CurProb) Bounds.Add(EProb - CurProb); }
	}

	Bounds.Sort(false);
	for (int i=0; i<Graph->GetEdges() && i<Bounds.Len(); i++) Bound += Bounds[i];

	return Bound;
}
Exemplo n.º 19
0
// Computes GINI coefficient of egonet as a subset of the parent graph (edges into and out of the egonet ARE considered)
double TSnap::GetGiniCoefficient(const TIntFltH DegH, const TIntV NIdV) {
  typename TIntV::TIter VI;
  typename TFltV::TIter DI;
  TFltV DegV;
  const int n = NIdV.Len();
  // DegV.Gen(n); // NOTE: don't use Gen() and Sort() on the same object (!)
  for (VI = NIdV.BegI(); VI < NIdV.EndI(); VI++) {
    DegV.Add(DegH.GetDat(VI->Val)); // might need to change this (in / out / undirected)
  }
  DegV.Sort();
  int i = 0;
  double numerator = 0.0, denominator = 0.0;
  for (DI = DegV.BegI(); DI < DegV.EndI(); DI++, i++) {
    numerator += (i + 1)*DegV[i];
    denominator += DegV[i];
  }
  return(double(2*numerator) / double(n*denominator) - double(n + 1) / double(n));
}
Exemplo n.º 20
0
TFltV TEnv::GetIfArgPrefixFltV(const TStr& PrefixStr,
                               TFltV& DfValV,
                               const TStr& DNm) const {
    // convert default-integer-values to
    // default-string-values
    TStrV DfValStrV;
    for (int ValN = 0; ValN < DfValV.Len(); ValN++)
        DfValStrV.Add(TFlt::GetStr(DfValV[ValN]));
    // get string-values
    TStrV ValStrV =
        GetIfArgPrefixStrV(PrefixStr, DfValStrV, DNm);
    // convert string-values to integer-values
    TFltV ValV;
    double Val;
    for (int ValN = 0; ValN < ValStrV.Len(); ValN++) {
        if (ValStrV[ValN].IsFlt(Val)) ValV.Add(Val);
    }
    // return value-vector
    return ValV;
}
Exemplo n.º 21
0
void TGnuPlot::Test() {
  TFltV DeltaY;
  TFltPrV ValV1, ValV2, ValV3;
  for (int i = 1; i < 30; i++) {
    ValV1.Add(TFltPr(i, pow(double(i), 1.2)));
    DeltaY.Add(5*TInt::Rnd.GetUniDev());
    ValV2.Add(TFltPr(i, 5*i-1));
  }
  for (int i = -10; i < 20; i++) {
    ValV3.Add(TFltPr(i, 2*i + 2 + TInt::Rnd.GetUniDev()));
  }
  TGnuPlot GnuPlot("testDat", "TestPlot", true);
  GnuPlot.SetXYLabel("X", "Y");
  const int id2 = GnuPlot.AddPlot(ValV2, gpwPoints, "y=5*x-1");
  const int id3 = GnuPlot.AddPlot(ValV3, gpwPoints, "y=2*x+2");
  GnuPlot.AddErrBar(ValV1, DeltaY, "y=x^2", "Error bar");
  GnuPlot.AddLinFit(id2, gpwLines);
  GnuPlot.AddLinFit(id3, gpwLines);
  GnuPlot.Plot();
  GnuPlot.SavePng("testPlot.png");
}
Exemplo n.º 22
0
PUNGraph TAGM::GenAGM(TVec<TIntV>& CmtyVV, const double& DensityCoef, const double& ScaleCoef, TRnd& Rnd){
	TFltV CProbV;
	double Prob;
	for(int i=0;i<CmtyVV.Len();i++) {
		Prob = ScaleCoef*pow(double(CmtyVV[i].Len()),-DensityCoef);
		if(Prob>1){Prob = 1;}
		CProbV.Add(Prob);
	}
	PUNGraph G = TUNGraph::New();
	printf("AGM begins\n");
	for(int i=0;i<CmtyVV.Len();i++) {
		TIntV& CmtyV = CmtyVV[i];
		for(int u=0;u<CmtyV.Len();u++) {
			G->AddNode(CmtyV[u]);
		}
		Prob = CProbV[i];
		printf("\r%d(%d)/%d",i,CmtyVV[i].Len(),CmtyVV.Len());
		RndConnectInsideCommunity(G,CmtyV,Prob,Rnd);
	}
	printf("AGM completed (%d nodes %d edges)\n",G->GetNodes(),G->GetEdges());
	return G;
}
Exemplo n.º 23
0
void TAGMFit::GetCmtyVV(TVec<TIntV>& CmtyVV, TFltV& QV, const double QMax) {
  CmtyVV.Gen(CIDNSetV.Len(), 0);
  QV.Gen(CIDNSetV.Len(), 0);
  TIntFltH CIDLambdaH(CIDNSetV.Len());
  for (int c = 0; c < CIDNSetV.Len(); c++) {
    CIDLambdaH.AddDat(c, LambdaV[c]);
  }
  CIDLambdaH.SortByDat(false);
  for (int c = 0; c < CIDNSetV.Len(); c++) {
    int CID = CIDLambdaH.GetKey(c);
    IAssert(LambdaV[CID] >= MinLambda);
    double Q = exp( - (double) LambdaV[CID]);
    if (Q > QMax) { continue; }
    TIntV CmtyV;
    CIDNSetV[CID].GetKeyV(CmtyV);
    if (CmtyV.Len() == 0) { continue; }
    if (CID == BaseCID) { //if the community is the base community(epsilon community), discard
      IAssert(CmtyV.Len() == G->GetNodes());
    } else {
      CmtyVV.Add(CmtyV);
      QV.Add(Q);
    }
  }
}
Exemplo n.º 24
0
int main(int argc, char* argv[]) {
  
  setbuf(stdout, NULL); // disables the buffer so that print statements are not buffered and display immediately (?)
   
  Env = TEnv(argc, argv, TNotify::StdNotify);
  Env.PrepArgs(TStr::Fmt("Vespignani backbone method. build: %s, %s. Time: %s", __TIME__, __DATE__, TExeTm::GetCurTm()));
  
  TExeTm ExeTm;
  
  Try
  
  const TStr InFNm = Env.GetIfArgPrefixStr("-i:", "", "input network");
  const TStr OutFNm = Env.GetIfArgPrefixStr("-o:", "", "output prefix (alpha value and filename extensions added)");
  double alpha = Env.GetIfArgPrefixFlt("-a:", 0.01, "alpha significance level threshold");
  const TStr AlphaVFNm = Env.GetIfArgPrefixStr("--alphav:", "", "vector of alpha significance level threshold (overrides -a)");
  const bool verbose = Env.GetIfArgPrefixBool("--verbose:", true, "verbose output for each step of the Vespignani method");

  const bool bootstrap = Env.GetIfArgPrefixBool("--bootstrap:", false, "bootstrap Vespignani method to retain --ratio of total weight W");
  const double ratio = Env.GetIfArgPrefixFlt("--ratio:", 0.50, "bootstrap target ratio of total weight W");
  const double lowerBound = Env.GetIfArgPrefixFlt("--lowerbound:", 0.0, "lower bound for alpha (binary search)");
  const double upperBound = Env.GetIfArgPrefixFlt("--upperbound:", 1.0, "upper bound for alpha (binary search)");
  const double tol = Env.GetIfArgPrefixFlt("--tolerance:", 5.0e-3, "tolerance for alpha (binary search)");
  const double spread = Env.GetIfArgPrefixFlt("--spread:", 2.0, "spread for bootstrapped alpha benchmark (binary search)");

  // Load graph and create directed and undirected graphs (pointer to the same memory)
  printf("\nLoading %s...", InFNm.CStr());
  const PFltWNGraph WGraph = TSnap::LoadFltWEdgeList<TWNGraph>(InFNm);
  printf(" DONE (time elapsed: %s (%s))\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
  
  TSnap::printFltWGraphSummary(WGraph, true, "\nWGraph\n------");
  
  // Variables
  
  PFltWNGraph WGraphCopy;
  TFltV AlphaV;
  TFltV::TIter VI;
  
  if (!AlphaVFNm.Empty()) {
    AlphaV = TSnap::LoadTxtFltV(AlphaVFNm);
  } else {
    if (bootstrap) {
      printf("\n");
      alpha = TSnap::FindVespignaniThreshold<TFlt, TWNGraph>(WGraph, ratio, tol, lowerBound, upperBound);
      AlphaV.Add(alpha / spread);
      AlphaV.Add(alpha);
      AlphaV.Add(alpha * spread);
    } else {
      AlphaV.Add(alpha);
    }
  }

  // VESPIGNANI METHOD

  Progress progress(ExeTm, AlphaV.Len(), 5, "Computing Vespignani method", !verbose);
  progress.start();
  if (verbose) {
    printf("\n");
  }
  int i = 0;
  for (VI = AlphaV.BegI(); VI < AlphaV.EndI(); VI++) {
    const double& alpha = VI->Val;
    
    // Compute method and save filtered

    printf("Computing Vespignani method (alpha: %e)\n\n", alpha);
    
    WGraphCopy = TSnap::FilterEdgesVespignani<TFlt, TWNGraph>(WGraph, alpha);
    TSnap::RemoveIsolated(WGraphCopy);
    TSnap::SaveFltWEdgeList(WGraphCopy, TStr::Fmt("%s-%9e.snap", OutFNm.CStr(), alpha), TStr::Fmt("Vespignani backbone with alpha: %e", alpha));

    // Save bootstrapped
    
    if (bootstrap && i == 1) {
      TSnap::SaveFltWEdgeList(WGraphCopy, TStr::Fmt("%s-bootstrapped.snap", OutFNm.CStr()), TStr::Fmt("Vespignani backbone with alpha: %e", alpha));
    }
    
    // Verbose summary

    if (verbose) {
      TSnap::printFltWGraphSummary(WGraphCopy, true, TStr::Fmt("WGraphCopy (alpha: %e)\n------", alpha));
      printf("\n");
    }

    i++;
    progress++;
  }

  Catch
  
  printf("\nrun time: %s (%s)\n", ExeTm.GetTmStr(), TSecTm::GetCurTm().GetTmStr().CStr());
  return 0;
  
}
Exemplo n.º 25
0
void TPropHazards::GetWgtV(TFltV& _WgtV) const {
	for (int i = 1; i < WgtV.Len(); i++) {
		_WgtV.Add(WgtV[i]);
	}
}
Exemplo n.º 26
0
/// estimate number of communities using AGM
int TAGMUtil::FindComsByAGM(const PUNGraph& Graph, const int InitComs, const int MaxIter, const int RndSeed, const double RegGap, const double PNoCom, const TStr PltFPrx) {
    TRnd Rnd(RndSeed);
    int LambdaIter = 100;
    if (Graph->GetNodes() < 200) {
        LambdaIter = 1;
    }
    if (Graph->GetNodes() < 200 && Graph->GetEdges() > 2000) {
        LambdaIter = 100;
    }

    //Find coms with large C
    TAGMFit AGMFitM(Graph, InitComs, RndSeed);
    if (PNoCom > 0.0) {
        AGMFitM.SetPNoCom(PNoCom);
    }
    AGMFitM.RunMCMC(MaxIter, LambdaIter, "");

    int TE = Graph->GetEdges();
    TFltV RegV;
    RegV.Add(0.3 * TE);
    for (int r = 0; r < 25; r++) {
        RegV.Add(RegV.Last() * RegGap);
    }
    TFltPrV RegComsV, RegLV, RegBICV;
    TFltV LV, BICV;
    //record likelihood and number of communities with nonzero P_c
    for (int r = 0; r < RegV.Len(); r++) {
        double RegCoef = RegV[r];
        AGMFitM.SetRegCoef(RegCoef);
        AGMFitM.MLEGradAscentGivenCAG(0.01, 1000);
        AGMFitM.SetRegCoef(0.0);

        TVec<TIntV> EstCmtyVV;
        AGMFitM.GetCmtyVV(EstCmtyVV, 0.99);
        int NumLowQ = EstCmtyVV.Len();
        RegComsV.Add(TFltPr(RegCoef, (double) NumLowQ));

        if (EstCmtyVV.Len() > 0) {
            TAGMFit AFTemp(Graph, EstCmtyVV, Rnd);
            AFTemp.MLEGradAscentGivenCAG(0.001, 1000);
            double CurL = AFTemp.Likelihood();
            LV.Add(CurL);
            BICV.Add(-2.0 * CurL + (double) EstCmtyVV.Len() * log((double) Graph->GetNodes() * (Graph->GetNodes() - 1) / 2.0));
        }
        else {
            break;
        }
    }
    // if likelihood does not exist or does not change at all, report the smallest number of communities or 2
    if (LV.Len() == 0) {
        return 2;
    }
    else if (LV[0] == LV.Last()) {
        return (int) TMath::Mx<TFlt>(2.0, RegComsV[LV.Len() - 1].Val2);
    }


    //normalize likelihood and BIC to 0~100
    int MaxL = 100;
    {
        TFltV& ValueV = LV;
        TFltPrV& RegValueV = RegLV;
        double MinValue = TFlt::Mx, MaxValue = TFlt::Mn;
        for (int l = 0; l < ValueV.Len(); l++) {
            if (ValueV[l] < MinValue) {
                MinValue = ValueV[l];
            }
            if (ValueV[l] > MaxValue) {
                MaxValue = ValueV[l];
            }
        }
        while (ValueV.Len() < RegV.Len()) {
            ValueV.Add(MinValue);
        }
        double RangeVal = MaxValue - MinValue;
        for (int l = 0; l < ValueV.Len(); l++) {
            RegValueV.Add(TFltPr(RegV[l], double(MaxL) * (ValueV[l] - MinValue) / RangeVal));
        }

    }
    {
        TFltV& ValueV = BICV;
        TFltPrV& RegValueV = RegBICV;
        double MinValue = TFlt::Mx, MaxValue = TFlt::Mn;
        for (int l = 0; l < ValueV.Len(); l++) {
            if (ValueV[l] < MinValue) {
                MinValue = ValueV[l];
            }
            if (ValueV[l] > MaxValue) {
                MaxValue = ValueV[l];
            }
        }
        while (ValueV.Len() < RegV.Len()) {
            ValueV.Add(MaxValue);
        }
        double RangeVal = MaxValue - MinValue;
        for (int l = 0; l < ValueV.Len(); l++) {
            RegValueV.Add(TFltPr(RegV[l], double(MaxL) * (ValueV[l] - MinValue) / RangeVal));
        }
    }

    //fit logistic regression to normalized likelihood.
    TVec<TFltV> XV(RegLV.Len());
    TFltV YV (RegLV.Len());
    for (int l = 0; l < RegLV.Len(); l++) {
        XV[l] = TFltV::GetV(log(RegLV[l].Val1));
        YV[l] = RegLV[l].Val2 / (double) MaxL;
    }
    TFltPrV LRVScaled, LRV;
    TLogRegFit LRFit;
    PLogRegPredict LRMd = LRFit.CalcLogRegNewton(XV, YV, PltFPrx);
    for (int l = 0; l < RegLV.Len(); l++) {
        LRV.Add(TFltPr(RegV[l], LRMd->GetCfy(XV[l])));
        LRVScaled.Add(TFltPr(RegV[l], double(MaxL) * LRV.Last().Val2));
    }

    //estimate # communities from fitted logistic regression
    int NumComs = 0, IdxRegDrop = 0;
    double LRThres = 1.1, RegDrop; // 1 / (1 + exp(1.1)) = 0.25
    double LeftReg = 0.0, RightReg = 0.0;
    TFltV Theta;
    LRMd->GetTheta(Theta);
    RegDrop = (- Theta[1] - LRThres) / Theta[0];
    if (RegDrop <= XV[0][0]) {
        NumComs = (int) RegComsV[0].Val2;
    }
    else if (RegDrop >= XV.Last()[0]) {
        NumComs = (int) RegComsV.Last().Val2;
    }
    else {  //interpolate for RegDrop
        for (int i = 0; i < XV.Len(); i++) {
            if (XV[i][0] > RegDrop) {
                IdxRegDrop = i;
                break;
            }
        }

        if (IdxRegDrop == 0) {
            printf("Error!! RegDrop:%f, Theta[0]:%f, Theta[1]:%f\n", RegDrop, Theta[0].Val, Theta[1].Val);
            for (int l = 0; l < RegLV.Len(); l++) {
                printf("X[%d]:%f, Y[%d]:%f\n", l, XV[l][0].Val, l, YV[l].Val);
            }
        }
        IAssert(IdxRegDrop > 0);
        LeftReg = RegDrop - XV[IdxRegDrop - 1][0];
        RightReg = XV[IdxRegDrop][0] - RegDrop;
        NumComs = (int) TMath::Round( (RightReg * RegComsV[IdxRegDrop - 1].Val2 + LeftReg * RegComsV[IdxRegDrop].Val2) / (LeftReg + RightReg));

    }
    //printf("Interpolation coeff: %f, %f, index at drop:%d (%f), Left-Right Vals: %f, %f\n", LeftReg, RightReg, IdxRegDrop, RegDrop, RegComsV[IdxRegDrop - 1].Val2, RegComsV[IdxRegDrop].Val2);
    printf("Num Coms:%d\n", NumComs);
    if (NumComs < 2) {
        NumComs = 2;
    }

    if (PltFPrx.Len() > 0) {
        TStr PlotTitle = TStr::Fmt("N:%d, E:%d ", Graph->GetNodes(), TE);
        TGnuPlot GPC(PltFPrx + ".l");
        GPC.AddPlot(RegComsV, gpwLinesPoints, "C");
        GPC.AddPlot(RegLV, gpwLinesPoints, "likelihood");
        GPC.AddPlot(RegBICV, gpwLinesPoints, "BIC");
        GPC.AddPlot(LRVScaled, gpwLinesPoints, "Sigmoid (scaled)");
        GPC.SetScale(gpsLog10X);
        GPC.SetTitle(PlotTitle);
        GPC.SavePng(PltFPrx + ".l.png");
    }

    return NumComs;
}
Exemplo n.º 27
0
void TNetInfBs::GreedyOpt(const int& MxEdges) {
    double CurProb = GetAllCascProb(-1, -1);
    double LastGain = TFlt::Mx;
    int attempts = 0;
    bool msort = false;

    for (int k = 0; k < MxEdges && EdgeGainV.Len() > 0; k++) {
      double prev = CurProb;

      const TIntPr BestE = GetBestEdge(CurProb, LastGain, msort, attempts);
      if (BestE == TIntPr(-1, -1)) // if we cannot add more edges, we stop
    	  break;

      if (CompareGroundTruth) {
    	  double precision = 0, recall = 0;
    	  if (PrecisionRecall.Len() > 1) {
    		  precision = PrecisionRecall[PrecisionRecall.Len()-1].Val2.Val;
    		  recall = PrecisionRecall[PrecisionRecall.Len()-1].Val1.Val;
    	  }
    	  if (GroundTruth->IsEdge(BestE.Val1, BestE.Val2)) {
			  recall++;
		  }	else {
			  precision++;
		  }

    	  PrecisionRecall.Add(TPair<TFlt, TFlt>(recall, precision));
      }

      Graph->AddEdge(BestE.Val1, BestE.Val2); // add edge to network

      double Bound = 0;
      if (BoundOn)
    	  Bound = GetBound(BestE, prev);

      // localized update!
      TIntV &CascsEdge = CascPerEdge.GetDat(BestE); // only check cascades that contain the edge
      for (int c = 0; c < CascsEdge.Len(); c++) {
    	  CascV[CascsEdge[c]].UpdateProb(BestE.Val1, BestE.Val2, true); // update probabilities
      }

      // some extra info for the added edge
      TInt Vol; TFlt AverageTimeDiff; TFltV TimeDiffs;
      Vol = 0; AverageTimeDiff = 0;
      for (int i=0; i< CascV.Len(); i++) {
    	  if (CascV[i].IsNode(BestE.Val2) && CascV[i].GetParent(BestE.Val2) == BestE.Val1) {
    		  Vol += 1; TimeDiffs.Add(CascV[i].GetTm(BestE.Val2)-CascV[i].GetTm(BestE.Val1));
    		  AverageTimeDiff += TimeDiffs[TimeDiffs.Len()-1]; }
      }
      AverageTimeDiff /= Vol;
      if (TimeDiffs.Len() > 0)
    	  TimeDiffs.Sort();
      else
    	  TimeDiffs.Add(0);

      // compute bound only if explicitly required
      EdgeInfoH.AddDat(BestE) = TEdgeInfo(Vol,
										  LastGain,
										  Bound,
										  TimeDiffs[(int)(TimeDiffs.Len()/2)],
										  AverageTimeDiff);
    }

    if (CompareGroundTruth) {
  	  for (int i=0; i<PrecisionRecall.Len(); i++) {
  		  PrecisionRecall[i].Val2 = 1.0 - PrecisionRecall[i].Val2/(PrecisionRecall[i].Val2+PrecisionRecall[i].Val1);
  		  PrecisionRecall[i].Val1 /= (double)GroundTruth->GetEdges();
  	  }
    }
}
Exemplo n.º 28
0
void GetMtxFromSepLine(const TStr& line, const TStr& separator, TFltV& matrix){
    TStrV strVals;
    line.SplitOnAllAnyCh(separator, strVals);
    for (int i = 0; i < strVals.Len(); i++) 
        matrix.Add(strVals[i].GetFlt());
}