SNAP Library 2.0, User Reference  2013-05-13 16:33:57
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
TMom Class Reference

#include <xmath.h>

List of all members.

Public Member Functions

 TMom ()
 TMom (const TMom &Mom)
 TMom (const TFltV &_ValV)
 TMom (TSIn &SIn)
void Save (TSOut &SOut) const
TMomoperator= (const TMom &Mom)
bool operator== (const TMom &Mom) const
bool operator< (const TMom &Mom) const
void Def ()
bool IsDef () const
void Add (const TFlt &Val, const TFlt &Wgt=1)
double GetWgt () const
int GetVals () const
TFlt GetVal (const int &ValN) const
bool IsUsable () const
double GetMn () const
double GetMx () const
double GetExtent () const
double GetMean () const
double GetVari () const
double GetSDev () const
double GetSErr () const
double GetMedian () const
double GetQuart1 () const
double GetQuart3 () const
double GetDecile (const int &DecileN) const
double GetPercentile (const int &PercentileN) const
double GetByNm (const TStr &MomNm) const
TStr GetStrByNm (const TStr &MomNm, char *FmtStr=NULL) const
TStr GetStr (const char &SepCh=' ', const char &DelimCh=':', const bool &DecileP=true, const bool &PercentileP=true, const TStr &FmtStr="%g") const
TStr GetValVStr (const char &SepCh='\t', const bool &DecileP=true, const bool &PercentileP=true) const

Static Public Member Functions

static PMom New ()
static void NewV (TMomV &MomV, const int &Moms)
static void NewVV (TVVec< PMom > &MomVV, const int &XMoms, const int &YMoms)
static PMom New (const TFltV &ValV)
static PMom Load (TSIn &SIn)
static void DefV (TMomV &MomV)
static void DefVV (TVVec< PMom > &MomVV)
static bool IsUsableV (const TMomV &MomV)
static bool IsUsableVV (const TVVec< PMom > &MomVV)
static TStr GetNmVStr (const TStr &VarPfx, const char &SepCh='\t', const bool &DecileP=true, const bool &PercentileP=true)

Private Attributes

TCRef CRef
TBool DefP
TFltPrV ValWgtV
TFlt SumW
TFlt ValSumW
TInt Vals
TBool UsableP
TFlt UnusableVal
TFlt Mn
TFlt Mx
TFlt Mean
TFlt Vari
TFlt SDev
TFlt SErr
TFlt Median
TFlt Quart1
TFlt Quart3
TFltV DecileV
TFltV PercentileV

Friends

class TPt< TMom >

Detailed Description

Definition at line 129 of file xmath.h.


Constructor & Destructor Documentation

TMom::TMom ( ) [inline]

Definition at line 143 of file xmath.h.

        :
    DefP(false), ValWgtV(),
    SumW(), ValSumW(), Vals(),
    UsableP(false), UnusableVal(-1),
    Mn(), Mx(),
    Mean(), Vari(), SDev(), SErr(),
    Median(), Quart1(), Quart3(),
    DecileV(), PercentileV(){}
TMom::TMom ( const TMom Mom) [inline]

Definition at line 151 of file xmath.h.

                       :
    DefP(Mom.DefP), ValWgtV(Mom.ValWgtV),
    SumW(Mom.SumW), ValSumW(Mom.ValSumW), Vals(Mom.Vals),
    UsableP(Mom.UsableP), UnusableVal(Mom.UnusableVal),
    Mn(Mom.Mn), Mx(Mom.Mx),
    Mean(Mom.Mean), Vari(Mom.Vari), SDev(Mom.SDev), SErr(Mom.SErr),
    Median(Mom.Median), Quart1(Mom.Quart1), Quart3(Mom.Quart3),
    DecileV(Mom.DecileV), PercentileV(Mom.PercentileV){}
TMom::TMom ( const TFltV _ValV)

