SNAP Library 3.0, User Reference  2016-07-20 17:56:49
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mag.h
Go to the documentation of this file.
1 #ifndef snap_mag_h
2 #define snap_mag_h
3 
4 #include "Snap.h"
5 #include "kronecker.h"
6 
8 // MAG affinity matrix
9 
10 class TMAGAffMtx {
11  static const double NInf;
12 private:
15 public:
16  TMAGAffMtx() : MtxDim(-1), SeedMtx() { }
17  TMAGAffMtx(const int& Dim) : MtxDim(Dim), SeedMtx(Dim*Dim) { }
18  TMAGAffMtx(const TFltV& SeedMatrix);
19  TMAGAffMtx(const TMAGAffMtx& Kronecker) : MtxDim(Kronecker.MtxDim), SeedMtx(Kronecker.SeedMtx) { }
20  TMAGAffMtx& operator = (const TMAGAffMtx& Kronecker);
21  bool operator == (const TMAGAffMtx& Kronecker) const { return SeedMtx==Kronecker.SeedMtx; }
22  int GetPrimHashCd() const { return SeedMtx.GetPrimHashCd(); }
23  int GetSecHashCd() const { return SeedMtx.GetSecHashCd(); }
24 
25  // seed matrix
26  int GetDim() const { return MtxDim; }
27  int Len() const { return SeedMtx.Len(); }
28  bool Empty() const { return SeedMtx.Empty(); }
29  bool IsProbMtx() const; // probability (not log-lihelihood) matrix
30 
31  TFltV& GetMtx() { return SeedMtx; }
32  const TFltV& GetMtx() const { return SeedMtx; }
33  void SetMtx(const TFltV& ParamV) { SeedMtx = ParamV; }
34  void SetRndMtx(TRnd& Rnd, const int& PrmMtxDim = 2, const double& MinProb = 0.0);
35  void PutAllMtx(const double& Val) { SeedMtx.PutAll(Val); }
36  void GenMtx(const int& Dim) { MtxDim=Dim; SeedMtx.Gen(Dim*Dim); }
37  void SetEpsMtx(const double& Eps1, const double& Eps0, const int& Eps1Val=1, const int& Eps0Val=0);
38  void AddRndNoise(TRnd& Rnd, const double& SDev);
39  TStr GetMtxStr() const;
40 
41  const double& At(const int& Row, const int& Col) const { return SeedMtx[MtxDim*Row+Col].Val; }
42  double& At(const int& Row, const int& Col) { return SeedMtx[MtxDim*Row+Col].Val; }
43  const double& At(const int& ValN) const { return SeedMtx[ValN].Val; }
44  double& At(const int& ValN) { return SeedMtx[ValN].Val; }
45 
46  double GetMtxSum() const;
47  double GetRowSum(const int& RowId) const;
48  double GetColSum(const int& ColId) const;
49  double Normalize();
50 
51  void GetLLMtx(TMAGAffMtx& LLMtx);
52  void GetProbMtx(TMAGAffMtx& ProbMtx);
53  void Swap(TMAGAffMtx& Mtx);
54 
55  void Dump(const TStr& MtxNm = TStr(), const bool& Sort = false) const;
56  static double GetAvgAbsErr(const TMAGAffMtx& Mtx1, const TMAGAffMtx& Mtx2); // avg L1 on (sorted) parameters
57  static double GetAvgFroErr(const TMAGAffMtx& Mtx1, const TMAGAffMtx& Mtx2); // avg L2 on (sorted) parameters
58  static TMAGAffMtx GetMtx(TStr MatlabMtxStr);
59  static TMAGAffMtx GetRndMtx(TRnd& Rnd, const int& Dim = 2, const double& MinProb = 0.0);
60 };
61 
63 
65 // Simple MAG node attributes (Same Bernoulli for every attribute)
66 
68 public:
69  static TRnd Rnd;
70 private:
73 public:
74  TMAGNodeSimple() : Mu(0.5), Dim(-1) {}
75  TMAGNodeSimple(const int& _Dim, const double& _Mu = 0.5) : Dim(_Dim) { IAssert(_Mu >= 0.0 && _Mu <= 1.0); Mu = _Mu; }
76  TMAGNodeSimple(const TMAGNodeSimple& Simple) : Mu(Simple.Mu), Dim(Simple.Dim) {}
77 
78  TMAGNodeSimple& operator=(const TMAGNodeSimple& Simple) { Mu = Simple.Mu; Dim = Simple.Dim; return (*this); }
79 
80  void LoadTxt(const TStr& InFNm);
81  void SaveTxt(TStrV& OutStrV) const;
82  void AttrGen(TIntVV& AttrVV, const int& NNodes);
83 };
84 
86 // MAG node attributes with (different) Bernoulli
87 
88 class TMAGNodeBern {
89 public:
90  static TRnd Rnd;
91 private:
94 public:
95  TMAGNodeBern() : MuV(), Dim(-1) {}
96  TMAGNodeBern(const int& _Dim, const double& _Mu = 0.5) : MuV(_Dim), Dim(_Dim) { IAssert(_Mu >= 0.0 && _Mu <= 1.0); MuV.PutAll(_Mu); }
97  TMAGNodeBern(const TFltV& _MuV) : MuV(_MuV), Dim(_MuV.Len()) {}
98  TMAGNodeBern(const TMAGNodeBern& Dist) : MuV(Dist.MuV), Dim(Dist.Dim) {}
99 
100  TMAGNodeBern& operator=(const TMAGNodeBern& Dist);
101 
102  void SetMuV(const TFltV& _MuV) { MuV = _MuV; }
103  const TFltV& GetMuV() const { return MuV; }
104  void SetMu(const int& Attr, const double& Prob) { IAssert(Prob >= 0.0 && Prob <= 1.0); MuV[Attr] = Prob; }
105  double GetMu(const int& Attr) const { return MuV[Attr]; }
106 
107  void LoadTxt(const TStr& InFNm);
108  void SaveTxt(TStrV& OutStrV) const;
109  void AttrGen(TIntVV& AttrVV, const int& NNodes);
110 };
111 
113 // MAG node attributes with Beta + Bernoulli family
114 
116 public:
117  static TRnd Rnd;
118 private:
124 public:
125  TMAGNodeBeta() : AlphaV(), BetaV(), MuV(), Dim(-1), Dirty(true) {}
126  TMAGNodeBeta(const int& _Dim, const double& Alpha = 1, const double& Beta = 1) : AlphaV(_Dim), BetaV(_Dim), MuV(_Dim), Dim(_Dim), Dirty(true) { AlphaV.PutAll(Alpha); BetaV.PutAll(Beta); }
127  TMAGNodeBeta(const TFltV& _AlphaV, const TFltV& _BetaV) : AlphaV(_AlphaV), BetaV(_BetaV), Dirty(true) { IAssert(_AlphaV.Len() == _BetaV.Len()); Dim = _AlphaV.Len(); MuV.Gen(Dim); }
128 
129  TMAGNodeBeta& operator=(const TMAGNodeBeta& Dist);
130 
131  void SetBeta(const int& Attr, const double& Alpha, const double& Beta);
132  void GetBeta(const int& Attr, double& Alpha, double& Beta) const { IAssert(Attr < Dim); Alpha = AlphaV[Attr]; Beta = BetaV[Attr]; }
133  void SetBetaV(const TFltV& _AlphaV, const TFltV& _BetaV);
134  void GetBetaV(TFltV& _AlphaV, TFltV& _BetaV) const { _AlphaV = AlphaV; _BetaV = BetaV; }
135  bool GetMuV(TFltV& _MuV) const { if(!Dirty) { _MuV = MuV; } return Dirty; }
136 
137  void LoadTxt(const TStr& InFNm);
138  void SaveTxt(TStrV& OutStrV) const;
139  void AttrGen(TIntVV& AttrVV, const int& NNodes);
140 };
141 
143 // MAG model param. (node attribtes + affinity matrices)
144 
145 template <class TNodeAttr>
146 class TMAGParam {
147 public:
148  static TRnd Rnd;
149 private:
152  TNodeAttr NodeAttr;
153  TMAGAffMtxV AffMtxV;
154 
155 public:
156  TMAGParam() : NNodes(-1), NAttrs(-1), NodeAttr(), AffMtxV() {}
157  TMAGParam(const int& NumNodes, const int& NumAttrs) : NNodes(NumNodes), NAttrs(NumAttrs), NodeAttr(), AffMtxV(NumAttrs) {}
158  TMAGParam(const int& NumNodes, const TMAGAffMtxV& MtxV) : NNodes(NumNodes), NAttrs(MtxV.Len()), NodeAttr(), AffMtxV(MtxV) {}
159  TMAGParam(const int& NumNodes, const TStr& ConfFNm) : NNodes(NumNodes) { LoadTxt(ConfFNm); }
160  TMAGParam(const TMAGParam<TNodeAttr>& MAGParam) : NNodes(MAGParam.NNodes), NAttrs(MAGParam.NAttrs), NodeAttr(MAGParam.NodeAttr), AffMtxV(MAGParam.AffMtxV) {}
161  TMAGParam(const int& NumNodes, const int& NumAttrs, const TNodeAttr& Dist) : NNodes(NumNodes), NAttrs(NumAttrs), NodeAttr(Dist), AffMtxV(NumAttrs) {}
162 
164 
165  void Clr() { AffMtxV.Clr(); }
166 
167  void LoadTxt(const TStr& InFNm);
168  void SaveTxt(const TStr& OutFNm) const;
169 
170  void SetNodes(const int& Nodes) { NNodes = Nodes; }
171  const int GetNodes() const { return NNodes; }
172 
173  void SetAttrs(const int& Attrs);
174  const int GetAttrs() const { return NAttrs; }
175 
176  void SetNodeAttr(const TNodeAttr& Dist) { NodeAttr = Dist; }
177  const TNodeAttr& GetNodeAttr() const { return NodeAttr; }
178 
179  const int GetDim(const int& Attr) const { return AffMtxV[Attr].GetDim(); }
180  void GetDimV(TIntV& DimV) const;
181 
182  void SetMtxVal(const int& Attr, const int& Row, const int& Col, const double& Val) { AffMtxV[Attr].At(Row, Col) = Val; }
183  void SetMtx(const int& Attr, const TMAGAffMtx& Mtx) { AffMtxV[Attr] = Mtx; }
184  void SetMtxV(const TMAGAffMtxV& MtxV) { NAttrs = AffMtxV.Len(); AffMtxV = MtxV; }
185  void SetMtxAll(const TMAGAffMtx& Mtx) { AffMtxV.PutAll(Mtx); }
186  const double GetMtxVal(const int& Attr, const int& Row, const int& Col) const { return AffMtxV[Attr].At(Row, Col); }
187  const TMAGAffMtx& GetMtx(const int& Attr) const { return AffMtxV[Attr]; }
188  void GetMtxV(TMAGAffMtxV& MtxV) const { MtxV = AffMtxV; }
189 
190  PNGraph GenMAG(TIntVV& AttrVV, const bool& IsDir = false, const int& Seed = 1);
191  PNGraph GenAttrMAG(const TIntVV& AttrVV, const bool& IsDir = false, const int& Seed = 1);
192 };
193 
194 template <class TNodeAttr>
196  NNodes = MAGParam.NNodes;
197  NAttrs = MAGParam.NAttrs;
198  AffMtxV = MAGParam.AffMtxV;
199  NodeAttr = MAGParam.NodeAttr;
200 
201  return (*this);
202 };
203 
204 template <class TNodeAttr>
206  NodeAttr.LoadTxt(InFNm);
207 
208  char buf[1024];
209  FILE *fp = fopen(InFNm.CStr(), "r");
210  NAttrs = 0;
211  AffMtxV.Gen(10, 0);
212  while(fgets(buf, sizeof(buf), fp) != NULL) {
213  strtok(buf, "&");
214  char *token = strtok(NULL, "\r\n");
215  NAttrs++;
216  AffMtxV.Add(TMAGAffMtx::GetMtx(TStr(token)));
217  }
218  fclose(fp);
219 };
220 
221 template <class TNodeAttr>
222 void TMAGParam<TNodeAttr>::SaveTxt(const TStr& OutFNm) const {
223  TStrV OutStrV;
224  NodeAttr.SaveTxt(OutStrV);
225 
226  FILE *fp = fopen(OutFNm.CStr(), "w");
227  fprintf(fp, "# %d nodes with %d attributes\n", NNodes(), NAttrs());
228  for(int i = 0; i < NAttrs; i++) {
229  TStr MtxStr = AffMtxV[i].GetMtxStr();
230  fprintf(fp, "%s & %s\n", OutStrV[i].CStr(), MtxStr.GetSubStr(1, MtxStr.Len()-2).CStr());
231  }
232  fclose(fp);
233 };
234 
235 template <class TNodeAttr>
236 void TMAGParam<TNodeAttr>::SetAttrs(const int& Attrs) {
237  NAttrs = Attrs;
238  AffMtxV.Gen(Attrs);
239 };
240 
241 template <class TNodeAttr>
243  DimV.Gen(NAttrs);
244  for(int i = 0; i < NAttrs; i++) {
245  DimV[i] = AffMtxV[i].GetDim();
246  }
247 };
248 
249 template <class TNodeAttr>
250 PNGraph TMAGParam<TNodeAttr>::GenAttrMAG(const TIntVV& AttrVV, const bool& IsDir, const int& Seed) {
251  PNGraph Graph = TNGraph::New(NNodes, -1);
252  for(int i = 0; i < NNodes; i++) {
253  Graph->AddNode(i);
254  }
255 
256  if(Seed > 0) {
257  TNodeAttr::Rnd.PutSeed(Seed);
258  }
259 
260  IAssert(AttrVV.GetYDim() == NAttrs);
261  for(int i = 0; i < NNodes; i++) {
262  for(int l = 0; l < NAttrs; l++) {
263  IAssert(AttrVV.At(i, l) < AffMtxV[l].GetDim());
264  }
265  }
266 
267  for(int i = 0; i < NNodes; i++) {
268  for(int j = i; j < NNodes; j++) {
269  double Prob = 1.0;
270  for(int l = 0; l < NAttrs; l++) {
271  Prob *= AffMtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l));
272  }
273 
274  if(TNodeAttr::Rnd.GetUniDev() < Prob) {
275  Graph->AddEdge(i, j);
276  if(! IsDir && i != j) { Graph->AddEdge(j, i); }
277  }
278 
279  if(IsDir && i != j) {
280  Prob = 1.0;
281  for(int l = 0; l < NAttrs; l++) {
282  Prob *= AffMtxV[l].At(AttrVV.At(j, l), AttrVV.At(i, l));
283  }
284  if(TNodeAttr::Rnd.GetUniDev() < Prob) {
285  Graph->AddEdge(j, i);
286  }
287  }
288  }
289  }
290 
291  return Graph;
292 };
293 
294 template <class TNodeAttr>
295 PNGraph TMAGParam<TNodeAttr>::GenMAG(TIntVV& AttrVV, const bool& IsDir, const int& Seed) {
296  TNodeAttr::Rnd.PutSeed(Seed);
297  NodeAttr.AttrGen(AttrVV, NNodes);
298 
299  IAssert(AttrVV.GetYDim() == NAttrs);
300  for(int i = 0; i < NNodes; i++) {
301  for(int l = 0; l < NAttrs; l++) {
302  IAssert(AttrVV.At(i, l) < AffMtxV[l].GetDim());
303  }
304  }
305 
306  PNGraph Graph = GenAttrMAG(AttrVV, IsDir, 0);
307  return Graph;
308 
309 /*
310  for(int i = 0; i < NNodes; i++) {
311  for(int j = i; j < NNodes; j++) {
312  double Prob = 1.0;
313  for(int l = 0; l < NAttrs; l++) {
314  Prob *= AffMtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l));
315  }
316  if(TNodeAttr::Rnd.GetUniDev() < Prob) {
317  Graph->AddEdge(i, j);
318  if(! IsDir && i != j) { Graph->AddEdge(j, i); }
319  }
320 
321  if(IsDir && i != j) {
322  Prob = 1.0;
323  for(int l = 0; l < NAttrs; l++) {
324  Prob *= AffMtxV[l].At(AttrVV.At(j, l), AttrVV.At(i, l));
325  }
326  if(TNodeAttr::Rnd.GetUniDev() < Prob) {
327  Graph->AddEdge(j, i);
328  }
329  }
330  }
331  }
332 
333  return Graph;
334 */
335 };
336 
337 
339 // MAGFit with Bernoulli node attributes
340 
341 class TMAGFitBern {
342 private:
351 
355 
356 public:
357  TMAGFitBern() : PhiVV(), KnownVV(), Graph(), Param(), ESpeedUp(true), MSpeedUp(true), Debug(false), AvgPhiV(), AvgPhiPairVV(), NormConst(1.0) { }
358  TMAGFitBern(const PNGraph& G, const int& NAttrs) : PhiVV(G->GetNodes(), NAttrs), KnownVV(G->GetNodes(), NAttrs), Graph(G), Param(G->GetNodes(), NAttrs), ESpeedUp(true), MSpeedUp(true), Debug(false), AvgPhiV(NAttrs), AvgPhiPairVV(NAttrs, NAttrs), NormConst(1.0) { }
359 
360  TMAGFitBern(const PNGraph& G, const TStr& InitFNm) : Param(G->GetNodes(), InitFNm), ESpeedUp(true), MSpeedUp(true), Debug(false), NormConst(1.0) {
361  const int NNodes = G->GetNodes();
362  const int NAttrs = Param.GetAttrs();
363 
364  printf("NAttrs = %d\n", NAttrs);
365  SetGraph(G);
366  PhiVV.Gen(NNodes, NAttrs);
367  KnownVV.Gen(NNodes, NAttrs);
368  AvgPhiV.Gen(NAttrs);
369  AvgPhiPairVV.Gen(NAttrs, NAttrs);
370  }
371 
372  void Clr() { PhiVV.Clr(); KnownVV.Clr(); }
373  void SaveTxt(const TStr& FNm);
374 
375  const int GetNodes() const { return Param.GetNodes(); }
376  const int GetAttrs() const { return Param.GetAttrs(); }
377  const TMAGParam<TMAGNodeBern>& GetParams() const { return Param; }
378  const TMAGNodeBern& GetNodeAttr() const { return Param.GetNodeAttr(); }
379  const TFltV& GetMuV() const { const TMAGNodeBern& Dist = Param.GetNodeAttr(); return Dist.GetMuV(); }
380  void SetMuV(const TFltV& MuV) { TMAGNodeBern Dist = Param.GetNodeAttr(); Dist.SetMuV(MuV); Param.SetNodeAttr(Dist); }
381  void GetMtxV(TMAGAffMtxV& MtxV) const { Param.GetMtxV(MtxV); }
382  void SetMtxV(const TMAGAffMtxV& MtxV) { Param.SetMtxV(MtxV); }
383  PNGraph GetGraph() { return Graph; }
384  void SetGraph(const PNGraph& GraphPt);
385 
386  void SetDebug(const bool _Debug) { Debug = _Debug; }
387  void SetAlgConf(const bool EStep = true, const bool MStep = true) { ESpeedUp = EStep; MSpeedUp = MStep; }
388 
389  void Init(const TFltV& MuV, const TMAGAffMtxV& AffMtxV);
390 // void PerturbInit(const TFltV& MuV, const TMAGAffMtxV& AffMtxV, const double& PerturbRate);
391  void RandomInit(const TFltV& MuV, const TMAGAffMtxV& AffMtxV, const int& Seed);
392  const TFltVV& GetPhiVV() const { return PhiVV; }
393  void SetPhiVV(const TIntVV& AttrVV, const int KnownIds = 0);
394  void SetPhiVV(const TFltVV& AttrVV, const int KnownIds = 0) { PhiVV = AttrVV; KnownVV.Gen(PhiVV.GetXDim(), PhiVV.GetYDim()); KnownVV.PutAll(false); for(int i = 0; i < KnownIds; i++) { KnownVV.PutY(i, true); } }
395 
396  const double GetInCoeff(const int& i, const int& j, const int& l, const int& A, const TMAGAffMtx& Theta) const;
397  const double GetAvgInCoeff(const int& i, const int& AId, const int& A, const TMAGAffMtx& Theta) const;
398  const double GetOutCoeff(const int& i, const int& j, const int& l, const int& A, const TMAGAffMtx& Theta) const;
399  const double GetAvgOutCoeff(const int& i, const int& AId, const int& A, const TMAGAffMtx& Theta) const;
400  const double GetProbPhi(const int& NId1, const int& NId2, const int& AId, const int& Attr1, const int& Attr2) const;
401  const double GetProbMu(const int& NId1, const int& NId2, const int& AId, const int& Attr1, const int& Attr2, const bool Left = false, const bool Right = false) const;
402  const double GetThetaLL(const int& NId1, const int& NId2, const int& AId) const;
403  const double GetAvgThetaLL(const int& NId1, const int& NId2, const int& AId, const bool Left = false, const bool Right = false) const;
404  const double GetSqThetaLL(const int& NId1, const int& NId2, const int& AId) const;
405  const double GetAvgSqThetaLL(const int& NId1, const int& NId2, const int& AId, const bool Left = false, const bool Right = false) const;
406  const double GetProdLinWeight(const int& NId1, const int& NId2) const;
407  const double GetAvgProdLinWeight(const int& NId1, const int& NId2, const bool Left = false, const bool Right = false) const;
408  const double GetProdSqWeight(const int& NId1, const int& NId2) const;
409  const double GetAvgProdSqWeight(const int& NId1, const int& NId2, const bool Left = false, const bool Right = false) const;
410 
411  const double GetEstNoEdgeLL(const int& NId, const int& AId) const;
412  const double GradPhiMI(const double& x, const int& NId, const int& AId, const double& Lambda, const double& DeltaQ, const TFltVV& CntVV);
413  const double ObjPhiMI(const double& x, const int& NId, const int& AId, const double& Lambda, const double& Q0, const double& Q1, const TFltVV& CntVV);
414  const double UpdatePhiMI(const double& Lambda, const int& NId, const int& AId, double& Phi);
415  const double UpdateApxPhiMI(const double& Lambda, const int& NId, const int& AId, double& Phi, TFltVV& ProdVV);
416  const double UpdatePhi(const int& NId, const int& AId, double& Phi);
417  const double UpdateMu(const int& AId);
418  const void PrepareUpdateAffMtx(TFltVV& ProdVV, TFltVV& SqVV);
419  const void PrepareUpdateApxAffMtx(TFltVV& ProdVV, TFltVV& SqVV);
420 
421  const double UpdateAffMtx(const int& AId, const double& LrnRate, const double& MaxGrad, const double& Lambda, TFltVV& ProdVV, TFltVV& SqVV, TMAGAffMtx& NewMtx);
422  const double UpdateAffMtxV(const int& GradIter, const double& LrnRate, const double& MaxGrad, const double& Lambda, const int& NReal = 0);
423  const void GradAffMtx(const int& AId, const TFltVV& ProdVV, const TFltVV& SqVV, const TMAGAffMtx& CurMtx, TFltV& GradV);
424  const void GradApxAffMtx(const int& AId, const TFltVV& ProdVV, const TFltVV& SqVV, const TMAGAffMtx& CurMtx, TFltV& GradV);
425 
426  /* EM algorithm */
427  double DoEStepOneIter(const TFltV& TrueMuV, TFltVV& NewPhi, const double& Lambda);
428  double DoEStepApxOneIter(const TFltV& TrueMuV, TFltVV& NewPhi, const double& Lambda);
429  double DoEStep(const TFltV& TrueMuV, const int& NIter, double& LL, const double& Lambda);
430  void DoMStep(const int& GradIter, const double& LrnRate, const double& MaxGrad, const double& Lambda, const int& NReal = 0);
431  void DoEMAlg(const int& NStep, const int& NEstep, const int& NMstep, const double& LrnRate, const double& MaxGrad, const double& Lambda, const double& ReInit, const int& NReal = 0);
432 
433  void CountAttr(TFltV& EstMuV) const;
434  void SortAttrOrdering(const TFltV& TrueMuV, TIntV& IndexV) const;
435 
436  const double ComputeJointOneLL(const TIntVV& AttrVV) const;
437  const double ComputeJointLL(int NSample) const;
438  const double ComputeJointAdjLL(const TIntVV& AttrVV) const;
439  const double ComputeApxLL() const;
440  const double ComputeApxAdjLL() const;
441 
442  void MakeCCDF(const TFltPrV& RawV, TFltPrV& CcdfV);
443  void PlotProperties(const TStr& FNm);
444 
445  static const double ComputeMI(const TIntVV& AttrV, const int AId1, const int AId2);
446  static const double ComputeMI(const TFltVV& AttrV, const int AId1, const int AId2);
447  static const double ComputeMI(const TIntVV& AttrV);
448  static const double ComputeMI(const TFltVV& AttrV);
449  void NormalizeAffMtxV(TMAGAffMtxV& MtxV, const bool UseMu = false);
450  void UnNormalizeAffMtxV(TMAGAffMtxV& MtxV, const bool UseMu = false);
451 private:
452  const bool NextPermutation(TIntV& IndexV) const;
453 };
454 
455 const double LogSumExp(const double LogVal1, const double LogVal2);
456 const double LogSumExp(const TFltV& LogValV);
457 const double LogSumExp(const double *LogValArray, const int Len);
458 
459 #endif // snap_mag_h
const double ComputeJointAdjLL(const TIntVV &AttrVV) const
Definition: mag.cpp:1942
#define IAssert(Cond)
Definition: bd.h:262
const int GetDim(const int &Attr) const
Definition: mag.h:179
void SetNodeAttr(const TNodeAttr &Dist)
Definition: mag.h:176
TMAGNodeBern & operator=(const TMAGNodeBern &Dist)
Definition: mag.cpp:244
const double GetAvgSqThetaLL(const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:620
void LoadTxt(const TStr &InFNm)
Definition: mag.h:205
void DoEMAlg(const int &NStep, const int &NEstep, const int &NMstep, const double &LrnRate, const double &MaxGrad, const double &Lambda, const double &ReInit, const int &NReal=0)
Definition: mag.cpp:1630
TFlt Mu
Definition: mag.h:71
void GetBeta(const int &Attr, double &Alpha, double &Beta) const
Definition: mag.h:132
TMAGNodeSimple(const TMAGNodeSimple &Simple)
Definition: mag.h:76
const bool NextPermutation(TIntV &IndexV) const
Definition: mag.cpp:1867
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
Definition: ds.h:948
int Len() const
Definition: dt.h:487
bool Debug
Definition: mag.h:347
const int GetNodes() const
Definition: mag.h:375
TVec< TFltV > MuHisV
Definition: mag.h:352
TFltV LLHisV
Definition: mag.h:354
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TMAGNodeBern(const int &_Dim, const double &_Mu=0.5)
Definition: mag.h:96
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:381
Definition: dt.h:11
const double GetAvgInCoeff(const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:558
TMAGAffMtx & operator=(const TMAGAffMtx &Kronecker)
Definition: mag.cpp:19
const double GetEstNoEdgeLL(const int &NId, const int &AId) const
Definition: mag.cpp:746
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
Definition: graph.h:425
void Swap(TMAGAffMtx &Mtx)
Definition: mag.cpp:90
PNGraph GenAttrMAG(const TIntVV &AttrVV, const bool &IsDir=false, const int &Seed=1)
Definition: mag.h:250
TFltV BetaV
Definition: mag.h:120
void SetDebug(const bool _Debug)
Definition: mag.h:386
void SetMuV(const TFltV &MuV)
Definition: mag.h:380
TMAGFitBern(const PNGraph &G, const TStr &InitFNm)
Definition: mag.h:360
TInt Dim
Definition: mag.h:122
TFltV SeedMtx
Definition: mag.h:14
void Dump(const TStr &MtxNm=TStr(), const bool &Sort=false) const
Definition: mag.cpp:128
TFltV MuV
Definition: mag.h:121
static double GetAvgAbsErr(const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
Definition: mag.cpp:147
void NormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1454
void SetAttrs(const int &Attrs)
Definition: mag.h:236
TMAGAffMtx(const int &Dim)
Definition: mag.h:17
TMAGAffMtxV AffMtxV
Definition: mag.h:153
int GetSecHashCd() const
Definition: mag.h:23
const double GetOutCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:554
TMAGNodeBern()
Definition: mag.h:95
TInt Dim
Definition: mag.h:93
TMAGParam(const int &NumNodes, const int &NumAttrs, const TNodeAttr &Dist)
Definition: mag.h:161
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:547
void MakeCCDF(const TFltPrV &RawV, TFltPrV &CcdfV)
Definition: mag.cpp:1703
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
Definition: ds.h:960
bool GetMuV(TFltV &_MuV) const
Definition: mag.h:135
static const double NInf
Definition: mag.h:11
void CountAttr(TFltV &EstMuV) const
Definition: mag.cpp:1814
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:438
TStr GetSubStr(const int &BChN, const int &EChN) const
Definition: dt.cpp:811
void SetNodes(const int &Nodes)
Definition: mag.h:170
void SaveTxt(TStrV &OutStrV) const
Definition: mag.cpp:232
double GetRowSum(const int &RowId) const
Definition: mag.cpp:102
int GetXDim() const
Definition: ds.h:2184
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
Definition: graph.cpp:236
TMAGAffMtx(const TMAGAffMtx &Kronecker)
Definition: mag.h:19
TMAGParam()
Definition: mag.h:156
TMAGNodeBeta & operator=(const TMAGNodeBeta &Dist)
Definition: mag.cpp:299
const double UpdatePhiMI(const double &Lambda, const int &NId, const int &AId, double &Phi)
Definition: mag.cpp:826
TMAGNodeBeta(const TFltV &_AlphaV, const TFltV &_BetaV)
Definition: mag.h:127
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
double Normalize()
Definition: mag.cpp:116
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
TVec< TMAGAffMtxV > MtxHisV
Definition: mag.h:353
void LoadTxt(const TStr &InFNm)
Definition: mag.cpp:343
double & At(const int &ValN)
Definition: mag.h:44
const void GradApxAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
Definition: mag.cpp:1346
static TRnd Rnd
Definition: mag.h:117
Definition: dt.h:1293
TBool Dirty
Definition: mag.h:123
TMAGParam(const int &NumNodes, const int &NumAttrs)
Definition: mag.h:157
bool Empty() const
Tests whether the vector is empty.
Definition: ds.h:542
void PutAllMtx(const double &Val)
Definition: mag.h:35
void UnNormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1492
void AttrGen(TIntVV &AttrVV, const int &NNodes)
Definition: mag.cpp:201
void SetPhiVV(const TIntVV &AttrVV, const int KnownIds=0)
Definition: mag.cpp:399
void RandomInit(const TFltV &MuV, const TMAGAffMtxV &AffMtxV, const int &Seed)
Definition: mag.cpp:510
static double GetAvgFroErr(const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
Definition: mag.cpp:160
void Clr()
Definition: ds.h:2180
PNGraph GetGraph()
Definition: mag.h:383
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
const TFltV & GetMtx() const
Definition: mag.h:32
void LoadTxt(const TStr &InFNm)
Definition: mag.cpp:215
const TMAGNodeBern & GetNodeAttr() const
Definition: mag.h:378
void SetGraph(const PNGraph &GraphPt)
Definition: mag.cpp:385
bool IsProbMtx() const
Definition: mag.cpp:27
const double GetAvgProdSqWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:664
const double GetInCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:550
const void GradAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
Definition: mag.cpp:1320
bool ESpeedUp
Definition: mag.h:347
TMAGParam(const int &NumNodes, const TStr &ConfFNm)
Definition: mag.h:159
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:971
void PlotProperties(const TStr &FNm)
Definition: mag.cpp:1727
const TFltV & GetMuV() const
Definition: mag.h:379
void SetAlgConf(const bool EStep=true, const bool MStep=true)
Definition: mag.h:387
int Len() const
Definition: mag.h:27
void GetProbMtx(TMAGAffMtx &ProbMtx)
Definition: mag.cpp:82
const double GetAvgThetaLL(const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:598
TVec< TMAGAffMtx > TMAGAffMtxV
Definition: mag.h:62
const double UpdateAffMtx(const int &AId, const double &LrnRate, const double &MaxGrad, const double &Lambda, TFltVV &ProdVV, TFltVV &SqVV, TMAGAffMtx &NewMtx)
Definition: mag.cpp:1418
TMAGParam(const TMAGParam< TNodeAttr > &MAGParam)
Definition: mag.h:160
Definition: ds.h:2157
TInt MtxDim
Definition: mag.h:13
TBoolVV KnownVV
Definition: mag.h:344
const void PrepareUpdateApxAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
Definition: mag.cpp:1545
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node IDs SrcNId to node DstNId to the graph.
Definition: graph.cpp:321
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
const double UpdateMu(const int &AId)
Definition: mag.cpp:1299
TFltV MuV
Definition: mag.h:92
const double ComputeJointOneLL(const TIntVV &AttrVV) const
Definition: mag.cpp:1900
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1166
TMAGNodeSimple & operator=(const TMAGNodeSimple &Simple)
Definition: mag.h:78
const double & At(const int &ValN) const
Definition: mag.h:43
void SetBetaV(const TFltV &_AlphaV, const TFltV &_BetaV)
Definition: mag.cpp:315
const TFltV & GetMuV() const
Definition: mag.h:103
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
void SetMtx(const TFltV &ParamV)
Definition: mag.h:33
void GetDimV(TIntV &DimV) const
Definition: mag.h:242
TNodeAttr NodeAttr
Definition: mag.h:152
const double UpdatePhi(const int &NId, const int &AId, double &Phi)
Definition: mag.cpp:756
TMAGNodeSimple()
Definition: mag.h:74
TMAGNodeSimple(const int &_Dim, const double &_Mu=0.5)
Definition: mag.h:75
const double GetMtxVal(const int &Attr, const int &Row, const int &Col) const
Definition: mag.h:186
double GetMu(const int &Attr) const
Definition: mag.h:105
double GetColSum(const int &ColId) const
Definition: mag.cpp:109
void Clr()
Definition: mag.h:372
double DoEStepOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
Definition: mag.cpp:1056
void Clr()
Definition: mag.h:165
TMAGAffMtx()
Definition: mag.h:16
const TMAGParam< TMAGNodeBern > & GetParams() const
Definition: mag.h:377
void AttrGen(TIntVV &AttrVV, const int &NNodes)
Definition: mag.cpp:250
void SetBeta(const int &Attr, const double &Alpha, const double &Beta)
Definition: mag.cpp:308
TFlt NormConst
Definition: mag.h:350
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
double DoEStepApxOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
Definition: mag.cpp:1134
void Gen(const int &_XDim, const int &_YDim)
Definition: ds.h:2181
void SetMuV(const TFltV &_MuV)
Definition: mag.h:102
Definition: mag.h:10
void AttrGen(TIntVV &AttrVV, const int &NNodes)
Definition: mag.cpp:323
static TRnd Rnd
Definition: mag.h:148
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184
static TMAGAffMtx GetRndMtx(TRnd &Rnd, const int &Dim=2, const double &MinProb=0.0)
Definition: mag.cpp:191
TFltV AlphaV
Definition: mag.h:119
const double ComputeJointLL(int NSample) const
Definition: mag.cpp:1976
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
const double LogSumExp(const double LogVal1, const double LogVal2)
Definition: mag.cpp:675
Definition: dt.h:1044
const double GetAvgProdLinWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:642
void SetMtxAll(const TMAGAffMtx &Mtx)
Definition: mag.h:185
void SortAttrOrdering(const TFltV &TrueMuV, TIntV &IndexV) const
Definition: mag.cpp:1828
TMAGParam< TNodeAttr > & operator=(const TMAGParam< TNodeAttr > &MAGParam)
Definition: mag.h:195
TStr GetMtxStr() const
Definition: mag.cpp:63
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:382
TFltV AvgPhiV
Definition: mag.h:348
PNGraph GenMAG(TIntVV &AttrVV, const bool &IsDir=false, const int &Seed=1)
Definition: mag.h:295
const double ComputeApxAdjLL() const
Definition: mag.cpp:2046
TMAGNodeBeta(const int &_Dim, const double &Alpha=1, const double &Beta=1)
Definition: mag.h:126
void SaveTxt(const TStr &OutFNm) const
Definition: mag.h:222
void SetMtxVal(const int &Attr, const int &Row, const int &Col, const double &Val)
Definition: mag.h:182
void SaveTxt(TStrV &OutStrV) const
Definition: mag.cpp:287
void SetPhiVV(const TFltVV &AttrVV, const int KnownIds=0)
Definition: mag.h:394
TMAGParam(const int &NumNodes, const TMAGAffMtxV &MtxV)
Definition: mag.h:158
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
TMAGFitBern(const PNGraph &G, const int &NAttrs)
Definition: mag.h:358
static TRnd Rnd
Definition: mag.h:69
Definition: dt.h:412
const void PrepareUpdateAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
Definition: mag.cpp:1532
TInt NAttrs
Definition: mag.h:151
TMAGNodeBeta()
Definition: mag.h:125
double DoEStep(const TFltV &TrueMuV, const int &NIter, double &LL, const double &Lambda)
Definition: mag.cpp:1272
const double GetAvgOutCoeff(const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:564
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const TFltVV & GetPhiVV() const
Definition: mag.h:392
TInt Dim
Definition: mag.h:72
static TRnd Rnd
Definition: mag.h:90
void SetMu(const int &Attr, const double &Prob)
Definition: mag.h:104
void SetEpsMtx(const double &Eps1, const double &Eps0, const int &Eps1Val=1, const int &Eps0Val=0)
Definition: mag.cpp:44
TMAGFitBern()
Definition: mag.h:357
double GetMtxSum() const
Definition: mag.cpp:95
void SetRndMtx(TRnd &Rnd, const int &PrmMtxDim=2, const double &MinProb=0.0)
Definition: mag.cpp:34
int GetYDim() const
Definition: ds.h:2185
void SaveTxt(const TStr &FNm)
Definition: mag.cpp:423
const int GetNodes() const
Definition: mag.h:171
const double UpdateApxPhiMI(const double &Lambda, const int &NId, const int &AId, double &Phi, TFltVV &ProdVV)
Definition: mag.cpp:930
bool Empty() const
Definition: mag.h:28
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:495
void DoMStep(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
Definition: mag.cpp:1608
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
const int GetAttrs() const
Definition: mag.h:376
int GetPrimHashCd() const
Definition: mag.h:22
double & At(const int &Row, const int &Col)
Definition: mag.h:42
TFltV & GetMtx()
Definition: mag.h:31
void GetBetaV(TFltV &_AlphaV, TFltV &_BetaV) const
Definition: mag.h:134
TInt NNodes
Definition: mag.h:150
void SaveTxt(TStrV &OutStrV) const
Definition: mag.cpp:373
char * CStr()
Definition: dt.h:476
TMAGNodeBern(const TMAGNodeBern &Dist)
Definition: mag.h:98
Definition: mag.h:146
static const double ComputeMI(const TIntVV &AttrV, const int AId1, const int AId2)
Definition: mag.cpp:2091
Definition: dt.h:881
void AddRndNoise(TRnd &Rnd, const double &SDev)
Definition: mag.cpp:52
PNGraph Graph
Definition: mag.h:345
const double UpdateAffMtxV(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
Definition: mag.cpp:1558
bool MSpeedUp
Definition: mag.h:347
void SetMtx(const int &Attr, const TMAGAffMtx &Mtx)
Definition: mag.h:183
bool operator==(const TMAGAffMtx &Kronecker) const
Definition: mag.h:21
void Init(const TFltV &MuV, const TMAGAffMtxV &AffMtxV)
Definition: mag.cpp:452
const TVal & At(const int &X, const int &Y) const
Definition: ds.h:2190
int GetDim() const
Definition: mag.h:26
void PutY(const int &Y, const TVal &Val)
Definition: ds.h:2205
void PutAll(const TVal &Val)
Definition: ds.h:2202
TFltVV AvgPhiPairVV
Definition: mag.h:349
const double ComputeApxLL() const
Definition: mag.cpp:2005
const double ObjPhiMI(const double &x, const int &NId, const int &AId, const double &Lambda, const double &Q0, const double &Q1, const TFltVV &CntVV)
Definition: mag.cpp:722
void LoadTxt(const TStr &InFNm)
Definition: mag.cpp:264
TMAGNodeBern(const TFltV &_MuV)
Definition: mag.h:97
void GenMtx(const int &Dim)
Definition: mag.h:36
const double GetProbMu(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:576
const double GradPhiMI(const double &x, const int &NId, const int &AId, const double &Lambda, const double &DeltaQ, const TFltVV &CntVV)
Definition: mag.cpp:703