1 #ifndef snap_kronecker_h
2 #define snap_kronecker_h
20 TKronMtx(
const int& Dim) : MtxDim(Dim), SeedMtx(Dim*Dim) { }
22 TKronMtx(
const TKronMtx& Kronecker) : MtxDim(Kronecker.MtxDim), SeedMtx(Kronecker.SeedMtx) { }
31 int Len()
const {
return SeedMtx.
Len(); }
38 void SetRndMtx(
const int& MtxDim,
const double& MinProb = 0.0);
40 void GenMtx(
const int& Dim) { MtxDim=Dim; SeedMtx.
Gen(Dim*Dim); }
41 void SetEpsMtx(
const double& Eps1,
const double& Eps0,
const int& Eps1Val=1,
const int& Eps0Val=0);
42 void SetForEdges(
const int& Nodes,
const int& Edges);
46 const double&
At(
const int& Row,
const int& Col)
const {
return SeedMtx[MtxDim*Row+Col].Val; }
47 double&
At(
const int& Row,
const int& Col) {
return SeedMtx[MtxDim*Row+Col].Val; }
48 const double&
At(
const int& ValN)
const {
return SeedMtx[ValN].Val; }
49 double&
At(
const int& ValN) {
return SeedMtx[ValN].Val; }
51 int GetNodes(
const int& NIter)
const;
52 int GetEdges(
const int& NIter)
const;
55 double GetEZero(
const int& Edges,
const int& KronIter)
const;
66 double GetEdgeProb(
int NId1,
int NId2,
const int& NKronIters)
const;
67 double GetNoEdgeProb(
int NId1,
int NId2,
const int& NKronIters)
const;
68 double GetEdgeLL(
int NId1,
int NId2,
const int& NKronIters)
const;
69 double GetNoEdgeLL(
int NId1,
int NId2,
const int& NKronIters)
const;
70 double GetApxNoEdgeLL(
int NId1,
int NId2,
const int& NKronIters)
const;
71 bool IsEdgePlace(
int NId1,
int NId2,
const int& NKronIters,
const double& ProbTresh)
const;
73 double GetEdgeDLL(
const int& ParamId,
int NId1,
int NId2,
const int& NKronIters)
const;
74 double GetNoEdgeDLL(
const int& ParamId,
int NId1,
int NId2,
const int& NKronIters)
const;
75 double GetApxNoEdgeDLL(
const int& ParamId,
int NId1,
int NId2,
const int& NKronIters)
const;
85 static int GetKronIter(
const int& GNodes,
const int& SeedMtxSz);
99 void Dump(
const TStr& MtxNm =
TStr(),
const bool& Sort =
false)
const;
150 TKroneckerLL() : Nodes(-1), KronIters(-1), PermSwapNodeProb(0.2), RealNodes(-1), RealEdges(-1), LogLike(
TKronMtx::NInf), EMType(
kronNodeMiss), MissEdges(-1), DebugMode(false) { }
167 void SetDebug(
const bool Debug) { DebugMode = Debug; }
173 bool IsObsEdge(
const int& NId1,
const int& NId2)
const {
IAssert(RealNodes > 0);
return ((NId1 < RealNodes) && (NId2 < RealNodes)); }
178 void SetPerm(
const char& PermId);
207 double SwapNodesLL(
const int& NId1,
const int& NId2);
216 double NodeDLLDelta(
const int ParamId,
const int& NId)
const;
219 double GetDLL(
const int& ParamId)
const {
return GradV[ParamId]; }
223 double GradDescent(
const int& NIter,
const double& LrnRate,
double MnStep,
double MxStep,
const int& WarmUp,
const int& NSamples);
224 double GradDescent2(
const int& NIter,
const double& LrnRate,
double MnStep,
double MxStep,
const int& WarmUp,
const int& NSamples);
231 double RunMStep(
const TFltV& LLV,
const TVec<TFltV>& DLLV,
const int& GradIter,
const double& LrnRate,
double MnStep,
double MxStep);
232 void RunKronEM(
const int& EMIter,
const int& GradIter,
double LrnRate,
double MnStep,
double MxStep,
const int& GibbsWarmUp,
const int& WarmUp,
const int& NSamples,
const TKronEMType& Type =
kronNodeMiss,
const int& NMissing = -1);
239 TFltQu TestKronDescent(
const bool& DoExact,
const bool& TruePerm,
double LearnRate,
const int& WarmUp,
const int& NSamples,
const TKronMtx& TrueParam);
241 double LearnRate,
const int& WarmUp,
const int& NSamples,
const int& AvgKGraphs,
const TKronMtx& TrueParam);
243 double LearnRate,
const int& WarmUp,
const int& NSamples,
const int& TrueN0);
244 static void TestGradDescent(
const int& KronIters,
const int& KiloSamples,
const TStr& Permutation);
274 TFEval(
const TFEval& FEval) : LogLike(FEval.LogLike), GradV(FEval.GradV) { }
276 LogLike=FEval.
LogLike; GradV=FEval.
GradV; }
return *
this; }
284 void SetPerm(
const char& PermId);
286 void GradDescent(
const int& NIter,
const double& LrnRate,
const double& MnStep,
const double& MxStep,
287 const double& WarmUp,
const double& NSamples);
308 Edges=0; Hairpins=0; Tripins=0; Triads=0;
310 const int d = NI.GetOutDeg();
312 Hairpins += d*(d-1.0);
313 Tripins += d*(d-1.0)*(d-2.0);
320 printf(
"E:%g\tH:%g\tT:%g\tD:%g\n", Edges, Hairpins, Tripins, Triads);
324 const double Step = 0.01;
326 double A=0, B=0, C=0;
328 for (
double a = 1.0; a > Step; a-=Step) {
329 for (
double b = Step; b <= 1.0; b+=Step) {
330 for (
double c = Step; c <= a; c+=Step) {
331 double EE = ( pow(a+2*b+c, R) - pow(a+c, R) ) / 2.0;
332 double EH = ( pow(pow(a+b,2) + pow(b+c,2), R)
333 -2*pow(a*(a+b)+c*(c+b), R)
334 -pow(a*a + 2*b*b + c*c, R)
335 +2*pow(a*a + c*c, R) ) / 2.0;
336 double ET = ( pow(pow(a+b,3)+pow(b+c,3), R)
337 -3*pow(a*pow(a+b,2)+c*pow(b+c,2), R)
338 -3*pow(a*a*a + c*c*c + b*(a*a+c*c) + b*b*(a+c) + 2*b*b*b ,R)
339 +2*pow(a*a*a + 2*b*b*b + c*c*c, R)
340 +5*pow(a*a*a + c*c*c + b*b*(a+c), R)
341 +4*pow(a*a*a + c*c*c + b*(a*a+c*c), R)
342 -6*pow(a*a*a + c*c*c, R) ) / 6.0;
343 double ED = ( pow(a*a*a + 3*b*b*(a+c) + c*c*c, R)
344 -3*pow(a*(a*a+b*b) + c*(b*b+c*c), R)
345 +2*pow(a*a*a+c*c*c, R) ) / 6.0;
346 if (EE < 0) { EE = 1; }
347 if (EH < 0) { EH = 1; }
348 if (ET < 0) { ET = 1; }
349 if (ED < 0) { ED = 1; }
351 double Score = pow(Edges-EE,2)/EE + pow(Hairpins-EH ,2)/EH + pow(Tripins-ET, 2)/ET + pow(Triads-ED, 2)/ED;
356 printf(
"%.03f %.03f %0.03f %10.4f %10.10g\t%10.10g\t%10.10g\t%10.10g\n", a,b,c, log10(Score), EE, EH, ET, ED);
358 A=a; B=b; C=c; MinScore=Score;
363 printf(
"\t\t\t %10.10g\t%10.10g\t%10.10g\t%10.10g\n", Edges, Hairpins, Tripins, Triads);
364 return TFltQu(A,B,C,MinScore);
368 TFIn FIn(
"as20.ngraph");
const TFltV & GetDLL() const
void SetRndMtx(const int &MtxDim, const double &MinProb=0.0)
TKronMomentsFit(const PUNGraph &G)
TKronMtx & operator=(const TKronMtx &Kronecker)
int64 GetTriads(const PGraph &Graph, int64 &ClosedTriads, int64 &OpenTriads, int SampleNodes=-1)
Computes the number of Closed and Open triads.
const double & At(const int &ValN) const
static int RemoveNodeNoise(PNGraph &Graph, const int &NNodes, const bool Random=true)
!!!!! MYUNGHWAN, CHECK!
static PNGraph GenKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir, const int &Seed=0)
static PKroneckerLL New()
double GetEdgeProb(int NId1, int NId2, const int &NKronIters) const
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
TFEval(const TFlt &LL, const TFltV &DLL)
int GetEdges(const int &NIter) const
static void TestBicCriterion(const TStr &OutFNm, const TStr &Desc1, const PNGraph &G, const int &GradIters, double LearnRate, const int &WarmUp, const int &NSamples, const int &TrueN0)
static void KronMul(const TKronMtx &LeftPt, const TKronMtx &RightPt, TKronMtx &OutMtx)
TFltV TestSamplePerm(const TStr &OutFNm, const int &WarmUp, const int &NSamples, const TKronMtx &TrueMtx, const bool &DoPlot=true)
static TKronMtx LoadTxt(const TStr &MtxFNm)
const TKronMtx & GetLLMtx() const
void SetRandomEdges(const int &NEdges, const bool isDir=true)
!!!!! MYUNGHWAN, CHECK!
int GetKronIter(const int &Nodes) const
void RunEStep(const int &GibbsWarmUp, const int &WarmUp, const int &NSamples, TFltV &LLV, TVec< TFltV > &DLLV)
void UpdateGraphDLL(const int &SwapNId1, const int &SwapNId2)
static double CalcChainR2(const TVec< TFltV > &ChainLLV)
double GetApxNoEdgeLL(int NId1, int NId2, const int &NKronIters) const
double & At(const int &Row, const int &Col)
double GetFullGraphLL() const
int GetEdges() const
Returns the number of edges in the graph.
void GradDescent(const int &NIter, const double &LrnRate, const double &MnStep, const double &MxStep, const double &WarmUp, const double &NSamples)
bool IsEdgePlace(int NId1, int NId2, const int &NKronIters, const double &ProbTresh) const
void Swap(TKronMtx &KronMtx)
TSizeTy Len() const
Returns the number of elements in the vector.
Node iterator. Only forward iteration (operator++) is supported.
const TFltV & GetLLHist() const
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
void Dump(const TStr &MtxNm=TStr(), const bool &Sort=false) const
TPt< TKroneckerLL > PKroneckerLL
double NodeDLLDelta(const int ParamId, const int &NId) const
double GetFullRowLL(int RowId) const
void SetPerm(const char &PermId)
double GetEdgeLL(int NId1, int NId2, const int &NKronIters) const
double GetEmptyGraphDLL(const int &ParamId) const
double GetEmptyGraphLL() const
static uint GetNodeSig(const double &OneProb=0.5)
bool SampleNextPerm(int &NId1, int &NId2)
static TKronMtx GetInitMtx(const int &Dim, const int &Nodes, const int &Edges)
void RunKronEM(const int &EMIter, const int &GradIter, double LrnRate, double MnStep, double MxStep, const int &GibbsWarmUp, const int &WarmUp, const int &NSamples, const TKronEMType &Type=kronNodeMiss, const int &NMissing=-1)
void SetIPerm(const TIntV &Perm)
!!!!! MYUNGHWAN, CHECK!
void SetForEdges(const int &Nodes, const int &Edges)
static void KronPwr(const TKronMtx &KronPt, const int &NIter, TKronMtx &OutMtx)
const double & At(const int &Row, const int &Col) const
void SetEpsMtx(const double &Eps1, const double &Eps0, const int &Eps1Val=1, const int &Eps0Val=0)
int GetNodes() const
Returns the number of nodes in the graph.
double GetEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
void RestoreGraph(const bool RestoreNodes=true)
!!!!! MYUNGHWAN, CHECK!
bool Empty() const
Tests whether the vector is empty.
TFEval(const TFEval &FEval)
static void TestGradDescent(const int &KronIters, const int &KiloSamples, const TStr &Permutation)
bool IsObsEdge(const int &NId1, const int &NId2) const
void GetProbMtx(TKronMtx &ProbMtx)
void SetBestDegPerm()
!!!!! MYUNGHWAN, CHECK!
void SetPerm(const char &PermId)
static int FlipEdgeNoise(PNGraph &Graph, const int &NEdges, const bool Random=true)
double GetApxEmptyGraphLL() const
static void PutRndSeed(const int &Seed)
double GetApxNoEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
static TKronMtx GetMtxFromNm(const TStr &MtxNm)
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
void SampleGradient(const int &WarmUp, const int &NSamples, double &AvgLL, TFltV &GradV)
static PNGraph Load(TSIn &SIn)
Static constructor that loads the graph from a stream SIn and returns a pointer to it...
PNGraph GenRndGraph(const double &RndFact=1.0) const
double NodeLLDelta(const int &NId) const
TKronMaxLL(const PNGraph &GraphPt, const TKronMtx &StartParam)
double GetApxEmptyGraphDLL(const int &ParamId) const
double RunMStep(const TFltV &LLV, const TVec< TFltV > &DLLV, const int &GradIter, const double &LrnRate, double MnStep, double MxStep)
bool IsObsNode(const int &NId) const
bool IsLatentNode(const int &NId) const
double GetDLL(const int &ParamId) const
double & At(const int &ValN)
void SetMtx(const TFltV &ParamV)
void AddRndNoise(const double &SDev)
static double GetAvgFroErr(const TKronMtx &Kron1, const TKronMtx &Kron2)
void GradDescentConvergence(const TStr &OutFNm, const TStr &Desc1, const bool &SamplePerm, const int &NIters, double LearnRate, const int &WarmUp, const int &NSamples, const int &AvgKGraphs, const TKronMtx &TrueParam)
static void ChainGelmapRubinPlot(const TVec< TFltV > &ChainLLV, const TStr &OutFNm, const TStr &Desc)
TQuad< TFlt, TFlt, TFlt, TFlt > TFltQu
double GetFullColLL(int ColId) const
void PutAllMtx(const double &Val)
void InitLL(const TFltV &ParamV)
const TKronMtx & GetProbMtx() const
double GetEZero(const int &Edges, const int &KronIter) const
double GetNoEdgeProb(int NId1, int NId2, const int &NKronIters) const
int GetNZeroK(const PNGraph &Graph) const
void GetLLMtx(TKronMtx &LLMtx)
const TFltV & CalcGraphDLL()
void MetroGibbsSampleSetup(const int &WarmUp)
const TFltV & CalcFullApxGraphDLL()
void MetroGibbsSampleNext(const int &WarmUp, const bool DLLUpdate=false)
const TVec< TKronMtx > & GetParamHist() const
static bool IsInEps(const T &Val, const T &Eps)
double GradDescent2(const int &NIter, const double &LrnRate, double MnStep, double MxStep, const int &WarmUp, const int &NSamples)
static PNGraph GenDetKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir)
void GenMtx(const int &Dim)
void PutSeed(const int &_Seed)
double GetColSum(const int &ColId) const
void SetPerm(const TIntV &NodePermV)
const TFltV & CalcApxGraphDLL()
double GetNoEdgeLL(int NId1, int NId2, const int &NKronIters) const
static TKronMtx GetRndMtx(const int &Dim, const double &MinProb)
void SaveTxt(const TStr &OutFNm) const
double SwapNodesLL(const int &NId1, const int &NId2)
THash< TKronMtx, TFEval > FEvalH
TFEval & operator=(const TFEval &FEval)
TNodeI EndNI() const
Returns an iterator referring to the past-the-end node in the graph.
bool IsLatentEdge(const int &NId1, const int &NId2) const
static void KronSum(const TKronMtx &LeftPt, const TKronMtx &RightPt, TKronMtx &OutMtx)
const TFltV & GetMtx() const
const TIntV & GetPermV() const
double GetRowSum(const int &RowId) const
int GetNodes(const int &NIter) const
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
TFltQu EstABC(const int &R)
PNGraph GenThreshGraph(const double &Thresh) const
double GetNoEdgeDLL(const int &ParamId, int NId1, int NId2, const int &NKronIters) const
void SetGraph(const PNGraph &GraphPt)
TFltQu TestKronDescent(const bool &DoExact, const bool &TruePerm, double LearnRate, const int &WarmUp, const int &NSamples, const TKronMtx &TrueParam)
TNodeI BegNI() const
Returns an iterator referring to the first node in the graph.
void DelSelfEdges(const PGraph &Graph)
Removes all the self-edges from the graph.
void SetDebug(const bool Debug)
TKronMtx(const TKronMtx &Kronecker)
static double GetAvgAbsErr(const TKronMtx &Kron1, const TKronMtx &Kron2)
static void PlotCmpGraphs(const TKronMtx &SeedMtx, const PNGraph &Graph, const TStr &OutFNm, const TStr &Desc)
static void RoundTheta(const TFltV &ThetaV, TFltV &NewThetaV)
double GradDescent(const int &NIter, const double &LrnRate, double MnStep, double MxStep, const int &WarmUp, const int &NSamples)
int GetPrimHashCd() const
bool operator==(const TKronMtx &Kronecker) const
static int RemoveEdgeNoise(PNGraph &Graph, const int &NEdges)
static PNGraph GenFastKronecker(const TKronMtx &SeedMtx, const int &NIter, const bool &IsDir, const int &Seed=0)
void PrintInfo(const PGraph &Graph, const TStr &Desc="", const TStr &OutFNm="", const bool &Fast=true)
Prints basic graph statistics.