Definition at line 326 of file xmath.cpp.

                            :
  //WgtV(_ValV.Len(), 0), ValV(_ValV.Len(), 0),
  ValWgtV(_ValV.Len(), 0),
  SumW(), ValSumW(),
  UsableP(false), UnusableVal(-1),
  Mn(), Mx(),
  Mean(), Vari(), SDev(), SErr(),
  Median(), Quart1(), Quart3(),
  DecileV(), PercentileV(){
  for (int ValN=0; ValN<_ValV.Len(); ValN++){Add(_ValV[ValN], 1);}
  Def();
}
TMom::TMom ( TSIn SIn) [inline]

Definition at line 170 of file xmath.h.

                 :
    DefP(SIn),
    ValWgtV(SIn),
    SumW(SIn), ValSumW(SIn), Vals(SIn),
    UsableP(SIn), UnusableVal(SIn),
    Mn(SIn), Mx(SIn),
    Mean(SIn), Vari(SIn), SDev(SIn), SErr(SIn),
    Median(SIn), Quart1(SIn), Quart3(SIn),
    DecileV(SIn), PercentileV(SIn){}

Member Function Documentation

void TMom::Add ( const TFlt Val,
const TFlt Wgt = 1 
) [inline]

Definition at line 216 of file xmath.h.

                                              {Assert(!DefP);
    ValWgtV.Add(TFltPr(Val, Wgt)); SumW+=Wgt; ValSumW+=Wgt*Val; Vals++;}
void TMom::Def ( )

Definition at line 339 of file xmath.cpp.

              {
  IAssert(!DefP); DefP=true;
  UsableP=(SumW>0)&&(ValWgtV.Len()>0);
  if (UsableP){
    // Mn, Mx
    Mn=ValWgtV[0].Val1;
    Mx=ValWgtV[0].Val1;
    // Mean, Variance (Mn, Mx), Standard-Error
    Mean=ValSumW/SumW;
    Vari=0;
    if (ValWgtV.Len()>1){
      for (int ValN=0; ValN<ValWgtV.Len(); ValN++){
        const double Val=ValWgtV[ValN].Val1;
        Vari+=ValWgtV[ValN].Val2*TMath::Sqr(Val-Mean);
        if (Val<Mn){Mn=Val;}
        if (Val>Mx){Mx=Val;}
      }
      Vari=Vari/SumW;
      SErr=sqrt(Vari/(ValWgtV.Len()*(ValWgtV.Len()-1)));
    }
    // Standard-Deviation
    SDev=sqrt(double(Vari));
    // Median
    ValWgtV.Sort();
    double CurSumW = 0;
    for (int ValN=0; ValN<ValWgtV.Len(); ValN++){
      CurSumW += ValWgtV[ValN].Val2;
      if (CurSumW > 0.5*SumW) { 
        Median = ValWgtV[ValN].Val1; break; }
      else if (CurSumW == 0.5*SumW) {
        Median = 0.5 * (ValWgtV[ValN].Val1+ValWgtV[ValN+1].Val1); break; }
    }
    // Quartile-1 and Quartile-3
    Quart1=Quart3=TFlt::Mn;
    CurSumW = 0;
    for (int ValN=0; ValN<ValWgtV.Len(); ValN++){
      CurSumW += ValWgtV[ValN].Val2;
      if (Quart1==TFlt::Mn) {
        if (CurSumW > 0.25*SumW) {  Quart1 = ValWgtV[ValN].Val1; }
        //else if (CurSumW == 0.25*SumW) { Quart1 = 0.5 * (ValWgtV[ValN].Val1+ValWgtV[ValN+1].Val1); }
      } 
      if (Quart3==TFlt::Mn) {
        if (CurSumW > 0.75*SumW) { Quart3 = ValWgtV[ValN].Val1; }
        //else if (CurSumW == 0.75*SumW) { Quart3 = 0.5 * (ValWgtV[ValN].Val1+ValWgtV[ValN+1].Val1); }
      }
    }
    // Deciles & Percentiles
    CurSumW = 0;
    int DecileN = 1, PercentileN = 1;
    DecileV.Gen(11);  PercentileV.Gen(101);
    DecileV[0]=Mn; DecileV[10]=Mx;
    PercentileV[0]=Mn; PercentileV[100]=Mx;
    for (int ValN=0; ValN<ValWgtV.Len(); ValN++){
      CurSumW += ValWgtV[ValN].Val2;
      if (CurSumW > SumW*DecileN*0.1) { 
        DecileV[DecileN] = ValWgtV[ValN].Val1;  DecileN++; }
      if (CurSumW > SumW*PercentileN*0.01) {
        PercentileV[PercentileN] = ValWgtV[ValN].Val1;  PercentileN++; }
    }
  }
  ValWgtV.Clr();
}
static void TMom::DefV ( TMomV MomV) [inline, static]

