SNAP Library 6.0, User Reference  2020-12-09 16:24:20
SNAP, a general purpose, high performance system for analysis and manipulation of large networks
TLinReg Class Reference

#include <xmath.h>

Public Member Functions

void GetXV (const int RecN, TFltV &VarV) const
 
double GetY (const int RecN) const
 
double GetSig (const int RecN) const
 
void NR_covsrt (TFltVV &CovarVV, const int &Vars, const TIntV &ia, const int &mfit)
 
void NR_gaussj (TFltVV &a, const int &n, TFltVV &b, const int &m)
 
void NR_lfit ()
 
 TLinReg ()
 
 ~TLinReg ()
 
 TLinReg (TSIn &)
 
void Save (TSOut &)
 
TLinRegoperator= (const TLinReg &)
 
int GetRecs () const
 
int GetVars () const
 
double GetCf (const int &VarN) const
 
double GetCfUncer (const int &VarN) const
 
double GetCovar (const int &VarN1, const int &VarN2) const
 
double GetChiSq () const
 
void Wr () const
 

Static Public Member Functions

static PLinReg New (const TFltVV &XVV, const TFltV &YV, const TFltV &SigV=TFltV())
 
static PLinReg Load (TSIn &SIn)
 
static double LinInterp (const double &x1, const double &y1, const double &x2, const double &y2, const double &AtX) _CMPWARN
 

Public Attributes

TFltVV XVV
 
TFltV YV
 
TFltV SigV
 
int Recs
 
int Vars
 
TFltVV CovarVV
 
TFltV CfV
 
double ChiSq
 

Private Attributes

TCRef CRef
 

Friends

class TPt< TLinReg >
 

Detailed Description

Definition at line 345 of file xmath.h.

Constructor & Destructor Documentation

TLinReg::TLinReg ( )
inline

Definition at line 364 of file xmath.h.

364 {}
TLinReg::~TLinReg ( )
inline

Definition at line 367 of file xmath.h.

367 {}
TLinReg::TLinReg ( TSIn )
inline

Definition at line 368 of file xmath.h.

368 {Fail;}
#define Fail
Definition: bd.h:238

Member Function Documentation

double TLinReg::GetCf ( const int &  VarN) const
inline

Definition at line 377 of file xmath.h.

377 {return CfV[VarN+1];}
TFltV CfV
Definition: xmath.h:352
double TLinReg::GetCfUncer ( const int &  VarN) const
inline

Definition at line 378 of file xmath.h.

378  {
379  return sqrt(double(CovarVV.At(VarN+1, VarN+1)));}
TFltVV CovarVV
Definition: xmath.h:351
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
double TLinReg::GetChiSq ( ) const
inline

Definition at line 383 of file xmath.h.

383 {return ChiSq;}
double ChiSq
Definition: xmath.h:353
double TLinReg::GetCovar ( const int &  VarN1,
const int &  VarN2 
) const
inline

Definition at line 380 of file xmath.h.

380  {
381  return CovarVV.At(VarN1+1, VarN2+1);}
TFltVV CovarVV
Definition: xmath.h:351
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
int TLinReg::GetRecs ( ) const
inline

Definition at line 374 of file xmath.h.

374 {return Recs;}
int Recs
Definition: xmath.h:350
double TLinReg::GetSig ( const int  RecN) const
inline

Definition at line 359 of file xmath.h.

359 {return SigV[RecN-1];}
TFltV SigV
Definition: xmath.h:349
int TLinReg::GetVars ( ) const
inline

Definition at line 375 of file xmath.h.

375 {return Vars;}
int Vars
Definition: xmath.h:350
void TLinReg::GetXV ( const int  RecN,
TFltV VarV 
) const
inline

Definition at line 354 of file xmath.h.

354  {
355  VarV.Gen(Vars+1);
356  for (int VarN=0; VarN<Vars; VarN++){VarV[VarN+1]=XVV.At(RecN-1, VarN);}
357  }
int Vars
Definition: xmath.h:350
TFltVV XVV
Definition: xmath.h:347
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
double TLinReg::GetY ( const int  RecN) const
inline

Definition at line 358 of file xmath.h.

358 {return YV[RecN-1];}
TFltV YV
Definition: xmath.h:348
static double TLinReg::LinInterp ( const double &  x1,
const double &  y1,
const double &  x2,
const double &  y2,
const double &  AtX 
)
inlinestatic

Definition at line 385 of file xmath.h.

386  {
387  if (x1 == x2) return (y1+y2)/2.0;
388  const double k = (y2 - y1) / (x2 - x1);
389  return k*(AtX - x1) + y1;
390  }
static PLinReg TLinReg::Load ( TSIn SIn)
inlinestatic

Definition at line 369 of file xmath.h.

369 {return new TLinReg(SIn);}
TLinReg()
Definition: xmath.h:364
PLinReg TLinReg::New ( const TFltVV XVV,
const TFltV YV,
const TFltV SigV = TFltV() 
)
static

Definition at line 778 of file xmath.cpp.

