SNAP Library , User Reference
2013-01-07 14:03:36
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
|
#include <mag.h>
Public Member Functions | |
TMAGFitBern () | |
TMAGFitBern (const PNGraph &G, const int &NAttrs) | |
TMAGFitBern (const PNGraph &G, const TStr &InitFNm) | |
void | Clr () |
void | SaveTxt (const TStr &FNm) |
const int | GetNodes () const |
const int | GetAttrs () const |
const TMAGParam< TMAGNodeBern > & | GetParams () const |
const TMAGNodeBern & | GetNodeAttr () const |
const TFltV & | GetMuV () const |
void | SetMuV (const TFltV &MuV) |
void | GetMtxV (TMAGAffMtxV &MtxV) const |
void | SetMtxV (const TMAGAffMtxV &MtxV) |
PNGraph | GetGraph () |
void | SetGraph (const PNGraph &GraphPt) |
void | SetDebug (const bool _Debug) |
void | SetAlgConf (const bool EStep=true, const bool MStep=true) |
void | Init (const TFltV &MuV, const TMAGAffMtxV &AffMtxV) |
void | RandomInit (const TFltV &MuV, const TMAGAffMtxV &AffMtxV, const int &Seed) |
const TFltVV & | GetPhiVV () const |
void | SetPhiVV (const TIntVV &AttrVV, const int KnownIds=0) |
void | SetPhiVV (const TFltVV &AttrVV, const int KnownIds=0) |
const double | GetInCoeff (const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const |
const double | GetAvgInCoeff (const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const |
const double | GetOutCoeff (const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const |
const double | GetAvgOutCoeff (const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const |
const double | GetProbPhi (const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const |
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 |
const double | GetThetaLL (const int &NId1, const int &NId2, const int &AId) const |
const double | GetAvgThetaLL (const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const |
const double | GetSqThetaLL (const int &NId1, const int &NId2, const int &AId) const |
const double | GetAvgSqThetaLL (const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const |
const double | GetProdLinWeight (const int &NId1, const int &NId2) const |
const double | GetAvgProdLinWeight (const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const |
const double | GetProdSqWeight (const int &NId1, const int &NId2) const |
const double | GetAvgProdSqWeight (const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const |
const double | GetEstNoEdgeLL (const int &NId, const int &AId) const |
const double | GradPhiMI (const double &x, const int &NId, const int &AId, const double &Lambda, const double &DeltaQ, const TFltVV &CntVV) |
const double | ObjPhiMI (const double &x, const int &NId, const int &AId, const double &Lambda, const double &Q0, const double &Q1, const TFltVV &CntVV) |
const double | UpdatePhiMI (const double &Lambda, const int &NId, const int &AId, double &Phi) |
const double | UpdateApxPhiMI (const double &Lambda, const int &NId, const int &AId, double &Phi, TFltVV &ProdVV) |
const double | UpdatePhi (const int &NId, const int &AId, double &Phi) |
const double | UpdateMu (const int &AId) |
const void | PrepareUpdateAffMtx (TFltVV &ProdVV, TFltVV &SqVV) |
const void | PrepareUpdateApxAffMtx (TFltVV &ProdVV, TFltVV &SqVV) |
const double | UpdateAffMtx (const int &AId, const double &LrnRate, const double &MaxGrad, const double &Lambda, TFltVV &ProdVV, TFltVV &SqVV, TMAGAffMtx &NewMtx) |
const double | UpdateAffMtxV (const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0) |
const void | GradAffMtx (const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV) |
const void | GradApxAffMtx (const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV) |
double | DoEStepOneIter (const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda) |
double | DoEStepApxOneIter (const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda) |
double | DoEStep (const TFltV &TrueMuV, const int &NIter, double &LL, const double &Lambda) |
void | DoMStep (const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0) |
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) |
void | CountAttr (TFltV &EstMuV) const |
void | SortAttrOrdering (const TFltV &TrueMuV, TIntV &IndexV) const |
const double | ComputeJointOneLL (const TIntVV &AttrVV) const |
const double | ComputeJointLL (int NSample) const |
const double | ComputeJointAdjLL (const TIntVV &AttrVV) const |
const double | ComputeApxLL () const |
const double | ComputeApxAdjLL () const |
void | MakeCCDF (const TFltPrV &RawV, TFltPrV &CcdfV) |
void | PlotProperties (const TStr &FNm) |
void | NormalizeAffMtxV (TMAGAffMtxV &MtxV, const bool UseMu=false) |
void | UnNormalizeAffMtxV (TMAGAffMtxV &MtxV, const bool UseMu=false) |
Static Public Member Functions | |
static const double | ComputeMI (const TIntVV &AttrV, const int AId1, const int AId2) |
static const double | ComputeMI (const TFltVV &AttrV, const int AId1, const int AId2) |
static const double | ComputeMI (const TIntVV &AttrV) |
static const double | ComputeMI (const TFltVV &AttrV) |
Private Member Functions | |
const bool | NextPermutation (TIntV &IndexV) const |
Private Attributes | |
TFltVV | PhiVV |
TBoolVV | KnownVV |
PNGraph | Graph |
TMAGParam< TMAGNodeBern > | Param |
bool | ESpeedUp |
bool | MSpeedUp |
bool | Debug |
TFltV | AvgPhiV |
TFltVV | AvgPhiPairVV |
TFlt | NormConst |
TVec< TFltV > | MuHisV |
TVec< TMAGAffMtxV > | MtxHisV |
TFltV | LLHisV |
TMAGFitBern::TMAGFitBern | ( | ) | [inline] |
TMAGFitBern::TMAGFitBern | ( | const PNGraph & | G, |
const int & | NAttrs | ||
) | [inline] |
TMAGFitBern::TMAGFitBern | ( | const PNGraph & | G, |
const TStr & | InitFNm | ||
) | [inline] |
Definition at line 360 of file mag.h.
: Param(G->GetNodes(), InitFNm), ESpeedUp(true), MSpeedUp(true), Debug(false), NormConst(1.0) { const int NNodes = G->GetNodes(); const int NAttrs = Param.GetAttrs(); printf("NAttrs = %d\n", NAttrs); SetGraph(G); PhiVV.Gen(NNodes, NAttrs); KnownVV.Gen(NNodes, NAttrs); AvgPhiV.Gen(NAttrs); AvgPhiPairVV.Gen(NAttrs, NAttrs); }
void TMAGFitBern::Clr | ( | ) | [inline] |
const double TMAGFitBern::ComputeApxAdjLL | ( | ) | const |
Definition at line 2046 of file mag.cpp.
{ double LL = 0.0; // double LLSelf = 0.0; const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TMAGNodeBern NodeAttr = Param.GetNodeAttr(); TFltV MuV = NodeAttr.GetMuV(); MuV.PutAll(0.0); TMAGAffMtxV LLMtxV(NAttrs); double TotalEdge = 0.0; for(int l = 0; l < NAttrs; l++) { TMAGAffMtx Theta = Param.GetMtx(l); Theta.GetLLMtx(LLMtxV[l]); } for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NNodes; j++) { if(i == j) { continue; } if(Graph->IsEdge(i, j)) { for(int l = 0; l < NAttrs; l++) { LL += GetProbPhi(i, j, l, 0, 0) * LLMtxV[l].At(0, 0); LL += GetProbPhi(i, j, l, 0, 1) * LLMtxV[l].At(0, 1); LL += GetProbPhi(i, j, l, 1, 0) * LLMtxV[l].At(1, 0); LL += GetProbPhi(i, j, l, 1, 1) * LLMtxV[l].At(1, 1); } } else { LL += log(1-exp(GetProdLinWeight(i, j))); } double TempLL = 1.0; for(int l = 0; l < NAttrs; l++) { int Ai = (double(PhiVV(i, l)) > 0.5) ? 0 : 1; int Aj = (double(PhiVV(j, l)) > 0.5) ? 0 : 1; TempLL *= Param.GetMtx(l).At(Ai, Aj); } if(TMAGNodeBern::Rnd.GetUniDev() < TempLL) { TotalEdge += 1.0; } } } return LL; }
const double TMAGFitBern::ComputeApxLL | ( | ) | const |
Definition at line 2005 of file mag.cpp.
{ double LL = 0.0; // double LLSelf = 0.0; const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TMAGNodeBern NodeAttr = Param.GetNodeAttr(); TFltV MuV = NodeAttr.GetMuV(); TMAGAffMtxV LLMtxV(NAttrs); for(int l = 0; l < NAttrs; l++) { for(int i = 0; i < NNodes; i++) { LL += PhiVV(i, l) * log(MuV[l]); LL += (1.0 - PhiVV(i, l)) * log(1.0 - MuV[l]); LL -= PhiVV(i, l) * log(PhiVV(i, l)); LL -= (1.0 - PhiVV(i, l)) * log(1.0 - PhiVV(i, l)); } TMAGAffMtx Theta = Param.GetMtx(l); Theta.GetLLMtx(LLMtxV[l]); } for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NNodes; j++) { if(i == j) { continue; } if(Graph->IsEdge(i, j)) { for(int l = 0; l < NAttrs; l++) { LL += GetProbPhi(i, j, l, 0, 0) * LLMtxV[l].At(0, 0); LL += GetProbPhi(i, j, l, 0, 1) * LLMtxV[l].At(0, 1); LL += GetProbPhi(i, j, l, 1, 0) * LLMtxV[l].At(1, 0); LL += GetProbPhi(i, j, l, 1, 1) * LLMtxV[l].At(1, 1); } LL += log(NormConst); } else { LL += log(1-exp(GetProdLinWeight(i, j))); } } } return LL; }
const double TMAGFitBern::ComputeJointAdjLL | ( | const TIntVV & | AttrVV | ) | const |
Definition at line 1942 of file mag.cpp.
{ double LL = 0.0; const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TMAGAffMtxV MtxV(NAttrs); Param.GetMtxV(MtxV); const TMAGNodeBern NodeAttr = Param.GetNodeAttr(); const TFltV MuV = NodeAttr.GetMuV(); for(int l = 0; l < NAttrs; l++) { for(int i = 0; i < MtxV[l].Len(); i++) { MtxV[l].At(i) = log(MtxV[l].At(i)); } } for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NNodes; j++) { if(i == j) { continue; } double ProbLL = 0.0; for(int l = 0; l < NAttrs; l++) { ProbLL += MtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l)); } if(Graph->IsEdge(i, j)) { LL += ProbLL; } else { LL += log(1-exp(ProbLL)); } } } return LL; }
const double TMAGFitBern::ComputeJointLL | ( | int | NSample | ) | const |
Definition at line 1976 of file mag.cpp.
{ double LL = 0.0; const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TRnd Rnd(2000); TIntVV AttrVV(NNodes, NAttrs); int count = 0; for(int s = 0; s < NSample; s++) { for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { if(Rnd.GetUniDev() <= PhiVV(i, l)) { AttrVV.At(i, l) = 0; } else { AttrVV.At(i, l) = 1; } if(PhiVV(i, l) > 0.05 && PhiVV(i, l) < 0.95) count++; } } LL += ComputeJointOneLL(AttrVV); } AttrVV.Clr(); return LL / double(NSample); }
const double TMAGFitBern::ComputeJointOneLL | ( | const TIntVV & | AttrVV | ) | const |
Definition at line 1900 of file mag.cpp.
{ double LL = 0.0; const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TMAGAffMtxV MtxV(NAttrs); Param.GetMtxV(MtxV); const TMAGNodeBern NodeAttr = Param.GetNodeAttr(); const TFltV MuV = NodeAttr.GetMuV(); for(int l = 0; l < NAttrs; l++) { for(int i = 0; i < MtxV[l].Len(); i++) { MtxV[l].At(i) = log(MtxV[l].At(i)); } } for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { if(AttrVV.At(i, l) == 0) { LL += log(MuV[l]); } else { LL += log(1.0 - MuV[l]); } } for(int j = 0; j < NNodes; j++) { if(i == j) { continue; } double ProbLL = 0.0; for(int l = 0; l < NAttrs; l++) { ProbLL += MtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l)); } if(Graph->IsEdge(i, j)) { LL += ProbLL; } else { LL += log(1-exp(ProbLL)); } } } return LL; }
const double TMAGFitBern::ComputeMI | ( | const TIntVV & | AttrV, |
const int | AId1, | ||
const int | AId2 | ||
) | [static] |
Definition at line 2091 of file mag.cpp.
{ const int NNodes = AttrV.GetXDim(); double MI = 0.0; double Cor = 0.0; TFltVV Pxy(2,2); TFltV Px(2), Py(2); Pxy.PutAll(0.0); Px.PutAll(0.0); Py.PutAll(0.0); for(int i = 0; i < NNodes; i++) { int X = AttrV(i, AId1); int Y = AttrV(i, AId2); Pxy(X, Y) = Pxy(X, Y) + 1; Px[X] = Px[X] + 1; Py[Y] = Py[Y] + 1; Cor += double(X * Y); } for(int x = 0; x < 2; x++) { for(int y = 0; y < 2; y++) { MI += Pxy(x, y) / double(NNodes) * (log(Pxy(x, y).Val) - log(Px[x].Val) - log(Py[y].Val) + log((double)NNodes)); } } return MI; }
const double TMAGFitBern::ComputeMI | ( | const TFltVV & | AttrV, |
const int | AId1, | ||
const int | AId2 | ||
) | [static] |
Definition at line 2120 of file mag.cpp.
{ const int NNodes = AttrV.GetXDim(); double MI = 0.0; double Cor = 0.0; TFltVV Pxy(2,2); TFltV Px(2), Py(2); Pxy.PutAll(0.0); Px.PutAll(0.0); Py.PutAll(0.0); for(int i = 0; i < NNodes; i++) { double X = AttrV(i, AId1); double Y = AttrV(i, AId2); Pxy(0, 0) = Pxy(0, 0) + X * Y; Pxy(0, 1) = Pxy(0, 1) + X * (1 - Y); Pxy(1, 0) = Pxy(1, 0) + (1 - X) * Y; Pxy(1, 1) = (i+1) - Pxy(0, 0) - Pxy(0, 1) - Pxy(1, 0); Px[0] = Px[0] + X; Py[0] = Py[0] + Y; Cor += double((1-X) * (1-Y)); } Px[1] = NNodes - Px[0]; Py[1] = NNodes - Py[0]; for(int x = 0; x < 2; x++) { for(int y = 0; y < 2; y++) { MI += Pxy(x, y) / double(NNodes) * (log(Pxy(x, y)) - log(Px[x]) - log(Py[y]) + log(double(NNodes))); } } return MI; }
const double TMAGFitBern::ComputeMI | ( | const TIntVV & | AttrV | ) | [static] |
const double TMAGFitBern::ComputeMI | ( | const TFltVV & | AttrV | ) | [static] |
void TMAGFitBern::CountAttr | ( | TFltV & | EstMuV | ) | const |
void TMAGFitBern::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 at line 1630 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TIntV IndexV; double LL; // double MuDist, MtxDist; MuHisV.Gen(NStep + 1, 0); MtxHisV.Gen(NStep + 1, 0); LLHisV.Gen(NStep + 1, 0); printf("--------------------------------------------\n"); printf("Before EM Iteration\n"); printf("--------------------------------------------\n"); TMAGAffMtxV InitMtxV; TMAGNodeBern NodeAttr = Param.GetNodeAttr(); Param.GetMtxV(InitMtxV); TFltV InitMuV = NodeAttr.GetMuV(); for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { if(! KnownVV(i, l)) { PhiVV.At(i, l) = TMAGNodeBern::Rnd.GetUniDev(); } } } if(Debug) { double LL = ComputeApxLL(); MuHisV.Add(InitMuV); MtxHisV.Add(InitMtxV); LLHisV.Add(LL); } NormalizeAffMtxV(InitMtxV, true); Param.SetMtxV(InitMtxV); for(int n = 0; n < NStep; n++) { printf("--------------------------------------------\n"); printf("EM Iteration : %d\n", (n+1)); printf("--------------------------------------------\n"); NodeAttr = Param.GetNodeAttr(); for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { if(!KnownVV(i, l) && TMAGNodeBern::Rnd.GetUniDev() < ReInit) { PhiVV.At(i, l) = TMAGNodeBern::Rnd.GetUniDev(); } } } DoEStep(InitMuV, NEstep, LL, Lambda); Param.GetMtxV(InitMtxV); // NormalizeAffMtxV(InitMtxV); Param.SetMtxV(InitMtxV); DoMStep(NMstep, LrnRate, MaxGrad, Lambda, NReal); printf("\n"); if(Debug) { double LL = ComputeApxLL(); MuHisV.Add(InitMuV); MtxHisV.Add(InitMtxV); LLHisV.Add(LL); printf(" ApxLL = %.2f (Const = %f)\n", LL, double(NormConst)); } } Param.GetMtxV(InitMtxV); UnNormalizeAffMtxV(InitMtxV, true); Param.SetMtxV(InitMtxV); }
double TMAGFitBern::DoEStep | ( | const TFltV & | TrueMuV, |
const int & | NIter, | ||
double & | LL, | ||
const double & | Lambda | ||
) |
Definition at line 1272 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TFltVV NewPhiVV(NNodes, NAttrs); // double MI; TFltV Delta(NIter); for(int i = 0; i < NIter; i++) { TExeTm IterTm; printf("EStep iteration : %d\n", (i+1)); if(ESpeedUp) { Delta[i] = DoEStepApxOneIter(TrueMuV, NewPhiVV, Lambda); } else { Delta[i] = DoEStepOneIter(TrueMuV, NewPhiVV, Lambda); } // PhiVV = NewPhiVV; printf(" (Time = %s)\n", IterTm.GetTmStr()); } printf("\n"); NewPhiVV.Clr(); return Delta.Last(); }
double TMAGFitBern::DoEStepApxOneIter | ( | const TFltV & | TrueMuV, |
TFltVV & | NewPhi, | ||
const double & | Lambda | ||
) |
Definition at line 1134 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); double MaxDelta = 0, L1 = 0; double Val; TFltIntIntTrV NewVal; int RndCount = 0; // double OldMI = 0.0, NewMI = 0.0; TFltV MuV(NAttrs); MuV.PutAll(0.0); TFltVV ProdVV(NNodes, 4); ProdVV.PutAll(0.0); TIntV NIndV(NNodes), AIndV(NAttrs); // Update Phi /* for(int i = 0; i < NNodes; i++) { NIndV[i] = i; } for(int l = 0; l < NAttrs; l++) { AIndV[l] = l; } if(Randomized) { NIndV.Shuffle(TMAGNodeBern::Rnd); AIndV.Shuffle(TMAGNodeBern::Rnd); } */ AvgPhiV.Gen(NAttrs); AvgPhiV.PutAll(0.0); AvgPhiPairVV.Gen(NAttrs, 4*NAttrs); AvgPhiPairVV.PutAll(0.0); for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { for(int p = l+1; p < NAttrs; p++) { int index = 4 * p; AvgPhiPairVV(l, index) += PhiVV(i, l) * PhiVV(i, p); AvgPhiPairVV(l, index+1) += PhiVV(i, l) * (1.0-PhiVV(i, p)); AvgPhiPairVV(l, index+2) += (1.0-PhiVV(i, l)) * PhiVV(i, p); AvgPhiPairVV(l, index+3) += (1.0-PhiVV(i, l)) * (1.0-PhiVV(i, p)); } AvgPhiV[l] += PhiVV(i, l); } } for(int i = 0; i < NNodes; i++) { ProdVV(i, 0) = GetAvgProdLinWeight(i, i, true, false); ProdVV(i, 1) = GetAvgProdLinWeight(i, i, false, true); ProdVV(i, 2) = GetAvgProdSqWeight(i, i, true, false); ProdVV(i, 3) = GetAvgProdSqWeight(i, i, false, true); } const int Iter = 3; int NId; NewVal.Gen(NAttrs * Iter); for(int i = 0; i < NNodes * Iter; i++) { for(int l = 0; l < NAttrs; l++) { const int AId = TMAGNodeBern::Rnd.GetUniDevInt(NAttrs); double Delta = 0.0; if(KnownVV(NId, AId)) { Val = PhiVV.At(NId, AId); } else { Delta = UpdateApxPhiMI(Lambda, NId, AId, Val, ProdVV); } // PhiVV.At(NId, AId) = Val; NewVal[l] = TFltIntIntTr(Val, NId, AId); // MuV[AId] = MuV[AId] + Val; if(fabs(Delta) > MaxDelta) { MaxDelta = fabs(Delta); } if(Val > 0.3 && Val < 0.7) { RndCount++; } } for(int l = 0; l < NAttrs; l++) { const int NId = NewVal[l].Val2; const int AId = NewVal[l].Val3; ProdVV(NId, 0) -= GetAvgThetaLL(NId, NId, AId, true, false); ProdVV(NId, 1) -= GetAvgThetaLL(NId, NId, AId, false, true); ProdVV(NId, 2) -= GetAvgSqThetaLL(NId, NId, AId, true, false); ProdVV(NId, 3) -= GetAvgSqThetaLL(NId, NId, AId, false, true); for(int p = 0; p < NAttrs; p++) { if(p > AId) { int index = 4 * p; AvgPhiPairVV(AId, index) -= PhiVV(NId, AId) * PhiVV(NId, p); AvgPhiPairVV(AId, index+1) -= PhiVV(NId, AId) * (1.0-PhiVV(NId, p)); AvgPhiPairVV(AId, index+2) -= (1.0-PhiVV(NId, AId)) * PhiVV(NId, p); AvgPhiPairVV(AId, index+3) -= (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, p)); } else if (p < AId) { int index = 4 * AId; AvgPhiPairVV(p, index) -= PhiVV(NId, p) * PhiVV(NId, AId); AvgPhiPairVV(p, index+1) -= PhiVV(NId, p) * (1.0-PhiVV(NId, AId)); AvgPhiPairVV(p, index+2) -= (1.0-PhiVV(NId, p)) * PhiVV(NId, AId); AvgPhiPairVV(p, index+3) -= (1.0-PhiVV(NId, p)) * (1.0-PhiVV(NId, AId)); } } AvgPhiV[AId] -= PhiVV(NId, AId); PhiVV.At(NId, AId) = NewVal[l].Val1; ProdVV(NId, 0) += GetAvgThetaLL(NId, NId, AId, true, false); ProdVV(NId, 1) += GetAvgThetaLL(NId, NId, AId, false, true); ProdVV(NId, 2) += GetAvgSqThetaLL(NId, NId, AId, true, false); ProdVV(NId, 3) += GetAvgSqThetaLL(NId, NId, AId, false, true); for(int p = 0; p < NAttrs; p++) { if(p > AId) { int index = 4 * p; AvgPhiPairVV(AId, index) += PhiVV(NId, AId) * PhiVV(NId, p); AvgPhiPairVV(AId, index+1) += PhiVV(NId, AId) * (1.0-PhiVV(NId, p)); AvgPhiPairVV(AId, index+2) += (1.0-PhiVV(NId, AId)) * PhiVV(NId, p); AvgPhiPairVV(AId, index+3) += (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, p)); } else if (p < AId) { int index = 4 * AId; AvgPhiPairVV(p, index) += PhiVV(NId, p) * PhiVV(NId, AId); AvgPhiPairVV(p, index+1) += PhiVV(NId, p) * (1.0-PhiVV(NId, AId)); AvgPhiPairVV(p, index+2) += (1.0-PhiVV(NId, p)) * PhiVV(NId, AId); AvgPhiPairVV(p, index+3) += (1.0-PhiVV(NId, p)) * (1.0-PhiVV(NId, AId)); } } AvgPhiV[AId] += PhiVV(NId, AId); } } for(int l = 0; l < NAttrs; l++) { MuV[l] = AvgPhiV[l] / double(NNodes); } TFltV SortMuV = MuV; double Avg = 0.0; // SortMuV.Sort(false); for(int l = 0; l < NAttrs; l++) { printf(" F[%d] = %.3f", l, double(MuV[l])); Avg += SortMuV[l]; // L1 += fabs(TrueMuV[l] - SortMuV[l]); } printf("\n"); printf(" Rnd = %d(%.3f)", RndCount, double(RndCount) / double(NNodes * NAttrs)); printf(" Avg = %.3f\n", Avg / double(NAttrs)); // printf(" Linf = %f\n", MaxDelta); // L1 /= double(NAttrs); return L1; }
double TMAGFitBern::DoEStepOneIter | ( | const TFltV & | TrueMuV, |
TFltVV & | NewPhi, | ||
const double & | Lambda | ||
) |
Definition at line 1056 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); double MaxDelta = 0, L1 = 0; double Val; TFltIntIntTrV NewVal; int RndCount = 0; // double OldMI = 0.0, NewMI = 0.0; TFltV MuV(NAttrs); MuV.PutAll(0.0); TIntV NIndV(NNodes), AIndV(NAttrs); // Update Phi /* for(int i = 0; i < NNodes; i++) { NIndV[i] = i; } for(int l = 0; l < NAttrs; l++) { AIndV[l] = l; } if(Randomized) { NIndV.Shuffle(TMAGNodeBern::Rnd); AIndV.Shuffle(TMAGNodeBern::Rnd); } */ NewVal.Gen(NAttrs * 2); for(int i = 0; i < NNodes; i++) { // const int NId = NIndV[i]%NNodes; for(int l = 0; l < NAttrs * 2; l++) { const int NId = TMAGNodeBern::Rnd.GetUniDevInt(NNodes); const int AId = TMAGNodeBern::Rnd.GetUniDevInt(NAttrs); // const int AId = AIndV[l]%NAttrs; // double Delta = UpdatePhi(NId, AId, Val); double Delta = 0.0; if(KnownVV(NId, AId)) { Val = PhiVV.At(NId, AId); } else { Delta = UpdatePhiMI(Lambda, NId, AId, Val); } // PhiVV.At(NId, AId) = Val; NewVal[l] = TFltIntIntTr(Val, NId, AId); // MuV[AId] = MuV[AId] + Val; if(fabs(Delta) > MaxDelta) { MaxDelta = fabs(Delta); } if(Val > 0.3 && Val < 0.7) { RndCount++; } } for(int l = 0; l < NAttrs * 2; l++) { const int NId = NewVal[l].Val2; const int AId = NewVal[l].Val3; PhiVV.At(NId, AId) = NewVal[l].Val1; } } for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { MuV[l] = MuV[l] + PhiVV.At(i, l); } } for(int l = 0; l < NAttrs; l++) { MuV[l] = MuV[l] / double(NNodes); } TFltV SortMuV = MuV; double Avg = 0.0; SortMuV.Sort(false); for(int l = 0; l < NAttrs; l++) { printf(" F[%d] = %.3f", l, double(MuV[l])); Avg += SortMuV[l]; L1 += fabs(TrueMuV[l] - SortMuV[l]); } printf("\n"); printf(" Rnd = %d(%.3f)", RndCount, double(RndCount) / double(NNodes * NAttrs)); printf(" Avg = %.3f\n", Avg / double(NAttrs)); L1 /= double(NAttrs); return L1; }
void TMAGFitBern::DoMStep | ( | const int & | GradIter, |
const double & | LrnRate, | ||
const double & | MaxGrad, | ||
const double & | Lambda, | ||
const int & | NReal = 0 |
||
) |
Definition at line 1608 of file mag.cpp.
{ // const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); double MuDelta = 0.0, AffMtxDelta = 0.0; TExeTm ExeTm; printf("\n"); AvgPhiV.Gen(NAttrs); AvgPhiV.PutAll(0.0); for(int l = 0; l < NAttrs; l++) { // printf(" [Attr = %d]\n", l); MuDelta += UpdateMu(l); } printf("\n"); printf(" == Update Theta\n"); AffMtxDelta += UpdateAffMtxV(GradIter, LrnRate, MaxGrad, Lambda, NReal); printf("\n"); printf("Elpased time = %s\n", ExeTm.GetTmStr()); printf("\n"); }
const int TMAGFitBern::GetAttrs | ( | ) | const [inline] |
const double TMAGFitBern::GetAvgInCoeff | ( | const int & | i, |
const int & | AId, | ||
const int & | A, | ||
const TMAGAffMtx & | Theta | ||
) | const |
const double TMAGFitBern::GetAvgOutCoeff | ( | const int & | i, |
const int & | AId, | ||
const int & | A, | ||
const TMAGAffMtx & | Theta | ||
) | const |
const double TMAGFitBern::GetAvgProdLinWeight | ( | const int & | NId1, |
const int & | NId2, | ||
const bool | Left = false , |
||
const bool | Right = false |
||
) | const |
const double TMAGFitBern::GetAvgProdSqWeight | ( | const int & | NId1, |
const int & | NId2, | ||
const bool | Left = false , |
||
const bool | Right = false |
||
) | const |
const double TMAGFitBern::GetAvgSqThetaLL | ( | const int & | NId1, |
const int & | NId2, | ||
const int & | AId, | ||
const bool | Left = false , |
||
const bool | Right = false |
||
) | const |
const double TMAGFitBern::GetAvgThetaLL | ( | const int & | NId1, |
const int & | NId2, | ||
const int & | AId, | ||
const bool | Left = false , |
||
const bool | Right = false |
||
) | const |
const double TMAGFitBern::GetEstNoEdgeLL | ( | const int & | NId, |
const int & | AId | ||
) | const |
Definition at line 746 of file mag.cpp.
{ // const int NNodes = Param.GetNodes(); // const int NAttrs = Param.GetAttrs(); TMAGNodeBern DistParam = Param.GetNodeAttr(); double LL = 0.0; return LL; }
PNGraph TMAGFitBern::GetGraph | ( | ) | [inline] |
const double TMAGFitBern::GetInCoeff | ( | const int & | i, |
const int & | j, | ||
const int & | l, | ||
const int & | A, | ||
const TMAGAffMtx & | Theta | ||
) | const |
void TMAGFitBern::GetMtxV | ( | TMAGAffMtxV & | MtxV | ) | const [inline] |
const TFltV& TMAGFitBern::GetMuV | ( | ) | const [inline] |
Definition at line 379 of file mag.h.
{ const TMAGNodeBern& Dist = Param.GetNodeAttr(); return Dist.GetMuV(); }
const TMAGNodeBern& TMAGFitBern::GetNodeAttr | ( | ) | const [inline] |
Definition at line 378 of file mag.h.
{ return Param.GetNodeAttr(); }
const int TMAGFitBern::GetNodes | ( | ) | const [inline] |
const double TMAGFitBern::GetOutCoeff | ( | const int & | i, |
const int & | j, | ||
const int & | l, | ||
const int & | A, | ||
const TMAGAffMtx & | Theta | ||
) | const |
const TMAGParam<TMAGNodeBern>& TMAGFitBern::GetParams | ( | ) | const [inline] |
const TFltVV& TMAGFitBern::GetPhiVV | ( | ) | const [inline] |
const double TMAGFitBern::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 at line 576 of file mag.cpp.
{ TMAGNodeBern DistParam = Param.GetNodeAttr(); // double Mu = DistParam.GetMu(AId); double Mu = AvgPhiV[AId] / double(Param.GetNodes()); double Prob1 = (Left) ? double(PhiVV.At(NId1, AId)) : double(Mu); double Prob2 = (Right)? double(PhiVV.At(NId2, AId)) : double(Mu); Prob1 = (Attr1 == 0) ? Prob1 : 1.0 - Prob1; Prob2 = (Attr2 == 0) ? Prob2 : 1.0 - Prob2; return (Prob1 * Prob2); }
const double TMAGFitBern::GetProbPhi | ( | const int & | NId1, |
const int & | NId2, | ||
const int & | AId, | ||
const int & | Attr1, | ||
const int & | Attr2 | ||
) | const |
const double TMAGFitBern::GetProdLinWeight | ( | const int & | NId1, |
const int & | NId2 | ||
) | const |
const double TMAGFitBern::GetProdSqWeight | ( | const int & | NId1, |
const int & | NId2 | ||
) | const |
const double TMAGFitBern::GetSqThetaLL | ( | const int & | NId1, |
const int & | NId2, | ||
const int & | AId | ||
) | const |
Definition at line 609 of file mag.cpp.
{ double LL = 0.0; const TMAGAffMtx& Mtx = Param.GetMtx(AId); for(int A1 = 0; A1 < 2; A1++) { for(int A2 = 0; A2 < 2; A2++) { LL += GetProbPhi(NId1, NId2, AId, A1, A2) * Mtx.At(A1, A2) * Mtx.At(A1, A2); } } return log(LL); }
const double TMAGFitBern::GetThetaLL | ( | const int & | NId1, |
const int & | NId2, | ||
const int & | AId | ||
) | const |
Definition at line 587 of file mag.cpp.
{ double LL = 0.0; const TMAGAffMtx& Mtx = Param.GetMtx(AId); for(int A1 = 0; A1 < 2; A1++) { for(int A2 = 0; A2 < 2; A2++) { LL += GetProbPhi(NId1, NId2, AId, A1, A2) * Mtx.At(A1, A2); } } return log(LL); }
const void TMAGFitBern::GradAffMtx | ( | const int & | AId, |
const TFltVV & | ProdVV, | ||
const TFltVV & | SqVV, | ||
const TMAGAffMtx & | CurMtx, | ||
TFltV & | GradV | ||
) |
Definition at line 1320 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); // const int NAttrs = Param.GetAttrs(); GradV.PutAll(0.0); for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NNodes; j++) { double Prod = ProdVV(i, j) - GetThetaLL(i, j, AId); double Sq = SqVV(i, j) - GetSqThetaLL(i, j, AId); for(int p = 0; p < 4; p++) { int Ai = p / 2; int Aj = p % 2; double Prob = GetProbPhi(i, j, AId, Ai, Aj); if(Graph->IsEdge(i, j)) { GradV[p] += Prob / CurMtx.At(p); } else { GradV[p] -= Prob * exp(Prod); GradV[p] -= Prob * exp(Sq) * CurMtx.At(p); } } } } }
const void TMAGFitBern::GradApxAffMtx | ( | const int & | AId, |
const TFltVV & | ProdVV, | ||
const TFltVV & | SqVV, | ||
const TMAGAffMtx & | CurMtx, | ||
TFltV & | GradV | ||
) |
Definition at line 1346 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); // const int NAttrs = Param.GetAttrs(); // const int NSq = NNodes * (NNodes - 1); GradV.PutAll(0.0); TFltV LogSumV; for(int p = 0; p < 4; p++) { int Ai = p / 2; int Aj = p % 2; LogSumV.Gen(NNodes * 4, 0); for(int i = 0; i < NNodes; i++) { const double LProd = ProdVV(i, 0) - GetAvgThetaLL(i, i, AId, true, false); const double LSq = SqVV(i, 0) - GetAvgSqThetaLL(i, i, AId, true, false); const double RProd = ProdVV(i, 1) - GetAvgThetaLL(i, i, AId, false, true); const double RSq = SqVV(i, 1) - GetAvgSqThetaLL(i, i, AId, false, true); LogSumV.Add(LProd + log(GetProbMu(i, i, AId, Ai, Aj, true, false))); LogSumV.Add(LSq + log(GetProbMu(i, i, AId, Ai, Aj, true, false)) + log(CurMtx.At(p))); LogSumV.Add(RProd + log(GetProbMu(i, i, AId, Ai, Aj, false, true))); LogSumV.Add(RSq + log(GetProbMu(i, i, AId, Ai, Aj, false, true)) + log(CurMtx.At(p))); } double LogSum = LogSumExp(LogSumV); GradV[p] -= (NNodes - 1) * 0.5 * exp(LogSum); } for(TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { const int NId1 = EI.GetSrcNId(); const int NId2 = EI.GetDstNId(); const double ProdOne = GetProdLinWeight(NId1, NId2) - GetThetaLL(NId1, NId2, AId); const double SqOne = GetProdSqWeight(NId1, NId2) - GetSqThetaLL(NId1, NId2, AId); for(int p = 0; p < 4; p++) { int Ai = p / 2; int Aj = p % 2; double Prob = GetProbPhi(NId1, NId2, AId, Ai, Aj); GradV[p] += Prob / CurMtx.At(p); GradV[p] += Prob * exp(ProdOne); GradV[p] += Prob * exp(SqOne) * CurMtx.At(p); } } #if 0 const double Prod = ProdVV(0, 0) - GetAvgThetaLL(0, 0, AId, false, false); const double Sq = SqVV(0, 0) - GetAvgSqThetaLL(0, 0, AId, false, false); for(int p = 0; p < 4; p++) { int Ai = p / 2; int Aj = p % 2; GradV[p] -= NSq * exp(Prod) * GetProbMu(0, 0, AId, Ai, Aj, false, false); GradV[p] -= NSq * exp(Sq) * GetProbMu(0, 0, AId, Ai, Aj, false, false) * CurMtx.At(p); } for(TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) { const int NId1 = EI.GetSrcNId(); const int NId2 = EI.GetDstNId(); const double ProdOne = GetProdLinWeight(NId1, NId2) - GetThetaLL(NId1, NId2, AId); const double SqOne = GetProdSqWeight(NId1, NId2) - GetSqThetaLL(NId1, NId2, AId); for(int p = 0; p < 4; p++) { int Ai = p / 2; int Aj = p % 2; double Prob = GetProbPhi(NId1, NId2, AId, Ai, Aj); // GradV[p] += Prob / CurMtx.At(p); // GradV[p] += Prob * exp(ProdOne); // GradV[p] += Prob * exp(SqOne) * CurMtx.At(p); } } #endif }
const double TMAGFitBern::GradPhiMI | ( | const double & | x, |
const int & | NId, | ||
const int & | AId, | ||
const double & | Lambda, | ||
const double & | DeltaQ, | ||
const TFltVV & | CntVV | ||
) |
Definition at line 703 of file mag.cpp.
{ const int NAttrs = CntVV.GetYDim(); double Grad = DeltaQ - log(x) + log(1.0-x); for(int l = 0; l < NAttrs; l++) { if(l == AId) { continue; } const double C0 = PhiVV(NId, l); const double C1 = 1.0 - C0; Grad -= Lambda * C0 * log(CntVV(0, l) + C0 * x); Grad -= Lambda * C1 * log(CntVV(1, l) + C1 * x); Grad += Lambda * C0 * log(CntVV(2, l) + C0 * (1-x)); Grad += Lambda * C1 * log(CntVV(3, l) + C1 * (1-x)); Grad -= Lambda * log(CntVV(0, l) + CntVV(1, l) + x); Grad += Lambda * log(CntVV(2, l) + CntVV(3, l) + (1-x)); } return Grad; }
void TMAGFitBern::Init | ( | const TFltV & | MuV, |
const TMAGAffMtxV & | AffMtxV | ||
) |
void TMAGFitBern::MakeCCDF | ( | const TFltPrV & | RawV, |
TFltPrV & | CcdfV | ||
) |
Definition at line 1703 of file mag.cpp.
{ double Total = 0.0; CcdfV.Gen(RawV.Len(), 0); for(int i = 0; i < RawV.Len(); i++) { if(RawV[i].Val2 <= 0) { continue; } Total += RawV[i].Val2; CcdfV.Add(RawV[i]); IAssert(RawV[i].Val2 > 0); } for(int i = 1; i < CcdfV.Len(); i++) { CcdfV[i].Val2 += CcdfV[i-1].Val2; } for(int i = CcdfV.Len() - 1; i > 0; i--) { CcdfV[i].Val2 = (Total - CcdfV[i-1].Val2) ; if(CcdfV[i].Val2 <= 0) { printf("CCDF = %f\n", double(CcdfV[i].Val2));} IAssert(CcdfV[i].Val2 > 0); } CcdfV[0].Val2 = Total; // CcdfV[0].Val2 = 1.0; }
const bool TMAGFitBern::NextPermutation | ( | TIntV & | IndexV | ) | const [private] |
Definition at line 1867 of file mag.cpp.
{ const int NAttrs = IndexV.Len(); int Pos = NAttrs - 1; while(Pos > 0) { if(IndexV[Pos-1] < IndexV[Pos]) { break; } Pos--; } if(Pos == 0) { return false; } int Val = NAttrs, NewPos = -1; for(int i = Pos; i < NAttrs; i++) { if(IndexV[i] > IndexV[Pos - 1] && IndexV[i] < Val) { NewPos = i; Val = IndexV[i]; } } IndexV[NewPos] = IndexV[Pos - 1]; IndexV[Pos - 1] = Val; TIntV SubIndexV; IndexV.GetSubValV(Pos, NAttrs - 1, SubIndexV); SubIndexV.Sort(true); for(int i = Pos; i < NAttrs; i++) { IndexV[i] = SubIndexV[i - Pos]; } return true; }
void TMAGFitBern::NormalizeAffMtxV | ( | TMAGAffMtxV & | MtxV, |
const bool | UseMu = false |
||
) |
Definition at line 1454 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = MtxV.Len(); TFltV MuV = GetMuV(); double Product = 1.0, ExpEdge = NNodes * (NNodes - 1); TFltV SumV(NAttrs), EdgeSumV(NAttrs); SumV.PutAll(0.0); EdgeSumV.PutAll(0.0); for(int l = 0; l < NAttrs; l++) { double Mu = (UseMu) ? double(MuV[l]) : (AvgPhiV[l] / double(NNodes)); EdgeSumV[l] += Mu * Mu * MtxV[l].At(0, 0); EdgeSumV[l] += Mu * (1.0-Mu) * MtxV[l].At(0, 1); EdgeSumV[l] += Mu * (1.0-Mu) * MtxV[l].At(1, 0); EdgeSumV[l] += (1.0-Mu) * (1.0-Mu) * MtxV[l].At(1, 1); SumV[l] = SumV[l] + MtxV[l].At(0, 0); SumV[l] = SumV[l] + MtxV[l].At(0, 1); SumV[l] = SumV[l] + MtxV[l].At(1, 0); SumV[l] = SumV[l] + MtxV[l].At(1, 1); Product *= SumV[l]; ExpEdge *= EdgeSumV[l]; } ExpEdge = Graph->GetEdges() / ExpEdge; NormConst *= Product; // NormConst = ExpEdge; Product = 1.0; // Product = pow(Product * ExpEdge, 1.0 / double(NAttrs)); for(int l = 0; l < NAttrs; l++) { for(int p = 0; p < 4; p++) { MtxV[l].At(p) = MtxV[l].At(p) * Product / SumV[l]; // MtxV[l].At(p) = MtxV[l].At(p) * Product / MtxV[l].At(0, 0); // MtxV[l].At(p) = MtxV[l].At(p) * Product; // if(MtxV[l].At(p) > 0.9999) { MtxV[l].At(p) = 0.9999; } // if(MtxV[l].At(p) < 0.0001) { MtxV[l].At(p) = 0.0001; } } } }
const double TMAGFitBern::ObjPhiMI | ( | const double & | x, |
const int & | NId, | ||
const int & | AId, | ||
const double & | Lambda, | ||
const double & | Q0, | ||
const double & | Q1, | ||
const TFltVV & | CntVV | ||
) |
Definition at line 722 of file mag.cpp.
{ const int NAttrs = CntVV.GetYDim(); double Val = x*(Q0 - log(x)) + (1-x)*(Q1 - log(1.0-x)); for(int l = 0; l < NAttrs; l++) { if(l == AId) { continue; } const double C0 = PhiVV(NId, l); const double C1 = 1.0 - C0; Val -= Lambda * (CntVV(0, l) + C0 * x) * log(CntVV(0, l) + C0 * x); Val -= Lambda * (CntVV(1, l) + C1 * x) * log(CntVV(1, l) + C1 * x); Val -= Lambda * (CntVV(2, l) + C0 * (1-x)) * log(CntVV(2, l) + C0 * (1-x)); Val -= Lambda * (CntVV(3, l) + C1 * (1-x)) * log(CntVV(3, l) + C1 * (1-x)); Val += Lambda * (CntVV(0, l) + CntVV(1, l) + x) * log(CntVV(0, l) + CntVV(1, l) + x); Val += Lambda * (CntVV(2, l) + CntVV(3, l) + 1 - x) * log(CntVV(2, l) + CntVV(3, l) + (1-x)); if(!(CntVV(0, l) > 0)) printf("CntVV(0, %d) = %.2f\n", l, double(CntVV(0, l))); if(!(CntVV(1, l) > 0)) printf("CntVV(1, %d) = %.2f\n", l, double(CntVV(1, l))); if(!(CntVV(2, l) > 0)) printf("CntVV(2, %d) = %.2f\n", l, double(CntVV(2, l))); if(!(CntVV(3, l) > 0)) printf("CntVV(3, %d) = %.2f\n", l, double(CntVV(3, l))); } return Val; }
void TMAGFitBern::PlotProperties | ( | const TStr & | FNm | ) |
Definition at line 1727 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); TMAGParam<TMAGNodeBern> MAGGen(NNodes, NAttrs); TMAGNodeBern MAGNode = Param.GetNodeAttr(); MAGGen.SetNodeAttr(MAGNode); TMAGAffMtxV MtxV; Param.GetMtxV(MtxV); MAGGen.SetMtxV(MtxV); PNGraph TrG = new TNGraph; *TrG = *Graph; TIntVV AttrVV(NNodes, NAttrs); for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NAttrs; j++) { if(PhiVV(i, j) > TMAGNodeBern::Rnd.GetUniDev()) AttrVV(i, j) = 0; else AttrVV(i, j) = 1; } } PNGraph MAG = MAGGen.GenMAG(AttrVV, true, 10000); // PNGraph MAG = MAGGen.GenAttrMAG(AttrVV, true, 10000); printf("%d edges created for MAG...\n", MAG->GetEdges()); TSnap::DelZeroDegNodes(TrG); TSnap::DelZeroDegNodes(MAG); TGStatVec GS(tmuNodes, TFSet() | gsdInDeg | gsdOutDeg | gsdWcc | gsdHops | gsdClustCf | gsdSngVec | gsdSngVal | gsdTriadPart); TGnuPlot InDegP(FNm + "-InDeg"), OutDegP(FNm + "-OutDeg"), SvalP(FNm + "-Sval"), SvecP(FNm + "-Svec"), WccP(FNm + "-Wcc"), HopP(FNm + "-Hop"), TriadP(FNm + "-Triad"), CcfP(FNm + "-Ccf");; InDegP.SetXYLabel("Degree", "# of nodes"); OutDegP.SetXYLabel("Degree", "# of nodes"); SvalP.SetXYLabel("Rank", "Singular value"); SvecP.SetXYLabel("Rank", "Primary SngVec component"); WccP.SetXYLabel("Size of component", "# of components"); CcfP.SetXYLabel("Degree", "Clustering coefficient"); HopP.SetXYLabel("Hops", "# of node pairs"); TriadP.SetXYLabel("# of triads", "# of participating nodes"); InDegP.SetScale(gpsLog10XY); InDegP.AddCmd("set key top right"); OutDegP.SetScale(gpsLog10XY); OutDegP.AddCmd("set key top right"); SvalP.SetScale(gpsLog10XY); SvalP.AddCmd("set key top right"); SvecP.SetScale(gpsLog10XY); SvecP.AddCmd("set key top right"); CcfP.SetScale(gpsLog10XY); CcfP.AddCmd("set key top right"); HopP.SetScale(gpsLog10XY); HopP.AddCmd("set key top right"); TriadP.SetScale(gpsLog10XY); TriadP.AddCmd("set key top right"); InDegP.ShowGrid(false); OutDegP.ShowGrid(false); SvalP.ShowGrid(false); SvecP.ShowGrid(false); CcfP.ShowGrid(false); HopP.ShowGrid(false); TriadP.ShowGrid(false); const TStr Style[2] = {"lt 1 lw 3 lc rgb 'black'", "lt 2 lw 3 lc rgb 'red'"}; const TStr Name[2] = {"Real", "MAG"}; GS.Add(Graph, TSecTm(1), "Real Graph"); GS.Add(MAG, TSecTm(2), "MAG"); TFltPrV InDegV, OutDegV, SvalV, SvecV, HopV, WccV, CcfV, TriadV; for(int i = 0; i < GS.Len(); i++) { MakeCCDF(GS.At(i)->GetDistr(gsdInDeg), InDegV); MakeCCDF(GS.At(i)->GetDistr(gsdOutDeg), OutDegV); SvalV = GS.At(i)->GetDistr(gsdSngVal); SvecV = GS.At(i)->GetDistr(gsdSngVec); MakeCCDF(GS.At(i)->GetDistr(gsdClustCf), CcfV); HopV = GS.At(i)->GetDistr(gsdHops); MakeCCDF(GS.At(i)->GetDistr(gsdTriadPart), TriadV); InDegP.AddPlot(InDegV, gpwLines, Name[i], Style[i]); OutDegP.AddPlot(OutDegV, gpwLines, Name[i], Style[i]); SvalP.AddPlot(SvalV, gpwLines, Name[i], Style[i]); SvecP.AddPlot(SvecV, gpwLines, Name[i], Style[i]); CcfP.AddPlot(CcfV, gpwLines, Name[i], Style[i]); HopP.AddPlot(HopV, gpwLines, Name[i], Style[i]); TriadP.AddPlot(TriadV, gpwLines, Name[i], Style[i]); } InDegP.SaveEps(30); OutDegP.SaveEps(30); SvalP.SaveEps(30); SvecP.SaveEps(30); CcfP.SaveEps(30); HopP.SaveEps(30); TriadP.SaveEps(30); }
const void TMAGFitBern::PrepareUpdateAffMtx | ( | TFltVV & | ProdVV, |
TFltVV & | SqVV | ||
) |
Definition at line 1532 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); ProdVV.Gen(NNodes, NNodes); SqVV.Gen(NNodes, NNodes); for(int i = 0; i < NNodes; i++) { for(int j = 0; j < NNodes; j++) { ProdVV(i, j) = GetProdLinWeight(i, j); SqVV(i, j) = GetProdSqWeight(i, j); } } }
const void TMAGFitBern::PrepareUpdateApxAffMtx | ( | TFltVV & | ProdVV, |
TFltVV & | SqVV | ||
) |
Definition at line 1545 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); ProdVV.Gen(NNodes, 2); SqVV.Gen(NNodes, 2); for(int i = 0; i < NNodes; i++) { ProdVV(i, 0) = GetAvgProdLinWeight(i, i, true, false); ProdVV(i, 1) = GetAvgProdLinWeight(i, i, false, true); SqVV(i, 0) = GetAvgProdSqWeight(i, i, true, false); SqVV(i, 1) = GetAvgProdSqWeight(i, i, false, true); } }
void TMAGFitBern::RandomInit | ( | const TFltV & | MuV, |
const TMAGAffMtxV & | AffMtxV, | ||
const int & | Seed | ||
) |
Definition at line 510 of file mag.cpp.
{ TRnd& Rnd = TMAGNodeBern::Rnd; Rnd.PutSeed(Seed); TFltV InitMuV = MuV; InitMuV.PutAll(0.5); TMAGNodeBern DistParam(InitMuV); Param.SetMtxV(AffMtxV); const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); PhiVV.Gen(NNodes, NAttrs); KnownVV.Gen(NNodes, NAttrs); KnownVV.PutAll(false); for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { PhiVV.At(i, l) = Rnd.GetUniDev(); // PhiVV.At(i, l) = 0.5; } } TMAGAffMtxV RndMtxV = AffMtxV; for(int l = 0; l < NAttrs; l++) { for(int p = 0; p < 4; p++) { RndMtxV[l].At(p) = TMAGNodeBern::Rnd.GetUniDev(); if(RndMtxV[l].At(p) < 0.1) { RndMtxV[l].At(p) = 0.1; } if(RndMtxV[l].At(p) > 0.9) { RndMtxV[l].At(p) = 0.9; } } RndMtxV[l].At(0, 1) = RndMtxV[l].At(1, 0); } printf("\n"); for(int l = 0; l < NAttrs; l++) { printf("AffMtx = %s\n", RndMtxV[l].GetMtxStr().GetCStr()); } Param.SetMtxV(RndMtxV); Param.SetNodeAttr(DistParam); }
void TMAGFitBern::SaveTxt | ( | const TStr & | FNm | ) |
Definition at line 423 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); const TFltV MuV = GetMuV(); TMAGAffMtxV MtxV; Param.GetMtxV(MtxV); FILE *fp = fopen(FNm.GetCStr(), "w"); for(int l = 0; l < NAttrs; l++) { fprintf(fp, "%.4f\t", double(MuV[l])); for(int row = 0; row < 2; row++) { for(int col = 0; col < 2; col++) { fprintf(fp, " %.4f", double(MtxV[l].At(row, col))); } fprintf(fp, (row == 0) ? ";" : "\n"); } } fclose(fp); fp = fopen((FNm + "f").CStr(), "w"); for(int i = 0; i < NNodes; i++) { for(int l = 0; l < NAttrs; l++) { fprintf(fp, "%f ", double(PhiVV(i, l))); } fprintf(fp, "\n"); } fclose(fp); }
void TMAGFitBern::SetAlgConf | ( | const bool | EStep = true , |
const bool | MStep = true |
||
) | [inline] |
void TMAGFitBern::SetDebug | ( | const bool | _Debug | ) | [inline] |
void TMAGFitBern::SetGraph | ( | const PNGraph & | GraphPt | ) |
Definition at line 385 of file mag.cpp.
{ Graph = GraphPt; bool NodesOk = true; // check that nodes IDs are {0,1,..,Nodes-1} for (int nid = 0; nid < Graph->GetNodes(); nid++) { if (! Graph->IsNode(nid)) { NodesOk=false; break; } } if (! NodesOk) { TIntV NIdV; GraphPt->GetNIdV(NIdV); Graph = TSnap::GetSubGraph(GraphPt, NIdV, true); for (int nid = 0; nid < Graph->GetNodes(); nid++) { IAssert(Graph->IsNode(nid)); } } }
void TMAGFitBern::SetMtxV | ( | const TMAGAffMtxV & | MtxV | ) | [inline] |
void TMAGFitBern::SetMuV | ( | const TFltV & | MuV | ) | [inline] |
Definition at line 380 of file mag.h.
{ TMAGNodeBern Dist = Param.GetNodeAttr(); Dist.SetMuV(MuV); Param.SetNodeAttr(Dist); }
void TMAGFitBern::SetPhiVV | ( | const TIntVV & | AttrVV, |
const int | KnownIds = 0 |
||
) |
Definition at line 399 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); PhiVV.Gen(NNodes, NAttrs); KnownVV.Gen(NNodes, NAttrs); for(int l = 0; l < NAttrs; l++) { for(int i = 0; i < NNodes; i++) { if(int(AttrVV(i, l)) == 0) { PhiVV(i, l) = 0.9999; } else { PhiVV(i, l) = 0.0001; } } if(l < KnownIds) { KnownVV.PutY(l, true); } else { KnownVV.PutY(l, false); } } }
void TMAGFitBern::SetPhiVV | ( | const TFltVV & | AttrVV, |
const int | KnownIds = 0 |
||
) | [inline] |
void TMAGFitBern::SortAttrOrdering | ( | const TFltV & | TrueMuV, |
TIntV & | IndexV | ||
) | const |
Definition at line 1828 of file mag.cpp.
{ const int NAttrs = TrueMuV.Len(); // const int NNodes = PhiVV.GetXDim(); TFltV EstMuV, SortedTrueMuV, SortedEstMuV, TrueIdxV, EstIdxV; IndexV.Gen(NAttrs); TrueIdxV.Gen(NAttrs); EstIdxV.Gen(NAttrs); for(int l = 0; l < NAttrs; l++) { TrueIdxV[l] = l; EstIdxV[l] = l; } CountAttr(EstMuV); SortedTrueMuV = TrueMuV; SortedEstMuV = EstMuV; for(int i = 0; i < NAttrs; i++) { if(SortedTrueMuV[i] > 0.5) { SortedTrueMuV[i] = 1.0 - SortedTrueMuV[i]; } if(SortedEstMuV[i] > 0.5) { SortedEstMuV[i] = 1.0 - SortedEstMuV[i]; } } for(int i = 0; i < NAttrs; i++) { for(int j = i+1; j < NAttrs; j++) { if(SortedTrueMuV[i] < SortedTrueMuV[j]) { SortedTrueMuV.Swap(i, j); TrueIdxV.Swap(i, j); } if(SortedEstMuV[i] < SortedEstMuV[j]) { EstIdxV.Swap((int)SortedEstMuV[i], (int)SortedEstMuV[j]); SortedEstMuV.Swap(i, j); } } } for(int l = 0; l < NAttrs; l++) { IndexV[l] = (int)TrueIdxV[(int)EstIdxV[l]]; } }
void TMAGFitBern::UnNormalizeAffMtxV | ( | TMAGAffMtxV & | MtxV, |
const bool | UseMu = false |
||
) |
Definition at line 1492 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = MtxV.Len(); TFltIntPrV MaxEntV(NAttrs); TFltV MuV = GetMuV(); NormalizeAffMtxV(MtxV, UseMu); double ExpEdge = NNodes * (NNodes - 1); for(int l = 0; l < NAttrs; l++) { double Mu = MuV[l]; double EdgeSum = Mu * Mu * MtxV[l].At(0, 0); EdgeSum += Mu * (1.0-Mu) * MtxV[l].At(0, 1); EdgeSum += Mu * (1.0-Mu) * MtxV[l].At(1, 0); EdgeSum += (1.0-Mu) * (1.0-Mu) * MtxV[l].At(1, 1); ExpEdge *= EdgeSum; } NormConst = double(Graph->GetEdges()) / ExpEdge; // NormConst *= ExpEdge; for(int l = 0; l < NAttrs; l++) { MaxEntV[l] = TFltIntPr(-1, l); for(int p = 0; p < 4; p++) { if(MaxEntV[l].Val1 < MtxV[l].At(p)) { MaxEntV[l].Val1 = MtxV[l].At(p); } } } MaxEntV.Sort(false); for(int l = 0; l < NAttrs; l++) { int CurId = MaxEntV[l].Val2; double Factor = pow(NormConst, 1.0 / double(NAttrs - l)); double MaxFactor = 0.9999 / MaxEntV[l].Val1; Factor = (Factor > MaxFactor) ? MaxFactor : Factor; NormConst = NormConst / Factor; for(int p = 0; p < 4; p++) { MtxV[CurId].At(p) = MtxV[CurId].At(p) * Factor; } } }
const double TMAGFitBern::UpdateAffMtx | ( | const int & | AId, |
const double & | LrnRate, | ||
const double & | MaxGrad, | ||
const double & | Lambda, | ||
TFltVV & | ProdVV, | ||
TFltVV & | SqVV, | ||
TMAGAffMtx & | NewMtx | ||
) |
Definition at line 1418 of file mag.cpp.
{ double Delta = 0.0; // const int NNodes = Param.GetNodes(); // const int NAttrs = Param.GetAttrs(); TMAGAffMtx AffMtx = Param.GetMtx(AId); TFltV GradV(4); TFltV HessV(4); if(MSpeedUp) { GradApxAffMtx(AId, ProdVV, SqVV, NewMtx, GradV); } else { GradAffMtx(AId, ProdVV, SqVV, NewMtx, GradV); } double Ratio = 1.0; for(int p = 0; p < 4; p++) { if(fabs(Ratio * LrnRate * GradV[p]) > MaxGrad) { Ratio = MaxGrad / fabs(LrnRate * GradV[p]); } } for(int p = 0; p < 4; p++) { GradV[p] *= (Ratio * LrnRate); NewMtx.At(p) = AffMtx.At(p) + GradV[p]; // if(NewMtx.At(p) > 0.9999) { NewMtx.At(p) = 0.9999; } if(NewMtx.At(p) < 0.0001) { NewMtx.At(p) = 0.0001; } } printf(" [Attr = %d]\n", AId); printf(" %s + [%f, %f; %f %f] -----> %s\n", (AffMtx.GetMtxStr()).GetCStr(), double(GradV[0]), double(GradV[1]), double(GradV[2]), double(GradV[3]), (NewMtx.GetMtxStr()).GetCStr()); // Param.SetMtx(AId, NewMtx); return Delta; }
const double TMAGFitBern::UpdateAffMtxV | ( | const int & | GradIter, |
const double & | LrnRate, | ||
const double & | MaxGrad, | ||
const double & | Lambda, | ||
const int & | NReal = 0 |
||
) |
Definition at line 1558 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); const TMAGNodeBern DistParam = Param.GetNodeAttr(); const TFltV MuV = DistParam.GetMuV(); double Delta = 0.0; double DecLrnRate = LrnRate, DecMaxGrad = MaxGrad; TFltVV ProdVV(NNodes, NNodes), SqVV(NNodes, NNodes); TMAGAffMtxV NewMtxV, OldMtxV; Param.GetMtxV(OldMtxV); Param.GetMtxV(NewMtxV); for(int g = 0; g < GradIter; g++) { if(MSpeedUp) { PrepareUpdateApxAffMtx(ProdVV, SqVV); } else { PrepareUpdateAffMtx(ProdVV, SqVV); } printf(" [Grad step = %d]\n", (g+1)); // for(int l = 0; l < NAttrs; l++) { for(int l = NReal; l < NAttrs; l++) { UpdateAffMtx(l, DecLrnRate, DecMaxGrad, Lambda, ProdVV, SqVV, NewMtxV[l]); Param.SetMtxV(NewMtxV); } DecLrnRate *= 0.97; DecMaxGrad *= 0.97; printf("\n"); NormalizeAffMtxV(NewMtxV, true); Param.SetMtxV(NewMtxV); } NormalizeAffMtxV(NewMtxV, true); printf( "\nFinal\n"); for(int l = 0; l < NAttrs; l++) { printf(" ["); for(int p = 0; p < 4; p++) { // NewMtxV[l].At(p) = NewMtxV[l].At(p) * Product / SumV[l]; Delta += fabs(OldMtxV[l].At(p) - NewMtxV[l].At(p)); printf(" %.4f ", double(NewMtxV[l].At(p))); } printf("]\n"); } Param.SetMtxV(NewMtxV); ProdVV.Clr(); SqVV.Clr(); return Delta; }
const double TMAGFitBern::UpdateApxPhiMI | ( | const double & | Lambda, |
const int & | NId, | ||
const int & | AId, | ||
double & | Phi, | ||
TFltVV & | ProdVV | ||
) |
Definition at line 930 of file mag.cpp.
{ TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId); const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); Theta.GetLLMtx(LLTheta); TMAGNodeBern DistParam = Param.GetNodeAttr(); const double Mu = DistParam.GetMu(AId); TMAGAffMtx SqTheta(Theta); for(int i = 0; i < Theta.Len(); i++) { SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i); } TFltV ProdV; ProdVV.GetRow(NId, ProdV); ProdV[0] -= GetAvgThetaLL(NId, NId, AId, true, false); ProdV[1] -= GetAvgThetaLL(NId, NId, AId, false, true); ProdV[2] -= log(2.0) + GetAvgSqThetaLL(NId, NId, AId, true, false); ProdV[3] -= log(2.0) + GetAvgSqThetaLL(NId, NId, AId, false, true); // Using log-sum-exp trick double EdgeQ[2], MaxExp[2]; TFltV NonEdgeLLV[2]; TFltVV CntVV(4, NAttrs); CntVV.PutAll(0.0); for(int i = 0; i < 2; i++) { EdgeQ[i] = 0.0; MaxExp[i] = -DBL_MAX; NonEdgeLLV[i].Gen(4 * NNodes, 0); } for(int F = 0; F < 2; F++) { NonEdgeLLV[F].Add(ProdV[0] + log(GetAvgOutCoeff(NId, AId, F, Theta))); NonEdgeLLV[F].Add(ProdV[1] + log(GetAvgInCoeff(NId, AId, F, Theta))); NonEdgeLLV[F].Add(ProdV[2] + log(GetAvgOutCoeff(NId, AId, F, SqTheta))); NonEdgeLLV[F].Add(ProdV[3] + log(GetAvgInCoeff(NId, AId, F, SqTheta))); } EdgeQ[0] = -(NNodes - 1) * exp(LogSumExp(NonEdgeLLV[0])); EdgeQ[1] = -(NNodes - 1) * exp(LogSumExp(NonEdgeLLV[1])); for(int l = 0; l < NAttrs; l++) { if(l == AId) { continue; } int BgId = (AId > l) ? AId : l; int SmId = (AId + l) - BgId; int SmL = (l < AId) ? 1 : 0; BgId *= 4; CntVV(0, l) = AvgPhiPairVV(SmId, BgId) - PhiVV(NId, AId) * PhiVV(NId, l); CntVV(1+SmL, l) = AvgPhiPairVV(SmId, BgId+1+SmL) - PhiVV(NId, AId) * (1.0-PhiVV(NId, l)); CntVV(2-SmL, l) = AvgPhiPairVV(SmId, BgId+2-SmL) - (1.0-PhiVV(NId, AId)) * PhiVV(NId, l); CntVV(3, l) = AvgPhiPairVV(SmId, BgId+3) - (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, l)); } TNGraph::TNodeI NI = Graph->GetNI(NId); for(int d = 0; d < NI.GetOutDeg(); d++) { int Out = NI.GetOutNId(d); if(NId == Out) { continue; } double LinW = GetProdLinWeight(NId, Out) - GetThetaLL(NId, Out, AId); double SqW = GetProdSqWeight(NId, Out) - GetSqThetaLL(NId, Out, AId); for(int F = 0; F < 2; F++) { EdgeQ[F] += GetOutCoeff(NId, Out, AId, F, LLTheta); EdgeQ[F] += exp(LinW + log(GetOutCoeff(NId, Out, AId, F, Theta))); EdgeQ[F] += 0.5 * exp(SqW + log(GetOutCoeff(NId, Out, AId, F, SqTheta))); } } for(int d = 0; d < NI.GetInDeg(); d++) { int In = NI.GetInNId(d); if(NId == In) { continue; } double LinW = GetProdLinWeight(In, NId) - GetThetaLL(In, NId, AId); double SqW = GetProdSqWeight(In, NId) - GetSqThetaLL(In, NId, AId); for(int F = 0; F < 2; F++) { EdgeQ[F] += GetInCoeff(In, NId, AId, F, LLTheta); EdgeQ[F] += exp(LinW + log(GetInCoeff(In, NId, AId, F, Theta))); EdgeQ[F] += 0.5 * exp(SqW + log(GetInCoeff(In, NId, AId, F, SqTheta))); } } EdgeQ[0] += log(Mu); EdgeQ[1] += log(1.0 - Mu); double DeltaQ = EdgeQ[0] - EdgeQ[1]; // printf("(%d, %d) :: Q[0] = %f, Q[1] = %f\n", NId, AId, EdgeQ[0], EdgeQ[1]); // double x[] = {0.1, 0.3, 0.5, 0.7, 0.9}; double x[] = {PhiVV(NId, AId)}; TFltV ObjValV; ObjValV.Gen(60, 0); // for(int n = 0; n < 5; n++) { for(int n = 0; n < 1; n++) { // double LrnRate = 0.0002; double LrnRate = 0.001; for(int step = 0; step < 50; step++) { // for(int step = 0; step < 10; step++) { double Grad = GradPhiMI(x[n], NId, AId, Lambda, DeltaQ, CntVV); if(Grad > 0.0) { x[n] += LrnRate; } else { x[n] -= LrnRate; } if(x[n] > 0.9999) { x[n] = 0.9999; } if(x[n] < 0.0001) { x[n] = 0.0001; } if(x[n] == 0.9999 || x[n] == 0.0001) { break; } LrnRate *= 0.995; } ObjValV.Add(x[n]); // ObjValV.Add(PhiVV(NId, AId)); } double MaxVal = -DBL_MAX; int MaxX = -1; // for(int n = 0; n < 5; n++) { for(int n = 0; n < ObjValV.Len(); n++) { double Val = ObjPhiMI(ObjValV[n], NId, AId, Lambda, EdgeQ[0], EdgeQ[1], CntVV); if(Val > MaxVal) { MaxVal = Val; MaxX = n; } else if(MaxX < 0) { printf("(%d, %d) : %f Q[0] = %f Q[1] = %f Val = %f\n", NId, AId, double(x[n]), double(EdgeQ[0]), double(EdgeQ[1]), Val); } } IAssert(MaxX >= 0); Phi = ObjValV[MaxX]; return Phi - PhiVV.At(NId, AId); }
const double TMAGFitBern::UpdateMu | ( | const int & | AId | ) |
Definition at line 1299 of file mag.cpp.
{ const int NNodes = Param.GetNodes(); TMAGNodeBern DistParam = Param.GetNodeAttr(); const double OldMu = DistParam.GetMu(AId); double NewMu = 0.0; for(int i = 0; i < NNodes; i++) { NewMu += PhiVV.At(i, AId); } AvgPhiV[AId] = NewMu; NewMu /= double(NNodes); printf(" [Posterior Mu] = %.4f\n", NewMu); double Delta = fabs(NewMu - OldMu); DistParam.SetMu(AId, NewMu); Param.SetNodeAttr(DistParam); return Delta; }
const double TMAGFitBern::UpdatePhi | ( | const int & | NId, |
const int & | AId, | ||
double & | Phi | ||
) |
Definition at line 756 of file mag.cpp.
{ TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId); TMAGAffMtx SqTheta(Theta); const int NNodes = Param.GetNodes(); // const int NAttrs = Param.GetAttrs(); Theta.GetLLMtx(LLTheta); TMAGNodeBern DistParam = Param.GetNodeAttr(); const double Mu = DistParam.GetMu(AId); for(int i = 0; i < Theta.Len(); i++) { SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i); } // Using log-sum-exp trick double EdgeQ[2], NonEdgeQ[2], MaxExp[2]; TFltV NonEdgeLLV[2]; for(int i = 0; i < 2; i++) { EdgeQ[i] = 0.0; NonEdgeQ[i] = 0.0; MaxExp[i] = -DBL_MAX; NonEdgeLLV[i].Gen(4 * NNodes, 0); } for(int j = 0; j < NNodes; j++) { if(j == NId) { continue; } if(Graph->IsEdge(NId, j)) { EdgeQ[0] += GetOutCoeff(NId, j, AId, 0, LLTheta); EdgeQ[1] += GetOutCoeff(NId, j, AId, 1, LLTheta); } else { double LinW = GetProdLinWeight(NId, j) - GetThetaLL(NId, j, AId); double SqW = GetProdSqWeight(NId, j) - GetSqThetaLL(NId, j, AId); for(int i = 0; i < 2; i++) { NonEdgeLLV[i].Add(LinW + log(GetOutCoeff(NId, j, AId, i, Theta))); NonEdgeLLV[i].Add(SqW + log(GetOutCoeff(NId, j, AId, i, SqTheta)) + log(0.5)); } } if(Graph->IsEdge(j, NId)) { EdgeQ[0] += GetInCoeff(j, NId, AId, 0, LLTheta); EdgeQ[1] += GetInCoeff(j, NId, AId, 1, LLTheta); } else { double LinW = GetProdLinWeight(j, NId) - GetThetaLL(j, NId, AId); double SqW = GetProdSqWeight(j, NId) - GetSqThetaLL(j, NId, AId); for(int i = 0; i < 2; i++) { NonEdgeLLV[i].Add(LinW + log(GetInCoeff(j, NId, AId, i, Theta))); NonEdgeLLV[i].Add(SqW + log(GetInCoeff(j, NId, AId, i, SqTheta)) + log(0.5)); } } } NonEdgeQ[0] = LogSumExp(NonEdgeLLV[0]); NonEdgeQ[1] = LogSumExp(NonEdgeLLV[1]); double Q[2]; Q[0] = log(Mu) + EdgeQ[0] - exp(NonEdgeQ[0]); Q[1] = log(1.0 - Mu) + EdgeQ[1] - exp(NonEdgeQ[1]); // double Q = Q1 - Q0; // printf(" [Phi_{%d}{%d}] :: Q0 = %f, Q1 = %f\n", NId, AId, Q0, Q1); // Phi = 1.0 / (1.0 + exp(Q)); Phi = Q[0] - LogSumExp(Q, 2); Phi = exp(Phi); return Phi - PhiVV.At(NId, AId); }
const double TMAGFitBern::UpdatePhiMI | ( | const double & | Lambda, |
const int & | NId, | ||
const int & | AId, | ||
double & | Phi | ||
) |
Definition at line 826 of file mag.cpp.
{ TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId); TMAGAffMtx SqTheta(Theta); const int NNodes = Param.GetNodes(); const int NAttrs = Param.GetAttrs(); Theta.GetLLMtx(LLTheta); TMAGNodeBern DistParam = Param.GetNodeAttr(); const double Mu = DistParam.GetMu(AId); for(int i = 0; i < Theta.Len(); i++) { SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i); } // Using log-sum-exp trick double EdgeQ[2], NonEdgeQ[2], MaxExp[2]; TFltV NonEdgeLLV[2]; TFltVV CntVV(4, NAttrs); CntVV.PutAll(0.0); for(int i = 0; i < 2; i++) { EdgeQ[i] = 0.0; NonEdgeQ[i] = 0.0; MaxExp[i] = -DBL_MAX; NonEdgeLLV[i].Gen(4 * NNodes, 0); } for(int j = 0; j < NNodes; j++) { if(j == NId) { continue; } for(int l = 0; l < NAttrs; l++) { if(l == AId) { continue; } CntVV(0, l) = CntVV(0, l) + PhiVV(j, AId) * PhiVV(j, l); CntVV(1, l) = CntVV(1, l) + PhiVV(j, AId) * (1.0-PhiVV(j, l)); CntVV(2, l) = CntVV(2, l) + (1.0-PhiVV(j, AId)) * PhiVV(j, l); CntVV(3, l) = CntVV(3, l) + (1.0-PhiVV(j, AId)) * (1.0-PhiVV(j, l)); } if(Graph->IsEdge(NId, j)) { EdgeQ[0] += GetOutCoeff(NId, j, AId, 0, LLTheta); EdgeQ[1] += GetOutCoeff(NId, j, AId, 1, LLTheta); } else { double LinW = GetProdLinWeight(NId, j) - GetThetaLL(NId, j, AId); double SqW = GetProdSqWeight(NId, j) - GetSqThetaLL(NId, j, AId); for(int i = 0; i < 2; i++) { NonEdgeLLV[i].Add(LinW + log(GetOutCoeff(NId, j, AId, i, Theta))); NonEdgeLLV[i].Add(SqW + log(GetOutCoeff(NId, j, AId, i, SqTheta)) + log(0.5)); } } if(Graph->IsEdge(j, NId)) { EdgeQ[0] += GetInCoeff(j, NId, AId, 0, LLTheta); EdgeQ[1] += GetInCoeff(j, NId, AId, 1, LLTheta); } else { double LinW = GetProdLinWeight(j, NId) - GetThetaLL(j, NId, AId); double SqW = GetProdSqWeight(j, NId) - GetSqThetaLL(j, NId, AId); for(int i = 0; i < 2; i++) { NonEdgeLLV[i].Add(LinW + log(GetInCoeff(j, NId, AId, i, Theta))); NonEdgeLLV[i].Add(SqW + log(GetInCoeff(j, NId, AId, i, SqTheta)) + log(0.5)); } } } NonEdgeQ[0] = LogSumExp(NonEdgeLLV[0]); NonEdgeQ[1] = LogSumExp(NonEdgeLLV[1]); double Q[2]; Q[0] = log(Mu) + EdgeQ[0] - exp(NonEdgeQ[0]); Q[1] = log(1.0 - Mu) + EdgeQ[1] - exp(NonEdgeQ[1]); double DeltaQ = Q[0] - Q[1]; // double x[] = {0.1, 0.3, 0.5, 0.7, 0.9}; double x[] = {PhiVV(NId, AId)}; // for(int n = 0; n < 5; n++) { for(int n = 0; n < 1; n++) { // double LrnRate = 0.0002; double LrnRate = 0.001; for(int step = 0; step < 200; step++) { double Grad = GradPhiMI(x[n], NId, AId, Lambda, DeltaQ, CntVV); if(Grad > 0.0) { x[n] += LrnRate; } else { x[n] -= LrnRate; } if(x[n] > 0.9999) { x[n] = 0.9999; } if(x[n] < 0.0001) { x[n] = 0.0001; } LrnRate *= 0.995; } } double MaxVal = -DBL_MAX; int MaxX = -1; // for(int n = 0; n < 5; n++) { for(int n = 0; n < 1; n++) { double Val = ObjPhiMI(x[n], NId, AId, Lambda, Q[0], Q[1], CntVV); if(Val > MaxVal) { MaxVal = Val; MaxX = n; } } IAssert(MaxX >= 0); Phi = x[MaxX]; return Phi - PhiVV.At(NId, AId); }
TFltVV TMAGFitBern::AvgPhiPairVV [private] |
TFltV TMAGFitBern::AvgPhiV [private] |
bool TMAGFitBern::Debug [private] |
bool TMAGFitBern::ESpeedUp [private] |
PNGraph TMAGFitBern::Graph [private] |
TBoolVV TMAGFitBern::KnownVV [private] |
TFltV TMAGFitBern::LLHisV [private] |
bool TMAGFitBern::MSpeedUp [private] |
TVec<TMAGAffMtxV> TMAGFitBern::MtxHisV [private] |
TVec<TFltV> TMAGFitBern::MuHisV [private] |
TFlt TMAGFitBern::NormConst [private] |
TMAGParam<TMAGNodeBern> TMAGFitBern::Param [private] |
TFltVV TMAGFitBern::PhiVV [private] |