Definition at line 207 of file xmath.h.

                               {
    for (int MomN=0; MomN<MomV.Len(); MomN++){MomV[MomN]->Def();}}
static void TMom::DefVV ( TVVec< PMom > &  MomVV) [inline, static]

Definition at line 209 of file xmath.h.

                                       {
    for (int XMomN=0; XMomN<MomVV.GetXDim(); XMomN++){
      for (int YMomN=0; YMomN<MomVV.GetYDim(); YMomN++){
        MomVV.At(XMomN, YMomN)->Def();}}}
double TMom::GetByNm ( const TStr MomNm) const

Definition at line 404 of file xmath.cpp.

                                            {
  if (MomNm=="Mean"){return GetMean();}
  else if (MomNm=="Vari"){return GetVari();}
  else if (MomNm=="SDev"){return GetSDev();}
  else if (MomNm=="SErr"){return GetSErr();}
  else if (MomNm=="Median"){return GetMedian();}
  else if (MomNm=="Quart1"){return GetQuart1();}
  else if (MomNm=="Quart3"){return GetQuart3();}
  else if (MomNm=="Decile0"){return GetDecile(0);}
  else if (MomNm=="Decile1"){return GetDecile(1);}
  else if (MomNm=="Decile2"){return GetDecile(2);}
  else if (MomNm=="Decile3"){return GetDecile(3);}
  else if (MomNm=="Decile4"){return GetDecile(4);}
  else if (MomNm=="Decile5"){return GetDecile(5);}
  else if (MomNm=="Decile6"){return GetDecile(6);}
  else if (MomNm=="Decile7"){return GetDecile(7);}
  else if (MomNm=="Decile8"){return GetDecile(8);}
  else if (MomNm=="Decile9"){return GetDecile(9);}
  else if (MomNm=="Decile10"){return GetDecile(10);}
  else {Fail; return 0;}
}
double TMom::GetDecile ( const int &  DecileN) const [inline]

Definition at line 246 of file xmath.h.

                                             {
    Assert(DefP&&UsableP); return DecileV[DecileN];}
double TMom::GetExtent ( ) const [inline]

Definition at line 238 of file xmath.h.

{Assert(DefP&&UsableP); return Mx-Mn;}
double TMom::GetMean ( ) const [inline]

Definition at line 239 of file xmath.h.

{Assert(DefP&&UsableP); return Mean;}
double TMom::GetMedian ( ) const [inline]

Definition at line 243 of file xmath.h.

{Assert(DefP&&UsableP); return Median;}
double TMom::GetMn ( ) const [inline]

Definition at line 236 of file xmath.h.

{Assert(DefP&&UsableP); return Mn;}
double TMom::GetMx ( ) const [inline]

Definition at line 237 of file xmath.h.

{Assert(DefP&&UsableP); return Mx;}
TStr TMom::GetNmVStr ( const TStr VarPfx,
const char &  SepCh = '\t',
const bool &  DecileP = true,
const bool &  PercentileP = true 
) [static]