778  {
779  PLinReg LinReg=PLinReg(new TLinReg());
780  LinReg->XVV=_XVV;
781  LinReg->YV=_YV;
782  if (_SigV.Empty()){
783  LinReg->SigV.Gen(LinReg->YV.Len());
784  LinReg->SigV.PutAll(1);
785  } else {
786  LinReg->SigV=_SigV;
787  }
788  LinReg->Recs=LinReg->XVV.GetXDim();
789  LinReg->Vars=LinReg->XVV.GetYDim();
790  IAssert(LinReg->Recs>0);
791  IAssert(LinReg->Vars>0);
792  IAssert(LinReg->YV.Len()==LinReg->Recs);
793  IAssert(LinReg->SigV.Len()==LinReg->Recs);
794  LinReg->CovarVV.Gen(LinReg->Vars+1, LinReg->Vars+1);
795  LinReg->CfV.Gen(LinReg->Vars+1);
796  LinReg->NR_lfit();
797  return LinReg;
798 }
#define IAssert(Cond)
Definition: bd.h:262
TLinReg()
Definition: xmath.h:364
TPt< TLinReg > PLinReg
Definition: xmath.h:345
Definition: bd.h:196
void TLinReg::NR_covsrt ( TFltVV CovarVV,
const int &  Vars,
const TIntV ia,
const int &  mfit 
)

Definition at line 800 of file xmath.cpp.

801  {
802  for (int i=mfit+1; i<=Vars; i++){
803  for (int j=1; j<=i; j++){
804  CovarVV.At(i, j)=0; CovarVV.At(j, i)=0.0;}
805  }
806  int k=mfit;
807  for (int j=Vars; j>=1; j--){
808  if (ia[j]!=0){
809  for (int i=1; i<=Vars; i++){Swap(CovarVV.At(i, k), CovarVV.At(i, j));}
810  {for (int i=1; i<=Vars; i++){Swap(CovarVV.At(k, i), CovarVV.At(j, i));}}
811  k--;
812  }
813  }
814 }
int Vars
Definition: xmath.h:350
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
void TLinReg::NR_gaussj ( TFltVV a,
const int &  n,
TFltVV b,
const int &  m 
)

Definition at line 816 of file xmath.cpp.

816  {
817  int i, icol=0, irow=0, j, k, l, ll;
818  double big, dum, pivinv;
819 
820  TIntV indxc(n+1);
821  TIntV indxr(n+1);
822  TIntV ipiv(n+1);
823  for (j=1; j<=n; j++){ipiv[j]=0;}
824  for (i=1; i<=n; i++){
825  big=0.0;
826  for (j=1; j<=n; j++){
827  if (ipiv[j]!=1){
828  for (k=1; k<=n; k++){
829  if (ipiv[k]==0){
830  if (fabs(double(a.At(j, k))) >= big){
831  big=fabs(double(a.At(j, k)));
832  irow=j;
833  icol=k;
834  }
835  } else
836  if (ipiv[k]>1){
837  TExcept::Throw("Singular Matrix(1) in Gauss");}
838  }
839  }
840  }
841  ipiv[icol]++;
842  if (irow != icol){
843  for (l=1; l<=n; l++){Swap(a.At(irow, l), a.At(icol, l));}
844  for (l=1; l<=m; l++){Swap(b.At(irow, l), b.At(icol, l));}
845  }
846  indxr[i]=irow;
847  indxc[i]=icol;
848  if (a.At(icol, icol)==0.0){
849  TExcept::Throw("Singular Matrix(1) in Gauss");}
850  pivinv=1.0/a.At(icol, icol);
851  a.At(icol, icol)=1.0;
852  for (l=1; l<=n; l++){a.At(icol, l)=a.At(icol, l)*pivinv;}
853  for (l=1; l<=m; l++){b.At(icol, l)=b.At(icol, l)*pivinv;}
854  for (ll=1; ll<=n; ll++){
855  if (ll != icol){
856  dum=a.At(ll, icol);
857  a.At(ll, icol)=0.0;
858  for (l=1;l<=n;l++){a.At(ll, l)-=a.At(icol, l)*dum;}
859  for (l=1;l<=m;l++){b.At(ll, l)-=b.At(icol, l)*dum;}
860  }
861  }
862  }
863  for (l=n; l>=1; l--){
864  if (indxr[l]!=indxc[l]){
865  for (k=1; k<=n; k++){
866  Swap(a.At(k, indxr[l]), a.At(k, indxc[l]));}
867  }
868  }
869 }
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
void Swap(TRec &Rec1, TRec &Rec2)
Definition: bd.h:568
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
void TLinReg::NR_lfit ( )

Definition at line 871 of file xmath.cpp.

