double CalcEffDiam(const TFltPrV& DistNbrsCdfV, const double& Percentile) { TIntFltKdV KdV(DistNbrsCdfV.Len(), 0); for (int i = 0; i < DistNbrsCdfV.Len(); i++) { KdV.Add(TIntFltKd(int(DistNbrsCdfV[i].Val1()), DistNbrsCdfV[i].Val2)); } return CalcEffDiam(KdV, Percentile); }
int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaYV, const TStr& Label) { TFltKdV XYFltValV(XYValV.Len(), 0); for (int i = 0; i < XYValV.Len(); i++) { XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); } return AddErrBar(XYFltValV, DeltaYV, Label); }
int TGnuPlot::AddPlot(const TFltPrV& XYValV, const TGpSeriesTy& SeriesTy, const TStr& Label, const TStr& Style) { TFltKdV XYFltValV(XYValV.Len(), 0); for (int i = 0; i < XYValV.Len(); i++) { XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); } return AddPlot(XYFltValV, SeriesTy, Label, Style); }
void TGUtil::Normalize(TFltPrV& PdfV) { double Sum = 0.0; for (int i = 0; i < PdfV.Len(); i++) { Sum += PdfV[i].Val2; } if (Sum <= 0.0) { return; } for (int i = 0; i < PdfV.Len(); i++) { PdfV[i].Val2 /= Sum; } }
void TSirSR2Model::SetMediaBlogV(const TFltPrV& _MediaV, const TFltPrV& _BlogV) { IAssert(_MediaV.Len() == _BlogV.Len()); MediaV.Clr(false); BlogV.Clr(false); for (int i = 0; i < _MediaV.Len(); i++) { MediaV.Add(_MediaV[i].Val2); BlogV.Add(_BlogV[i].Val2); } }
int TGnuPlot::AddErrBar(const TFltPrV& XYValV, const TFltV& DeltaV, const TStr& DatLabel, const TStr& ErrLabel) { TFltKdV XYFltValV(XYValV.Len(), 0); for (int i = 0; i < XYValV.Len(); i++) { XYFltValV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); } const int PlotId = AddPlot(XYFltValV, gpwLinesPoints, DatLabel); AddErrBar(XYFltValV, DeltaV, ErrLabel); return PlotId; }
void TGnuPlot::MakeExpBins(const TFltPrV& XYValV, TFltPrV& ExpXYValV, const double& BinFactor, const double& MinYVal) { TFltKdV KdV(XYValV.Len(), 0), OutV; for (int i = 0; i < XYValV.Len(); i++) { KdV.Add(TFltKd(XYValV[i].Val1, XYValV[i].Val2)); } KdV.Sort(); TGnuPlot::MakeExpBins(KdV, OutV, BinFactor, MinYVal); ExpXYValV.Gen(OutV.Len(), 0); for (int i = 0; i < OutV.Len(); i++) { ExpXYValV.Add(TFltPr(OutV[i].Key, OutV[i].Dat)); } }
void GetNEFromAccDistr(const TFltPrV& deg, int& nodes, int& edges){ double nodesD = deg[0].Val2.Val, edgesD = 0; for (int i = 0; i < deg.Len(); i++){ if (i == deg.Len()-1) edgesD += deg[i].Val1.Val * deg[i].Val2.Val; else edgesD += deg[i].Val1.Val * (deg[i].Val2.Val - deg[i+1].Val2.Val); } nodes = static_cast<int>(nodesD); edges = static_cast<int>(edgesD); // edges /= 2; as Deg = inDeg + outDeg }
void GetCumDistr(const TFltPrV& nonCum, TFltPrV& res){ for (int i = nonCum.Len() - 1; i >=0; i--){ TFlt count; if (i == nonCum.Len() - 1) count = nonCum[i].Val2.Val; else count = nonCum[i].Val2.Val + res[res.Len()-1].Val2.Val; TFltPr val(nonCum[i].Val1, count); res.Add(val); } res.Sort(); }
// MLE power-coefficient int TGnuPlot::AddPwrFit2(const int& PlotId, const TGpSeriesTy& SeriesTy, const double& MinX, const TStr& Style) { const TGpSeries& Plot = SeriesV[PlotId]; if(Plot.XYValV.Empty()) return -1; const TFltKdV& XY = Plot.XYValV; // power fit TFltPrV XYPr; double MinY = TFlt::Mx; for (int s = 0; s < XY.Len(); s++) { if (XY[s].Key > 0.0) { XYPr.Add(TFltPr(XY[s].Key, XY[s].Dat)); MinY = TMath::Mn(MinY, XY[s].Dat()); } } if (XYPr.Empty()) return -1; MinY = TMath::Mn(1.0, MinY); // determine the sign of power coefficient double CoefSign = 0.0; { double A, B, R2, SigA, SigB, Chi2; TSpecFunc::PowerFit(XYPr, A, B, SigA, SigB, Chi2, R2); CoefSign = B > 0.0 ? +1.0 : -1.0; } const double PowerCf = CoefSign * TSpecFunc::GetPowerCoef(XYPr, MinX); int Mid = (int) exp(log((double)XYPr.Len())/2.0); if (Mid >= XYPr.Len()) { Mid = XYPr.Len()-1; } const double MidX = XYPr[Mid].Val1(); const double MidY = XYPr[Mid].Val2(); const double B = MidY / pow(MidX, PowerCf); TStr StyleStr=Style; if (StyleStr.Empty()) { StyleStr = "linewidth 3"; } const int FitId = AddFunc(TStr::Fmt("%f*x**%f", B, PowerCf), SeriesTy, TStr::Fmt("MLE = x^{%.4g}", PowerCf), StyleStr); return FitId; /*SeriesV.Add(); TGpSeries& NewPlot = SeriesV.Last(); TFltKdV& XYFit = NewPlot.XYValV; XYFit.Gen(XYPr.Len(), 0); for (int s = 0; s < XYPr.Len(); s++) { const double XVal = XYPr[s].Val1; const double YVal = B * pow(XYPr[s].Val1(), PowerCf); if (YVal < MinY || XVal < MinX) continue; XYFit.Add(TFltKd(XVal, YVal)); } NewPlot.Label = TStr::Fmt("PowerFit: %g", PowerCf); NewPlot.SeriesTy = SeriesTy; if (Style.Empty()) { NewPlot.WithStyle = "linewidth 3"; } else { NewPlot.WithStyle = Style; } return SeriesV.Len() - 1;*/ }
double CalcAvgDiamPdf(const TFltPrV& DistNbrsPdfV) { double Paths=0, SumLen=0; for (int i = 0; i < DistNbrsPdfV.Len(); i++) { SumLen += DistNbrsPdfV[i].Val1 * DistNbrsPdfV[i].Val2; Paths += DistNbrsPdfV[i].Val2; } return SumLen/Paths; }
// CHECK void ExpBinning(const TFltPrV& deg, TFltPrV& degSparse, const int& BinRadix){ TFlt maxDeg(deg[deg.Len()-1].Val1.Val), minDeg(deg[0].Val1.Val); bool maxPowerReached = false; // idx - index of border, previdx - index of previous border int power = 0, previdx = 0, idx, binSize; TFltPr val; double binBorder = 0.0; while (binBorder <= minDeg) binBorder = pow(static_cast<double>(BinRadix), power++); TFltPr v(minDeg, deg[0].Val2.Val); degSparse.Add(v); bool isExact = false; while (!maxPowerReached){ if (binBorder >= maxDeg){ // when last element of deg was previous bin border if (previdx == deg.Len() - 1) break; // if we have another elements binBorder = maxDeg; maxPowerReached = true; } // find next element idx = FindVal1Elem(deg, binBorder, isExact); // if bin size == 0 if (previdx + 1 == idx && !isExact) continue; if (!isExact) idx = idx - 1; double sum = 0.0; binSize = idx - previdx; for (int i = previdx + 1; i <= idx; i++){ sum += deg[i].Val2.Val; } sum /= binSize; // if prevBinBorder was the smallest degree, it can be more than binBorder / BinRadix double SumBinBorder = previdx > 0 ? binBorder + static_cast<double>(binBorder) / BinRadix : binBorder + static_cast<double>(minDeg); double avgDeg = SumBinBorder / 2.0; val.Val1 = avgDeg; val.Val2 = sum; degSparse.Add(val); previdx = idx; binBorder = pow(static_cast<double>(BinRadix), power++); } }
// Inverse participation ratio: normalize EigVec to have L2=1 and then I=sum_k EigVec[i]^4 // see Spectra of "real-world" graphs: Beyond the semicircle law by Farkas, Derenyi, Barabasi and Vicsek void PlotInvParticipRat(const PUNGraph& Graph, const int& MaxEigVecs, const int& TimeLimit, const TStr& FNmPref, TStr DescStr) { TFltPrV EigIprV; GetInvParticipRat(Graph, MaxEigVecs, TimeLimit, EigIprV); if (DescStr.Empty()) { DescStr = FNmPref; } if (EigIprV.Empty()) { DescStr+=". FAIL"; EigIprV.Add(TFltPr(-1,-1)); return; } TGnuPlot::PlotValV(EigIprV, "eigIPR."+FNmPref, TStr::Fmt("%s. G(%d, %d). Largest eig val = %f (%d values)", DescStr.CStr(), Graph->GetNodes(), Graph->GetEdges(), EigIprV.Last().Val1(), EigIprV.Len()), "Eigenvalue", "Inverse Participation Ratio of corresponding Eigenvector", gpsLog10Y, false, gpwPoints); }
void GetNodesEdgesCountFromDegDistr(const TFltPrV& deg, int& nodes, int& edges){ double nodesD = 0, edgesD = 0; for (int i = 0; i < deg.Len(); i++){ nodesD += deg[i].Val2.Val; edgesD += deg[i].Val1.Val * deg[i].Val2.Val; } nodes = static_cast<int>(nodesD); edges = static_cast<int>(edgesD); // edges /= 2; as Deg = inDeg + outDeg }
int FindVal1Elem(const TFltPrV& vec, const TFlt& elem, bool& isExact){ for (int i = 0; i < vec.Len(); i++){ if (vec[i].Val1.Val == elem){ isExact = true; return i; } if (vec[i].Val1.Val > elem){ return i-1; } } return false; }
void GetPoints(const TFlt& maxDegLog, const TFlt& minDegLog, const int& NInt, const TFltPrV& base, TFltPrV& points){ int beginIndex = 0; // ignore nodes with zero degree (for Kronecker graphs) /*if (base[0].Val1.Val != 0) points.Add(base[beginIndex]); else { points.Add(base[++beginIndex]); }*/ points.Add(base[beginIndex]); TFlt baseMaxDeg = base[base.Len()-1].Val1.Val, baseMinDeg = base[beginIndex].Val1.Val; for (int i = beginIndex + 1; i < NInt; i++){ // deg - degree to be found in base TFlt degRound (pow (10, minDegLog.Val + i * (maxDegLog.Val - minDegLog.Val) / NInt)); TInt degInt(static_cast<int>(degRound.Val)); TFlt deg(degInt); // if deg < baseMinDeg (for cases when baseMinDeg > minDeg) if (deg.Val <= baseMinDeg) continue; // if deg > baseMaxDeg, add last point and finish if (deg.Val >= baseMaxDeg){ points.Add(base[base.Len()-1]); break; } // we have two cases: when we can find an exact value of deg, or when we have not this value bool isExact = false; int index = FindVal1Elem(base, deg, isExact); if (isExact){ points.Add(base[index]); } else { TFltPr x; x.Val1.Val = deg; x.Val2.Val = ( base[index].Val2.Val + base [index + 1].Val2.Val ) / 2; points.Add(x); } } }
// get degrees from current and add it to degrees void AddDegreeStat(const TFltPrV& current, TFltPrV& degrees, TIntPrV& samples){ for (int j = 0; j < current.Len(); j++){ const TFltPr& elem = current[j]; const double& deg = elem.Val1.Val, &nodesCount = elem.Val2.Val; bool wasFound = false; // silly search for (int k = 0; k < degrees.Len(); k++){ if (degrees[k].Val1.Val == deg){ degrees[k].Val2.Val += nodesCount; samples[k].Val2.Val++; wasFound = true; break; } } if (!wasFound){ TFlt d(deg), n(nodesCount); TFltPr val(d,n); degrees.Add(val); TInt di(static_cast<int>(deg)); TIntPr valI(di, 1); samples.Add(valI); } } }
void TGStat::AvgGStat(const TGStatV& GStatV, const bool& ClipAt1) { if (GStatV.Empty()) return; Time = GStatV[0]->Time; GraphNm = GStatV[0]->GraphNm; // values for (int statVal = 0; statVal > gsvMx; statVal++) { const TGStatVal GStatVal = TGStatVal(statVal); TMom Mom; for (int i = 0; i < GStatV.Len(); i++) { if (GStatV[i]->HasVal(GStatVal)) { Mom.Add(GStatV[i]->GetVal(GStatVal)); } } Mom.Def(); if (Mom.IsUsable()) { IAssert(Mom.GetVals() == GStatV.Len()); // all must have the value SetVal(GStatVal, Mom.GetMean()); } } // distributions for (int distr = gsdUndef; distr < gsdMx; distr++) { const TGStatDistr GStatDistr = TGStatDistr(distr); THash<TFlt, TFlt> ValToSumH; int DistrCnt = 0; for (int i = 0; i < GStatV.Len(); i++) { if (GStatV[i]->HasDistr(GStatDistr)) { const TFltPrV& D = GStatV[i]->GetDistr(GStatDistr); for (int d = 0; d < D.Len(); d++) { ValToSumH.AddDat(D[d].Val1) += D[d].Val2; } DistrCnt++; } } IAssert(DistrCnt==0 || DistrCnt==GStatV.Len()); // all must have distribution TFltPrV AvgStatV; ValToSumH.GetKeyDatPrV(AvgStatV); AvgStatV.Sort(); for (int i = 0; i < AvgStatV.Len(); i++) { AvgStatV[i].Val2 /= double(DistrCnt); if (ClipAt1 && AvgStatV[i].Val2 < 1) { AvgStatV[i].Val2 = 1; } } SetDistr(GStatDistr, AvgStatV); } }
void TGUtil::GetCdf(const TFltPrV& PdfV, TFltPrV& CdfV) { CdfV = PdfV; for (int i = 1; i < CdfV.Len(); i++) { CdfV[i].Val2 = CdfV[i-1].Val2 + CdfV[i].Val2; } }
void TGUtil::GetPdf(const TFltPrV& CdfV, TFltPrV& PdfV) { PdfV = CdfV; for (int i = PdfV.Len()-1; i > 0; i--) { PdfV[i].Val2 = PdfV[i].Val2 - PdfV[i-1].Val2; } }
void TGUtil::GetCCdf(const TFltPrV& PdfV, TFltPrV& CCdfV) { CCdfV = PdfV; for (int i = CCdfV.Len()-2; i >= 0; i--) { CCdfV[i].Val2 = CCdfV[i+1].Val2 + CCdfV[i].Val2; } }
void GetAvgDegreeStat (TFltPrV& deg, const TIntPrV& samples){ for (int i = 0; i < deg.Len(); i++) deg[i].Val2.Val /= static_cast<double>(samples[i].Val2.Val); }
void GetAvgDegreeStat (TFltPrV& deg, const TInt& NKron){ for (int i = 0; i < deg.Len(); i++) deg[i].Val2.Val /= NKron; }