Definition at line 475 of file xmath.cpp.

                                                                 {
  TChA ChA;
  ChA+=VarPfx; ChA+="Vals"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Min"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Max"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Mean"; ChA+=SepCh;
  //ChA+=VarPfx; ChA+="Vari"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="SDev"; ChA+=SepCh;
  //ChA+=VarPfx; ChA+="SErr"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Quart1"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Median"; ChA+=SepCh;
  ChA+=VarPfx; ChA+="Quart3";
  if (DecileP){
    ChA+=SepCh;
    for (int DecileN=0; DecileN<=10; DecileN++){
      ChA+=VarPfx; ChA+="Dec"; ChA+=TInt::GetStr(DecileN);
      if (DecileN<10){ChA+=SepCh;}
    }
  }
  if (PercentileP){
    ChA+=SepCh;
    for (int PercentileN=0; PercentileN<=100; PercentileN++){
      ChA+=VarPfx; ChA+="Per"; ChA+=TInt::GetStr(PercentileN);
      if (PercentileN<100){ChA+=SepCh;}
    }
  }
  return ChA;
}
double TMom::GetPercentile ( const int &  PercentileN) const [inline]

Definition at line 248 of file xmath.h.

                                                     {
    Assert(DefP&&UsableP); return PercentileV[PercentileN];}
double TMom::GetQuart1 ( ) const [inline]

Definition at line 244 of file xmath.h.

{Assert(DefP&&UsableP); return Quart1;}
double TMom::GetQuart3 ( ) const [inline]

Definition at line 245 of file xmath.h.

{Assert(DefP&&UsableP); return Quart3;}
double TMom::GetSDev ( ) const [inline]

Definition at line 241 of file xmath.h.

{Assert(DefP&&UsableP); return SDev;}
double TMom::GetSErr ( ) const [inline]

Definition at line 242 of file xmath.h.

{Assert(DefP&&UsableP); return SErr;}
TStr TMom::GetStr ( const char &  SepCh = ' ',
const char &  DelimCh = ':',
const bool &  DecileP = true,
const bool &  PercentileP = true,
const TStr FmtStr = "%g" 
) const

