25 TAGMFast(
const PUNGraph& GraphPt,
const int& InitComs,
const int RndSeed = 0): Rnd(RndSeed), RegCoef(0),
26 NodesOk(true), MinVal(0.0), MaxVal(1000.0), NegWgt(1.0) {
SetGraph(GraphPt);
RandomInit(InitComs); }
28 void SetRegCoef(
const double _RegCoef) { RegCoef = _RegCoef; }
33 double Likelihood(
const bool DoParallel =
false);
36 int MLENewton(
const double& Thres,
const int& MaxIter,
const TStr& PlotNm =
TStr());
43 int FindComsByCV(
TIntV& ComsV,
const double HOFrac = 0.2,
const int NumThreads = 20,
const TStr& PlotLFNm =
TStr(),
const double StepAlpha = 0.3,
const double StepBeta = 0.1);
44 int FindComsByCV(
const int NumThreads,
const int MaxComs,
const int MinComs,
const int DivComs,
const TStr& OutFNm,
const double StepAlpha = 0.3,
const double StepBeta = 0.3);
47 int MLEGradAscent(
const double& Thres,
const int& MaxIter,
const TStr& PlotNm,
const double StepAlpha = 0.3,
const double StepBeta = 0.1);
48 int MLEGradAscentParallel(
const double& Thres,
const int& MaxIter,
const int ChunkNum,
const int ChunkSize,
const TStr& PlotNm,
const double StepAlpha = 0.3,
const double StepBeta = 0.1);
49 int MLEGradAscentParallel(
const double& Thres,
const int& MaxIter,
const int ChunkNum,
const TStr& PlotNm =
TStr(),
const double StepAlpha = 0.3,
const double StepBeta = 0.1) {
50 int ChunkSize = G->
GetNodes() / 10 / ChunkNum;
51 if (ChunkSize == 0) { ChunkSize = 1; }
56 void Load(
TSIn& SIn,
const int& RndSeed = 0);
57 double inline GetCom(
const int& NID,
const int& CID) {
58 if (F[NID].IsKey(CID)) {
64 void inline AddCom(
const int& NID,
const int& CID,
const double& Val) {
65 if (F[NID].IsKey(CID)) {
66 SumFV[CID] -= F[NID].
GetDat(CID);
68 F[NID].AddDat(CID) = Val;
72 void inline DelCom(
const int& NID,
const int& CID) {
73 if (F[NID].IsKey(CID)) {
74 SumFV[CID] -= F[NID].
GetDat(CID);
82 if (VV.
IsKey(HI.GetKey())) {
83 DP += VV.
GetDat(HI.GetKey()) * HI.GetDat();
88 if (UV.
IsKey(HI.GetKey())) {
89 DP += UV.
GetDat(HI.GetKey()) * HI.GetDat();
95 double inline DotProduct(
const int& UID,
const int& VID) {
99 double DP = log (1.0 / (1.0 - PNoCom)) +
DotProduct(FU, FV);
116 N += HI.GetDat() * HI.GetDat();
130 template<
class PGraph>
134 if (GraphType) { Edges2 = Edges >= 0 ? Edges : Graph->GetEdges(); }
135 else { Edges2 = Edges >= 0 ? 2 * Edges : Graph->GetEdges(); }
136 int Vol = 0, Cut = 0;
138 for (
int i = 0; i < CmtyS.
Len(); i++) {
139 if (! Graph->IsNode(CmtyS[i])) {
continue; }
140 typename PGraph::TObj::TNodeI NI = Graph->GetNI(CmtyS[i]);
141 for (
int e = 0; e < NI.GetOutDeg(); e++) {
142 if (! CmtyS.
IsKey(NI.GetOutNId(e))) { Cut += 1; }
144 Vol += NI.GetOutDeg();
148 if (2 * Vol > Edges2) { Phi = Cut / double (Edges2 - Vol); }
149 else if (Vol == 0) { Phi = 0.0; }
150 else { Phi = Cut / double(Vol); }
152 if (Vol == Edges2) { Phi = 1.0; }
158 template<
class PGraph>
160 TIntPrV EdgeV(G->GetEdges(), 0);
161 for (
typename PGraph::TObj::TEdgeI EI = G->BegEI(); EI < G->EndEI(); EI++) {
162 EdgeV.
Add(
TIntPr(EI.GetSrcNId(), EI.GetDstNId()));
167 HoldOutSet.
Gen(G->GetNodes());
168 int HOTotal = int(HOFrac * G->GetNodes() * (G->GetNodes() - 1) / 2.0);
169 if (GraphType) { HOTotal *= 2;}
171 int HOEdges = (int)
TMath::Round(HOFrac * G->GetEdges());
172 printf(
"holding out %d edges...\n", HOEdges);
173 for (
int he = 0; he < (int) HOEdges; he++) {
174 HoldOutSet[EdgeV[he].Val1].AddKey(EdgeV[he].Val2);
175 if (! GraphType) { HoldOutSet[EdgeV[he].Val2].AddKey(EdgeV[he].Val1); }
178 printf(
"%d Edges hold out\n", HOCnt);
179 while(HOCnt++ < HOTotal) {
182 if (SrcNID == DstNID) {
continue; }
183 HoldOutSet[SrcNID].AddKey(DstNID);
184 if (! GraphType) { HoldOutSet[DstNID].AddKey(SrcNID); }
188 template<
class PGraph>
190 typename PGraph::TObj::TNodeI NI = Graph->GetNI(NID);
191 NBCmtyS.
Gen(NI.GetDeg());
193 for (
int e = 0; e < NI.GetDeg(); e++) {
194 NBCmtyS.
AddKey(NI.GetNbrNId(e));
197 template<
class PGraph>
199 NIdPhiV.
Gen(G->GetNodes(), 0);
200 const int Edges = G->GetEdges();
203 for (
typename PGraph::TObj::TNodeI NI = G->BegNI(); NI < G->EndNI(); NI++) {
204 TIntSet NBCmty(NI.GetDeg() + 1);
206 if (NI.GetDeg() < 5) {
209 TAGMFastUtil::GetNbhCom<PGraph>(G, NI.GetId(), NBCmty);
214 printf(
"conductance computation completed [%s]\n", RunTm.
GetTmStr());
double Norm2(const TIntFltH &UV)
TPair< TInt, TInt > TIntPr
void SetCmtyVV(const TVec< TIntV > &CmtyVV)
int MLENewton(const double &Thres, const int &MaxIter, const TStr &PlotNm=TStr())
Newton method: DEPRECATED.
double Prediction(const int &UID, const int &VID)
#define IAssertR(Cond, Reason)
TPair< TFlt, TInt > TFltIntPr
int MLEGradAscentParallel(const double &Thres, const int &MaxIter, const int ChunkNum, const int ChunkSize, const TStr &PlotNm, const double StepAlpha=0.3, const double StepBeta=0.1)
double LikelihoodHoldOut(const bool DoParallel=false)
double Prediction(const TIntFltH &FU, const TIntFltH &FV)
void NeighborComInit(const int InitComs)
static double GetConductance(const PGraph &Graph, const TIntSet &CmtyS, const int Edges)
double GradientForOneVar(const TFltV &AlphaKV, const int UID, const int CID, const double &Val)
void DelCom(const int &NID, const int &CID)
static void GetNbhCom(const PGraph &Graph, const int NID, TIntSet &NBCmtyS)
int FindComsByCV(TIntV &ComsV, const double HOFrac=0.2, const int NumThreads=20, const TStr &PlotLFNm=TStr(), const double StepAlpha=0.3, const double StepBeta=0.1)
void SetRegCoef(const double _RegCoef)
void Gen(const int &ExpectVals)
const TDat & GetDat(const TKey &Key) const
bool IsKey(const TKey &Key) const
int MLEGradAscent(const double &Thres, const int &MaxIter, const TStr &PlotNm, const double StepAlpha=0.3, const double StepBeta=0.1)
int GetNodes() const
Returns the number of nodes in the graph.
double GetCom(const int &NID, const int &CID)
double LikelihoodForOneVar(const TFltV &AlphaKV, const int UID, const int CID, const double &Val)
double DotProduct(const int &UID, const int &VID)
const char * GetTmStr() const
#define HasGraphFlag(TGraph, Flag)
For quick testing of the properties of the graph/network object (see TGraphFlag). ...
static void GetNIdPhiV(const PGraph &G, TFltIntPrV &NIdPhiV)
double GetStepSizeByLineSearch(const int UID, const TIntFltH &DeltaV, const TIntFltH &GradV, const double &Alpha, const double &Beta, const int MaxIter=10)
const TVal & GetDat(const TVal &Val) const
Returns reference to the first occurrence of element Val.
static double Round(const double &Val)
void GetCmtyVV(TVec< TIntV > &CmtyVV)
int AddKey(const TKey &Key)
int MLEGradAscentParallel(const double &Thres, const int &MaxIter, const int ChunkNum, const TStr &PlotNm=TStr(), const double StepAlpha=0.3, const double StepBeta=0.1)
void SetGraph(const PUNGraph &GraphPt)
Utility functions for BigCLAM, Coda.
directed graph (TNGraph, TNEGraph), else graph is undirected TUNGraph
void Load(TSIn &SIn, const int &RndSeed=0)
double LikelihoodForRow(const int UID)
double HessianForOneVar(const TFltV &AlphaKV, const int UID, const int CID, const double &Val)
Community detection with AGM. Sparse AGM-fast with coordinate ascent.
static TStr Fmt(const char *FmtStr,...)
TAGMFast(const PUNGraph &GraphPt, const int &InitComs, const int RndSeed=0)
double Likelihood(const bool DoParallel=false)
double Sum(const TIntFltH &UV)
static void GenHoldOutPairs(const PGraph &G, TVec< TIntSet > &HoldOutSet, double HOFrac, TRnd &Rnd)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
void AddCom(const int &NID, const int &CID, const double &Val)
int GetUniDevInt(const int &Range=0)
void RandomInit(const int InitComs)
bool IsKey(const TKey &Key) const
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
double DotProduct(const TIntFltH &UV, const TIntFltH &VV)
void GradientForRow(const int UID, TIntFltH &GradU, const TIntSet &CIDSet)
Vector is a sequence TVal objects representing an array that can change in size.