34 void SetRndMtx(
TRnd& Rnd,
const int& PrmMtxDim = 2,
const double& MinProb = 0.0);
37 void SetEpsMtx(
const double& Eps1,
const double& Eps0,
const int& Eps1Val=1,
const int& Eps0Val=0);
41 const double&
At(
const int& Row,
const int& Col)
const {
return SeedMtx[
MtxDim*Row+Col].Val; }
42 double&
At(
const int& Row,
const int& Col) {
return SeedMtx[
MtxDim*Row+Col].Val; }
43 const double&
At(
const int& ValN)
const {
return SeedMtx[ValN].Val; }
44 double&
At(
const int& ValN) {
return SeedMtx[ValN].Val; }
55 void Dump(
const TStr& MtxNm =
TStr(),
const bool& Sort =
false)
const;
104 void SetMu(
const int& Attr,
const double& Prob) {
IAssert(Prob >= 0.0 && Prob <= 1.0);
MuV[Attr] = Prob; }
105 double GetMu(
const int& Attr)
const {
return MuV[Attr]; }
131 void SetBeta(
const int& Attr,
const double& Alpha,
const double& Beta);
145 template <
class TNodeAttr>
182 void SetMtxVal(
const int& Attr,
const int& Row,
const int& Col,
const double& Val) {
AffMtxV[Attr].At(Row, Col) = Val; }
186 const double GetMtxVal(
const int& Attr,
const int& Row,
const int& Col)
const {
return AffMtxV[Attr].At(Row, Col); }
194 template <
class TNodeAttr>
204 template <
class TNodeAttr>
206 NodeAttr.LoadTxt(InFNm);
209 FILE *fp = fopen(InFNm.
CStr(),
"r");
212 while(fgets(buf,
sizeof(buf), fp) != NULL) {
214 char *token = strtok(NULL,
"\r\n");
221 template <
class TNodeAttr>
224 NodeAttr.SaveTxt(OutStrV);
226 FILE *fp = fopen(OutFNm.
CStr(),
"w");
227 fprintf(fp,
"# %d nodes with %d attributes\n", NNodes(), NAttrs());
228 for(
int i = 0; i < NAttrs; i++) {
229 TStr MtxStr = AffMtxV[i].GetMtxStr();
230 fprintf(fp,
"%s & %s\n", OutStrV[i].CStr(), MtxStr.
GetSubStr(1, MtxStr.
Len()-2).CStr());
235 template <
class TNodeAttr>
241 template <
class TNodeAttr>
244 for(
int i = 0; i < NAttrs; i++) {
245 DimV[i] = AffMtxV[i].GetDim();
249 template <
class TNodeAttr>
252 for(
int i = 0; i < NNodes; i++) {
257 TNodeAttr::Rnd.PutSeed(Seed);
261 for(
int i = 0; i < NNodes; i++) {
262 for(
int l = 0; l < NAttrs; l++) {
263 IAssert(AttrVV.
At(i, l) < AffMtxV[l].GetDim());
267 for(
int i = 0; i < NNodes; i++) {
268 for(
int j = i; j < NNodes; j++) {
270 for(
int l = 0; l < NAttrs; l++) {
271 Prob *= AffMtxV[l].At(AttrVV.
At(i, l), AttrVV.
At(j, l));
274 if(TNodeAttr::Rnd.GetUniDev() < Prob) {
276 if(! IsDir && i != j) { Graph->
AddEdge(j, i); }
279 if(IsDir && i != j) {
281 for(
int l = 0; l < NAttrs; l++) {
282 Prob *= AffMtxV[l].At(AttrVV.
At(j, l), AttrVV.
At(i, l));
284 if(TNodeAttr::Rnd.GetUniDev() < Prob) {
294 template <
class TNodeAttr>
296 TNodeAttr::Rnd.PutSeed(Seed);
297 NodeAttr.AttrGen(AttrVV, NNodes);
300 for(
int i = 0; i < NNodes; i++) {
301 for(
int l = 0; l < NAttrs; l++) {
302 IAssert(AttrVV.
At(i, l) < AffMtxV[l].GetDim());
306 PNGraph Graph = GenAttrMAG(AttrVV, IsDir, 0);
358 TMAGFitBern(
const PNGraph& G,
const int& NAttrs) :
PhiVV(G->
GetNodes(), NAttrs),
KnownVV(G->
GetNodes(), NAttrs),
Graph(G),
Param(G->
GetNodes(), NAttrs),
ESpeedUp(true),
MSpeedUp(true),
Debug(false),
AvgPhiV(NAttrs),
AvgPhiPairVV(NAttrs, NAttrs),
NormConst(1.0) { }
364 printf(
"NAttrs = %d\n", NAttrs);
389 void Init(
const TFltV& MuV,
const TMAGAffMtxV& AffMtxV);
391 void RandomInit(
const TFltV& MuV,
const TMAGAffMtxV& AffMtxV,
const int& Seed);
396 const double GetInCoeff(
const int& i,
const int& j,
const int& l,
const int& A,
const TMAGAffMtx& Theta)
const;
398 const double GetOutCoeff(
const int& i,
const int& j,
const int& l,
const int& A,
const TMAGAffMtx& Theta)
const;
400 const double GetProbPhi(
const int& NId1,
const int& NId2,
const int& AId,
const int& Attr1,
const int& Attr2)
const;
401 const double GetProbMu(
const int& NId1,
const int& NId2,
const int& AId,
const int& Attr1,
const int& Attr2,
const bool Left =
false,
const bool Right =
false)
const;
402 const double GetThetaLL(
const int& NId1,
const int& NId2,
const int& AId)
const;
403 const double GetAvgThetaLL(
const int& NId1,
const int& NId2,
const int& AId,
const bool Left =
false,
const bool Right =
false)
const;
404 const double GetSqThetaLL(
const int& NId1,
const int& NId2,
const int& AId)
const;
405 const double GetAvgSqThetaLL(
const int& NId1,
const int& NId2,
const int& AId,
const bool Left =
false,
const bool Right =
false)
const;
407 const double GetAvgProdLinWeight(
const int& NId1,
const int& NId2,
const bool Left =
false,
const bool Right =
false)
const;
409 const double GetAvgProdSqWeight(
const int& NId1,
const int& NId2,
const bool Left =
false,
const bool Right =
false)
const;
411 const double GetEstNoEdgeLL(
const int& NId,
const int& AId)
const;
412 const double GradPhiMI(
const double& x,
const int& NId,
const int& AId,
const double& Lambda,
const double& DeltaQ,
const TFltVV& CntVV);
413 const double ObjPhiMI(
const double& x,
const int& NId,
const int& AId,
const double& Lambda,
const double& Q0,
const double& Q1,
const TFltVV& CntVV);
414 const double UpdatePhiMI(
const double& Lambda,
const int& NId,
const int& AId,
double& Phi);
415 const double UpdateApxPhiMI(
const double& Lambda,
const int& NId,
const int& AId,
double& Phi,
TFltVV& ProdVV);
416 const double UpdatePhi(
const int& NId,
const int& AId,
double& Phi);
417 const double UpdateMu(
const int& AId);
422 const double UpdateAffMtxV(
const int& GradIter,
const double& LrnRate,
const double& MaxGrad,
const double& Lambda,
const int& NReal = 0);
429 double DoEStep(
const TFltV& TrueMuV,
const int& NIter,
double& LL,
const double& Lambda);
430 void DoMStep(
const int& GradIter,
const double& LrnRate,
const double& MaxGrad,
const double& Lambda,
const int& NReal = 0);
431 void DoEMAlg(
const int& NStep,
const int& NEstep,
const int& NMstep,
const double& LrnRate,
const double& MaxGrad,
const double& Lambda,
const double& ReInit,
const int& NReal = 0);
445 static const double ComputeMI(
const TIntVV& AttrV,
const int AId1,
const int AId2);
446 static const double ComputeMI(
const TFltVV& AttrV,
const int AId1,
const int AId2);
455 const double LogSumExp(
const double LogVal1,
const double LogVal2);
457 const double LogSumExp(
const double *LogValArray,
const int Len);
const double ComputeJointAdjLL(const TIntVV &AttrVV) const
const int GetDim(const int &Attr) const
void SetNodeAttr(const TNodeAttr &Dist)
TMAGNodeBern & operator=(const TMAGNodeBern &Dist)
const double GetAvgSqThetaLL(const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const
void LoadTxt(const TStr &InFNm)
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 GetBeta(const int &Attr, double &Alpha, double &Beta) const
TMAGNodeSimple(const TMAGNodeSimple &Simple)
const bool NextPermutation(TIntV &IndexV) const
int GetPrimHashCd() const
Returns primary hash code of the vector. Used by THash.
const int GetNodes() const
const int GetAttrs() const
TMAGParam< TMAGNodeBern > Param
TMAGNodeBern(const int &_Dim, const double &_Mu=0.5)
void GetMtxV(TMAGAffMtxV &MtxV) const
const double GetAvgInCoeff(const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const
TMAGAffMtx & operator=(const TMAGAffMtx &Kronecker)
const double GetEstNoEdgeLL(const int &NId, const int &AId) const
static PNGraph New()
Static constructor that returns a pointer to the graph. Call: PNGraph Graph = TNGraph::New().
void Swap(TMAGAffMtx &Mtx)
PNGraph GenAttrMAG(const TIntVV &AttrVV, const bool &IsDir=false, const int &Seed=1)
void SetDebug(const bool _Debug)
void SetMuV(const TFltV &MuV)
TMAGFitBern(const PNGraph &G, const TStr &InitFNm)
void Dump(const TStr &MtxNm=TStr(), const bool &Sort=false) const
static double GetAvgAbsErr(const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
void NormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
void SetAttrs(const int &Attrs)
TMAGAffMtx(const int &Dim)
const double GetOutCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
TMAGParam(const int &NumNodes, const int &NumAttrs, const TNodeAttr &Dist)
TSizeTy Len() const
Returns the number of elements in the vector.
void MakeCCDF(const TFltPrV &RawV, TFltPrV &CcdfV)
int GetSecHashCd() const
Returns secondary hash code of the vector. Used by THash.
bool GetMuV(TFltV &_MuV) const
void CountAttr(TFltV &EstMuV) const
int GetNodes() const
Returns the number of nodes in the graph.
TStr GetSubStr(const int &BChN, const int &EChN) const
void SetNodes(const int &Nodes)
void SaveTxt(TStrV &OutStrV) const
double GetRowSum(const int &RowId) const
int AddNode(int NId=-1)
Adds a node of ID NId to the graph.
TMAGAffMtx(const TMAGAffMtx &Kronecker)
TMAGNodeBeta & operator=(const TMAGNodeBeta &Dist)
const double UpdatePhiMI(const double &Lambda, const int &NId, const int &AId, double &Phi)
TMAGNodeBeta(const TFltV &_AlphaV, const TFltV &_BetaV)
const double GetSqThetaLL(const int &NId1, const int &NId2, const int &AId) const
const double GetProdSqWeight(const int &NId1, const int &NId2) const
TVec< TMAGAffMtxV > MtxHisV
void LoadTxt(const TStr &InFNm)
double & At(const int &ValN)
const void GradApxAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
TMAGParam(const int &NumNodes, const int &NumAttrs)
bool Empty() const
Tests whether the vector is empty.
void PutAllMtx(const double &Val)
void UnNormalizeAffMtxV(TMAGAffMtxV &MtxV, const bool UseMu=false)
void AttrGen(TIntVV &AttrVV, const int &NNodes)
void SetPhiVV(const TIntVV &AttrVV, const int KnownIds=0)
void RandomInit(const TFltV &MuV, const TMAGAffMtxV &AffMtxV, const int &Seed)
static double GetAvgFroErr(const TMAGAffMtx &Mtx1, const TMAGAffMtx &Mtx2)
void GetMtxV(TMAGAffMtxV &MtxV) const
const TFltV & GetMtx() const
void LoadTxt(const TStr &InFNm)
const TMAGNodeBern & GetNodeAttr() const
void SetGraph(const PNGraph &GraphPt)
const double GetAvgProdSqWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
const double GetInCoeff(const int &i, const int &j, const int &l, const int &A, const TMAGAffMtx &Theta) const
const void GradAffMtx(const int &AId, const TFltVV &ProdVV, const TFltVV &SqVV, const TMAGAffMtx &CurMtx, TFltV &GradV)
TMAGParam(const int &NumNodes, const TStr &ConfFNm)
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
void PlotProperties(const TStr &FNm)
const TFltV & GetMuV() const
void SetAlgConf(const bool EStep=true, const bool MStep=true)
void GetProbMtx(TMAGAffMtx &ProbMtx)
const double GetAvgThetaLL(const int &NId1, const int &NId2, const int &AId, const bool Left=false, const bool Right=false) const
TVec< TMAGAffMtx > TMAGAffMtxV
const double UpdateAffMtx(const int &AId, const double &LrnRate, const double &MaxGrad, const double &Lambda, TFltVV &ProdVV, TFltVV &SqVV, TMAGAffMtx &NewMtx)
TMAGParam(const TMAGParam< TNodeAttr > &MAGParam)
const void PrepareUpdateApxAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
int AddEdge(const int &SrcNId, const int &DstNId)
Adds an edge from node SrcNId to node DstNId to the graph.
const double GetProdLinWeight(const int &NId1, const int &NId2) const
const double UpdateMu(const int &AId)
const double ComputeJointOneLL(const TIntVV &AttrVV) const
void PutAll(const TVal &Val)
Sets all elements of the vector to value Val.
TMAGNodeSimple & operator=(const TMAGNodeSimple &Simple)
const double & At(const int &ValN) const
void SetBetaV(const TFltV &_AlphaV, const TFltV &_BetaV)
const TFltV & GetMuV() const
void PutAll(const TVal &Val)
const double GetThetaLL(const int &NId1, const int &NId2, const int &AId) const
void SetMtx(const TFltV &ParamV)
void GetDimV(TIntV &DimV) const
const double UpdatePhi(const int &NId, const int &AId, double &Phi)
TMAGNodeSimple(const int &_Dim, const double &_Mu=0.5)
const double GetMtxVal(const int &Attr, const int &Row, const int &Col) const
double GetMu(const int &Attr) const
double GetColSum(const int &ColId) const
double DoEStepOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
const TMAGParam< TMAGNodeBern > & GetParams() const
void AttrGen(TIntVV &AttrVV, const int &NNodes)
void SetBeta(const int &Attr, const double &Alpha, const double &Beta)
const double GetProbPhi(const int &NId1, const int &NId2, const int &AId, const int &Attr1, const int &Attr2) const
double DoEStepApxOneIter(const TFltV &TrueMuV, TFltVV &NewPhi, const double &Lambda)
void SetMuV(const TFltV &_MuV)
void AttrGen(TIntVV &AttrVV, const int &NNodes)
void SetMtxV(const TMAGAffMtxV &MtxV)
static TMAGAffMtx GetRndMtx(TRnd &Rnd, const int &Dim=2, const double &MinProb=0.0)
const double ComputeJointLL(int NSample) const
const TMAGAffMtx & GetMtx(const int &Attr) const
const double LogSumExp(const double LogVal1, const double LogVal2)
const double GetAvgProdLinWeight(const int &NId1, const int &NId2, const bool Left=false, const bool Right=false) const
void SetMtxAll(const TMAGAffMtx &Mtx)
void SortAttrOrdering(const TFltV &TrueMuV, TIntV &IndexV) const
TMAGParam< TNodeAttr > & operator=(const TMAGParam< TNodeAttr > &MAGParam)
void SetMtxV(const TMAGAffMtxV &MtxV)
PNGraph GenMAG(TIntVV &AttrVV, const bool &IsDir=false, const int &Seed=1)
const double ComputeApxAdjLL() const
TMAGNodeBeta(const int &_Dim, const double &Alpha=1, const double &Beta=1)
void SaveTxt(const TStr &OutFNm) const
void SetMtxVal(const int &Attr, const int &Row, const int &Col, const double &Val)
void SaveTxt(TStrV &OutStrV) const
void SetPhiVV(const TFltVV &AttrVV, const int KnownIds=0)
TMAGParam(const int &NumNodes, const TMAGAffMtxV &MtxV)
void GetLLMtx(TMAGAffMtx &LLMtx)
TMAGFitBern(const PNGraph &G, const int &NAttrs)
const void PrepareUpdateAffMtx(TFltVV &ProdVV, TFltVV &SqVV)
double DoEStep(const TFltV &TrueMuV, const int &NIter, double &LL, const double &Lambda)
const double GetAvgOutCoeff(const int &i, const int &AId, const int &A, const TMAGAffMtx &Theta) const
const TNodeAttr & GetNodeAttr() const
const TFltVV & GetPhiVV() const
void SetMu(const int &Attr, const double &Prob)
void SetEpsMtx(const double &Eps1, const double &Eps0, const int &Eps1Val=1, const int &Eps0Val=0)
void SetRndMtx(TRnd &Rnd, const int &PrmMtxDim=2, const double &MinProb=0.0)
void SaveTxt(const TStr &FNm)
const int GetNodes() const
const double UpdateApxPhiMI(const double &Lambda, const int &NId, const int &AId, double &Phi, TFltVV &ProdVV)
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
void DoMStep(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
const double & At(const int &Row, const int &Col) const
const int GetAttrs() const
int GetPrimHashCd() const
double & At(const int &Row, const int &Col)
void GetBetaV(TFltV &_AlphaV, TFltV &_BetaV) const
void SaveTxt(TStrV &OutStrV) const
TMAGNodeBern(const TMAGNodeBern &Dist)
static const double ComputeMI(const TIntVV &AttrV, const int AId1, const int AId2)
void AddRndNoise(TRnd &Rnd, const double &SDev)
const double UpdateAffMtxV(const int &GradIter, const double &LrnRate, const double &MaxGrad, const double &Lambda, const int &NReal=0)
void SetMtx(const int &Attr, const TMAGAffMtx &Mtx)
bool operator==(const TMAGAffMtx &Kronecker) const
void Init(const TFltV &MuV, const TMAGAffMtxV &AffMtxV)
void Gen(const TSizeTy &_XDim, const TSizeTy &_YDim)
void PutY(const TSizeTy &Y, const TVal &Val)
const double ComputeApxLL() const
const double ObjPhiMI(const double &x, const int &NId, const int &AId, const double &Lambda, const double &Q0, const double &Q1, const TFltVV &CntVV)
void LoadTxt(const TStr &InFNm)
TMAGNodeBern(const TFltV &_MuV)
void GenMtx(const int &Dim)
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 GradPhiMI(const double &x, const int &NId, const int &AId, const double &Lambda, const double &DeltaQ, const TFltVV &CntVV)
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const