SNAP Library , Developer Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
00001 #ifndef snap_mag_h 00002 #define snap_mag_h 00003 00004 #include "Snap.h" 00005 #include "kronecker.h" 00006 00008 // MAG affinity matrix 00009 00010 class TMAGAffMtx { 00011 static const double NInf; 00012 private: 00013 TInt MtxDim; 00014 TFltV SeedMtx; 00015 public: 00016 TMAGAffMtx() : MtxDim(-1), SeedMtx() { } 00017 TMAGAffMtx(const int& Dim) : MtxDim(Dim), SeedMtx(Dim*Dim) { } 00018 TMAGAffMtx(const TFltV& SeedMatrix); 00019 TMAGAffMtx(const TMAGAffMtx& Kronecker) : MtxDim(Kronecker.MtxDim), SeedMtx(Kronecker.SeedMtx) { } 00020 TMAGAffMtx& operator = (const TMAGAffMtx& Kronecker); 00021 bool operator == (const TMAGAffMtx& Kronecker) const { return SeedMtx==Kronecker.SeedMtx; } 00022 int GetPrimHashCd() const { return SeedMtx.GetPrimHashCd(); } 00023 int GetSecHashCd() const { return SeedMtx.GetSecHashCd(); } 00024 00025 // seed matrix 00026 int GetDim() const { return MtxDim; } 00027 int Len() const { return SeedMtx.Len(); } 00028 bool Empty() const { return SeedMtx.Empty(); } 00029 bool IsProbMtx() const; // probability (not log-lihelihood) matrix 00030 00031 TFltV& GetMtx() { return SeedMtx; } 00032 const TFltV& GetMtx() const { return SeedMtx; } 00033 void SetMtx(const TFltV& ParamV) { SeedMtx = ParamV; } 00034 void SetRndMtx(TRnd& Rnd, const int& PrmMtxDim = 2, const double& MinProb = 0.0); 00035 void PutAllMtx(const double& Val) { SeedMtx.PutAll(Val); } 00036 void GenMtx(const int& Dim) { MtxDim=Dim; SeedMtx.Gen(Dim*Dim); } 00037 void SetEpsMtx(const double& Eps1, const double& Eps0, const int& Eps1Val=1, const int& Eps0Val=0); 00038 void AddRndNoise(TRnd& Rnd, const double& SDev); 00039 TStr GetMtxStr() const; 00040 00041 const double& At(const int& Row, const int& Col) const { return SeedMtx[MtxDim*Row+Col].Val; } 00042 double& At(const int& Row, const int& Col) { return SeedMtx[MtxDim*Row+Col].Val; } 00043 const double& At(const int& ValN) const { return SeedMtx[ValN].Val; } 00044 double& At(const int& ValN) { return SeedMtx[ValN].Val; } 00045 00046 double GetMtxSum() const; 00047 double GetRowSum(const int& RowId) const; 00048 double GetColSum(const int& ColId) const; 00049 double Normalize(); 00050 00051 void GetLLMtx(TMAGAffMtx& LLMtx); 00052 void GetProbMtx(TMAGAffMtx& ProbMtx); 00053 void Swap(TMAGAffMtx& Mtx); 00054 00055 void Dump(const TStr& MtxNm = TStr(), const bool& Sort = false) const; 00056 static double GetAvgAbsErr(const TMAGAffMtx& Mtx1, const TMAGAffMtx& Mtx2); // avg L1 on (sorted) parameters 00057 static double GetAvgFroErr(const TMAGAffMtx& Mtx1, const TMAGAffMtx& Mtx2); // avg L2 on (sorted) parameters 00058 static TMAGAffMtx GetMtx(TStr MatlabMtxStr); 00059 static TMAGAffMtx GetRndMtx(TRnd& Rnd, const int& Dim = 2, const double& MinProb = 0.0); 00060 }; 00061 00062 typedef TVec<TMAGAffMtx> TMAGAffMtxV; 00063 00065 // Simple MAG node attributes (Same Bernoulli for every attribute) 00066 00067 class TMAGNodeSimple { 00068 public: 00069 static TRnd Rnd; 00070 private: 00071 TFlt Mu; 00072 TInt Dim; 00073 public: 00074 TMAGNodeSimple() : Mu(0.5), Dim(-1) {} 00075 TMAGNodeSimple(const int& _Dim, const double& _Mu = 0.5) : Dim(_Dim) { IAssert(_Mu >= 0.0 && _Mu <= 1.0); Mu = _Mu; } 00076 TMAGNodeSimple(const TMAGNodeSimple& Simple) : Mu(Simple.Mu), Dim(Simple.Dim) {} 00077 00078 TMAGNodeSimple& operator=(const TMAGNodeSimple& Simple) { Mu = Simple.Mu; Dim = Simple.Dim; return (*this); } 00079 00080 void LoadTxt(const TStr& InFNm); 00081 void SaveTxt(TStrV& OutStrV) const; 00082 void AttrGen(TIntVV& AttrVV, const int& NNodes); 00083 }; 00084 00086 // MAG node attributes with (different) Bernoulli 00087 00088 class TMAGNodeBern { 00089 public: 00090 static TRnd Rnd; 00091 private: 00092 TFltV MuV; 00093 TInt Dim; 00094 public: 00095 TMAGNodeBern() : MuV(), Dim(-1) {} 00096 TMAGNodeBern(const int& _Dim, const double& _Mu = 0.5) : MuV(_Dim), Dim(_Dim) { IAssert(_Mu >= 0.0 && _Mu <= 1.0); MuV.PutAll(_Mu); } 00097 TMAGNodeBern(const TFltV& _MuV) : MuV(_MuV), Dim(_MuV.Len()) {} 00098 TMAGNodeBern(const TMAGNodeBern& Dist) : MuV(Dist.MuV), Dim(Dist.Dim) {} 00099 00100 TMAGNodeBern& operator=(const TMAGNodeBern& Dist); 00101 00102 void SetMuV(const TFltV& _MuV) { MuV = _MuV; } 00103 const TFltV& GetMuV() const { return MuV; } 00104 void SetMu(const int& Attr, const double& Prob) { IAssert(Prob >= 0.0 && Prob <= 1.0); MuV[Attr] = Prob; } 00105 double GetMu(const int& Attr) const { return MuV[Attr]; } 00106 00107 void LoadTxt(const TStr& InFNm); 00108 void SaveTxt(TStrV& OutStrV) const; 00109 void AttrGen(TIntVV& AttrVV, const int& NNodes); 00110 }; 00111 00113 // MAG node attributes with Beta + Bernoulli family 00114 00115 class TMAGNodeBeta { 00116 public: 00117 static TRnd Rnd; 00118 private: 00119 TFltV AlphaV; 00120 TFltV BetaV; 00121 TFltV MuV; 00122 TInt Dim; 00123 TBool Dirty; 00124 public: 00125 TMAGNodeBeta() : AlphaV(), BetaV(), MuV(), Dim(-1), Dirty(true) {} 00126 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); } 00127 TMAGNodeBeta(const TFltV& _AlphaV, const TFltV& _BetaV) : AlphaV(_AlphaV), BetaV(_BetaV), Dirty(true) { IAssert(_AlphaV.Len() == _BetaV.Len()); Dim = _AlphaV.Len(); MuV.Gen(Dim); } 00128 00129 TMAGNodeBeta& operator=(const TMAGNodeBeta& Dist); 00130 00131 void SetBeta(const int& Attr, const double& Alpha, const double& Beta); 00132 void GetBeta(const int& Attr, double& Alpha, double& Beta) const { IAssert(Attr < Dim); Alpha = AlphaV[Attr]; Beta = BetaV[Attr]; } 00133 void SetBetaV(const TFltV& _AlphaV, const TFltV& _BetaV); 00134 void GetBetaV(TFltV& _AlphaV, TFltV& _BetaV) const { _AlphaV = AlphaV; _BetaV = BetaV; } 00135 bool GetMuV(TFltV& _MuV) const { if(!Dirty) { _MuV = MuV; } return Dirty; } 00136 00137 void LoadTxt(const TStr& InFNm); 00138 void SaveTxt(TStrV& OutStrV) const; 00139 void AttrGen(TIntVV& AttrVV, const int& NNodes); 00140 }; 00141 00143 // MAG model param. (node attribtes + affinity matrices) 00144 00145 template <class TNodeAttr> 00146 class TMAGParam { 00147 public: 00148 static TRnd Rnd; 00149 private: 00150 TInt NNodes; 00151 TInt NAttrs; 00152 TNodeAttr NodeAttr; 00153 TMAGAffMtxV AffMtxV; 00154 00155 public: 00156 TMAGParam() : NNodes(-1), NAttrs(-1), NodeAttr(), AffMtxV() {} 00157 TMAGParam(const int& NumNodes, const int& NumAttrs) : NNodes(NumNodes), NAttrs(NumAttrs), NodeAttr(), AffMtxV(NumAttrs) {} 00158 TMAGParam(const int& NumNodes, const TMAGAffMtxV& MtxV) : NNodes(NumNodes), NAttrs(MtxV.Len()), NodeAttr(), AffMtxV(MtxV) {} 00159 TMAGParam(const int& NumNodes, const TStr& ConfFNm) : NNodes(NumNodes) { LoadTxt(ConfFNm); } 00160 TMAGParam(const TMAGParam<TNodeAttr>& MAGParam) : NNodes(MAGParam.NNodes), NAttrs(MAGParam.NAttrs), NodeAttr(MAGParam.NodeAttr), AffMtxV(MAGParam.AffMtxV) {} 00161 TMAGParam(const int& NumNodes, const int& NumAttrs, const TNodeAttr& Dist) : NNodes(NumNodes), NAttrs(NumAttrs), NodeAttr(Dist), AffMtxV(NumAttrs) {} 00162 00163 TMAGParam<TNodeAttr>& operator=(const TMAGParam<TNodeAttr>& MAGParam); 00164 00165 void Clr() { AffMtxV.Clr(); } 00166 00167 void LoadTxt(const TStr& InFNm); 00168 void SaveTxt(const TStr& OutFNm) const; 00169 00170 void SetNodes(const int& Nodes) { NNodes = Nodes; } 00171 const int GetNodes() const { return NNodes; } 00172 00173 void SetAttrs(const int& Attrs); 00174 const int GetAttrs() const { return NAttrs; } 00175 00176 void SetNodeAttr(const TNodeAttr& Dist) { NodeAttr = Dist; } 00177 const TNodeAttr& GetNodeAttr() const { return NodeAttr; } 00178 00179 const int GetDim(const int& Attr) const { return AffMtxV[Attr].GetDim(); } 00180 void GetDimV(TIntV& DimV) const; 00181 00182 void SetMtxVal(const int& Attr, const int& Row, const int& Col, const double& Val) { AffMtxV[Attr].At(Row, Col) = Val; } 00183 void SetMtx(const int& Attr, const TMAGAffMtx& Mtx) { AffMtxV[Attr] = Mtx; } 00184 void SetMtxV(const TMAGAffMtxV& MtxV) { NAttrs = AffMtxV.Len(); AffMtxV = MtxV; } 00185 void SetMtxAll(const TMAGAffMtx& Mtx) { AffMtxV.PutAll(Mtx); } 00186 const double GetMtxVal(const int& Attr, const int& Row, const int& Col) const { return AffMtxV[Attr].At(Row, Col); } 00187 const TMAGAffMtx& GetMtx(const int& Attr) const { return AffMtxV[Attr]; } 00188 void GetMtxV(TMAGAffMtxV& MtxV) const { MtxV = AffMtxV; } 00189 00190 PNGraph GenMAG(TIntVV& AttrVV, const bool& IsDir = false, const int& Seed = 1); 00191 PNGraph GenAttrMAG(const TIntVV& AttrVV, const bool& IsDir = false, const int& Seed = 1); 00192 }; 00193 00194 template <class TNodeAttr> 00195 TMAGParam<TNodeAttr>& TMAGParam<TNodeAttr>::operator=(const TMAGParam<TNodeAttr>& MAGParam) { 00196 NNodes = MAGParam.NNodes; 00197 NAttrs = MAGParam.NAttrs; 00198 AffMtxV = MAGParam.AffMtxV; 00199 NodeAttr = MAGParam.NodeAttr; 00200 00201 return (*this); 00202 }; 00203 00204 template <class TNodeAttr> 00205 void TMAGParam<TNodeAttr>::LoadTxt(const TStr& InFNm) { 00206 NodeAttr.LoadTxt(InFNm); 00207 00208 char buf[1024]; 00209 FILE *fp = fopen(InFNm.CStr(), "r"); 00210 NAttrs = 0; 00211 AffMtxV.Gen(10, 0); 00212 while(fgets(buf, sizeof(buf), fp) != NULL) { 00213 strtok(buf, "&"); 00214 char *token = strtok(NULL, "\r\n"); 00215 NAttrs++; 00216 AffMtxV.Add(TMAGAffMtx::GetMtx(TStr(token))); 00217 } 00218 fclose(fp); 00219 }; 00220 00221 template <class TNodeAttr> 00222 void TMAGParam<TNodeAttr>::SaveTxt(const TStr& OutFNm) const { 00223 TStrV OutStrV; 00224 NodeAttr.SaveTxt(OutStrV); 00225 00226 FILE *fp = fopen(OutFNm.CStr(), "w"); 00227 fprintf(fp, "# %d nodes with %d attributes\n", NNodes(), NAttrs()); 00228 for(int i = 0; i < NAttrs; i++) { 00229 TStr MtxStr = AffMtxV[i].GetMtxStr(); 00230 fprintf(fp, "%s & %s\n", OutStrV[i].CStr(), MtxStr.GetSubStr(1, MtxStr.Len()-2).CStr()); 00231 } 00232 fclose(fp); 00233 }; 00234 00235 template <class TNodeAttr> 00236 void TMAGParam<TNodeAttr>::SetAttrs(const int& Attrs) { 00237 NAttrs = Attrs; 00238 AffMtxV.Gen(Attrs); 00239 }; 00240 00241 template <class TNodeAttr> 00242 void TMAGParam<TNodeAttr>::GetDimV(TIntV& DimV) const { 00243 DimV.Gen(NAttrs); 00244 for(int i = 0; i < NAttrs; i++) { 00245 DimV[i] = AffMtxV[i].GetDim(); 00246 } 00247 }; 00248 00249 template <class TNodeAttr> 00250 PNGraph TMAGParam<TNodeAttr>::GenAttrMAG(const TIntVV& AttrVV, const bool& IsDir, const int& Seed) { 00251 PNGraph Graph = TNGraph::New(NNodes, -1); 00252 for(int i = 0; i < NNodes; i++) { 00253 Graph->AddNode(i); 00254 } 00255 00256 if(Seed > 0) { 00257 TNodeAttr::Rnd.PutSeed(Seed); 00258 } 00259 00260 IAssert(AttrVV.GetYDim() == NAttrs); 00261 for(int i = 0; i < NNodes; i++) { 00262 for(int l = 0; l < NAttrs; l++) { 00263 IAssert(AttrVV.At(i, l) < AffMtxV[l].GetDim()); 00264 } 00265 } 00266 00267 for(int i = 0; i < NNodes; i++) { 00268 for(int j = i; j < NNodes; j++) { 00269 double Prob = 1.0; 00270 for(int l = 0; l < NAttrs; l++) { 00271 Prob *= AffMtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l)); 00272 } 00273 00274 if(TNodeAttr::Rnd.GetUniDev() < Prob) { 00275 Graph->AddEdge(i, j); 00276 if(! IsDir && i != j) { Graph->AddEdge(j, i); } 00277 } 00278 00279 if(IsDir && i != j) { 00280 Prob = 1.0; 00281 for(int l = 0; l < NAttrs; l++) { 00282 Prob *= AffMtxV[l].At(AttrVV.At(j, l), AttrVV.At(i, l)); 00283 } 00284 if(TNodeAttr::Rnd.GetUniDev() < Prob) { 00285 Graph->AddEdge(j, i); 00286 } 00287 } 00288 } 00289 } 00290 00291 return Graph; 00292 }; 00293 00294 template <class TNodeAttr> 00295 PNGraph TMAGParam<TNodeAttr>::GenMAG(TIntVV& AttrVV, const bool& IsDir, const int& Seed) { 00296 TNodeAttr::Rnd.PutSeed(Seed); 00297 NodeAttr.AttrGen(AttrVV, NNodes); 00298 00299 IAssert(AttrVV.GetYDim() == NAttrs); 00300 for(int i = 0; i < NNodes; i++) { 00301 for(int l = 0; l < NAttrs; l++) { 00302 IAssert(AttrVV.At(i, l) < AffMtxV[l].GetDim()); 00303 } 00304 } 00305 00306 PNGraph Graph = GenAttrMAG(AttrVV, IsDir, 0); 00307 return Graph; 00308 00309 /* 00310 for(int i = 0; i < NNodes; i++) { 00311 for(int j = i; j < NNodes; j++) { 00312 double Prob = 1.0; 00313 for(int l = 0; l < NAttrs; l++) { 00314 Prob *= AffMtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l)); 00315 } 00316 if(TNodeAttr::Rnd.GetUniDev() < Prob) { 00317 Graph->AddEdge(i, j); 00318 if(! IsDir && i != j) { Graph->AddEdge(j, i); } 00319 } 00320 00321 if(IsDir && i != j) { 00322 Prob = 1.0; 00323 for(int l = 0; l < NAttrs; l++) { 00324 Prob *= AffMtxV[l].At(AttrVV.At(j, l), AttrVV.At(i, l)); 00325 } 00326 if(TNodeAttr::Rnd.GetUniDev() < Prob) { 00327 Graph->AddEdge(j, i); 00328 } 00329 } 00330 } 00331 } 00332 00333 return Graph; 00334 */ 00335 }; 00336 00337 00339 // MAGFit with Bernoulli node attributes 00340 00341 class TMAGFitBern { 00342 private: 00343 TFltVV PhiVV; 00344 TBoolVV KnownVV; 00345 PNGraph Graph; 00346 TMAGParam<TMAGNodeBern> Param; 00347 bool ESpeedUp, MSpeedUp, Debug; 00348 TFltV AvgPhiV; 00349 TFltVV AvgPhiPairVV; 00350 TFlt NormConst; 00351 00352 TVec<TFltV> MuHisV; 00353 TVec<TMAGAffMtxV> MtxHisV; 00354 TFltV LLHisV; 00355 00356 public: 00357 TMAGFitBern() : PhiVV(), KnownVV(), Graph(), Param(), ESpeedUp(true), MSpeedUp(true), Debug(false), AvgPhiV(), AvgPhiPairVV(), NormConst(1.0) { } 00358 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) { } 00359 00360 TMAGFitBern(const PNGraph& G, const TStr& InitFNm) : Param(G->GetNodes(), InitFNm), ESpeedUp(true), MSpeedUp(true), Debug(false), NormConst(1.0) { 00361 const int NNodes = G->GetNodes(); 00362 const int NAttrs = Param.GetAttrs(); 00363 00364 printf("NAttrs = %d\n", NAttrs); 00365 SetGraph(G); 00366 PhiVV.Gen(NNodes, NAttrs); 00367 KnownVV.Gen(NNodes, NAttrs); 00368 AvgPhiV.Gen(NAttrs); 00369 AvgPhiPairVV.Gen(NAttrs, NAttrs); 00370 } 00371 00372 void Clr() { PhiVV.Clr(); KnownVV.Clr(); } 00373 void SaveTxt(const TStr& FNm); 00374 00375 const int GetNodes() const { return Param.GetNodes(); } 00376 const int GetAttrs() const { return Param.GetAttrs(); } 00377 const TMAGParam<TMAGNodeBern>& GetParams() const { return Param; } 00378 const TMAGNodeBern& GetNodeAttr() const { return Param.GetNodeAttr(); } 00379 const TFltV& GetMuV() const { const TMAGNodeBern& Dist = Param.GetNodeAttr(); return Dist.GetMuV(); } 00380 void SetMuV(const TFltV& MuV) { TMAGNodeBern Dist = Param.GetNodeAttr(); Dist.SetMuV(MuV); Param.SetNodeAttr(Dist); } 00381 void GetMtxV(TMAGAffMtxV& MtxV) const { Param.GetMtxV(MtxV); } 00382 void SetMtxV(const TMAGAffMtxV& MtxV) { Param.SetMtxV(MtxV); } 00383 PNGraph GetGraph() { return Graph; } 00384 void SetGraph(const PNGraph& GraphPt); 00385 00386 void SetDebug(const bool _Debug) { Debug = _Debug; } 00387 void SetAlgConf(const bool EStep = true, const bool MStep = true) { ESpeedUp = EStep; MSpeedUp = MStep; } 00388 00389 void Init(const TFltV& MuV, const TMAGAffMtxV& AffMtxV); 00390 // void PerturbInit(const TFltV& MuV, const TMAGAffMtxV& AffMtxV, const double& PerturbRate); 00391 void RandomInit(const TFltV& MuV, const TMAGAffMtxV& AffMtxV, const int& Seed); 00392 const TFltVV& GetPhiVV() const { return PhiVV; } 00393 void SetPhiVV(const TIntVV& AttrVV, const int KnownIds = 0); 00394 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); } } 00395 00396 const double GetInCoeff(const int& i, const int& j, const int& l, const int& A, const TMAGAffMtx& Theta) const; 00397 const double GetAvgInCoeff(const int& i, const int& AId, const int& A, const TMAGAffMtx& Theta) const; 00398 const double GetOutCoeff(const int& i, const int& j, const int& l, const int& A, const TMAGAffMtx& Theta) const; 00399 const double GetAvgOutCoeff(const int& i, const int& AId, const int& A, const TMAGAffMtx& Theta) const; 00400 const double GetProbPhi(const int& NId1, const int& NId2, const int& AId, const int& Attr1, const int& Attr2) const; 00401 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; 00402 const double GetThetaLL(const int& NId1, const int& NId2, const int& AId) const; 00403 const double GetAvgThetaLL(const int& NId1, const int& NId2, const int& AId, const bool Left = false, const bool Right = false) const; 00404 const double GetSqThetaLL(const int& NId1, const int& NId2, const int& AId) const; 00405 const double GetAvgSqThetaLL(const int& NId1, const int& NId2, const int& AId, const bool Left = false, const bool Right = false) const; 00406 const double GetProdLinWeight(const int& NId1, const int& NId2) const; 00407 const double GetAvgProdLinWeight(const int& NId1, const int& NId2, const bool Left = false, const bool Right = false) const; 00408 const double GetProdSqWeight(const int& NId1, const int& NId2) const; 00409 const double GetAvgProdSqWeight(const int& NId1, const int& NId2, const bool Left = false, const bool Right = false) const; 00410 00411 const double GetEstNoEdgeLL(const int& NId, const int& AId) const; 00412 const double GradPhiMI(const double& x, const int& NId, const int& AId, const double& Lambda, const double& DeltaQ, const TFltVV& CntVV); 00413 const double ObjPhiMI(const double& x, const int& NId, const int& AId, const double& Lambda, const double& Q0, const double& Q1, const TFltVV& CntVV); 00414 const double UpdatePhiMI(const double& Lambda, const int& NId, const int& AId, double& Phi); 00415 const double UpdateApxPhiMI(const double& Lambda, const int& NId, const int& AId, double& Phi, TFltVV& ProdVV); 00416 const double UpdatePhi(const int& NId, const int& AId, double& Phi); 00417 const double UpdateMu(const int& AId); 00418 const void PrepareUpdateAffMtx(TFltVV& ProdVV, TFltVV& SqVV); 00419 const void PrepareUpdateApxAffMtx(TFltVV& ProdVV, TFltVV& SqVV); 00420 00421 const double UpdateAffMtx(const int& AId, const double& LrnRate, const double& MaxGrad, const double& Lambda, TFltVV& ProdVV, TFltVV& SqVV, TMAGAffMtx& NewMtx); 00422 const double UpdateAffMtxV(const int& GradIter, const double& LrnRate, const double& MaxGrad, const double& Lambda, const int& NReal = 0); 00423 const void GradAffMtx(const int& AId, const TFltVV& ProdVV, const TFltVV& SqVV, const TMAGAffMtx& CurMtx, TFltV& GradV); 00424 const void GradApxAffMtx(const int& AId, const TFltVV& ProdVV, const TFltVV& SqVV, const TMAGAffMtx& CurMtx, TFltV& GradV); 00425 00426 /* EM algorithm */ 00427 double DoEStepOneIter(const TFltV& TrueMuV, TFltVV& NewPhi, const double& Lambda); 00428 double DoEStepApxOneIter(const TFltV& TrueMuV, TFltVV& NewPhi, const double& Lambda); 00429 double DoEStep(const TFltV& TrueMuV, const int& NIter, double& LL, const double& Lambda); 00430 void DoMStep(const int& GradIter, const double& LrnRate, const double& MaxGrad, const double& Lambda, const int& NReal = 0); 00431 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); 00432 00433 void CountAttr(TFltV& EstMuV) const; 00434 void SortAttrOrdering(const TFltV& TrueMuV, TIntV& IndexV) const; 00435 00436 const double ComputeJointOneLL(const TIntVV& AttrVV) const; 00437 const double ComputeJointLL(int NSample) const; 00438 const double ComputeJointAdjLL(const TIntVV& AttrVV) const; 00439 const double ComputeApxLL() const; 00440 const double ComputeApxAdjLL() const; 00441 00442 void MakeCCDF(const TFltPrV& RawV, TFltPrV& CcdfV); 00443 void PlotProperties(const TStr& FNm); 00444 00445 static const double ComputeMI(const TIntVV& AttrV, const int AId1, const int AId2); 00446 static const double ComputeMI(const TFltVV& AttrV, const int AId1, const int AId2); 00447 static const double ComputeMI(const TIntVV& AttrV); 00448 static const double ComputeMI(const TFltVV& AttrV); 00449 void NormalizeAffMtxV(TMAGAffMtxV& MtxV, const bool UseMu = false); 00450 void UnNormalizeAffMtxV(TMAGAffMtxV& MtxV, const bool UseMu = false); 00451 private: 00452 const bool NextPermutation(TIntV& IndexV) const; 00453 }; 00454 00455 const double LogSumExp(const double LogVal1, const double LogVal2); 00456 const double LogSumExp(const TFltV& LogValV); 00457 const double LogSumExp(const double *LogValArray, const int Len); 00458 00459 #endif // snap_mag_h