SNAP Library 4.0, Developer Reference  2017-07-27 13:18:06
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
TMAGFitBern Class Reference

#include <mag.h>

Collaboration diagram for TMAGFitBern:

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 TMAGNodeBernGetNodeAttr () const
 
const TFltVGetMuV () 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 TFltVVGetPhiVV () 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< TMAGNodeBernParam
 
bool ESpeedUp
 
bool MSpeedUp
 
bool Debug
 
TFltV AvgPhiV
 
TFltVV AvgPhiPairVV
 
TFlt NormConst
 
TVec< TFltVMuHisV
 
TVec< TMAGAffMtxVMtxHisV
 
TFltV LLHisV
 

Detailed Description

Definition at line 341 of file mag.h.

Constructor & Destructor Documentation

TMAGFitBern::TMAGFitBern ( )
inline

Definition at line 357 of file mag.h.

357 : PhiVV(), KnownVV(), Graph(), Param(), ESpeedUp(true), MSpeedUp(true), Debug(false), AvgPhiV(), AvgPhiPairVV(), NormConst(1.0) { }
bool Debug
Definition: mag.h:347
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
bool ESpeedUp
Definition: mag.h:347
TBoolVV KnownVV
Definition: mag.h:344
TFlt NormConst
Definition: mag.h:350
TFltVV PhiVV
Definition: mag.h:343
TFltV AvgPhiV
Definition: mag.h:348
PNGraph Graph
Definition: mag.h:345
bool MSpeedUp
Definition: mag.h:347
TFltVV AvgPhiPairVV
Definition: mag.h:349
TMAGFitBern::TMAGFitBern ( const PNGraph G,
const int &  NAttrs 
)
inline

Definition at line 358 of file mag.h.

358 : 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) { }
bool Debug
Definition: mag.h:347
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
bool ESpeedUp
Definition: mag.h:347
TBoolVV KnownVV
Definition: mag.h:344
TFlt NormConst
Definition: mag.h:350
TFltVV PhiVV
Definition: mag.h:343
TFltV AvgPhiV
Definition: mag.h:348
PNGraph Graph
Definition: mag.h:345
bool MSpeedUp
Definition: mag.h:347
TFltVV AvgPhiPairVV
Definition: mag.h:349
TMAGFitBern::TMAGFitBern ( const PNGraph G,
const TStr InitFNm 
)
inline

Definition at line 360 of file mag.h.

References AvgPhiPairVV, AvgPhiV, TVec< TVal, TSizeTy >::Gen(), TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TNGraph::GetNodes(), KnownVV, Param, PhiVV, and SetGraph().

360  : 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  }
bool Debug
Definition: mag.h:347
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
void SetGraph(const PNGraph &GraphPt)
Definition: mag.cpp:385
bool ESpeedUp
Definition: mag.h:347
TBoolVV KnownVV
Definition: mag.h:344
TFlt NormConst
Definition: mag.h:350
TFltVV PhiVV
Definition: mag.h:343
TFltV AvgPhiV
Definition: mag.h:348
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
bool MSpeedUp
Definition: mag.h:347
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246
TFltVV AvgPhiPairVV
Definition: mag.h:349

Here is the call graph for this function:

Member Function Documentation

void TMAGFitBern::Clr ( )
inline

Definition at line 372 of file mag.h.

References TVVec< TVal, TSizeTy >::Clr(), KnownVV, and PhiVV.

372 { PhiVV.Clr(); KnownVV.Clr(); }
void Clr()
Definition: ds.h:2245
TBoolVV KnownVV
Definition: mag.h:344
TFltVV PhiVV
Definition: mag.h:343

Here is the call graph for this function:

const double TMAGFitBern::ComputeApxAdjLL ( ) const

Definition at line 2046 of file mag.cpp.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGAffMtx::GetLLMtx(), TMAGParam< TNodeAttr >::GetMtx(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), GetProbPhi(), GetProdLinWeight(), Graph, TNGraph::IsEdge(), Param, PhiVV, and TVec< TVal, TSizeTy >::PutAll().

2046  {
2047  double LL = 0.0;
2048  // double LLSelf = 0.0;
2049  const int NNodes = Param.GetNodes();
2050  const int NAttrs = Param.GetAttrs();
2051  TMAGNodeBern NodeAttr = Param.GetNodeAttr();
2052  TFltV MuV = NodeAttr.GetMuV();
2053  MuV.PutAll(0.0);
2054  TMAGAffMtxV LLMtxV(NAttrs);
2055  double TotalEdge = 0.0;
2056  for(int l = 0; l < NAttrs; l++) {
2057  TMAGAffMtx Theta = Param.GetMtx(l);
2058  Theta.GetLLMtx(LLMtxV[l]);
2059  }
2060 
2061  for(int i = 0; i < NNodes; i++) {
2062  for(int j = 0; j < NNodes; j++) {
2063  if(i == j) { continue; }
2064 
2065  if(Graph->IsEdge(i, j)) {
2066  for(int l = 0; l < NAttrs; l++) {
2067  LL += GetProbPhi(i, j, l, 0, 0) * LLMtxV[l].At(0, 0);
2068  LL += GetProbPhi(i, j, l, 0, 1) * LLMtxV[l].At(0, 1);
2069  LL += GetProbPhi(i, j, l, 1, 0) * LLMtxV[l].At(1, 0);
2070  LL += GetProbPhi(i, j, l, 1, 1) * LLMtxV[l].At(1, 1);
2071  }
2072  } else {
2073  LL += log(1-exp(GetProdLinWeight(i, j)));
2074  }
2075 
2076  double TempLL = 1.0;
2077  for(int l = 0; l < NAttrs; l++) {
2078  int Ai = (double(PhiVV(i, l)) > 0.5) ? 0 : 1;
2079  int Aj = (double(PhiVV(j, l)) > 0.5) ? 0 : 1;
2080  TempLL *= Param.GetMtx(l).At(Ai, Aj);
2081  }
2082  if(TMAGNodeBern::Rnd.GetUniDev() < TempLL) {
2083  TotalEdge += 1.0;
2084  }
2085  }
2086  }
2087 
2088  return LL;
2089 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
const TFltV & GetMuV() const
Definition: mag.h:103
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
Definition: mag.h:10
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
static TRnd Rnd
Definition: mag.h:90
const int GetNodes() const
Definition: mag.h:171
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

const double TMAGFitBern::ComputeApxLL ( ) const

Definition at line 2005 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), TMAGAffMtx::GetLLMtx(), TMAGParam< TNodeAttr >::GetMtx(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), GetProbPhi(), GetProdLinWeight(), Graph, TNGraph::IsEdge(), NormConst, Param, and PhiVV.

Referenced by DoEMAlg().

2005  {
2006  double LL = 0.0;
2007  // double LLSelf = 0.0;
2008  const int NNodes = Param.GetNodes();
2009  const int NAttrs = Param.GetAttrs();
2010  TMAGNodeBern NodeAttr = Param.GetNodeAttr();
2011  TFltV MuV = NodeAttr.GetMuV();
2012  TMAGAffMtxV LLMtxV(NAttrs);
2013 
2014  for(int l = 0; l < NAttrs; l++) {
2015  for(int i = 0; i < NNodes; i++) {
2016  LL += PhiVV(i, l) * log(MuV[l]);
2017  LL += (1.0 - PhiVV(i, l)) * log(1.0 - MuV[l]);
2018  LL -= PhiVV(i, l) * log(PhiVV(i, l));
2019  LL -= (1.0 - PhiVV(i, l)) * log(1.0 - PhiVV(i, l));
2020  }
2021  TMAGAffMtx Theta = Param.GetMtx(l);
2022  Theta.GetLLMtx(LLMtxV[l]);
2023  }
2024 
2025  for(int i = 0; i < NNodes; i++) {
2026  for(int j = 0; j < NNodes; j++) {
2027  if(i == j) { continue; }
2028 
2029  if(Graph->IsEdge(i, j)) {
2030  for(int l = 0; l < NAttrs; l++) {
2031  LL += GetProbPhi(i, j, l, 0, 0) * LLMtxV[l].At(0, 0);
2032  LL += GetProbPhi(i, j, l, 0, 1) * LLMtxV[l].At(0, 1);
2033  LL += GetProbPhi(i, j, l, 1, 0) * LLMtxV[l].At(1, 0);
2034  LL += GetProbPhi(i, j, l, 1, 1) * LLMtxV[l].At(1, 1);
2035  }
2036  LL += log(NormConst);
2037  } else {
2038  LL += log(1-exp(GetProdLinWeight(i, j)));
2039  }
2040  }
2041  }
2042 
2043  return LL;
2044 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
const TFltV & GetMuV() const
Definition: mag.h:103
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
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
Definition: mag.h:10
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::ComputeJointAdjLL ( const TIntVV AttrVV) const

Definition at line 1942 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetMtxV(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), Graph, TNGraph::IsEdge(), TVec< TVal, TSizeTy >::Len(), and Param.

1942  {
1943  double LL = 0.0;
1944  const int NNodes = Param.GetNodes();
1945  const int NAttrs = Param.GetAttrs();
1946  TMAGAffMtxV MtxV(NAttrs); Param.GetMtxV(MtxV);
1947  const TMAGNodeBern NodeAttr = Param.GetNodeAttr();
1948  const TFltV MuV = NodeAttr.GetMuV();
1949 
1950  for(int l = 0; l < NAttrs; l++) {
1951  for(int i = 0; i < MtxV[l].Len(); i++) {
1952  MtxV[l].At(i) = log(MtxV[l].At(i));
1953  }
1954  }
1955 
1956  for(int i = 0; i < NNodes; i++) {
1957  for(int j = 0; j < NNodes; j++) {
1958  if(i == j) { continue; }
1959 
1960  double ProbLL = 0.0;
1961  for(int l = 0; l < NAttrs; l++) {
1962  ProbLL += MtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l));
1963  }
1964 
1965  if(Graph->IsEdge(i, j)) {
1966  LL += ProbLL;
1967  } else {
1968  LL += log(1-exp(ProbLL));
1969  }
1970  }
1971  }
1972 
1973  return LL;
1974 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
const TFltV & GetMuV() const
Definition: mag.h:103
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

const double TMAGFitBern::ComputeJointLL ( int  NSample) const

Definition at line 1976 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), TVVec< TVal, TSizeTy >::Clr(), ComputeJointOneLL(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), TRnd::GetUniDev(), Param, and PhiVV.