871  {
872  int i,j,k,l,m,mfit=0;
873  double ym,wt,sum,sig2i;
874 
875  TIntV ia(Vars+1); for (i=1; i<=Vars; i++){ia[i]=1;}
876  TFltVV beta(Vars+1, 1+1);
877  TFltV afunc(Vars+1);
878  for (j=1;j<=Vars;j++){
879  if (ia[j]!=0){mfit++;}}
880  if (mfit==0){TExcept::Throw("No parameters to be fitted in LFit");}
881  for (j=1; j<=mfit; j++){
882  for (k=1; k<=mfit; k++){CovarVV.At(j, k)=0.0;}
883  beta.At(j, 1)=0.0;
884  }
885  for (i=1; i<=Recs; i++){
886  GetXV(i, afunc); // funcs(XVV[i],afunc,Vars);
887  ym=GetY(i);
888  if (mfit<Vars){
889  for (j=1;j<=Vars;j++){
890  if (ia[j]==0){ym-=CfV[j]*afunc[j];}}
891  }
892  sig2i=1.0/TMath::Sqr(GetSig(i));
893  for (j=0, l=1; l<=Vars; l++){
894  if (ia[l]!=0){
895  wt=afunc[l]*sig2i;
896  for (j++, k=0, m=1; m<=l; m++){
897  if (ia[m]!=0){CovarVV.At(j, ++k)+=wt*afunc[m];}
898  }
899  beta.At(j, 1)+=ym*wt;
900  }
901  }
902  }
903  for (j=2; j<=mfit; j++){
904  for (k=1; k<j; k++){CovarVV.At(k, j)=CovarVV.At(j, k);}
905  }
906  NR_gaussj(CovarVV, mfit, beta, 1);
907  for (j=0, l=1; l<=Vars; l++){
908  if (ia[l]!=0){CfV[l]=beta.At(++j, 1);}
909  }
910  ChiSq=0.0;
911  for (i=1; i<=Recs; i++){
912  GetXV(i, afunc); // funcs(XVV[i],afunc,Vars);
913  for (sum=0.0, j=1; j<=Vars; j++){sum+=CfV[j]*afunc[j];}
914  ChiSq+=TMath::Sqr((GetY(i)-sum)/GetSig(i));
915  }
916  NR_covsrt(CovarVV, Vars, ia, mfit);
917 }
double GetSig(const int RecN) const
Definition: xmath.h:359
int Vars
Definition: xmath.h:350
void NR_gaussj(TFltVV &a, const int &n, TFltVV &b, const int &m)
Definition: xmath.cpp:816
static double Sqr(const double &x)
Definition: xmath.h:12
int Recs
Definition: xmath.h:350
void NR_covsrt(TFltVV &CovarVV, const int &Vars, const TIntV &ia, const int &mfit)
Definition: xmath.cpp:800
static void Throw(const TStr &MsgStr)
Definition: ut.h:187
double ChiSq
Definition: xmath.h:353
TFltV CfV
Definition: xmath.h:352
void GetXV(const int RecN, TFltV &VarV) const
Definition: xmath.h:354
double GetY(const int RecN) const
Definition: xmath.h:358
TFltVV CovarVV
Definition: xmath.h:351
const TVal & At(const TSizeTy &X, const TSizeTy &Y) const
Definition: ds.h:2256
TLinReg& TLinReg::operator= ( const TLinReg )
inline

Definition at line 372 of file xmath.h.

372 {Fail; return *this;}
#define Fail
Definition: bd.h:238
void TLinReg::Save ( TSOut )
inline

Definition at line 370 of file xmath.h.

370 {Fail;}
#define Fail
Definition: bd.h:238
void TLinReg::Wr ( ) const

Definition at line 919 of file xmath.cpp.

919  {
920  printf("\n%11s %21s\n","parameter","uncertainty");
921  for (int i=0; i<Vars;i++){
922  printf(" a[%1d] = %8.6f %12.6f\n",
923  i+1, GetCf(i), GetCfUncer(i));
924  }
925  printf("chi-squared = %12f\n", GetChiSq());
926 
927  printf("full covariance matrix\n");
928  {for (int i=0;i<Vars;i++){
929  for (int j=0;j<Vars;j++){
930  printf("%12f", GetCovar(i, j));}
931  printf("\n");
932  }}
933 }
double GetCf(const int &VarN) const
Definition: xmath.h:377
int Vars
Definition: xmath.h:350
double GetCovar(const int &VarN1, const int &VarN2) const
Definition: xmath.h:380
double GetChiSq() const
Definition: xmath.h:383
double GetCfUncer(const int &VarN) const
Definition: xmath.h:378

Friends And Related Function Documentation

friend class TPt< TLinReg >
friend

Definition at line 345 of file xmath.h.

Member Data Documentation

TFltV TLinReg::CfV

Definition at line 352 of file xmath.h.

double TLinReg::ChiSq

Definition at line 353 of file xmath.h.

TFltVV TLinReg::CovarVV

Definition at line 351 of file xmath.h.

TCRef TLinReg::CRef
private

Definition at line 345 of file xmath.h.

int TLinReg::Recs

Definition at line 350 of file xmath.h.

TFltV TLinReg::SigV

Definition at line 349 of file xmath.h.

int TLinReg::Vars

Definition at line 350 of file xmath.h.

TFltVV TLinReg::XVV

Definition at line 347 of file xmath.h.

TFltV TLinReg::YV

Definition at line 348 of file xmath.h.


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