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
TSpecFunc Class Reference

#include <xmath.h>

Static Public Member Functions

static void GammaPSeries (double &gamser, const double &a, const double &x, double &gln)
 
static void GammaQContFrac (double &gammcf, const double &a, const double &x, double &gln)
 
static double GammaQ (const double &a, const double &x)
 
static double LnGamma (const double &xx)
 
static double BetaCf (const double &a, const double &b, const double &x)
 
static double BetaI (const double &a, const double &b, const double &x)
 
static void LinearFit (const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
 
static void PowerFit (const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
 
static void LogFit (const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
 
static void ExpFit (const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
 
static double LnComb (const int &n, const int &k)
 
static double Entropy (const TIntV &ValV)
 
static double Entropy (const TFltV &ValV)
 
static void EntropyFracDim (const TIntV &ValV, TFltV &EntropyV)
 
static void EntropyFracDim (const TFltV &ValV, TFltV &EntropyV)
 
static double EntropyBias (const double &B)
 
static double GetPowerCoef (const TFltV &XValV, double MinX=-1.0)
 
static double GetPowerCoef (const TFltPrV &XValCntV, double MinX=-1.0)
 

Detailed Description

Definition at line 89 of file xmath.h.

Member Function Documentation

double TSpecFunc::BetaCf ( const double &  a,
const double &  b,
const double &  x 
)
static

Definition at line 99 of file xmath.cpp.

99  {
100  static const double MAXIT=100;
101  static const double EPS=3.0e-7;
102  static const double FPMIN=1.0e-30;
103  int m,m2;
104  double aa,c,d,del,h,qab,qam,qap;
105 
106  qab=a+b;
107  qap=a+1.0;
108  qam=a-1.0;
109  c=1.0;
110  d=1.0-qab*x/qap;
111  if (fabs(d) < FPMIN) d=FPMIN;
112  d=1.0/d;
113  h=d;
114  for (m=1;m<=MAXIT;m++) {
115  m2=2*m;
116  aa=m*(b-m)*x/((qam+m2)*(a+m2));
117  d=1.0+aa*d;
118  if (fabs(d) < FPMIN) d=FPMIN;
119  c=1.0+aa/c;
120  if (fabs(c) < FPMIN) c=FPMIN;
121  d=1.0/d;
122  h *= d*c;
123  aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
124  d=1.0+aa*d;
125  if (fabs(d) < FPMIN) d=FPMIN;
126  c=1.0+aa/c;
127  if (fabs(c) < FPMIN) c=FPMIN;
128  d=1.0/d;
129  del=d*c;
130  h *= del;
131  if (fabs(del-1.0) < EPS) break;
132  }
133  if (m > MAXIT){Fail;}// a or b too big, or MAXIT too small in betacf
134  return h;
135 }
#define Fail
Definition: bd.h:238
double TSpecFunc::BetaI ( const double &  a,
const double &  b,
const double &  x 
)
static

Definition at line 137 of file xmath.cpp.

137  {
138  double bt;
139 
140  if (x < 0.0 || x > 1.0){Fail;} // Bad x in routine betai
141  if (x == 0.0 || x == 1.0) bt=0.0;
142  else
143  bt=exp(LnGamma(a+b)-LnGamma(a)-LnGamma(b)+a*log(x)+b*log(1.0-x));
144  if (x < (a+1.0)/(a+b+2.0))
145  return bt*BetaCf(a,b,x)/a;
146  else
147  return 1.0-bt*BetaCf(b,a,1.0-x)/b;
148 }
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
#define Fail
Definition: bd.h:238
static double BetaCf(const double &a, const double &b, const double &x)
Definition: xmath.cpp:99
double TSpecFunc::Entropy ( const TIntV ValV)
static

Definition at line 231 of file xmath.cpp.

231  {
232  TFltV NewValV(ValV.Len());
233  for (int i = 0; i < ValV.Len(); i++) { NewValV[i] = ValV[i]; }
234  return Entropy(NewValV);
235 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static double Entropy(const TIntV &ValV)
Definition: xmath.cpp:231
double TSpecFunc::Entropy ( const TFltV ValV)
static

Definition at line 238 of file xmath.cpp.

238  {
239  double Sum=0, Ent=0;
240  for (int i = 0; i < ValV.Len(); i++) {
241  const double& Val = ValV[i];
242  if (Val > 0.0) { Ent -= Val * log(Val); Sum += Val; }
243  }
244  if (Sum > 0.0) {
245  Ent /= Sum;
246  Ent += log(Sum);
247  Ent /= TMath::LogOf2;
248  } else return 1.0;
249  return Ent;
250 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static double LogOf2
Definition: xmath.h:9
double TSpecFunc::EntropyBias ( const double &  B)
static

Definition at line 285 of file xmath.cpp.

285  {
286  static TFltFltH BToP;
287  if (BToP.Empty()) {
288  for (double p = 0.5; p < 1.0; p +=0.0001) {
289  double H = p * log(p) + (1.0-p) * log(1.0 - p);
290  H = -H / log(2.0);
291  BToP.AddDat(TMath::Round(H, 3), p);
292  }
293  }
294  if (BToP.IsKey(TMath::Round(B, 3))) { return BToP.GetDat(TMath::Round(B, 3)); }
295  else { return -1.0; }
296 }
bool Empty() const
Definition: hash.h:227
const TDat & GetDat(const TKey &Key) const
Definition: hash.h:262
static double Round(const double &Val)
Definition: xmath.h:16
Definition: hash.h:97
bool IsKey(const TKey &Key) const
Definition: hash.h:258
TDat & AddDat(const TKey &Key)
Definition: hash.h:238
void TSpecFunc::EntropyFracDim ( const TIntV ValV,
TFltV EntropyV 
)
static

Definition at line 252 of file xmath.cpp.

252  {
253  TFltV NewValV(ValV.Len());
254  for (int i = 0; i < ValV.Len(); i++) {
255  IAssert(ValV[i]==1 || ValV[i] == 0);
256  NewValV[i] = ValV[i];
257  }
258  EntropyFracDim(NewValV, EntropyV);
259 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static void EntropyFracDim(const TIntV &ValV, TFltV &EntropyV)
Definition: xmath.cpp:252
void TSpecFunc::EntropyFracDim ( const TFltV ValV,
TFltV EntropyV 
)
static

Definition at line 264 of file xmath.cpp.

264  {
265  TFltV ValV1, ValV2;
266  int Pow2 = 1;
267  while (2*Pow2 <= ValV.Len()) { Pow2 *= 2; }
268  ValV1.Gen(Pow2);
269  for (int i = 0; i < Pow2; i++) { ValV1[i] = ValV[i];
270  IAssert(ValV[i]==1.0 || ValV[i] == 0.0); }
271  EntropyV.Clr();
272  EntropyV.Add(Entropy(ValV1)); // 2^Pow2 windows
273  while (ValV1.Len() > 2) {
274  ValV2.Gen(ValV1.Len() / 2);
275  for (int i = 0; i < ValV1.Len(); i++) {
276  ValV2[i/2] += ValV1[i];
277  }
278  EntropyV.Add(Entropy(ValV2));
279  ValV1.MoveFrom(ValV2);
280  }
281  EntropyV.Reverse();
282 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void Clr(const bool &DoDel=true, const TSizeTy &NoDelLim=-1)
Clears the contents of the vector.
Definition: ds.h:1022
static double Entropy(const TIntV &ValV)
Definition: xmath.cpp:231
void Reverse()
Reverses the order of the elements in the vector.
Definition: ds.h:1350
void Gen(const TSizeTy &_Vals)
Constructs a vector (an array) of _Vals elements.
Definition: ds.h:523
void MoveFrom(TVec< TVal, TSizeTy > &Vec)
Takes over the data and the capacity from Vec.
Definition: ds.h:1073
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
void TSpecFunc::ExpFit ( const TVec< TFltPr > &  XY,
double &  A,
double &  B,
double &  SigA,
double &  SigB,
double &  Chi2,
double &  R2 
)
static

Definition at line 218 of file xmath.cpp.

219  {
220  // Y = A * exp(B*X)
221  TFltPrV XLogY(XY.Len(), 0);
222  double AA, BB;
223  for (int s = 0; s < XY.Len(); s++) {
224  XLogY.Add(TFltPr(XY[s].Val1, log((double)XY[s].Val2)));
225  }
226  TSpecFunc::LinearFit(XLogY, AA, BB, SigA, SigB, Chi2, R2);
227  A = exp(AA);
228  B = BB;
229 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
static void LinearFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:150
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSpecFunc::GammaPSeries ( double &  gamser,
const double &  a,
const double &  x,
double &  gln 
)
static

Definition at line 9 of file xmath.cpp.

10  {
11  static const int ITMAX=100;
12  static const double EPS=3.0e-7;
13  int n;
14  double sum, del, ap;
15 
16  gln=LnGamma(a);
17  if (x <= 0.0){
18  IAssert(x>=0); /*if (x < 0.0) nrerror("x less than 0 in routine gser");*/
19  gamser=0.0;
20  return;
21  } else {
22  ap=a;
23  del=sum=1.0/a;
24  for (n=1; n<=ITMAX; n++){
25  ++ap;
26  del *= x/ap;
27  sum += del;
28  if (fabs(del) < fabs(sum)*EPS){
29  gamser=sum*exp(-x+a*log(x)-(gln));
30  return;
31  }
32  }
33  Fail; /*nrerror("a too large, ITMAX too small in routine gser");*/
34  return;
35  }
36 }
#define IAssert(Cond)
Definition: bd.h:262
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
#define Fail
Definition: bd.h:238
double TSpecFunc::GammaQ ( const double &  a,
const double &  x 
)
static

Definition at line 68 of file xmath.cpp.

68  {
69  IAssert((x>=0)&&(a>0));
70  double gamser, gammcf, gln;
71  if (x<(a+1.0)){
72  GammaPSeries(gamser,a,x,gln);
73  return 1.0-gamser;
74  } else {
75  GammaQContFrac(gammcf,a,x,gln);
76  return gammcf;
77  }
78 }
#define IAssert(Cond)
Definition: bd.h:262
static void GammaQContFrac(double &gammcf, const double &a, const double &x, double &gln)
Definition: xmath.cpp:38
static void GammaPSeries(double &gamser, const double &a, const double &x, double &gln)
Definition: xmath.cpp:9
void TSpecFunc::GammaQContFrac ( double &  gammcf,
const double &  a,
const double &  x,
double &  gln 
)
static

Definition at line 38 of file xmath.cpp.

39  {
40  static const int ITMAX=100;
41  static const double EPS=3.0e-7;
42  static const double FPMIN=1.0e-30;
43  int i;
44  double an, b, c, d, del, h;
45 
46  gln=LnGamma(a);
47  b=x+1.0-a;
48  c=1.0/FPMIN;
49  d=1.0/b;
50  h=d;
51  for (i=1;i<=ITMAX;i++){
52  an = -i*(i-a);
53  b += 2.0;
54  d=an*d+b;
55  if (fabs(d) < FPMIN) d=FPMIN;
56  c=b+an/c;
57  if (fabs(c) < FPMIN) c=FPMIN;
58  d=1.0/d;
59  del=d*c;
60  h *= del;
61  if (fabs(del-1.0) < EPS) break;
62  }
63  IAssert(i<=ITMAX);
64  /*if (i > ITMAX) nrerror("a too large, ITMAX too small in gcf");*/
65  gammcf=exp(-x+a*log(x)-(gln))*h;
66 }
#define IAssert(Cond)
Definition: bd.h:262
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
double TSpecFunc::GetPowerCoef ( const TFltV XValV,
double  MinX = -1.0 
)
static

Definition at line 299 of file xmath.cpp.

299  {
300  for (int i = 0; MinX <= 0.0 && i < XValV.Len(); i++) {
301  MinX = XValV[i]; }
302  IAssert(MinX > 0.0);
303  double LnSum=0.0;
304  for (int i = 0; i < XValV.Len(); i++) {
305  if (XValV[i].Val < MinX) continue;
306  LnSum += log(XValV[i] / MinX);
307  }
308  return 1.0 + double(XValV.Len()) / LnSum;
309 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
double TSpecFunc::GetPowerCoef ( const TFltPrV XValCntV,
double  MinX = -1.0 
)
static

Definition at line 311 of file xmath.cpp.

311  {
312  for (int i = 0; MinX <= 0.0 && i < XValCntV.Len(); i++) {
313  MinX = XValCntV[i].Val1; }
314  IAssert(MinX > 0.0);
315  double NSamples=0.0, LnSum=0.0;
316  for (int i = 0; i < XValCntV.Len(); i++) {
317  if (XValCntV[i].Val1() < MinX) continue;
318  LnSum += XValCntV[i].Val2 * log(XValCntV[i].Val1 / MinX);
319  NSamples += XValCntV[i].Val2;
320  }
321  return 1.0 + NSamples / LnSum;
322 }
#define IAssert(Cond)
Definition: bd.h:262
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
void TSpecFunc::LinearFit ( const TVec< TFltPr > &  XY,
double &  A,
double &  B,
double &  SigA,
double &  SigB,
double &  Chi2,
double &  R2 
)
static

Definition at line 150 of file xmath.cpp.

152  {
153  // y = a + bx :: SigA (SigB): A's uncertainty; Chi2: std dev on all points
154  int i;
155  double t, sxoss, sx = 0.0, sy = 0.0, st2 = 0.0, ss, sigdat;
156 
157  A = B = SigA = SigB = Chi2 = 0.0;
158  for (i = 0; i < XY.Len(); i++) {
159  sx += XY[i].Val1;
160  sy += XY[i].Val2;
161  }
162  ss = XY.Len();
163  sxoss = sx / ss;
164  for (i = 0; i <XY.Len(); i++) {
165  t = XY[i].Val1 - sxoss;
166  st2 += t*t;
167  B += t * XY[i].Val2;
168  }
169  B /= st2;
170  A = (sy - sx * B) / ss;
171  SigA = sqrt((1.0 + sx * sx / (ss * st2)) / ss);
172  SigB = sqrt(1.0 / st2);
173  for (i = 0; i < XY.Len(); i++)
174  Chi2 += TMath::Sqr(XY[i].Val2 - A - B * XY[i].Val1);
175  sigdat = sqrt(Chi2 / (XY.Len() - 2));
176  SigA *= sigdat;
177  SigB *= sigdat;
178 
179  // calculate R squared
180  { double N = XY.Len(), sXY=0.0, sX=0.0, sY=0.0, sSqX=0.0, sSqY=0.0;
181  for (int s =0; s < XY.Len(); s++) {
182  sX += XY[s].Val1; sY += XY[s].Val2;
183  sXY += XY[s].Val1 * XY[s].Val2;
184  sSqX += TMath::Sqr(XY[s].Val1);
185  sSqY += TMath::Sqr(XY[s].Val2);
186  }
187  R2 = TMath::Sqr(N*sXY - sX*sY) / ((N*sSqX - sX*sX) * (N*sSqY - sY*sY)); }
188  if (1.1 < R2 || -1.1 > R2) R2 = 0.0;
189  if (_isnan(A) || ! _finite(A)) A = 0.0;
190  if (_isnan(B) || ! _finite(B)) B = 0.0;
191 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
static double Sqr(const double &x)
Definition: xmath.h:12
double TSpecFunc::LnComb ( const int &  n,
const int &  k 
)
static

Definition at line 95 of file xmath.cpp.

95  {
96  return LnGamma(n+1)-LnGamma(k+1)-LnGamma(n-k+1);
97 }
static double LnGamma(const double &xx)
Definition: xmath.cpp:80
double TSpecFunc::LnGamma ( const double &  xx)
static

Definition at line 80 of file xmath.cpp.

80  {
81  double x, y, tmp, ser;
82  static double cof[6]={76.18009172947146,-86.50532032941677,
83  24.01409824083091,-1.231739572450155,
84  0.1208650973866179e-2,-0.5395239384953e-5};
85  int j;
86 
87  y=x=xx;
88  tmp=x+5.5;
89  tmp -= (x+0.5)*log(tmp);
90  ser=1.000000000190015;
91  for (j=0;j<=5;j++) ser += cof[j]/++y;
92  return -tmp+log(2.5066282746310005*ser/x);
93 }
void TSpecFunc::LogFit ( const TVec< TFltPr > &  XY,
double &  A,
double &  B,
double &  SigA,
double &  SigB,
double &  Chi2,
double &  R2 
)
static

Definition at line 208 of file xmath.cpp.

209  {
210  // Y = A + B*log(X)
211  TFltPrV LogXY(XY.Len(), 0);
212  for (int s = 0; s < XY.Len(); s++) {
213  LogXY.Add(TFltPr(log((double)XY[s].Val1), XY[s].Val2));
214  }
215  TSpecFunc::LinearFit(LogXY, A, B, SigA, SigB, Chi2, R2);
216 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
static void LinearFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:150
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430
void TSpecFunc::PowerFit ( const TVec< TFltPr > &  XY,
double &  A,
double &  B,
double &  SigA,
double &  SigB,
double &  Chi2,
double &  R2 
)
static

Definition at line 193 of file xmath.cpp.

194  {
195  // y = a x^b :: SigA (SigB): A's uncertainty; Chi2: std dev on all points
196  // log fit
197  double AA, BB;
198  TFltPrV LogXY(XY.Len(), 0);
199  for (int s = 0; s < XY.Len(); s++) {
200  LogXY.Add(TFltPr(log((double)XY[s].Val1), log((double)XY[s].Val2)));
201  }
202  TSpecFunc::LinearFit(LogXY, AA, BB, SigA, SigB, Chi2, R2);
203  A = exp(AA); B = BB;
204  if (_isnan(AA) || ! _finite(AA)) A = 0.0;
205  if (_isnan(BB) || ! _finite(BB)) B = 0.0;
206 }
TSizeTy Len() const
Returns the number of elements in the vector.
Definition: ds.h:575
TPair< TFlt, TFlt > TFltPr
Definition: ds.h:99
static void LinearFit(const TVec< TFltPr > &XY, double &A, double &B, double &SigA, double &SigB, double &Chi2, double &R2)
Definition: xmath.cpp:150
TSizeTy Add()
Adds a new element at the end of the vector, after its current last element.
Definition: ds.h:602
Vector is a sequence TVal objects representing an array that can change in size.
Definition: ds.h:430

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