1976  {
1977  double LL = 0.0;
1978  const int NNodes = Param.GetNodes();
1979  const int NAttrs = Param.GetAttrs();
1980 
1981  TRnd Rnd(2000);
1982  TIntVV AttrVV(NNodes, NAttrs);
1983  int count = 0;
1984  for(int s = 0; s < NSample; s++) {
1985  for(int i = 0; i < NNodes; i++) {
1986  for(int l = 0; l < NAttrs; l++) {
1987 
1988  if(Rnd.GetUniDev() <= PhiVV(i, l)) {
1989  AttrVV.At(i, l) = 0;
1990  } else {
1991  AttrVV.At(i, l) = 1;
1992  }
1993 
1994  if(PhiVV(i, l) > 0.05 && PhiVV(i, l) < 0.95) count++;
1995  }
1996  }
1997 
1998  LL += ComputeJointOneLL(AttrVV);
1999  }
2000  AttrVV.Clr();
2001 
2002  return LL / double(NSample);
2003 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
Definition: dt.h:11
Definition: ds.h:2222
const double ComputeJointOneLL(const TIntVV &AttrVV) const
Definition: mag.cpp:1900
TFltVV PhiVV
Definition: mag.h:343
const int GetNodes() const
Definition: mag.h:171

Here is the call graph for this function:

const double TMAGFitBern::ComputeJointOneLL ( const TIntVV AttrVV) const

Definition at line 1900 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetMtxV(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), Graph, TNGraph::IsEdge(), TVec< TVal, TSizeTy >::Len(), and Param.

Referenced by ComputeJointLL().

1900  {
1901  double LL = 0.0;
1902  const int NNodes = Param.GetNodes();
1903  const int NAttrs = Param.GetAttrs();
1904  TMAGAffMtxV MtxV(NAttrs); Param.GetMtxV(MtxV);
1905  const TMAGNodeBern NodeAttr = Param.GetNodeAttr();
1906  const TFltV MuV = NodeAttr.GetMuV();
1907 
1908  for(int l = 0; l < NAttrs; l++) {
1909  for(int i = 0; i < MtxV[l].Len(); i++) {
1910  MtxV[l].At(i) = log(MtxV[l].At(i));
1911  }
1912  }
1913 
1914  for(int i = 0; i < NNodes; i++) {
1915  for(int l = 0; l < NAttrs; l++) {
1916  if(AttrVV.At(i, l) == 0) {
1917  LL += log(MuV[l]);
1918  } else {
1919  LL += log(1.0 - MuV[l]);
1920  }
1921  }
1922 
1923  for(int j = 0; j < NNodes; j++) {
1924  if(i == j) { continue; }
1925 
1926  double ProbLL = 0.0;
1927  for(int l = 0; l < NAttrs; l++) {
1928  ProbLL += MtxV[l].At(AttrVV.At(i, l), AttrVV.At(j, l));
1929  }
1930 
1931  if(Graph->IsEdge(i, j)) {
1932  LL += ProbLL;
1933  } else {
1934  LL += log(1-exp(ProbLL));
1935  }
1936  }
1937  }
1938 
1939  return LL;
1940 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
const TFltV & GetMuV() const
Definition: mag.h:103
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::ComputeMI ( const TIntVV AttrV,
const int  AId1,
const int  AId2 
)
static

Definition at line 2091 of file mag.cpp.

References TVVec< TVal, TSizeTy >::GetXDim(), TVec< TVal, TSizeTy >::PutAll(), and TVVec< TVal, TSizeTy >::PutAll().

Referenced by ComputeMI().

2091  {
2092  const int NNodes = AttrV.GetXDim();
2093  double MI = 0.0;
2094  double Cor = 0.0;
2095 
2096  TFltVV Pxy(2,2);
2097  TFltV Px(2), Py(2);
2098  Pxy.PutAll(0.0);
2099  Px.PutAll(0.0);
2100  Py.PutAll(0.0);
2101 
2102  for(int i = 0; i < NNodes; i++) {
2103  int X = AttrV(i, AId1);
2104  int Y = AttrV(i, AId2);
2105  Pxy(X, Y) = Pxy(X, Y) + 1;
2106  Px[X] = Px[X] + 1;
2107  Py[Y] = Py[Y] + 1;
2108  Cor += double(X * Y);
2109  }
2110 
2111  for(int x = 0; x < 2; x++) {
2112  for(int y = 0; y < 2; y++) {
2113  MI += Pxy(x, y) / double(NNodes) * (log(Pxy(x, y).Val) - log(Px[x].Val) - log(Py[y].Val) + log((double)NNodes));
2114  }
2115  }
2116 
2117  return MI;
2118 }
TSizeTy GetXDim() const
Definition: ds.h:2249

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::ComputeMI ( const TFltVV AttrV,
const int  AId1,
const int  AId2 
)
static

Definition at line 2120 of file mag.cpp.

References TVVec< TVal, TSizeTy >::GetXDim(), TVec< TVal, TSizeTy >::PutAll(), and TVVec< TVal, TSizeTy >::PutAll().

2120  {
2121  const int NNodes = AttrV.GetXDim();
2122  double MI = 0.0;
2123  double Cor = 0.0;
2124 
2125  TFltVV Pxy(2,2);
2126  TFltV Px(2), Py(2);
2127  Pxy.PutAll(0.0);
2128  Px.PutAll(0.0);
2129  Py.PutAll(0.0);
2130 
2131  for(int i = 0; i < NNodes; i++) {
2132  double X = AttrV(i, AId1);
2133  double Y = AttrV(i, AId2);
2134  Pxy(0, 0) = Pxy(0, 0) + X * Y;
2135  Pxy(0, 1) = Pxy(0, 1) + X * (1 - Y);
2136  Pxy(1, 0) = Pxy(1, 0) + (1 - X) * Y;
2137  Pxy(1, 1) = (i+1) - Pxy(0, 0) - Pxy(0, 1) - Pxy(1, 0);
2138  Px[0] = Px[0] + X;
2139  Py[0] = Py[0] + Y;
2140  Cor += double((1-X) * (1-Y));
2141  }
2142  Px[1] = NNodes - Px[0];
2143  Py[1] = NNodes - Py[0];
2144 
2145  for(int x = 0; x < 2; x++) {
2146  for(int y = 0; y < 2; y++) {
2147  MI += Pxy(x, y) / double(NNodes) * (log(Pxy(x, y)) - log(Px[x]) - log(Py[y]) + log(double(NNodes)));
2148  }
2149  }
2150 
2151  return MI;
2152 }
TSizeTy GetXDim() const
Definition: ds.h:2249

Here is the call graph for this function:

const double TMAGFitBern::ComputeMI ( const TIntVV AttrV)
static

Definition at line 2154 of file mag.cpp.

References ComputeMI(), and TVVec< TVal, TSizeTy >::GetYDim().

2154  {
2155  // const int NNodes = AttrV.GetXDim();
2156  const int NAttrs = AttrV.GetYDim();
2157  double MI = 0.0;
2158 
2159  for(int l = 0; l < NAttrs; l++) {
2160  for(int k = l+1; k < NAttrs; k++) {
2161  MI += ComputeMI(AttrV, l, k);
2162  }
2163  }
2164 
2165  return MI;
2166 }
TSizeTy GetYDim() const
Definition: ds.h:2250
static const double ComputeMI(const TIntVV &AttrV, const int AId1, const int AId2)
Definition: mag.cpp:2091

Here is the call graph for this function:

const double TMAGFitBern::ComputeMI ( const TFltVV AttrV)
static

Definition at line 2168 of file mag.cpp.

References ComputeMI(), and TVVec< TVal, TSizeTy >::GetYDim().

2168  {
2169  // const int NNodes = AttrV.GetXDim();
2170  const int NAttrs = AttrV.GetYDim();
2171  double MI = 0.0;
2172 
2173  for(int l = 0; l < NAttrs; l++) {
2174  for(int k = l+1; k < NAttrs; k++) {
2175  MI += ComputeMI(AttrV, l, k);
2176  }
2177  }
2178 
2179  return MI;
2180 }
TSizeTy GetYDim() const
Definition: ds.h:2250
static const double ComputeMI(const TIntVV &AttrV, const int AId1, const int AId2)
Definition: mag.cpp:2091

Here is the call graph for this function:

void TMAGFitBern::CountAttr ( TFltV EstMuV) const

Definition at line 1814 of file mag.cpp.

References TVec< TVal, TSizeTy >::Gen(), TVVec< TVal, TSizeTy >::GetXDim(), TVVec< TVal, TSizeTy >::GetYDim(), PhiVV, and TVec< TVal, TSizeTy >::PutAll().

Referenced by SortAttrOrdering().

1814  {
1815  const int NNodes = PhiVV.GetXDim();
1816  const int NAttrs = PhiVV.GetYDim();
1817  EstMuV.Gen(NAttrs);
1818  EstMuV.PutAll(0.0);
1819 
1820  for(int l = 0; l < NAttrs; l++) {
1821  for(int i = 0; i < NNodes; i++) {
1822  EstMuV[l] = EstMuV[l] + PhiVV(i, l);
1823  }
1824  EstMuV[l] = EstMuV[l] / double(NNodes);
1825  }
1826 }
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
TSizeTy GetYDim() const
Definition: ds.h:2250
TFltVV PhiVV
Definition: mag.h:343
TSizeTy GetXDim() const
Definition: ds.h:2249
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TVVec< TVal, TSizeTy >::At(), ComputeApxLL(), Debug, DoEStep(), DoMStep(), TVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetMtxV(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), TRnd::GetUniDev(), KnownVV, LLHisV, MtxHisV, MuHisV, NormalizeAffMtxV(), NormConst, Param, PhiVV, TMAGParam< TNodeAttr >::SetMtxV(), and UnNormalizeAffMtxV().

1630  {
1631  const int NNodes = Param.GetNodes();
1632  const int NAttrs = Param.GetAttrs();
1633  TIntV IndexV;
1634  double LL;
1635  // double MuDist, MtxDist;
1636 
1637  MuHisV.Gen(NStep + 1, 0);
1638  MtxHisV.Gen(NStep + 1, 0);
1639  LLHisV.Gen(NStep + 1, 0);
1640 
1641  printf("--------------------------------------------\n");
1642  printf("Before EM Iteration\n");
1643  printf("--------------------------------------------\n");
1644 
1645  TMAGAffMtxV InitMtxV;
1646  TMAGNodeBern NodeAttr = Param.GetNodeAttr();
1647  Param.GetMtxV(InitMtxV);
1648  TFltV InitMuV = NodeAttr.GetMuV();
1649 
1650  for(int i = 0; i < NNodes; i++) {
1651  for(int l = 0; l < NAttrs; l++) {
1652  if(! KnownVV(i, l)) {
1653  PhiVV.At(i, l) = TMAGNodeBern::Rnd.GetUniDev();
1654  }
1655  }
1656  }
1657 
1658  if(Debug) {
1659  double LL = ComputeApxLL();
1660  MuHisV.Add(InitMuV);
1661  MtxHisV.Add(InitMtxV);
1662  LLHisV.Add(LL);
1663  }
1664 
1665  NormalizeAffMtxV(InitMtxV, true);
1666  Param.SetMtxV(InitMtxV);
1667 
1668  for(int n = 0; n < NStep; n++) {
1669  printf("--------------------------------------------\n");
1670  printf("EM Iteration : %d\n", (n+1));
1671  printf("--------------------------------------------\n");
1672 
1673  NodeAttr = Param.GetNodeAttr();
1674  for(int i = 0; i < NNodes; i++) {
1675  for(int l = 0; l < NAttrs; l++) {
1676  if(!KnownVV(i, l) && TMAGNodeBern::Rnd.GetUniDev() < ReInit) {
1677  PhiVV.At(i, l) = TMAGNodeBern::Rnd.GetUniDev();
1678  }
1679  }
1680  }
1681  DoEStep(InitMuV, NEstep, LL, Lambda);
1682  Param.GetMtxV(InitMtxV);
1683 // NormalizeAffMtxV(InitMtxV);
1684  Param.SetMtxV(InitMtxV);
1685  DoMStep(NMstep, LrnRate, MaxGrad, Lambda, NReal);
1686 
1687  printf("\n");
1688 
1689  if(Debug) {
1690  double LL = ComputeApxLL();
1691  MuHisV.Add(InitMuV);
1692  MtxHisV.Add(InitMtxV);
1693  LLHisV.Add(LL);
1694  printf(" ApxLL = %.2f (Const = %f)\n", LL, double(NormConst));
1695  }
1696 
1697  }
1698  Param.GetMtxV(InitMtxV);
1699  UnNormalizeAffMtxV(InitMtxV, true);
1700  Param.SetMtxV(InitMtxV);
1701 }
bool Debug
Definition: mag.h:347
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
void NormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1454
TVec< TMAGAffMtxV > MtxHisV
Definition: mag.h:353
void UnNormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1492
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
TBoolVV KnownVV
Definition: mag.h:344
const TFltV & GetMuV() const
Definition: mag.h:103
TFlt NormConst
Definition: mag.h:350
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184
TFltVV PhiVV
Definition: mag.h:343
double DoEStep(const TFltV &TrueMuV, const int &NIter, double &LL, const double &Lambda)
Definition: mag.cpp:1272
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
static TRnd Rnd
Definition: mag.h:90
double GetUniDev()
Definition: dt.h:30
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
void DoMStep(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
Definition: mag.cpp:1608
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
const double ComputeApxLL() const
Definition: mag.cpp:2005
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

double TMAGFitBern::DoEStep ( const TFltV TrueMuV,
const int &  NIter,
double &  LL,
const double &  Lambda 
)

Definition at line 1272 of file mag.cpp.

References TVVec< TVal, TSizeTy >::Clr(), DoEStepApxOneIter(), DoEStepOneIter(), ESpeedUp, TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), TExeTm::GetTmStr(), TVec< TVal, TSizeTy >::Last(), and Param.

Referenced by DoEMAlg().

1272  {
1273  const int NNodes = Param.GetNodes();
1274  const int NAttrs = Param.GetAttrs();
1275  TFltVV NewPhiVV(NNodes, NAttrs);
1276  // double MI;
1277 
1278  TFltV Delta(NIter);
1279 
1280  for(int i = 0; i < NIter; i++) {
1281  TExeTm IterTm;
1282  printf("EStep iteration : %d\n", (i+1));
1283  if(ESpeedUp) {
1284  Delta[i] = DoEStepApxOneIter(TrueMuV, NewPhiVV, Lambda);
1285  } else {
1286  Delta[i] = DoEStepOneIter(TrueMuV, NewPhiVV, Lambda);
1287  }
1288 // PhiVV = NewPhiVV;
1289 
1290  printf(" (Time = %s)\n", IterTm.GetTmStr());
1291  }
1292  printf("\n");
1293 
1294  NewPhiVV.Clr();
1295 
1296  return Delta.Last();
1297 }
Definition: tm.h:355
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const char * GetTmStr() const
Definition: tm.h:370
bool ESpeedUp
Definition: mag.h:347
double DoEStepOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
Definition: mag.cpp:1056
double DoEStepApxOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
Definition: mag.cpp:1134
const int GetNodes() const
Definition: mag.h:171

Here is the call graph for this function:

Here is the caller graph for this function:

double TMAGFitBern::DoEStepApxOneIter ( const TFltV TrueMuV,
TFltVV NewPhi,
const double &  Lambda 
)

Definition at line 1134 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), AvgPhiPairVV, AvgPhiV, TVec< TVal, TSizeTy >::Gen(), TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), GetAvgProdLinWeight(), GetAvgProdSqWeight(), GetAvgSqThetaLL(), GetAvgThetaLL(), TMAGParam< TNodeAttr >::GetNodes(), TRnd::GetUniDevInt(), KnownVV, Param, PhiVV, TVec< TVal, TSizeTy >::PutAll(), TVVec< TVal, TSizeTy >::PutAll(), and UpdateApxPhiMI().

Referenced by DoEStep().

1134  {
1135  const int NNodes = Param.GetNodes();
1136  const int NAttrs = Param.GetAttrs();
1137  double MaxDelta = 0, L1 = 0;
1138  double Val;
1139  TFltIntIntTrV NewVal;
1140  int RndCount = 0;
1141  // double OldMI = 0.0, NewMI = 0.0;
1142  TFltV MuV(NAttrs); MuV.PutAll(0.0);
1143  TFltVV ProdVV(NNodes, 4); ProdVV.PutAll(0.0);
1144  TIntV NIndV(NNodes), AIndV(NAttrs);
1145 
1146  // Update Phi
1147  /*
1148  for(int i = 0; i < NNodes; i++) { NIndV[i] = i; }
1149  for(int l = 0; l < NAttrs; l++) { AIndV[l] = l; }
1150  if(Randomized) {
1151  NIndV.Shuffle(TMAGNodeBern::Rnd);
1152  AIndV.Shuffle(TMAGNodeBern::Rnd);
1153  }
1154  */
1155 
1156  AvgPhiV.Gen(NAttrs); AvgPhiV.PutAll(0.0);
1157  AvgPhiPairVV.Gen(NAttrs, 4*NAttrs); AvgPhiPairVV.PutAll(0.0);
1158  for(int i = 0; i < NNodes; i++) {
1159  for(int l = 0; l < NAttrs; l++) {
1160  for(int p = l+1; p < NAttrs; p++) {
1161  int index = 4 * p;
1162  AvgPhiPairVV(l, index) += PhiVV(i, l) * PhiVV(i, p);
1163  AvgPhiPairVV(l, index+1) += PhiVV(i, l) * (1.0-PhiVV(i, p));
1164  AvgPhiPairVV(l, index+2) += (1.0-PhiVV(i, l)) * PhiVV(i, p);
1165  AvgPhiPairVV(l, index+3) += (1.0-PhiVV(i, l)) * (1.0-PhiVV(i, p));
1166  }
1167  AvgPhiV[l] += PhiVV(i, l);
1168  }
1169  }
1170  for(int i = 0; i < NNodes; i++) {
1171  ProdVV(i, 0) = GetAvgProdLinWeight(i, i, true, false);
1172  ProdVV(i, 1) = GetAvgProdLinWeight(i, i, false, true);
1173  ProdVV(i, 2) = GetAvgProdSqWeight(i, i, true, false);
1174  ProdVV(i, 3) = GetAvgProdSqWeight(i, i, false, true);
1175  }
1176 
1177  const int Iter = 3;
1178 
1179  NewVal.Gen(NAttrs * Iter);
1180  for(int i = 0; i < NNodes * Iter; i++) {
1181  for(int l = 0; l < NAttrs; l++) {
1182  const int NId = TMAGNodeBern::Rnd.GetUniDevInt(NNodes);
1183  const int AId = TMAGNodeBern::Rnd.GetUniDevInt(NAttrs);
1184  double Delta = 0.0;
1185  if(KnownVV(NId, AId)) {
1186  Val = PhiVV.At(NId, AId);
1187  } else {
1188  Delta = UpdateApxPhiMI(Lambda, NId, AId, Val, ProdVV);
1189  }
1190 
1191 // PhiVV.At(NId, AId) = Val;
1192  NewVal[l] = TFltIntIntTr(Val, NId, AId);
1193 
1194 // MuV[AId] = MuV[AId] + Val;
1195  if(fabs(Delta) > MaxDelta) {
1196  MaxDelta = fabs(Delta);
1197  }
1198  if(Val > 0.3 && Val < 0.7) { RndCount++; }
1199  }
1200 
1201  for(int l = 0; l < NAttrs; l++) {
1202  const int NId = NewVal[l].Val2;
1203  const int AId = NewVal[l].Val3;
1204 
1205  ProdVV(NId, 0) -= GetAvgThetaLL(NId, NId, AId, true, false);
1206  ProdVV(NId, 1) -= GetAvgThetaLL(NId, NId, AId, false, true);
1207  ProdVV(NId, 2) -= GetAvgSqThetaLL(NId, NId, AId, true, false);
1208  ProdVV(NId, 3) -= GetAvgSqThetaLL(NId, NId, AId, false, true);
1209  for(int p = 0; p < NAttrs; p++) {
1210  if(p > AId) {
1211  int index = 4 * p;
1212  AvgPhiPairVV(AId, index) -= PhiVV(NId, AId) * PhiVV(NId, p);
1213  AvgPhiPairVV(AId, index+1) -= PhiVV(NId, AId) * (1.0-PhiVV(NId, p));
1214  AvgPhiPairVV(AId, index+2) -= (1.0-PhiVV(NId, AId)) * PhiVV(NId, p);
1215  AvgPhiPairVV(AId, index+3) -= (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, p));
1216  } else if (p < AId) {
1217  int index = 4 * AId;
1218  AvgPhiPairVV(p, index) -= PhiVV(NId, p) * PhiVV(NId, AId);
1219  AvgPhiPairVV(p, index+1) -= PhiVV(NId, p) * (1.0-PhiVV(NId, AId));
1220  AvgPhiPairVV(p, index+2) -= (1.0-PhiVV(NId, p)) * PhiVV(NId, AId);
1221  AvgPhiPairVV(p, index+3) -= (1.0-PhiVV(NId, p)) * (1.0-PhiVV(NId, AId));
1222  }
1223  }
1224  AvgPhiV[AId] -= PhiVV(NId, AId);
1225 
1226  PhiVV.At(NId, AId) = NewVal[l].Val1;
1227 
1228  ProdVV(NId, 0) += GetAvgThetaLL(NId, NId, AId, true, false);
1229  ProdVV(NId, 1) += GetAvgThetaLL(NId, NId, AId, false, true);
1230  ProdVV(NId, 2) += GetAvgSqThetaLL(NId, NId, AId, true, false);
1231  ProdVV(NId, 3) += GetAvgSqThetaLL(NId, NId, AId, false, true);
1232  for(int p = 0; p < NAttrs; p++) {
1233  if(p > AId) {
1234  int index = 4 * p;
1235  AvgPhiPairVV(AId, index) += PhiVV(NId, AId) * PhiVV(NId, p);
1236  AvgPhiPairVV(AId, index+1) += PhiVV(NId, AId) * (1.0-PhiVV(NId, p));
1237  AvgPhiPairVV(AId, index+2) += (1.0-PhiVV(NId, AId)) * PhiVV(NId, p);
1238  AvgPhiPairVV(AId, index+3) += (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, p));
1239  } else if (p < AId) {
1240  int index = 4 * AId;
1241  AvgPhiPairVV(p, index) += PhiVV(NId, p) * PhiVV(NId, AId);
1242  AvgPhiPairVV(p, index+1) += PhiVV(NId, p) * (1.0-PhiVV(NId, AId));
1243  AvgPhiPairVV(p, index+2) += (1.0-PhiVV(NId, p)) * PhiVV(NId, AId);
1244  AvgPhiPairVV(p, index+3) += (1.0-PhiVV(NId, p)) * (1.0-PhiVV(NId, AId));
1245  }
1246  }
1247  AvgPhiV[AId] += PhiVV(NId, AId);
1248  }
1249  }
1250 
1251  for(int l = 0; l < NAttrs; l++) {
1252  MuV[l] = AvgPhiV[l] / double(NNodes);
1253  }
1254 
1255  TFltV SortMuV = MuV;
1256  double Avg = 0.0;
1257 // SortMuV.Sort(false);
1258  for(int l = 0; l < NAttrs; l++) {
1259  printf(" F[%d] = %.3f", l, double(MuV[l]));
1260  Avg += SortMuV[l];
1261 // L1 += fabs(TrueMuV[l] - SortMuV[l]);
1262  }
1263  printf("\n");
1264  printf(" Rnd = %d(%.3f)", RndCount, double(RndCount) / double(NNodes * NAttrs));
1265  printf(" Avg = %.3f\n", Avg / double(NAttrs));
1266 // printf(" Linf = %f\n", MaxDelta);
1267 // L1 /= double(NAttrs);
1268 
1269  return L1;
1270 }
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
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetAvgProdSqWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:664
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
TBoolVV KnownVV
Definition: mag.h:344
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
void PutAll(const TVal &Val)
Definition: ds.h:2267
TFltVV PhiVV
Definition: mag.h:343
const double GetAvgProdLinWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:642
TFltV AvgPhiV
Definition: mag.h:348
static TRnd Rnd
Definition: mag.h:90
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
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246
TFltVV AvgPhiPairVV
Definition: mag.h:349
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:182
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