Definition at line 438 of file xmath.cpp.

                                                                         {
  TChA ChA;
  if (IsUsable()){
    ChA+="["; ChA+=SepCh;
    ChA+="Vals"; ChA+=DelimCh; ChA+=TInt::GetStr(GetVals()); ChA+=SepCh;
    ChA+="Min"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetMn(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="Max"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetMx(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="Mean"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetMean(), FmtStr.CStr()); ChA+=SepCh;
    //ChA+="Vari"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetVari(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="SDev"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetSDev(), FmtStr.CStr()); ChA+=SepCh;
    //ChA+="SErr"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetSErr(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="Quart1"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetQuart1(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="Median"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetMedian(), FmtStr.CStr()); ChA+=SepCh;
    ChA+="Quart3"; ChA+=DelimCh; ChA+=TFlt::GetStr(GetQuart3(), FmtStr.CStr()); ChA+=SepCh;
    if (DecileP){
      for (int DecileN=0; DecileN<=10; DecileN++){
        ChA+="Dec"; ChA+=TInt::GetStr(DecileN);
        ChA+=DelimCh; ChA+=TFlt::GetStr(GetDecile(DecileN), FmtStr.CStr());
        ChA+=SepCh;
      }
    }
    if (PercentileP){
      for (int PercentileN=0; PercentileN<=100; PercentileN++){
        ChA+="Per"; ChA+=TInt::GetStr(PercentileN);
        ChA+=DelimCh; ChA+=TFlt::GetStr(GetPercentile(PercentileN), FmtStr.CStr());
        ChA+=SepCh;
      }
    }
    ChA+="]";
  } else {
    ChA="[Unusable]";
  }
  return ChA;
}
TStr TMom::GetStrByNm ( const TStr MomNm,
char *  FmtStr = NULL 
) const

Definition at line 426 of file xmath.cpp.

                                                           {
  if (IsUsable()){
    if (FmtStr==NULL){
      return TFlt::GetStr(GetByNm(MomNm));
    } else {
      return TFlt::GetStr(GetByNm(MomNm), FmtStr);
    }
  } else {
    return "X";
  }
}
TFlt TMom::GetVal ( const int &  ValN) const [inline]

Definition at line 220 of file xmath.h.

{IAssert(!IsDef()); return ValWgtV[ValN].Val1;}
int TMom::GetVals ( ) const [inline]

Definition at line 219 of file xmath.h.

{return Vals;}
TStr TMom::GetValVStr ( const char &  SepCh = '\t',
const bool &  DecileP = true,
const bool &  PercentileP = true 
) const

Definition at line 505 of file xmath.cpp.

                                                                        {
  TChA ChA;
  if (IsUsable()){
    ChA+=TInt::GetStr(GetVals()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetMn()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetMx()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetMean()); ChA+=SepCh;
    //ChA+=TFlt::GetStr(GetVari()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetSDev()); ChA+=SepCh;
    //ChA+=TFlt::GetStr(GetSErr()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetQuart1()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetMedian()); ChA+=SepCh;
    ChA+=TFlt::GetStr(GetQuart3()); ChA+=SepCh;
    if (DecileP){
      for (int DecileN=0; DecileN<=10; DecileN++){
        ChA+=TFlt::GetStr(GetDecile(DecileN)); ChA+=SepCh;
      }
    }
    if (PercentileP){
      for (int PercentileN=0; PercentileN<=100; PercentileN++){
        ChA+=TFlt::GetStr(GetPercentile(PercentileN)); ChA+=SepCh;
      }
    }
  } else {
    int Vals=8;
    if (DecileP){Vals+=11;}
    if (PercentileP){Vals+=101;}
    for (int ValN=0; ValN<Vals; ValN++){
      ChA="[Unusable]";
      if (ValN<Vals-1){ChA+=SepCh;}
    }
  }
  return ChA;
}
double TMom::GetVari ( ) const [inline]

Definition at line 240 of file xmath.h.

{Assert(DefP&&UsableP); return Vari;}
double TMom::GetWgt ( ) const [inline]

Definition at line 218 of file xmath.h.

{return SumW;}
bool TMom::IsDef ( ) const [inline]

Definition at line 213 of file xmath.h.

{return DefP;}
bool TMom::IsUsable ( ) const [inline]

Definition at line 224 of file xmath.h.

{Assert(DefP); return UsableP;}
static bool TMom::IsUsableV ( const TMomV MomV) [inline, static]

Definition at line 225 of file xmath.h.

                                          {
    for (int MomN=0; MomN<MomV.Len(); MomN++){
      if (!MomV[MomN]->IsUsable()){return false;}}
    return true;}
static bool TMom::IsUsableVV ( const TVVec< PMom > &  MomVV) [inline, static]

Definition at line 229 of file xmath.h.

                                                  {
    for (int XMomN=0; XMomN<MomVV.GetXDim(); XMomN++){
      for (int YMomN=0; YMomN<MomVV.GetYDim(); YMomN++){
        if (!MomVV.At(XMomN, YMomN)->IsUsable()){return false;}}}
    return true;}
static PMom TMom::Load ( TSIn SIn) [inline, static]

Definition at line 179 of file xmath.h.

{return new TMom(SIn);}
static PMom TMom::New ( ) [inline, static]

Definition at line 159 of file xmath.h.

{return PMom(new TMom());}
static PMom TMom::New ( const TFltV ValV) [inline, static]

Definition at line 168 of file xmath.h.

                                    {
    return PMom(new TMom(ValV));}
static void TMom::NewV ( TMomV MomV,
const int &  Moms 
) [inline, static]

Definition at line 160 of file xmath.h.

                                                {
    MomV.Gen(Moms); for (int MomN=0; MomN<Moms; MomN++){MomV[MomN]=New();}}
static void TMom::NewVV ( TVVec< PMom > &  MomVV,
const int &  XMoms,
const int &  YMoms 
) [inline, static]

Definition at line 162 of file xmath.h.

                                                                           {
    MomVV.Gen(XMoms, YMoms);
    for (int XMomN=0; XMomN<XMoms; XMomN++){
      for (int YMomN=0; YMomN<YMoms; YMomN++){
        MomVV.At(XMomN, YMomN)=New();}}}
bool TMom::operator< ( const TMom Mom) const [inline]

Definition at line 202 of file xmath.h.

                                        {
    return Vals<Mom.Vals;}
TMom& TMom::operator= ( const TMom Mom) [inline]

Definition at line 190 of file xmath.h.

                                  {
    Assert(!DefP); DefP=Mom.DefP;
    ValWgtV=Mom.ValWgtV;
    SumW=Mom.SumW; ValSumW=Mom.ValSumW; Vals=Mom.Vals;
    UsableP=Mom.UsableP; UnusableVal=Mom.UnusableVal;
    Mn=Mom.Mn; Mx=Mom.Mx;
    Mean=Mom.Mean; Vari=Mom.Vari; SDev=Mom.SDev; SErr=Mom.SErr;
    Median=Mom.Median; Quart1=Mom.Quart1; Quart3=Mom.Quart3;
    DecileV=Mom.DecileV; PercentileV=Mom.PercentileV;
    return *this;}
bool TMom::operator== ( const TMom Mom) const [inline]

Definition at line 200 of file xmath.h.

                                         {
    return Vals==Mom.Vals;}
void TMom::Save ( TSOut SOut) const [inline]

Definition at line 180 of file xmath.h.

                               {
    DefP.Save(SOut);
    ValWgtV.Save(SOut);
    SumW.Save(SOut); ValSumW.Save(SOut); Vals.Save(SOut);
    UsableP.Save(SOut); UnusableVal.Save(SOut);
    Mn.Save(SOut); Mx.Save(SOut);
    Mean.Save(SOut); Vari.Save(SOut); SDev.Save(SOut); SErr.Save(SOut);
    Median.Save(SOut); Quart1.Save(SOut); Quart3.Save(SOut);
    DecileV.Save(SOut); PercentileV.Save(SOut);}

Friends And Related Function Documentation

friend class TPt< TMom > [friend]

Definition at line 129 of file xmath.h.


Member Data Documentation

TCRef TMom::CRef [private]

Definition at line 129 of file xmath.h.

TFltV TMom::DecileV [private]

Definition at line 140 of file xmath.h.

TBool TMom::DefP [private]

Definition at line 131 of file xmath.h.

TFlt TMom::Mean [private]

Definition at line 138 of file xmath.h.

TFlt TMom::Median [private]

Definition at line 139 of file xmath.h.

TFlt TMom::Mn [private]

Definition at line 137 of file xmath.h.

TFlt TMom::Mx [private]

Definition at line 137 of file xmath.h.

Definition at line 141 of file xmath.h.

TFlt TMom::Quart1 [private]

Definition at line 139 of file xmath.h.

TFlt TMom::Quart3 [private]

Definition at line 139 of file xmath.h.

TFlt TMom::SDev [private]

Definition at line 138 of file xmath.h.

TFlt TMom::SErr [private]

Definition at line 138 of file xmath.h.

TFlt TMom::SumW [private]

Definition at line 133 of file xmath.h.

Definition at line 136 of file xmath.h.

TBool TMom::UsableP [private]

Definition at line 135 of file xmath.h.

TInt TMom::Vals [private]

Definition at line 134 of file xmath.h.

TFlt TMom::ValSumW [private]

Definition at line 133 of file xmath.h.

Definition at line 132 of file xmath.h.

TFlt TMom::Vari [private]

Definition at line 138 of file xmath.h.


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