double TMAGFitBern::DoEStepOneIter ( const TFltV TrueMuV,
TFltVV NewPhi,
const double &  Lambda 
)

Definition at line 1056 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), TVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), TRnd::GetUniDevInt(), KnownVV, Param, PhiVV, TVec< TVal, TSizeTy >::PutAll(), TVec< TVal, TSizeTy >::Sort(), and UpdatePhiMI().

Referenced by DoEStep().

1056  {
1057  const int NNodes = Param.GetNodes();
1058  const int NAttrs = Param.GetAttrs();
1059  double MaxDelta = 0, L1 = 0;
1060  double Val;
1061  TFltIntIntTrV NewVal;
1062  int RndCount = 0;
1063  // double OldMI = 0.0, NewMI = 0.0;
1064  TFltV MuV(NAttrs); MuV.PutAll(0.0);
1065  TIntV NIndV(NNodes), AIndV(NAttrs);
1066 
1067  // Update Phi
1068  /*
1069  for(int i = 0; i < NNodes; i++) { NIndV[i] = i; }
1070  for(int l = 0; l < NAttrs; l++) { AIndV[l] = l; }
1071  if(Randomized) {
1072  NIndV.Shuffle(TMAGNodeBern::Rnd);
1073  AIndV.Shuffle(TMAGNodeBern::Rnd);
1074  }
1075  */
1076 
1077  NewVal.Gen(NAttrs * 2);
1078  for(int i = 0; i < NNodes; i++) {
1079 // const int NId = NIndV[i]%NNodes;
1080  for(int l = 0; l < NAttrs * 2; l++) {
1081  const int NId = TMAGNodeBern::Rnd.GetUniDevInt(NNodes);
1082  const int AId = TMAGNodeBern::Rnd.GetUniDevInt(NAttrs);
1083 // const int AId = AIndV[l]%NAttrs;
1084 // double Delta = UpdatePhi(NId, AId, Val);
1085  double Delta = 0.0;
1086  if(KnownVV(NId, AId)) {
1087  Val = PhiVV.At(NId, AId);
1088  } else {
1089  Delta = UpdatePhiMI(Lambda, NId, AId, Val);
1090  }
1091 
1092 // PhiVV.At(NId, AId) = Val;
1093  NewVal[l] = TFltIntIntTr(Val, NId, AId);
1094 
1095 // MuV[AId] = MuV[AId] + Val;
1096  if(fabs(Delta) > MaxDelta) {
1097  MaxDelta = fabs(Delta);
1098  }
1099  if(Val > 0.3 && Val < 0.7) { RndCount++; }
1100  }
1101 
1102  for(int l = 0; l < NAttrs * 2; l++) {
1103  const int NId = NewVal[l].Val2;
1104  const int AId = NewVal[l].Val3;
1105  PhiVV.At(NId, AId) = NewVal[l].Val1;
1106  }
1107  }
1108  for(int i = 0; i < NNodes; i++) {
1109  for(int l = 0; l < NAttrs; l++) {
1110  MuV[l] = MuV[l] + PhiVV.At(i, l);
1111  }
1112  }
1113  for(int l = 0; l < NAttrs; l++) {
1114  MuV[l] = MuV[l] / double(NNodes);
1115  }
1116 
1117  TFltV SortMuV = MuV;
1118  double Avg = 0.0;
1119  SortMuV.Sort(false);
1120  for(int l = 0; l < NAttrs; l++) {
1121  printf(" F[%d] = %.3f", l, double(MuV[l]));
1122  Avg += SortMuV[l];
1123  L1 += fabs(TrueMuV[l] - SortMuV[l]);
1124  }
1125  printf("\n");
1126  printf(" Rnd = %d(%.3f)", RndCount, double(RndCount) / double(NNodes * NAttrs));
1127  printf(" Avg = %.3f\n", Avg / double(NAttrs));
1128  L1 /= double(NAttrs);
1129 
1130  return L1;
1131 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double UpdatePhiMI(const double &Lambda, const int &NId, const int &AId, double &Phi)
Definition: mag.cpp:826
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TBoolVV KnownVV
Definition: mag.h:344
TFltVV PhiVV
Definition: mag.h:343
static TRnd Rnd
Definition: mag.h:90
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
int GetUniDevInt(const int &Range=0)
Definition: dt.cpp:39
TTriple< TFlt, TInt, TInt > TFltIntIntTr
Definition: ds.h:182
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References AvgPhiV, TVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TExeTm::GetTmStr(), Param, TVec< TVal, TSizeTy >::PutAll(), UpdateAffMtxV(), and UpdateMu().

Referenced by DoEMAlg().

1608  {
1609  // const int NNodes = Param.GetNodes();
1610  const int NAttrs = Param.GetAttrs();
1611  double MuDelta = 0.0, AffMtxDelta = 0.0;
1612 
1613  TExeTm ExeTm;
1614 
1615  printf("\n");
1616  AvgPhiV.Gen(NAttrs); AvgPhiV.PutAll(0.0);
1617  for(int l = 0; l < NAttrs; l++) {
1618 // printf(" [Attr = %d]\n", l);
1619  MuDelta += UpdateMu(l);
1620  }
1621  printf("\n");
1622 
1623  printf(" == Update Theta\n");
1624  AffMtxDelta += UpdateAffMtxV(GradIter, LrnRate, MaxGrad, Lambda, NReal);
1625  printf("\n");
1626  printf("Elpased time = %s\n", ExeTm.GetTmStr());
1627  printf("\n");
1628 }
Definition: tm.h:355
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const char * GetTmStr() const
Definition: tm.h:370
const double UpdateMu(const int &AId)
Definition: mag.cpp:1299
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
TFltV AvgPhiV
Definition: mag.h:348
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
const double UpdateAffMtxV(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
Definition: mag.cpp:1558

Here is the call graph for this function:

Here is the caller graph for this function:

const int TMAGFitBern::GetAttrs ( ) const
inline

Definition at line 376 of file mag.h.

References TMAGParam< TNodeAttr >::GetAttrs(), and Param.

376 { return Param.GetAttrs(); }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346

Here is the call graph for this function:

const double TMAGFitBern::GetAvgInCoeff ( const int &  i,
const int &  AId,
const int &  A,
const TMAGAffMtx Theta 
) const

Definition at line 558 of file mag.cpp.

References TMAGAffMtx::At(), AvgPhiV, TMAGParam< TNodeAttr >::GetNodes(), and Param.

Referenced by UpdateApxPhiMI().

558  {
559  const int NNodes = Param.GetNodes();
560  const double Mu_l = AvgPhiV[AId] / double(NNodes);
561  return (Mu_l * Theta.At(0, A) + (1.0 - Mu_l) * Theta.At(1, A));
562 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TFltV AvgPhiV
Definition: mag.h:348
const int GetNodes() const
Definition: mag.h:171
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetAvgOutCoeff ( const int &  i,
const int &  AId,
const int &  A,
const TMAGAffMtx Theta 
) const

Definition at line 564 of file mag.cpp.

References TMAGAffMtx::At(), AvgPhiV, TMAGParam< TNodeAttr >::GetNodes(), and Param.

Referenced by UpdateApxPhiMI().

564  {
565  const int NNodes = Param.GetNodes();
566  const double Mu_l = AvgPhiV[AId] / double(NNodes);
567  return (Mu_l * Theta.At(A, 0) + (1.0 - Mu_l) * Theta.At(A, 1));
568 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TFltV AvgPhiV
Definition: mag.h:348
const int GetNodes() const
Definition: mag.h:171
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetAvgProdLinWeight ( const int &  NId1,
const int &  NId2,
const bool  Left = false,
const bool  Right = false 
) const

Definition at line 642 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), GetAvgThetaLL(), NormConst, and Param.

Referenced by DoEStepApxOneIter(), and PrepareUpdateApxAffMtx().

642  {
643  const int NAttrs = Param.GetAttrs();
644  double LL = 0.0;
645 
646  for(int l = 0; l < NAttrs; l++) {
647  LL += GetAvgThetaLL(NId1, NId2, l, Left, Right);
648  }
649 // return LL;
650  return LL + log(NormConst);
651 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
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
TFlt NormConst
Definition: mag.h:350

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetAvgProdSqWeight ( const int &  NId1,
const int &  NId2,
const bool  Left = false,
const bool  Right = false 
) const

Definition at line 664 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), GetAvgSqThetaLL(), NormConst, and Param.

Referenced by DoEStepApxOneIter(), and PrepareUpdateApxAffMtx().

664  {
665  const int NAttrs = Param.GetAttrs();
666  double LL = 0.0;
667 
668  for(int l = 0; l < NAttrs; l++) {
669  LL += GetAvgSqThetaLL(NId1, NId2, l, Left, Right);
670  }
671 // return LL;
672  return LL + 2 * log(NormConst);
673 }
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
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TFlt NormConst
Definition: mag.h:350

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetAvgSqThetaLL ( const int &  NId1,
const int &  NId2,
const int &  AId,
const bool  Left = false,
const bool  Right = false 
) const

Definition at line 620 of file mag.cpp.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetMtx(), GetProbMu(), and Param.

Referenced by DoEStepApxOneIter(), GetAvgProdSqWeight(), GradApxAffMtx(), and UpdateApxPhiMI().

620  {
621  double LL = 0.0;
622  const TMAGAffMtx& Mtx = Param.GetMtx(AId);
623  for(int A1 = 0; A1 < 2; A1++) {
624  for(int A2 = 0; A2 < 2; A2++) {
625  LL += GetProbMu(NId1, NId2, AId, A1, A2, Left, Right) * Mtx.At(A1, A2) * Mtx.At(A1, A2);
626  }
627  }
628  return log(LL);
629 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
Definition: mag.h:10
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
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

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetAvgThetaLL ( const int &  NId1,
const int &  NId2,
const int &  AId,
const bool  Left = false,
const bool  Right = false 
) const

Definition at line 598 of file mag.cpp.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetMtx(), GetProbMu(), and Param.

Referenced by DoEStepApxOneIter(), GetAvgProdLinWeight(), GradApxAffMtx(), and UpdateApxPhiMI().

598  {
599  double LL = 0.0;
600  const TMAGAffMtx& Mtx = Param.GetMtx(AId);
601  for(int A1 = 0; A1 < 2; A1++) {
602  for(int A2 = 0; A2 < 2; A2++) {
603  LL += GetProbMu(NId1, NId2, AId, A1, A2, Left, Right) * Mtx.At(A1, A2);
604  }
605  }
606  return log(LL);
607 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
Definition: mag.h:10
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
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

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetEstNoEdgeLL ( const int &  NId,
const int &  AId 
) const

Definition at line 746 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetNodeAttr(), and Param.

746  {
747  // const int NNodes = Param.GetNodes();
748  // const int NAttrs = Param.GetAttrs();
749 
750  TMAGNodeBern DistParam = Param.GetNodeAttr();
751  double LL = 0.0;
752 
753  return LL;
754 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177

Here is the call graph for this function:

PNGraph TMAGFitBern::GetGraph ( )
inline

Definition at line 383 of file mag.h.

References Graph.

383 { return Graph; }
PNGraph Graph
Definition: mag.h:345
const double TMAGFitBern::GetInCoeff ( const int &  i,
const int &  j,
const int &  l,
const int &  A,
const TMAGAffMtx Theta 
) const

Definition at line 550 of file mag.cpp.

References TMAGAffMtx::At(), TVVec< TVal, TSizeTy >::At(), and PhiVV.

Referenced by UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

550  {
551  return (PhiVV.At(i, l) * Theta.At(0, A) + (1.0 - PhiVV.At(i, l)) * Theta.At(1, A));
552 }
TFltVV PhiVV
Definition: mag.h:343
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

void TMAGFitBern::GetMtxV ( TMAGAffMtxV MtxV) const
inline

Definition at line 381 of file mag.h.

References TMAGParam< TNodeAttr >::GetMtxV(), and Param.

381 { Param.GetMtxV(MtxV); }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188

Here is the call graph for this function:

const TFltV& TMAGFitBern::GetMuV ( ) const
inline

Definition at line 379 of file mag.h.

References TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), and Param.

Referenced by NormalizeAffMtxV(), SaveTxt(), and UnNormalizeAffMtxV().

379 { const TMAGNodeBern& Dist = Param.GetNodeAttr(); return Dist.GetMuV(); }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const TFltV & GetMuV() const
Definition: mag.h:103
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177

Here is the call graph for this function:

Here is the caller graph for this function:

const TMAGNodeBern& TMAGFitBern::GetNodeAttr ( ) const
inline

Definition at line 378 of file mag.h.

References TMAGParam< TNodeAttr >::GetNodeAttr(), and Param.

378 { return Param.GetNodeAttr(); }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177

Here is the call graph for this function:

const int TMAGFitBern::GetNodes ( ) const
inline

Definition at line 375 of file mag.h.

References TMAGParam< TNodeAttr >::GetNodes(), and Param.

375 { return Param.GetNodes(); }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const int GetNodes() const
Definition: mag.h:171

Here is the call graph for this function:

const double TMAGFitBern::GetOutCoeff ( const int &  i,
const int &  j,
const int &  l,
const int &  A,
const TMAGAffMtx Theta 
) const

Definition at line 554 of file mag.cpp.

References TMAGAffMtx::At(), TVVec< TVal, TSizeTy >::At(), and PhiVV.

Referenced by UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

554  {
555  return (PhiVV.At(j, l) * Theta.At(A, 0) + (1.0 - PhiVV.At(j, l)) * Theta.At(A, 1));
556 }
TFltVV PhiVV
Definition: mag.h:343
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const TMAGParam<TMAGNodeBern>& TMAGFitBern::GetParams ( ) const
inline

Definition at line 377 of file mag.h.

References Param.

377 { return Param; }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const TFltVV& TMAGFitBern::GetPhiVV ( ) const
inline

Definition at line 392 of file mag.h.

References PhiVV.

392 { return PhiVV; }
TFltVV PhiVV
Definition: mag.h:343
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.

References TVVec< TVal, TSizeTy >::At(), AvgPhiV, TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), Param, and PhiVV.

Referenced by GetAvgSqThetaLL(), GetAvgThetaLL(), and GradApxAffMtx().

576  {
577  TMAGNodeBern DistParam = Param.GetNodeAttr();
578 // double Mu = DistParam.GetMu(AId);
579  double Mu = AvgPhiV[AId] / double(Param.GetNodes());
580  double Prob1 = (Left) ? double(PhiVV.At(NId1, AId)) : double(Mu);
581  double Prob2 = (Right)? double(PhiVV.At(NId2, AId)) : double(Mu);
582  Prob1 = (Attr1 == 0) ? Prob1 : 1.0 - Prob1;
583  Prob2 = (Attr2 == 0) ? Prob2 : 1.0 - Prob2;
584  return (Prob1 * Prob2);
585 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TFltVV PhiVV
Definition: mag.h:343
TFltV AvgPhiV
Definition: mag.h:348
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetProbPhi ( const int &  NId1,
const int &  NId2,
const int &  AId,
const int &  Attr1,
const int &  Attr2 
) const

Definition at line 570 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), and PhiVV.

Referenced by ComputeApxAdjLL(), ComputeApxLL(), GetSqThetaLL(), GetThetaLL(), GradAffMtx(), and GradApxAffMtx().

570  {
571  double Prob1 = (Attr1 == 0) ? double(PhiVV.At(NId1, AId)) : (1.0 - PhiVV.At(NId1, AId));
572  double Prob2 = (Attr2 == 0) ? double(PhiVV.At(NId2, AId)) : (1.0 - PhiVV.At(NId2, AId));
573  return (Prob1 * Prob2);
574 }
TFltVV PhiVV
Definition: mag.h:343
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetProdLinWeight ( const int &  NId1,
const int &  NId2 
) const

Definition at line 631 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), GetThetaLL(), NormConst, and Param.

Referenced by ComputeApxAdjLL(), ComputeApxLL(), GradApxAffMtx(), PrepareUpdateAffMtx(), UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

631  {
632  const int NAttrs = Param.GetAttrs();
633  double LL = 0.0;
634 
635  for(int l = 0; l < NAttrs; l++) {
636  LL += GetThetaLL(NId1, NId2, l);
637  }
638 // return LL;
639  return LL + log(NormConst);
640 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
TFlt NormConst
Definition: mag.h:350

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetProdSqWeight ( const int &  NId1,
const int &  NId2 
) const

Definition at line 653 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), GetSqThetaLL(), NormConst, and Param.

Referenced by GradApxAffMtx(), PrepareUpdateAffMtx(), UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

653  {
654  const int NAttrs = Param.GetAttrs();
655  double LL = 0.0;
656 
657  for(int l = 0; l < NAttrs; l++) {
658  LL += GetSqThetaLL(NId1, NId2, l);
659  }
660 // return LL;
661  return LL + 2 * log(NormConst);
662 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
TFlt NormConst
Definition: mag.h:350

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetSqThetaLL ( const int &  NId1,
const int &  NId2,
const int &  AId 
) const

Definition at line 609 of file mag.cpp.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetMtx(), GetProbPhi(), and Param.

Referenced by GetProdSqWeight(), GradAffMtx(), GradApxAffMtx(), UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

609  {
610  double LL = 0.0;
611  const TMAGAffMtx& Mtx = Param.GetMtx(AId);
612  for(int A1 = 0; A1 < 2; A1++) {
613  for(int A2 = 0; A2 < 2; A2++) {
614  LL += GetProbPhi(NId1, NId2, AId, A1, A2) * Mtx.At(A1, A2) * Mtx.At(A1, A2);
615  }
616  }
617  return log(LL);
618 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
Definition: mag.h:10
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::GetThetaLL ( const int &  NId1,
const int &  NId2,
const int &  AId 
) const

Definition at line 587 of file mag.cpp.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetMtx(), GetProbPhi(), and Param.

Referenced by GetProdLinWeight(), GradAffMtx(), GradApxAffMtx(), UpdateApxPhiMI(), UpdatePhi(), and UpdatePhiMI().

587  {
588  double LL = 0.0;
589  const TMAGAffMtx& Mtx = Param.GetMtx(AId);
590  for(int A1 = 0; A1 < 2; A1++) {
591  for(int A2 = 0; A2 < 2; A2++) {
592  LL += GetProbPhi(NId1, NId2, AId, A1, A2) * Mtx.At(A1, A2);
593  }
594  }
595  return log(LL);
596 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
Definition: mag.h:10
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetNodes(), GetProbPhi(), GetSqThetaLL(), GetThetaLL(), Graph, TNGraph::IsEdge(), Param, and TVec< TVal, TSizeTy >::PutAll().

Referenced by UpdateAffMtx().

1320  {
1321  const int NNodes = Param.GetNodes();
1322  // const int NAttrs = Param.GetAttrs();
1323  GradV.PutAll(0.0);
1324 
1325  for(int i = 0; i < NNodes; i++) {
1326  for(int j = 0; j < NNodes; j++) {
1327  double Prod = ProdVV(i, j) - GetThetaLL(i, j, AId);
1328  double Sq = SqVV(i, j) - GetSqThetaLL(i, j, AId);
1329 
1330  for(int p = 0; p < 4; p++) {
1331  int Ai = p / 2;
1332  int Aj = p % 2;
1333  double Prob = GetProbPhi(i, j, AId, Ai, Aj);
1334  if(Graph->IsEdge(i, j)) {
1335  GradV[p] += Prob / CurMtx.At(p);
1336  } else {
1337  GradV[p] -= Prob * exp(Prod);
1338  GradV[p] -= Prob * exp(Sq) * CurMtx.At(p);
1339  }
1340  }
1341  }
1342  }
1343 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
const int GetNodes() const
Definition: mag.h:171
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TMAGAffMtx::At(), TNGraph::BegEI(), TNGraph::EndEI(), TVec< TVal, TSizeTy >::Gen(), GetAvgSqThetaLL(), GetAvgThetaLL(), TMAGParam< TNodeAttr >::GetNodes(), GetProbMu(), GetProbPhi(), GetProdLinWeight(), GetProdSqWeight(), GetSqThetaLL(), GetThetaLL(), Graph, LogSumExp(), Param, and TVec< TVal, TSizeTy >::PutAll().

Referenced by UpdateAffMtx().

1346  {
1347  const int NNodes = Param.GetNodes();
1348  // const int NAttrs = Param.GetAttrs();
1349  // const int NSq = NNodes * (NNodes - 1);
1350  GradV.PutAll(0.0);
1351 
1352  TFltV LogSumV;
1353  for(int p = 0; p < 4; p++) {
1354  int Ai = p / 2;
1355  int Aj = p % 2;
1356  LogSumV.Gen(NNodes * 4, 0);
1357 
1358  for(int i = 0; i < NNodes; i++) {
1359  const double LProd = ProdVV(i, 0) - GetAvgThetaLL(i, i, AId, true, false);
1360  const double LSq = SqVV(i, 0) - GetAvgSqThetaLL(i, i, AId, true, false);
1361  const double RProd = ProdVV(i, 1) - GetAvgThetaLL(i, i, AId, false, true);
1362  const double RSq = SqVV(i, 1) - GetAvgSqThetaLL(i, i, AId, false, true);
1363 
1364  LogSumV.Add(LProd + log(GetProbMu(i, i, AId, Ai, Aj, true, false)));
1365  LogSumV.Add(LSq + log(GetProbMu(i, i, AId, Ai, Aj, true, false)) + log(CurMtx.At(p)));
1366  LogSumV.Add(RProd + log(GetProbMu(i, i, AId, Ai, Aj, false, true)));
1367  LogSumV.Add(RSq + log(GetProbMu(i, i, AId, Ai, Aj, false, true)) + log(CurMtx.At(p)));
1368  }
1369  double LogSum = LogSumExp(LogSumV);
1370  GradV[p] -= (NNodes - 1) * 0.5 * exp(LogSum);
1371  }
1372 
1373  for(TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
1374  const int NId1 = EI.GetSrcNId();
1375  const int NId2 = EI.GetDstNId();
1376  const double ProdOne = GetProdLinWeight(NId1, NId2) - GetThetaLL(NId1, NId2, AId);
1377  const double SqOne = GetProdSqWeight(NId1, NId2) - GetSqThetaLL(NId1, NId2, AId);
1378 
1379  for(int p = 0; p < 4; p++) {
1380  int Ai = p / 2;
1381  int Aj = p % 2;
1382  double Prob = GetProbPhi(NId1, NId2, AId, Ai, Aj);
1383  GradV[p] += Prob / CurMtx.At(p);
1384  GradV[p] += Prob * exp(ProdOne);
1385  GradV[p] += Prob * exp(SqOne) * CurMtx.At(p);
1386  }
1387  }
1388 
1389 #if 0
1390  const double Prod = ProdVV(0, 0) - GetAvgThetaLL(0, 0, AId, false, false);
1391  const double Sq = SqVV(0, 0) - GetAvgSqThetaLL(0, 0, AId, false, false);
1392  for(int p = 0; p < 4; p++) {
1393  int Ai = p / 2;
1394  int Aj = p % 2;
1395  GradV[p] -= NSq * exp(Prod) * GetProbMu(0, 0, AId, Ai, Aj, false, false);
1396  GradV[p] -= NSq * exp(Sq) * GetProbMu(0, 0, AId, Ai, Aj, false, false) * CurMtx.At(p);
1397  }
1398 
1399  for(TNGraph::TEdgeI EI = Graph->BegEI(); EI < Graph->EndEI(); EI++) {
1400  const int NId1 = EI.GetSrcNId();
1401  const int NId2 = EI.GetDstNId();
1402  const double ProdOne = GetProdLinWeight(NId1, NId2) - GetThetaLL(NId1, NId2, AId);
1403  const double SqOne = GetProdSqWeight(NId1, NId2) - GetSqThetaLL(NId1, NId2, AId);
1404 
1405  for(int p = 0; p < 4; p++) {
1406  int Ai = p / 2;
1407  int Aj = p % 2;
1408  double Prob = GetProbPhi(NId1, NId2, AId, Ai, Aj);
1409 // GradV[p] += Prob / CurMtx.At(p);
1410 // GradV[p] += Prob * exp(ProdOne);
1411 // GradV[p] += Prob * exp(SqOne) * CurMtx.At(p);
1412  }
1413  }
1414 #endif
1415 }
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
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TEdgeI EndEI() const
Returns an iterator referring to the past-the-end edge in the graph.
Definition: graph.h:588
TEdgeI BegEI() const
Returns an iterator referring to the first edge in the graph.
Definition: graph.h:586
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
const double LogSumExp(const double LogVal1, const double LogVal2)
Definition: mag.cpp:675
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
Edge iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:426
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
Definition: mag.cpp:570
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
PNGraph Graph
Definition: mag.h:345
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

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVVec< TVal, TSizeTy >::GetYDim(), and PhiVV.

Referenced by UpdateApxPhiMI(), and UpdatePhiMI().

703  {
704  const int NAttrs = CntVV.GetYDim();
705  double Grad = DeltaQ - log(x) + log(1.0-x);
706 
707  for(int l = 0; l < NAttrs; l++) {
708  if(l == AId) { continue; }
709  const double C0 = PhiVV(NId, l);
710  const double C1 = 1.0 - C0;
711  Grad -= Lambda * C0 * log(CntVV(0, l) + C0 * x);
712  Grad -= Lambda * C1 * log(CntVV(1, l) + C1 * x);
713  Grad += Lambda * C0 * log(CntVV(2, l) + C0 * (1-x));
714  Grad += Lambda * C1 * log(CntVV(3, l) + C1 * (1-x));
715  Grad -= Lambda * log(CntVV(0, l) + CntVV(1, l) + x);
716  Grad += Lambda * log(CntVV(2, l) + CntVV(3, l) + (1-x));
717  }
718 
719  return Grad;
720 }
TSizeTy GetYDim() const
Definition: ds.h:2250
TFltVV PhiVV
Definition: mag.h:343

Here is the call graph for this function:

Here is the caller graph for this function:

void TMAGFitBern::Init ( const TFltV MuV,
const TMAGAffMtxV AffMtxV 
)

Definition at line 452 of file mag.cpp.

References TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), KnownVV, Param, PhiVV, TVVec< TVal, TSizeTy >::PutAll(), TMAGParam< TNodeAttr >::SetMtxV(), and TMAGParam< TNodeAttr >::SetNodeAttr().

452  {
453  TMAGNodeBern DistParam(MuV);
454  Param.SetNodeAttr(DistParam);
455  Param.SetMtxV(AffMtxV);
456 
457  const int NNodes = Param.GetNodes();
458  const int NAttrs = Param.GetAttrs();
459 
460  PhiVV.Gen(NNodes, NAttrs);
461  KnownVV.Gen(NNodes, NAttrs);
462  KnownVV.PutAll(false);
463 }
void SetNodeAttr(const TNodeAttr &Dist)
Definition: mag.h:176
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TBoolVV KnownVV
Definition: mag.h:344
void PutAll(const TVal &Val)
Definition: ds.h:2267
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184
TFltVV PhiVV
Definition: mag.h:343
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246

Here is the call graph for this function:

void TMAGFitBern::MakeCCDF ( const TFltPrV RawV,
TFltPrV CcdfV 
)

Definition at line 1703 of file mag.cpp.

References TVec< TVal, TSizeTy >::Add(), TVec< TVal, TSizeTy >::Gen(), IAssert, and TVec< TVal, TSizeTy >::Len().

Referenced by PlotProperties().

1703  {
1704  double Total = 0.0;
1705  CcdfV.Gen(RawV.Len(), 0);
1706 
1707  for(int i = 0; i < RawV.Len(); i++) {
1708  if(RawV[i].Val2 <= 0) { continue; }
1709  Total += RawV[i].Val2;
1710  CcdfV.Add(RawV[i]);
1711  IAssert(RawV[i].Val2 > 0);
1712  }
1713  for(int i = 1; i < CcdfV.Len(); i++) {
1714  CcdfV[i].Val2 += CcdfV[i-1].Val2;
1715  }
1716 
1717  for(int i = CcdfV.Len() - 1; i > 0; i--) {
1718  CcdfV[i].Val2 = (Total - CcdfV[i-1].Val2) ;
1719  if(CcdfV[i].Val2 <= 0) { printf("CCDF = %f\n", double(CcdfV[i].Val2));}
1720  IAssert(CcdfV[i].Val2 > 0);
1721  }
1722  CcdfV[0].Val2 = Total;
1723 // CcdfV[0].Val2 = 1.0;
1724 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602

Here is the call graph for this function:

Here is the caller graph for this function:

const bool TMAGFitBern::NextPermutation ( TIntV IndexV) const
private

Definition at line 1867 of file mag.cpp.

References TVec< TVal, TSizeTy >::GetSubValV(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Sort().

1867  {
1868  const int NAttrs = IndexV.Len();
1869  int Pos = NAttrs - 1;
1870  while(Pos > 0) {
1871  if(IndexV[Pos-1] < IndexV[Pos]) {
1872  break;
1873  }
1874  Pos--;
1875  }
1876  if(Pos == 0) {
1877  return false;
1878  }
1879 
1880  int Val = NAttrs, NewPos = -1;
1881  for(int i = Pos; i < NAttrs; i++) {
1882  if(IndexV[i] > IndexV[Pos - 1] && IndexV[i] < Val) {
1883  NewPos = i;
1884  Val = IndexV[i];
1885  }
1886  }
1887  IndexV[NewPos] = IndexV[Pos - 1];
1888  IndexV[Pos - 1] = Val;
1889 
1890  TIntV SubIndexV;
1891  IndexV.GetSubValV(Pos, NAttrs - 1, SubIndexV);
1892  SubIndexV.Sort(true);
1893  for(int i = Pos; i < NAttrs; i++) {
1894  IndexV[i] = SubIndexV[i - Pos];
1895  }
1896 
1897  return true;
1898 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
void GetSubValV(const TSizeTy &BValN, const TSizeTy &EValN, TVec< TVal, TSizeTy > &ValV) const
Fills ValV with elements at positions BValN...EValN.
Definition: ds.h:1170

Here is the call graph for this function:

void TMAGFitBern::NormalizeAffMtxV ( TMAGAffMtxV MtxV,
const bool  UseMu = false 
)

Definition at line 1454 of file mag.cpp.

References AvgPhiV, TNGraph::GetEdges(), GetMuV(), TMAGParam< TNodeAttr >::GetNodes(), Graph, TVec< TVal, TSizeTy >::Len(), NormConst, and Param.

Referenced by DoEMAlg(), UnNormalizeAffMtxV(), and UpdateAffMtxV().

1454  {
1455  const int NNodes = Param.GetNodes();
1456  const int NAttrs = MtxV.Len();
1457  TFltV MuV = GetMuV();
1458  double Product = 1.0, ExpEdge = NNodes * (NNodes - 1);
1459 
1460  TFltV SumV(NAttrs), EdgeSumV(NAttrs);
1461  SumV.PutAll(0.0); EdgeSumV.PutAll(0.0);
1462  for(int l = 0; l < NAttrs; l++) {
1463  double Mu = (UseMu) ? double(MuV[l]) : (AvgPhiV[l] / double(NNodes));
1464  EdgeSumV[l] += Mu * Mu * MtxV[l].At(0, 0);
1465  EdgeSumV[l] += Mu * (1.0-Mu) * MtxV[l].At(0, 1);
1466  EdgeSumV[l] += Mu * (1.0-Mu) * MtxV[l].At(1, 0);
1467  EdgeSumV[l] += (1.0-Mu) * (1.0-Mu) * MtxV[l].At(1, 1);
1468  SumV[l] = SumV[l] + MtxV[l].At(0, 0);
1469  SumV[l] = SumV[l] + MtxV[l].At(0, 1);
1470  SumV[l] = SumV[l] + MtxV[l].At(1, 0);
1471  SumV[l] = SumV[l] + MtxV[l].At(1, 1);
1472  Product *= SumV[l];
1473  ExpEdge *= EdgeSumV[l];
1474  }
1475  ExpEdge = Graph->GetEdges() / ExpEdge;
1476  NormConst *= Product;
1477 // NormConst = ExpEdge;
1478  Product = 1.0;
1479 // Product = pow(Product * ExpEdge, 1.0 / double(NAttrs));
1480 
1481  for(int l = 0; l < NAttrs; l++) {
1482  for(int p = 0; p < 4; p++) {
1483  MtxV[l].At(p) = MtxV[l].At(p) * Product / SumV[l];
1484 // MtxV[l].At(p) = MtxV[l].At(p) * Product / MtxV[l].At(0, 0);
1485 // MtxV[l].At(p) = MtxV[l].At(p) * Product;
1486 // if(MtxV[l].At(p) > 0.9999) { MtxV[l].At(p) = 0.9999; }
1487 // if(MtxV[l].At(p) < 0.0001) { MtxV[l].At(p) = 0.0001; }
1488  }
1489  }
1490 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TFltV & GetMuV() const
Definition: mag.h:379
TFlt NormConst
Definition: mag.h:350
TFltV AvgPhiV
Definition: mag.h:348
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVVec< TVal, TSizeTy >::GetYDim(), and PhiVV.

Referenced by UpdateApxPhiMI(), and UpdatePhiMI().

722  {
723  const int NAttrs = CntVV.GetYDim();
724  double Val = x*(Q0 - log(x)) + (1-x)*(Q1 - log(1.0-x));
725 
726  for(int l = 0; l < NAttrs; l++) {
727  if(l == AId) { continue; }
728  const double C0 = PhiVV(NId, l);
729  const double C1 = 1.0 - C0;
730  Val -= Lambda * (CntVV(0, l) + C0 * x) * log(CntVV(0, l) + C0 * x);
731  Val -= Lambda * (CntVV(1, l) + C1 * x) * log(CntVV(1, l) + C1 * x);
732  Val -= Lambda * (CntVV(2, l) + C0 * (1-x)) * log(CntVV(2, l) + C0 * (1-x));
733  Val -= Lambda * (CntVV(3, l) + C1 * (1-x)) * log(CntVV(3, l) + C1 * (1-x));
734  Val += Lambda * (CntVV(0, l) + CntVV(1, l) + x) * log(CntVV(0, l) + CntVV(1, l) + x);
735  Val += Lambda * (CntVV(2, l) + CntVV(3, l) + 1 - x) * log(CntVV(2, l) + CntVV(3, l) + (1-x));
736 
737  if(!(CntVV(0, l) > 0)) printf("CntVV(0, %d) = %.2f\n", l, double(CntVV(0, l)));
738  if(!(CntVV(1, l) > 0)) printf("CntVV(1, %d) = %.2f\n", l, double(CntVV(1, l)));
739  if(!(CntVV(2, l) > 0)) printf("CntVV(2, %d) = %.2f\n", l, double(CntVV(2, l)));
740  if(!(CntVV(3, l) > 0)) printf("CntVV(3, %d) = %.2f\n", l, double(CntVV(3, l)));
741  }
742 
743  return Val;
744 }
TSizeTy GetYDim() const
Definition: ds.h:2250
TFltVV PhiVV
Definition: mag.h:343

Here is the call graph for this function:

Here is the caller graph for this function:

void TMAGFitBern::PlotProperties ( const TStr FNm)

Definition at line 1727 of file mag.cpp.

References TGStatVec::Add(), TGnuPlot::AddCmd(), TGnuPlot::AddPlot(), TGStatVec::At(), TSnap::DelZeroDegNodes(), TMAGParam< TNodeAttr >::GenMAG(), TMAGParam< TNodeAttr >::GetAttrs(), TNGraph::GetEdges(), TMAGParam< TNodeAttr >::GetMtxV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), gpsLog10XY, gpwLines, Graph, gsdClustCf, gsdHops, gsdInDeg, gsdOutDeg, gsdSngVal, gsdSngVec, gsdTriadPart, gsdWcc, TGStatVec::Len(), MakeCCDF(), Param, PhiVV, TGnuPlot::SaveEps(), TMAGParam< TNodeAttr >::SetMtxV(), TMAGParam< TNodeAttr >::SetNodeAttr(), TGnuPlot::SetScale(), TGnuPlot::SetXYLabel(), TGnuPlot::ShowGrid(), and tmuNodes.

1727  {
1728  const int NNodes = Param.GetNodes();
1729  const int NAttrs = Param.GetAttrs();
1730  TMAGParam<TMAGNodeBern> MAGGen(NNodes, NAttrs);
1731  TMAGNodeBern MAGNode = Param.GetNodeAttr();
1732  MAGGen.SetNodeAttr(MAGNode);
1733  TMAGAffMtxV MtxV; Param.GetMtxV(MtxV);
1734  MAGGen.SetMtxV(MtxV);
1735 
1736  PNGraph TrG = new TNGraph;
1737  *TrG = *Graph;
1738 
1739  TIntVV AttrVV(NNodes, NAttrs);
1740  for(int i = 0; i < NNodes; i++) {
1741  for(int j = 0; j < NAttrs; j++) {
1742  if(PhiVV(i, j) > TMAGNodeBern::Rnd.GetUniDev()) AttrVV(i, j) = 0;
1743  else AttrVV(i, j) = 1;
1744  }
1745  }
1746  PNGraph MAG = MAGGen.GenMAG(AttrVV, true, 10000);
1747 // PNGraph MAG = MAGGen.GenAttrMAG(AttrVV, true, 10000);
1748  printf("%d edges created for MAG...\n", MAG->GetEdges());
1749 
1752 
1754 
1755  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");;
1756 
1757  InDegP.SetXYLabel("Degree", "# of nodes");
1758  OutDegP.SetXYLabel("Degree", "# of nodes");
1759  SvalP.SetXYLabel("Rank", "Singular value");
1760  SvecP.SetXYLabel("Rank", "Primary SngVec component");
1761  WccP.SetXYLabel("Size of component", "# of components");
1762  CcfP.SetXYLabel("Degree", "Clustering coefficient");
1763  HopP.SetXYLabel("Hops", "# of node pairs");
1764  TriadP.SetXYLabel("# of triads", "# of participating nodes");
1765 
1766  InDegP.SetScale(gpsLog10XY); InDegP.AddCmd("set key top right");
1767  OutDegP.SetScale(gpsLog10XY); OutDegP.AddCmd("set key top right");
1768  SvalP.SetScale(gpsLog10XY); SvalP.AddCmd("set key top right");
1769  SvecP.SetScale(gpsLog10XY); SvecP.AddCmd("set key top right");
1770  CcfP.SetScale(gpsLog10XY); CcfP.AddCmd("set key top right");
1771  HopP.SetScale(gpsLog10XY); HopP.AddCmd("set key top right");
1772  TriadP.SetScale(gpsLog10XY); TriadP.AddCmd("set key top right");
1773  InDegP.ShowGrid(false);
1774  OutDegP.ShowGrid(false);
1775  SvalP.ShowGrid(false);
1776  SvecP.ShowGrid(false);
1777  CcfP.ShowGrid(false);
1778  HopP.ShowGrid(false);
1779  TriadP.ShowGrid(false);
1780 
1781  const TStr Style[2] = {"lt 1 lw 3 lc rgb 'black'", "lt 2 lw 3 lc rgb 'red'"};
1782  const TStr Name[2] = {"Real", "MAG"};
1783  GS.Add(Graph, TSecTm(1), "Real Graph");
1784  GS.Add(MAG, TSecTm(2), "MAG");
1785 
1786  TFltPrV InDegV, OutDegV, SvalV, SvecV, HopV, WccV, CcfV, TriadV;
1787  for(int i = 0; i < GS.Len(); i++) {
1788  MakeCCDF(GS.At(i)->GetDistr(gsdInDeg), InDegV);
1789  MakeCCDF(GS.At(i)->GetDistr(gsdOutDeg), OutDegV);
1790  SvalV = GS.At(i)->GetDistr(gsdSngVal);
1791  SvecV = GS.At(i)->GetDistr(gsdSngVec);
1792  MakeCCDF(GS.At(i)->GetDistr(gsdClustCf), CcfV);
1793  HopV = GS.At(i)->GetDistr(gsdHops);
1794  MakeCCDF(GS.At(i)->GetDistr(gsdTriadPart), TriadV);
1795 
1796  InDegP.AddPlot(InDegV, gpwLines, Name[i], Style[i]);
1797  OutDegP.AddPlot(OutDegV, gpwLines, Name[i], Style[i]);
1798  SvalP.AddPlot(SvalV, gpwLines, Name[i], Style[i]);
1799  SvecP.AddPlot(SvecV, gpwLines, Name[i], Style[i]);
1800  CcfP.AddPlot(CcfV, gpwLines, Name[i], Style[i]);
1801  HopP.AddPlot(HopV, gpwLines, Name[i], Style[i]);
1802  TriadP.AddPlot(TriadV, gpwLines, Name[i], Style[i]);
1803  }
1804 
1805  InDegP.SaveEps(30);
1806  OutDegP.SaveEps(30);
1807  SvalP.SaveEps(30);
1808  SvecP.SaveEps(30);
1809  CcfP.SaveEps(30);
1810  HopP.SaveEps(30);
1811  TriadP.SaveEps(30);
1812 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
Definition: gstat.h:28
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
Definition: bits.h:119
void MakeCCDF(const TFltPrV &RawV, TFltPrV &CcdfV)
Definition: mag.cpp:1703
Definition: gstat.h:29
Graph Statistics Sequence.
Definition: gstat.h:155
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
void DelZeroDegNodes(PGraph &Graph)
Removes all the zero-degree nodes, that isolated nodes, from the graph.
Definition: alg.h:432
Definition: ds.h:2222
Definition: tm.h:14
TFltVV PhiVV
Definition: mag.h:343
Directed graph.
Definition: graph.h:342
Definition: tm.h:81
Definition: gstat.h:28
Definition: dt.h:412
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
static TRnd Rnd
Definition: mag.h:90
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

const void TMAGFitBern::PrepareUpdateAffMtx ( TFltVV ProdVV,
TFltVV SqVV 
)

Definition at line 1532 of file mag.cpp.

References TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetNodes(), GetProdLinWeight(), GetProdSqWeight(), and Param.

Referenced by UpdateAffMtxV().

1532  {
1533  const int NNodes = Param.GetNodes();
1534  ProdVV.Gen(NNodes, NNodes);
1535  SqVV.Gen(NNodes, NNodes);
1536 
1537  for(int i = 0; i < NNodes; i++) {
1538  for(int j = 0; j < NNodes; j++) {
1539  ProdVV(i, j) = GetProdLinWeight(i, j);
1540  SqVV(i, j) = GetProdSqWeight(i, j);
1541  }
1542  }
1543 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246

Here is the call graph for this function:

Here is the caller graph for this function:

const void TMAGFitBern::PrepareUpdateApxAffMtx ( TFltVV ProdVV,
TFltVV SqVV 
)

Definition at line 1545 of file mag.cpp.

References TVVec< TVal, TSizeTy >::Gen(), GetAvgProdLinWeight(), GetAvgProdSqWeight(), TMAGParam< TNodeAttr >::GetNodes(), and Param.

Referenced by UpdateAffMtxV().

1545  {
1546  const int NNodes = Param.GetNodes();
1547  ProdVV.Gen(NNodes, 2);
1548  SqVV.Gen(NNodes, 2);
1549 
1550  for(int i = 0; i < NNodes; i++) {
1551  ProdVV(i, 0) = GetAvgProdLinWeight(i, i, true, false);
1552  ProdVV(i, 1) = GetAvgProdLinWeight(i, i, false, true);
1553  SqVV(i, 0) = GetAvgProdSqWeight(i, i, true, false);
1554  SqVV(i, 1) = GetAvgProdSqWeight(i, i, false, true);
1555  }
1556 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetAvgProdSqWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:664
const double GetAvgProdLinWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
Definition: mag.cpp:642
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246

Here is the call graph for this function:

Here is the caller graph for this function:

void TMAGFitBern::RandomInit ( const TFltV MuV,
const TMAGAffMtxV AffMtxV,
const int &  Seed 
)

Definition at line 510 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), TRnd::GetUniDev(), KnownVV, Param, PhiVV, TVec< TVal, TSizeTy >::PutAll(), TVVec< TVal, TSizeTy >::PutAll(), TRnd::PutSeed(), TMAGNodeBern::Rnd, TMAGParam< TNodeAttr >::SetMtxV(), and TMAGParam< TNodeAttr >::SetNodeAttr().

510  {
511  TRnd& Rnd = TMAGNodeBern::Rnd;
512  Rnd.PutSeed(Seed);
513 
514  TFltV InitMuV = MuV; InitMuV.PutAll(0.5);
515  TMAGNodeBern DistParam(InitMuV);
516  Param.SetMtxV(AffMtxV);
517 
518  const int NNodes = Param.GetNodes();
519  const int NAttrs = Param.GetAttrs();
520 
521  PhiVV.Gen(NNodes, NAttrs);
522  KnownVV.Gen(NNodes, NAttrs);
523  KnownVV.PutAll(false);
524 
525  for(int i = 0; i < NNodes; i++) {
526  for(int l = 0; l < NAttrs; l++) {
527  PhiVV.At(i, l) = Rnd.GetUniDev();
528 // PhiVV.At(i, l) = 0.5;
529  }
530  }
531 
532  TMAGAffMtxV RndMtxV = AffMtxV;
533  for(int l = 0; l < NAttrs; l++) {
534  for(int p = 0; p < 4; p++) {
535  RndMtxV[l].At(p) = TMAGNodeBern::Rnd.GetUniDev();
536  if(RndMtxV[l].At(p) < 0.1) { RndMtxV[l].At(p) = 0.1; }
537  if(RndMtxV[l].At(p) > 0.9) { RndMtxV[l].At(p) = 0.9; }
538  }
539  RndMtxV[l].At(0, 1) = RndMtxV[l].At(1, 0);
540  }
541 
542  printf("\n");
543  for(int l = 0; l < NAttrs; l++) {
544  printf("AffMtx = %s\n", RndMtxV[l].GetMtxStr().GetCStr());
545  }
546  Param.SetMtxV(RndMtxV);
547  Param.SetNodeAttr(DistParam);
548 }
void SetNodeAttr(const TNodeAttr &Dist)
Definition: mag.h:176
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
Definition: dt.h:11
TBoolVV KnownVV
Definition: mag.h:344
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
Definition: ds.h:1229
void PutAll(const TVal &Val)
Definition: ds.h:2267
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184
TFltVV PhiVV
Definition: mag.h:343
void PutSeed(const int &_Seed)
Definition: dt.cpp:18
static TRnd Rnd
Definition: mag.h:90
double GetUniDev()
Definition: dt.h:30
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

void TMAGFitBern::SaveTxt ( const TStr FNm)

Definition at line 423 of file mag.cpp.

References TMAGParam< TNodeAttr >::GetAttrs(), TStr::GetCStr(), TMAGParam< TNodeAttr >::GetMtxV(), GetMuV(), TMAGParam< TNodeAttr >::GetNodes(), Param, and PhiVV.

423  {
424  const int NNodes = Param.GetNodes();
425  const int NAttrs = Param.GetAttrs();
426  const TFltV MuV = GetMuV();
427  TMAGAffMtxV MtxV;
428  Param.GetMtxV(MtxV);
429 
430  FILE *fp = fopen(FNm.GetCStr(), "w");
431  for(int l = 0; l < NAttrs; l++) {
432  fprintf(fp, "%.4f\t", double(MuV[l]));
433  for(int row = 0; row < 2; row++) {
434  for(int col = 0; col < 2; col++) {
435  fprintf(fp, " %.4f", double(MtxV[l].At(row, col)));
436  }
437  fprintf(fp, (row == 0) ? ";" : "\n");
438  }
439  }
440  fclose(fp);
441 
442  fp = fopen((FNm + "f").CStr(), "w");
443  for(int i = 0; i < NNodes; i++) {
444  for(int l = 0; l < NAttrs; l++) {
445  fprintf(fp, "%f ", double(PhiVV(i, l)));
446  }
447  fprintf(fp, "\n");
448  }
449  fclose(fp);
450 }
char * GetCStr() const
Definition: dt.h:685
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
const TFltV & GetMuV() const
Definition: mag.h:379
TFltVV PhiVV
Definition: mag.h:343
const int GetNodes() const
Definition: mag.h:171

Here is the call graph for this function:

void TMAGFitBern::SetAlgConf ( const bool  EStep = true,
const bool  MStep = true 
)
inline

Definition at line 387 of file mag.h.

References ESpeedUp, and MSpeedUp.

387 { ESpeedUp = EStep; MSpeedUp = MStep; }
bool ESpeedUp
Definition: mag.h:347
bool MSpeedUp
Definition: mag.h:347
void TMAGFitBern::SetDebug ( const bool  _Debug)
inline

Definition at line 386 of file mag.h.

References Debug.

386 { Debug = _Debug; }
bool Debug
Definition: mag.h:347
void TMAGFitBern::SetGraph ( const PNGraph GraphPt)

Definition at line 385 of file mag.cpp.

References TNGraph::GetNIdV(), TNGraph::GetNodes(), TSnap::GetSubGraph(), Graph, IAssert, and TNGraph::IsNode().

Referenced by TMAGFitBern().

385  {
386  Graph = GraphPt;
387  bool NodesOk = true;
388  // check that nodes IDs are {0,1,..,Nodes-1}
389  for (int nid = 0; nid < Graph->GetNodes(); nid++) {
390  if (! Graph->IsNode(nid)) { NodesOk=false; break; } }
391  if (! NodesOk) {
392  TIntV NIdV; GraphPt->GetNIdV(NIdV);
393  Graph = TSnap::GetSubGraph(GraphPt, NIdV, true);
394  for (int nid = 0; nid < Graph->GetNodes(); nid++) {
395  IAssert(Graph->IsNode(nid)); }
396  }
397 }
#define IAssert(Cond)
Definition: bd.h:262
void GetNIdV(TIntV &NIdV) const
Gets a vector IDs of all nodes in the graph.
Definition: graph.cpp:376
int GetNodes() const
Returns the number of nodes in the graph.
Definition: graph.h:499
PUNGraph GetSubGraph(const PUNGraph &Graph, const TIntV &NIdV, const bool &RenumberNodes)
Returns an induced subgraph of an undirected graph Graph with NIdV nodes with an optional node renumb...
Definition: subgraph.cpp:7
bool IsNode(const int &NId) const
Tests whether ID NId is a node.
Definition: graph.h:542
PNGraph Graph
Definition: mag.h:345

Here is the call graph for this function:

Here is the caller graph for this function:

void TMAGFitBern::SetMtxV ( const TMAGAffMtxV MtxV)
inline

Definition at line 382 of file mag.h.

References Param, and TMAGParam< TNodeAttr >::SetMtxV().

382 { Param.SetMtxV(MtxV); }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184

Here is the call graph for this function:

void TMAGFitBern::SetMuV ( const TFltV MuV)
inline

Definition at line 380 of file mag.h.

References TMAGParam< TNodeAttr >::GetNodeAttr(), Param, TMAGNodeBern::SetMuV(), and TMAGParam< TNodeAttr >::SetNodeAttr().

380 { TMAGNodeBern Dist = Param.GetNodeAttr(); Dist.SetMuV(MuV); Param.SetNodeAttr(Dist); }
void SetNodeAttr(const TNodeAttr &Dist)
Definition: mag.h:176
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void SetMuV(const TFltV &_MuV)
Definition: mag.h:102
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177

Here is the call graph for this function:

void TMAGFitBern::SetPhiVV ( const TIntVV AttrVV,
const int  KnownIds = 0 
)

Definition at line 399 of file mag.cpp.

References TVVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetNodes(), KnownVV, Param, PhiVV, and TVVec< TVal, TSizeTy >::PutY().

399  {
400  const int NNodes = Param.GetNodes();
401  const int NAttrs = Param.GetAttrs();
402 
403  PhiVV.Gen(NNodes, NAttrs);
404  KnownVV.Gen(NNodes, NAttrs);
405 
406  for(int l = 0; l < NAttrs; l++) {
407  for(int i = 0; i < NNodes; i++) {
408  if(int(AttrVV(i, l)) == 0) {
409  PhiVV(i, l) = 0.9999;
410  } else {
411  PhiVV(i, l) = 0.0001;
412  }
413  }
414 
415  if(l < KnownIds) {
416  KnownVV.PutY(l, true);
417  } else {
418  KnownVV.PutY(l, false);
419  }
420  }
421 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
TBoolVV KnownVV
Definition: mag.h:344
TFltVV PhiVV
Definition: mag.h:343
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246
void PutY(const TSizeTy &Y, const TVal &Val)
Definition: ds.h:2270

Here is the call graph for this function:

void TMAGFitBern::SetPhiVV ( const TFltVV AttrVV,
const int  KnownIds = 0 
)
inline

Definition at line 394 of file mag.h.

References TVVec< TVal, TSizeTy >::Gen(), TVVec< TVal, TSizeTy >::GetXDim(), TVVec< TVal, TSizeTy >::GetYDim(), KnownVV, PhiVV, TVVec< TVal, TSizeTy >::PutAll(), and TVVec< TVal, TSizeTy >::PutY().

394 { PhiVV = AttrVV; KnownVV.Gen(PhiVV.GetXDim(), PhiVV.GetYDim()); KnownVV.PutAll(false); for(int i = 0; i < KnownIds; i++) { KnownVV.PutY(i, true); } }
TBoolVV KnownVV
Definition: mag.h:344
TSizeTy GetYDim() const
Definition: ds.h:2250
void PutAll(const TVal &Val)
Definition: ds.h:2267
TFltVV PhiVV
Definition: mag.h:343
TSizeTy GetXDim() const
Definition: ds.h:2249
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
Definition: ds.h:2246
void PutY(const TSizeTy &Y, const TVal &Val)
Definition: ds.h:2270

Here is the call graph for this function:

void TMAGFitBern::SortAttrOrdering ( const TFltV TrueMuV,
TIntV IndexV 
) const

Definition at line 1828 of file mag.cpp.

References CountAttr(), TVec< TVal, TSizeTy >::Gen(), TVec< TVal, TSizeTy >::Len(), and TVec< TVal, TSizeTy >::Swap().

1828  {
1829  const int NAttrs = TrueMuV.Len();
1830  // const int NNodes = PhiVV.GetXDim();
1831  TFltV EstMuV, SortedTrueMuV, SortedEstMuV, TrueIdxV, EstIdxV;
1832  IndexV.Gen(NAttrs);
1833  TrueIdxV.Gen(NAttrs);
1834  EstIdxV.Gen(NAttrs);
1835 
1836  for(int l = 0; l < NAttrs; l++) {
1837  TrueIdxV[l] = l;
1838  EstIdxV[l] = l;
1839  }
1840 
1841  CountAttr(EstMuV);
1842  SortedTrueMuV = TrueMuV;
1843  SortedEstMuV = EstMuV;
1844  for(int i = 0; i < NAttrs; i++) {
1845  if(SortedTrueMuV[i] > 0.5) { SortedTrueMuV[i] = 1.0 - SortedTrueMuV[i]; }
1846  if(SortedEstMuV[i] > 0.5) { SortedEstMuV[i] = 1.0 - SortedEstMuV[i]; }
1847  }
1848 
1849  for(int i = 0; i < NAttrs; i++) {
1850  for(int j = i+1; j < NAttrs; j++) {
1851  if(SortedTrueMuV[i] < SortedTrueMuV[j]) {
1852  SortedTrueMuV.Swap(i, j);
1853  TrueIdxV.Swap(i, j);
1854  }
1855  if(SortedEstMuV[i] < SortedEstMuV[j]) {
1856  EstIdxV.Swap((int)SortedEstMuV[i], (int)SortedEstMuV[j]);
1857  SortedEstMuV.Swap(i, j);
1858  }
1859  }
1860  }
1861 
1862  for(int l = 0; l < NAttrs; l++) {
1863  IndexV[l] = (int)TrueIdxV[(int)EstIdxV[l]];
1864  }
1865 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void CountAttr(TFltV &EstMuV) const
Definition: mag.cpp:1814
void Swap(TVec< TVal, TSizeTy > &Vec)
Swaps the contents of the vector with Vec.
Definition: ds.h:1101
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523

Here is the call graph for this function:

void TMAGFitBern::UnNormalizeAffMtxV ( TMAGAffMtxV MtxV,
const bool  UseMu = false 
)

Definition at line 1492 of file mag.cpp.

References TNGraph::GetEdges(), GetMuV(), TMAGParam< TNodeAttr >::GetNodes(), Graph, TVec< TVal, TSizeTy >::Len(), NormalizeAffMtxV(), NormConst, Param, and TVec< TVal, TSizeTy >::Sort().

Referenced by DoEMAlg().

1492  {
1493  const int NNodes = Param.GetNodes();
1494  const int NAttrs = MtxV.Len();
1495  TFltIntPrV MaxEntV(NAttrs);
1496  TFltV MuV = GetMuV();
1497  NormalizeAffMtxV(MtxV, UseMu);
1498 
1499  double ExpEdge = NNodes * (NNodes - 1);
1500  for(int l = 0; l < NAttrs; l++) {
1501  double Mu = MuV[l];
1502  double EdgeSum = Mu * Mu * MtxV[l].At(0, 0);
1503  EdgeSum += Mu * (1.0-Mu) * MtxV[l].At(0, 1);
1504  EdgeSum += Mu * (1.0-Mu) * MtxV[l].At(1, 0);
1505  EdgeSum += (1.0-Mu) * (1.0-Mu) * MtxV[l].At(1, 1);
1506  ExpEdge *= EdgeSum;
1507  }
1508  NormConst = double(Graph->GetEdges()) / ExpEdge;
1509 // NormConst *= ExpEdge;
1510 
1511  for(int l = 0; l < NAttrs; l++) {
1512  MaxEntV[l] = TFltIntPr(-1, l);
1513  for(int p = 0; p < 4; p++) {
1514  if(MaxEntV[l].Val1 < MtxV[l].At(p)) { MaxEntV[l].Val1 = MtxV[l].At(p); }
1515  }
1516  }
1517  MaxEntV.Sort(false);
1518 
1519  for(int l = 0; l < NAttrs; l++) {
1520  int CurId = MaxEntV[l].Val2;
1521  double Factor = pow(NormConst, 1.0 / double(NAttrs - l));
1522  double MaxFactor = 0.9999 / MaxEntV[l].Val1;
1523  Factor = (Factor > MaxFactor) ? MaxFactor : Factor;
1524  NormConst = NormConst / Factor;
1525 
1526  for(int p = 0; p < 4; p++) {
1527  MtxV[CurId].At(p) = MtxV[CurId].At(p) * Factor;
1528  }
1529  }
1530 }
TPair< TFlt, TInt > TFltIntPr
Definition: ds.h:97
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void NormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1454
int GetEdges() const
Returns the number of edges in the graph.
Definition: graph.cpp:313
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const TFltV & GetMuV() const
Definition: mag.h:379
void Sort(const bool &Asc=true)
Sorts the elements of the vector.
Definition: ds.h:1318
TFlt NormConst
Definition: mag.h:350
const int GetNodes() const
Definition: mag.h:171
PNGraph Graph
Definition: mag.h:345
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TMAGAffMtx::At(), TMAGParam< TNodeAttr >::GetMtx(), TMAGAffMtx::GetMtxStr(), GradAffMtx(), GradApxAffMtx(), MSpeedUp, and Param.

Referenced by UpdateAffMtxV().

1418  {
1419  double Delta = 0.0;
1420  // const int NNodes = Param.GetNodes();
1421  // const int NAttrs = Param.GetAttrs();
1422  TMAGAffMtx AffMtx = Param.GetMtx(AId);
1423 
1424  TFltV GradV(4);
1425  TFltV HessV(4);
1426  if(MSpeedUp) {
1427  GradApxAffMtx(AId, ProdVV, SqVV, NewMtx, GradV);
1428  } else {
1429  GradAffMtx(AId, ProdVV, SqVV, NewMtx, GradV);
1430  }
1431 
1432  double Ratio = 1.0;
1433  for(int p = 0; p < 4; p++) {
1434  if(fabs(Ratio * LrnRate * GradV[p]) > MaxGrad) {
1435  Ratio = MaxGrad / fabs(LrnRate * GradV[p]);
1436  }
1437  }
1438 
1439  for(int p = 0; p < 4; p++) {
1440  GradV[p] *= (Ratio * LrnRate);
1441  NewMtx.At(p) = AffMtx.At(p) + GradV[p];
1442 // if(NewMtx.At(p) > 0.9999) { NewMtx.At(p) = 0.9999; }
1443  if(NewMtx.At(p) < 0.0001) { NewMtx.At(p) = 0.0001; }
1444  }
1445 
1446  printf(" [Attr = %d]\n", AId);
1447  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());
1448 
1449 // Param.SetMtx(AId, NewMtx);
1450  return Delta;
1451 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const void GradApxAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
Definition: mag.cpp:1346
const void GradAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
Definition: mag.cpp:1320
Definition: mag.h:10
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
TStr GetMtxStr() const
Definition: mag.cpp:63
const double & At(const int &Row, const int &Col) const
Definition: mag.h:41
bool MSpeedUp
Definition: mag.h:347

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVVec< TVal, TSizeTy >::Clr(), TMAGParam< TNodeAttr >::GetAttrs(), TMAGParam< TNodeAttr >::GetMtxV(), TMAGNodeBern::GetMuV(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), MSpeedUp, NormalizeAffMtxV(), Param, PrepareUpdateAffMtx(), PrepareUpdateApxAffMtx(), TMAGParam< TNodeAttr >::SetMtxV(), and UpdateAffMtx().

Referenced by DoMStep().

1558  {
1559  const int NNodes = Param.GetNodes();
1560  const int NAttrs = Param.GetAttrs();
1561  const TMAGNodeBern DistParam = Param.GetNodeAttr();
1562  const TFltV MuV = DistParam.GetMuV();
1563  double Delta = 0.0;
1564  double DecLrnRate = LrnRate, DecMaxGrad = MaxGrad;
1565 
1566  TFltVV ProdVV(NNodes, NNodes), SqVV(NNodes, NNodes);
1567  TMAGAffMtxV NewMtxV, OldMtxV;
1568  Param.GetMtxV(OldMtxV);
1569  Param.GetMtxV(NewMtxV);
1570 
1571  for(int g = 0; g < GradIter; g++) {
1572  if(MSpeedUp) {
1573  PrepareUpdateApxAffMtx(ProdVV, SqVV);
1574  } else {
1575  PrepareUpdateAffMtx(ProdVV, SqVV);
1576  }
1577 
1578  printf(" [Grad step = %d]\n", (g+1));
1579 // for(int l = 0; l < NAttrs; l++) {
1580  for(int l = NReal; l < NAttrs; l++) {
1581  UpdateAffMtx(l, DecLrnRate, DecMaxGrad, Lambda, ProdVV, SqVV, NewMtxV[l]);
1582  Param.SetMtxV(NewMtxV);
1583  }
1584  DecLrnRate *= 0.97;
1585  DecMaxGrad *= 0.97;
1586 
1587  printf("\n");
1588  NormalizeAffMtxV(NewMtxV, true);
1589  Param.SetMtxV(NewMtxV);
1590  }
1591  NormalizeAffMtxV(NewMtxV, true);
1592 
1593  printf( "\nFinal\n");
1594  for(int l = 0; l < NAttrs; l++) {
1595  printf(" [");
1596  for(int p = 0; p < 4; p++) {
1597 // NewMtxV[l].At(p) = NewMtxV[l].At(p) * Product / SumV[l];
1598  Delta += fabs(OldMtxV[l].At(p) - NewMtxV[l].At(p));
1599  printf(" %.4f ", double(NewMtxV[l].At(p)));
1600  }
1601  printf("]\n");
1602  }
1603  Param.SetMtxV(NewMtxV);
1604  ProdVV.Clr(); SqVV.Clr();
1605  return Delta;
1606 }
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
void NormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
Definition: mag.cpp:1454
void GetMtxV(TMAGAffMtxV &MtxV) const
Definition: mag.h:188
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
const void PrepareUpdateApxAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
Definition: mag.cpp:1545
const TFltV & GetMuV() const
Definition: mag.h:103
void SetMtxV(const TMAGAffMtxV &MtxV)
Definition: mag.h:184
const void PrepareUpdateAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
Definition: mag.cpp:1532
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
bool MSpeedUp
Definition: mag.h:347

Here is the call graph for this function:

Here is the caller graph for this function:

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.

References TVec< TVal, TSizeTy >::Add(), TMAGAffMtx::At(), TVVec< TVal, TSizeTy >::At(), AvgPhiPairVV, TVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), GetAvgInCoeff(), GetAvgOutCoeff(), GetAvgSqThetaLL(), GetAvgThetaLL(), GetInCoeff(), TNGraph::TNodeI::GetInDeg(), TNGraph::TNodeI::GetInNId(), TMAGAffMtx::GetLLMtx(), TMAGParam< TNodeAttr >::GetMtx(), TMAGNodeBern::GetMu(), TNGraph::GetNI(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), GetOutCoeff(), TNGraph::TNodeI::GetOutDeg(), TNGraph::TNodeI::GetOutNId(), GetProdLinWeight(), GetProdSqWeight(), TVVec< TVal, TSizeTy >::GetRow(), GetSqThetaLL(), GetThetaLL(), GradPhiMI(), Graph, IAssert, TMAGAffMtx::Len(), TVec< TVal, TSizeTy >::Len(), LogSumExp(), MaxExp, ObjPhiMI(), Param, PhiVV, and TVVec< TVal, TSizeTy >::PutAll().

Referenced by DoEStepApxOneIter().

930  {
931  TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId);
932  const int NNodes = Param.GetNodes();
933  const int NAttrs = Param.GetAttrs();
934  Theta.GetLLMtx(LLTheta);
935  TMAGNodeBern DistParam = Param.GetNodeAttr();
936  const double Mu = DistParam.GetMu(AId);
937 
938  TMAGAffMtx SqTheta(Theta);
939  for(int i = 0; i < Theta.Len(); i++) {
940  SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i);
941  }
942 
943  TFltV ProdV; ProdVV.GetRow(NId, ProdV);
944  ProdV[0] -= GetAvgThetaLL(NId, NId, AId, true, false);
945  ProdV[1] -= GetAvgThetaLL(NId, NId, AId, false, true);
946  ProdV[2] -= log(2.0) + GetAvgSqThetaLL(NId, NId, AId, true, false);
947  ProdV[3] -= log(2.0) + GetAvgSqThetaLL(NId, NId, AId, false, true);
948 
949  // Using log-sum-exp trick
950  double EdgeQ[2], MaxExp[2];
951  TFltV NonEdgeLLV[2];
952  TFltVV CntVV(4, NAttrs); CntVV.PutAll(0.0);
953  for(int i = 0; i < 2; i++) {
954  EdgeQ[i] = 0.0;
955  MaxExp[i] = -DBL_MAX;
956  NonEdgeLLV[i].Gen(4 * NNodes, 0);
957  }
958 
959  for(int F = 0; F < 2; F++) {
960  NonEdgeLLV[F].Add(ProdV[0] + log(GetAvgOutCoeff(NId, AId, F, Theta)));
961  NonEdgeLLV[F].Add(ProdV[1] + log(GetAvgInCoeff(NId, AId, F, Theta)));
962  NonEdgeLLV[F].Add(ProdV[2] + log(GetAvgOutCoeff(NId, AId, F, SqTheta)));
963  NonEdgeLLV[F].Add(ProdV[3] + log(GetAvgInCoeff(NId, AId, F, SqTheta)));
964  }
965  EdgeQ[0] = -(NNodes - 1) * exp(LogSumExp(NonEdgeLLV[0]));
966  EdgeQ[1] = -(NNodes - 1) * exp(LogSumExp(NonEdgeLLV[1]));
967 
968 
969  for(int l = 0; l < NAttrs; l++) {
970  if(l == AId) { continue; }
971  int BgId = (AId > l) ? AId : l;
972  int SmId = (AId + l) - BgId;
973  int SmL = (l < AId) ? 1 : 0;
974  BgId *= 4;
975  CntVV(0, l) = AvgPhiPairVV(SmId, BgId) - PhiVV(NId, AId) * PhiVV(NId, l);
976  CntVV(1+SmL, l) = AvgPhiPairVV(SmId, BgId+1+SmL) - PhiVV(NId, AId) * (1.0-PhiVV(NId, l));
977  CntVV(2-SmL, l) = AvgPhiPairVV(SmId, BgId+2-SmL) - (1.0-PhiVV(NId, AId)) * PhiVV(NId, l);
978  CntVV(3, l) = AvgPhiPairVV(SmId, BgId+3) - (1.0-PhiVV(NId, AId)) * (1.0-PhiVV(NId, l));
979  }
980 
981  TNGraph::TNodeI NI = Graph->GetNI(NId);
982  for(int d = 0; d < NI.GetOutDeg(); d++) {
983  int Out = NI.GetOutNId(d);
984  if(NId == Out) { continue; }
985  double LinW = GetProdLinWeight(NId, Out) - GetThetaLL(NId, Out, AId);
986  double SqW = GetProdSqWeight(NId, Out) - GetSqThetaLL(NId, Out, AId);
987 
988  for(int F = 0; F < 2; F++) {
989  EdgeQ[F] += GetOutCoeff(NId, Out, AId, F, LLTheta);
990  EdgeQ[F] += exp(LinW + log(GetOutCoeff(NId, Out, AId, F, Theta)));
991  EdgeQ[F] += 0.5 * exp(SqW + log(GetOutCoeff(NId, Out, AId, F, SqTheta)));
992  }
993  }
994  for(int d = 0; d < NI.GetInDeg(); d++) {
995  int In = NI.GetInNId(d);
996  if(NId == In) { continue; }
997  double LinW = GetProdLinWeight(In, NId) - GetThetaLL(In, NId, AId);
998  double SqW = GetProdSqWeight(In, NId) - GetSqThetaLL(In, NId, AId);
999 
1000  for(int F = 0; F < 2; F++) {
1001  EdgeQ[F] += GetInCoeff(In, NId, AId, F, LLTheta);
1002  EdgeQ[F] += exp(LinW + log(GetInCoeff(In, NId, AId, F, Theta)));
1003  EdgeQ[F] += 0.5 * exp(SqW + log(GetInCoeff(In, NId, AId, F, SqTheta)));
1004  }
1005  }
1006 
1007  EdgeQ[0] += log(Mu);
1008  EdgeQ[1] += log(1.0 - Mu);
1009  double DeltaQ = EdgeQ[0] - EdgeQ[1];
1010 // printf("(%d, %d) :: Q[0] = %f, Q[1] = %f\n", NId, AId, EdgeQ[0], EdgeQ[1]);
1011 
1012 // double x[] = {0.1, 0.3, 0.5, 0.7, 0.9};
1013  double x[] = {PhiVV(NId, AId)};
1014  TFltV ObjValV; ObjValV.Gen(60, 0);
1015 // for(int n = 0; n < 5; n++) {
1016  for(int n = 0; n < 1; n++) {
1017 // double LrnRate = 0.0002;
1018  double LrnRate = 0.001;
1019  for(int step = 0; step < 50; step++) {
1020 // for(int step = 0; step < 10; step++) {
1021  double Grad = GradPhiMI(x[n], NId, AId, Lambda, DeltaQ, CntVV);
1022  if(Grad > 0.0) { x[n] += LrnRate; }
1023  else { x[n] -= LrnRate; }
1024  if(x[n] > 0.9999) { x[n] = 0.9999; }
1025  if(x[n] < 0.0001) { x[n] = 0.0001; }
1026  if(x[n] == 0.9999 || x[n] == 0.0001) {
1027  break;
1028  }
1029  LrnRate *= 0.995;
1030  }
1031  ObjValV.Add(x[n]);
1032 // ObjValV.Add(PhiVV(NId, AId));
1033  }
1034 
1035  double MaxVal = -DBL_MAX;
1036  int MaxX = -1;
1037 // for(int n = 0; n < 5; n++) {
1038  for(int n = 0; n < ObjValV.Len(); n++) {
1039  double Val = ObjPhiMI(ObjValV[n], NId, AId, Lambda, EdgeQ[0], EdgeQ[1], CntVV);
1040  if(Val > MaxVal) {
1041  MaxVal = Val;
1042  MaxX = n;
1043  } else if(MaxX < 0) {
1044  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);
1045  }
1046  }
1047  IAssert(MaxX >= 0);
1048 
1049  Phi = ObjValV[MaxX];
1050 
1051  return Phi - PhiVV.At(NId, AId);
1052 }
#define IAssert(Cond)
Definition: bd.h:262
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
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetAvgInCoeff(const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:558
TNodeI GetNI(const int &NId) const
Returns an iterator referring to the node of ID NId in the graph.
Definition: graph.h:548
const double GetOutCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:554
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
const int MaxExp
Definition: word2vec.h:9
const double LogSumExp(const double LogVal1, const double LogVal2)
Definition: mag.cpp:675
const double GetInCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:550
int Len() const
Definition: mag.h:27
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
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
double GetMu(const int &Attr) const
Definition: mag.h:105
Definition: mag.h:10
void GetRow(const TSizeTy &RowN, TVec< TVal, TSizeTy > &Vec) const
Definition: ds.h:2381
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
int GetOutDeg() const
Returns out-degree of the current node.
Definition: graph.h:402
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
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
Node iterator. Only forward iteration (operator++) is supported.
Definition: graph.h:379
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
int GetInDeg() const
Returns in-degree of the current node.
Definition: graph.h:400
int GetInNId(const int &NodeN) const
Returns ID of NodeN-th in-node (the node pointing to the current node).
Definition: graph.h:408
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
PNGraph Graph
Definition: mag.h:345
int GetOutNId(const int &NodeN) const
Returns ID of NodeN-th out-node (the node the current node points to).
Definition: graph.h:412
TFltVV AvgPhiPairVV
Definition: mag.h:349
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
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
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::UpdateMu ( const int &  AId)

Definition at line 1299 of file mag.cpp.

References TVVec< TVal, TSizeTy >::At(), AvgPhiV, TMAGNodeBern::GetMu(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), Param, PhiVV, TMAGNodeBern::SetMu(), and TMAGParam< TNodeAttr >::SetNodeAttr().

Referenced by DoMStep().

1299  {
1300  const int NNodes = Param.GetNodes();
1301  TMAGNodeBern DistParam = Param.GetNodeAttr();
1302  const double OldMu = DistParam.GetMu(AId);
1303  double NewMu = 0.0;
1304 
1305  for(int i = 0; i < NNodes; i++) {
1306  NewMu += PhiVV.At(i, AId);
1307  }
1308  AvgPhiV[AId] = NewMu;
1309  NewMu /= double(NNodes);
1310 
1311  printf(" [Posterior Mu] = %.4f\n", NewMu);
1312 
1313  double Delta = fabs(NewMu - OldMu);
1314  DistParam.SetMu(AId, NewMu);
1315  Param.SetNodeAttr(DistParam);
1316 
1317  return Delta;
1318 }
void SetNodeAttr(const TNodeAttr &Dist)
Definition: mag.h:176
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
double GetMu(const int &Attr) const
Definition: mag.h:105
TFltVV PhiVV
Definition: mag.h:343
TFltV AvgPhiV
Definition: mag.h:348
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
void SetMu(const int &Attr, const double &Prob)
Definition: mag.h:104
const int GetNodes() const
Definition: mag.h:171
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

const double TMAGFitBern::UpdatePhi ( const int &  NId,
const int &  AId,
double &  Phi 
)

Definition at line 756 of file mag.cpp.

References TVec< TVal, TSizeTy >::Add(), TMAGAffMtx::At(), TVVec< TVal, TSizeTy >::At(), TVec< TVal, TSizeTy >::Gen(), GetInCoeff(), TMAGAffMtx::GetLLMtx(), TMAGParam< TNodeAttr >::GetMtx(), TMAGNodeBern::GetMu(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), GetOutCoeff(), GetProdLinWeight(), GetProdSqWeight(), GetSqThetaLL(), GetThetaLL(), Graph, TNGraph::IsEdge(), TMAGAffMtx::Len(), LogSumExp(), MaxExp, Param, and PhiVV.

756  {
757  TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId);
758  TMAGAffMtx SqTheta(Theta);
759  const int NNodes = Param.GetNodes();
760  // const int NAttrs = Param.GetAttrs();
761  Theta.GetLLMtx(LLTheta);
762  TMAGNodeBern DistParam = Param.GetNodeAttr();
763  const double Mu = DistParam.GetMu(AId);
764 
765  for(int i = 0; i < Theta.Len(); i++) {
766  SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i);
767  }
768 
769  // Using log-sum-exp trick
770  double EdgeQ[2], NonEdgeQ[2], MaxExp[2];
771  TFltV NonEdgeLLV[2];
772  for(int i = 0; i < 2; i++) {
773  EdgeQ[i] = 0.0;
774  NonEdgeQ[i] = 0.0;
775  MaxExp[i] = -DBL_MAX;
776  NonEdgeLLV[i].Gen(4 * NNodes, 0);
777  }
778 
779  for(int j = 0; j < NNodes; j++) {
780  if(j == NId) { continue; }
781 
782  if(Graph->IsEdge(NId, j)) {
783  EdgeQ[0] += GetOutCoeff(NId, j, AId, 0, LLTheta);
784  EdgeQ[1] += GetOutCoeff(NId, j, AId, 1, LLTheta);
785  } else {
786  double LinW = GetProdLinWeight(NId, j) - GetThetaLL(NId, j, AId);
787  double SqW = GetProdSqWeight(NId, j) - GetSqThetaLL(NId, j, AId);
788 
789  for(int i = 0; i < 2; i++) {
790  NonEdgeLLV[i].Add(LinW + log(GetOutCoeff(NId, j, AId, i, Theta)));
791  NonEdgeLLV[i].Add(SqW + log(GetOutCoeff(NId, j, AId, i, SqTheta)) + log(0.5));
792  }
793  }
794 
795  if(Graph->IsEdge(j, NId)) {
796  EdgeQ[0] += GetInCoeff(j, NId, AId, 0, LLTheta);
797  EdgeQ[1] += GetInCoeff(j, NId, AId, 1, LLTheta);
798  } else {
799  double LinW = GetProdLinWeight(j, NId) - GetThetaLL(j, NId, AId);
800  double SqW = GetProdSqWeight(j, NId) - GetSqThetaLL(j, NId, AId);
801 
802  for(int i = 0; i < 2; i++) {
803  NonEdgeLLV[i].Add(LinW + log(GetInCoeff(j, NId, AId, i, Theta)));
804  NonEdgeLLV[i].Add(SqW + log(GetInCoeff(j, NId, AId, i, SqTheta)) + log(0.5));
805  }
806  }
807  }
808 
809  NonEdgeQ[0] = LogSumExp(NonEdgeLLV[0]);
810  NonEdgeQ[1] = LogSumExp(NonEdgeLLV[1]);
811 
812  double Q[2];
813  Q[0] = log(Mu) + EdgeQ[0] - exp(NonEdgeQ[0]);
814  Q[1] = log(1.0 - Mu) + EdgeQ[1] - exp(NonEdgeQ[1]);
815 // double Q = Q1 - Q0;
816 // printf(" [Phi_{%d}{%d}] :: Q0 = %f, Q1 = %f\n", NId, AId, Q0, Q1);
817 
818 // Phi = 1.0 / (1.0 + exp(Q));
819  Phi = Q[0] - LogSumExp(Q, 2);
820  Phi = exp(Phi);
821 
822  return Phi - PhiVV.At(NId, AId);
823 }
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetOutCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:554
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
const int MaxExp
Definition: word2vec.h:9
const double LogSumExp(const double LogVal1, const double LogVal2)
Definition: mag.cpp:675
const double GetInCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:550
int Len() const
Definition: mag.h:27
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
double GetMu(const int &Attr) const
Definition: mag.h:105
Definition: mag.h:10
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
PNGraph Graph
Definition: mag.h:345
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

const double TMAGFitBern::UpdatePhiMI ( const double &  Lambda,
const int &  NId,
const int &  AId,
double &  Phi 
)

Definition at line 826 of file mag.cpp.

References TVec< TVal, TSizeTy >::Add(), TMAGAffMtx::At(), TVVec< TVal, TSizeTy >::At(), TVec< TVal, TSizeTy >::Gen(), TMAGParam< TNodeAttr >::GetAttrs(), GetInCoeff(), TMAGAffMtx::GetLLMtx(), TMAGParam< TNodeAttr >::GetMtx(), TMAGNodeBern::GetMu(), TMAGParam< TNodeAttr >::GetNodeAttr(), TMAGParam< TNodeAttr >::GetNodes(), GetOutCoeff(), GetProdLinWeight(), GetProdSqWeight(), GetSqThetaLL(), GetThetaLL(), GradPhiMI(), Graph, IAssert, TNGraph::IsEdge(), TMAGAffMtx::Len(), LogSumExp(), MaxExp, ObjPhiMI(), Param, PhiVV, and TVVec< TVal, TSizeTy >::PutAll().

Referenced by DoEStepOneIter().

826  {
827  TMAGAffMtx LLTheta, Theta = Param.GetMtx(AId);
828  TMAGAffMtx SqTheta(Theta);
829  const int NNodes = Param.GetNodes();
830  const int NAttrs = Param.GetAttrs();
831  Theta.GetLLMtx(LLTheta);
832  TMAGNodeBern DistParam = Param.GetNodeAttr();
833  const double Mu = DistParam.GetMu(AId);
834 
835  for(int i = 0; i < Theta.Len(); i++) {
836  SqTheta.At(i) = SqTheta.At(i) * SqTheta.At(i);
837  }
838 
839  // Using log-sum-exp trick
840  double EdgeQ[2], NonEdgeQ[2], MaxExp[2];
841  TFltV NonEdgeLLV[2];
842  TFltVV CntVV(4, NAttrs); CntVV.PutAll(0.0);
843  for(int i = 0; i < 2; i++) {
844  EdgeQ[i] = 0.0;
845  NonEdgeQ[i] = 0.0;
846  MaxExp[i] = -DBL_MAX;
847  NonEdgeLLV[i].Gen(4 * NNodes, 0);
848  }
849 
850  for(int j = 0; j < NNodes; j++) {
851  if(j == NId) { continue; }
852 
853  for(int l = 0; l < NAttrs; l++) {
854  if(l == AId) { continue; }
855  CntVV(0, l) = CntVV(0, l) + PhiVV(j, AId) * PhiVV(j, l);
856  CntVV(1, l) = CntVV(1, l) + PhiVV(j, AId) * (1.0-PhiVV(j, l));
857  CntVV(2, l) = CntVV(2, l) + (1.0-PhiVV(j, AId)) * PhiVV(j, l);
858  CntVV(3, l) = CntVV(3, l) + (1.0-PhiVV(j, AId)) * (1.0-PhiVV(j, l));
859  }
860 
861  if(Graph->IsEdge(NId, j)) {
862  EdgeQ[0] += GetOutCoeff(NId, j, AId, 0, LLTheta);
863  EdgeQ[1] += GetOutCoeff(NId, j, AId, 1, LLTheta);
864  } else {
865  double LinW = GetProdLinWeight(NId, j) - GetThetaLL(NId, j, AId);
866  double SqW = GetProdSqWeight(NId, j) - GetSqThetaLL(NId, j, AId);
867 
868  for(int i = 0; i < 2; i++) {
869  NonEdgeLLV[i].Add(LinW + log(GetOutCoeff(NId, j, AId, i, Theta)));
870  NonEdgeLLV[i].Add(SqW + log(GetOutCoeff(NId, j, AId, i, SqTheta)) + log(0.5));
871  }
872  }
873 
874  if(Graph->IsEdge(j, NId)) {
875  EdgeQ[0] += GetInCoeff(j, NId, AId, 0, LLTheta);
876  EdgeQ[1] += GetInCoeff(j, NId, AId, 1, LLTheta);
877  } else {
878  double LinW = GetProdLinWeight(j, NId) - GetThetaLL(j, NId, AId);
879  double SqW = GetProdSqWeight(j, NId) - GetSqThetaLL(j, NId, AId);
880 
881  for(int i = 0; i < 2; i++) {
882  NonEdgeLLV[i].Add(LinW + log(GetInCoeff(j, NId, AId, i, Theta)));
883  NonEdgeLLV[i].Add(SqW + log(GetInCoeff(j, NId, AId, i, SqTheta)) + log(0.5));
884  }
885  }
886  }
887 
888  NonEdgeQ[0] = LogSumExp(NonEdgeLLV[0]);
889  NonEdgeQ[1] = LogSumExp(NonEdgeLLV[1]);
890 
891  double Q[2];
892  Q[0] = log(Mu) + EdgeQ[0] - exp(NonEdgeQ[0]);
893  Q[1] = log(1.0 - Mu) + EdgeQ[1] - exp(NonEdgeQ[1]);
894  double DeltaQ = Q[0] - Q[1];
895 
896 // double x[] = {0.1, 0.3, 0.5, 0.7, 0.9};
897  double x[] = {PhiVV(NId, AId)};
898 // for(int n = 0; n < 5; n++) {
899  for(int n = 0; n < 1; n++) {
900 // double LrnRate = 0.0002;
901  double LrnRate = 0.001;
902  for(int step = 0; step < 200; step++) {
903  double Grad = GradPhiMI(x[n], NId, AId, Lambda, DeltaQ, CntVV);
904  if(Grad > 0.0) { x[n] += LrnRate; }
905  else { x[n] -= LrnRate; }
906  if(x[n] > 0.9999) { x[n] = 0.9999; }
907  if(x[n] < 0.0001) { x[n] = 0.0001; }
908  LrnRate *= 0.995;
909  }
910  }
911 
912  double MaxVal = -DBL_MAX;
913  int MaxX = -1;
914 // for(int n = 0; n < 5; n++) {
915  for(int n = 0; n < 1; n++) {
916  double Val = ObjPhiMI(x[n], NId, AId, Lambda, Q[0], Q[1], CntVV);
917  if(Val > MaxVal) {
918  MaxVal = Val;
919  MaxX = n;
920  }
921  }
922  IAssert(MaxX >= 0);
923 
924  Phi = x[MaxX];
925 
926  return Phi - PhiVV.At(NId, AId);
927 }
#define IAssert(Cond)
Definition: bd.h:262
const int GetAttrs() const
Definition: mag.h:174
TMAGParam< TMAGNodeBern > Param
Definition: mag.h:346
const double GetOutCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:554
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:609
const double GetProdSqWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:653
const int MaxExp
Definition: word2vec.h:9
const double LogSumExp(const double LogVal1, const double LogVal2)
Definition: mag.cpp:675
const double GetInCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
Definition: mag.cpp:550
int Len() const
Definition: mag.h:27
const double GetProdLinWeight(const int &NId1, const int &NId2) const
Definition: mag.cpp:631
bool IsEdge(const int &SrcNId, const int &DstNId, const bool &IsDir=true) const
Tests whether an edge from node IDs SrcNId to DstNId exists in the graph.
Definition: graph.cpp:363
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
Definition: mag.cpp:587
double GetMu(const int &Attr) const
Definition: mag.h:105
Definition: mag.h:10
TFltVV PhiVV
Definition: mag.h:343
const TMAGAffMtx & GetMtx(const int &Attr) const
Definition: mag.h:187
void GetLLMtx(TMAGAffMtx &LLMtx)
Definition: mag.cpp:74
const TNodeAttr & GetNodeAttr() const
Definition: mag.h:177
const int GetNodes() const
Definition: mag.h:171
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
PNGraph Graph
Definition: mag.h:345
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
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
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2255

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

TFltVV TMAGFitBern::AvgPhiPairVV
private

Definition at line 349 of file mag.h.

Referenced by DoEStepApxOneIter(), TMAGFitBern(), and UpdateApxPhiMI().

TFltV TMAGFitBern::AvgPhiV
private
bool TMAGFitBern::Debug
private

Definition at line 347 of file mag.h.

Referenced by DoEMAlg(), and SetDebug().

bool TMAGFitBern::ESpeedUp
private

Definition at line 347 of file mag.h.

Referenced by DoEStep(), and SetAlgConf().

TBoolVV TMAGFitBern::KnownVV
private
TFltV TMAGFitBern::LLHisV
private

Definition at line 354 of file mag.h.

Referenced by DoEMAlg().

bool TMAGFitBern::MSpeedUp
private

Definition at line 347 of file mag.h.

Referenced by SetAlgConf(), UpdateAffMtx(), and UpdateAffMtxV().

TVec<TMAGAffMtxV> TMAGFitBern::MtxHisV
private

Definition at line 353 of file mag.h.

Referenced by DoEMAlg().

TVec<TFltV> TMAGFitBern::MuHisV
private

Definition at line 352 of file mag.h.

Referenced by DoEMAlg().


The documentation for this class was generated from the following files: