Logo Coherent WaveBurst  
Reference Guide
Logo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
watfun.hh
Go to the documentation of this file.
1 // Wavelet Analysis Tool
2 // S.Klimenko, University of Florida
3 // library of general functions
4 
5 #ifndef WATFUN_HH
6 #define WATFUN_HH
7 
8 #include <iostream>
9 #include <stdlib.h>
10 #include <math.h>
11 #include <TMath.h>
12 #include "wat.hh"
13 
14 #define PI 3.141592653589793
15 #define speedlight 299792458.0
16 
17 // WAT functions
18 
19 // Calculates polynomial interpolation coefficient using Lagrange formula.
20 inline double Lagrange(const int n, const int i, const double x)
21 {
22  double c = 1.;
23  double xn = x+n/2.-0.5; // shift to the center of interpolation interval
24 
25  for(int j=0; j<n; j++)
26  if(j!=i) c *= (xn-j)/(i-j);
27 
28  return c;
29 }
30 
31 
32 // Nevill's polynomial interpolation.
33 // x - polynom argument (x stride is allways 1)
34 // n - number of interpolation samples
35 // p - pointer to a sample with x=0.
36 // q - double array of length n
37 template<class DataType_t>
38 inline double Nevill(const double x0,
39  int n,
40  DataType_t* p,
41  double* q)
42 {
43  int i;
44  double x = x0;
45  double xm = 0.5;
46 
47  n--;
48  *q = *p;
49 
50  for(i=0; i<n; i++)
51  q[i] = p[i] + (x--)*(p[i+1]-p[i]);
52 
53  while(--n >= 1){
54  x = x0;
55 
56  q[0] += xm*(x--)*(q[1]-q[0]);
57  if(n == 1) goto M0;
58  q[1] += xm*(x--)*(q[2]-q[1]);
59  if(n == 2) goto M0;
60  q[2] += xm*(x--)*(q[3]-q[2]);
61  if(n == 3) goto M0;
62  q[3] += xm*(x--)*(q[4]-q[3]);
63  if(n == 4) goto M0;
64  q[4] += xm*(x--)*(q[5]-q[4]);
65  if(n == 5) goto M0;
66  q[5] += xm*(x--)*(q[6]-q[5]);
67  if(n == 6) goto M0;
68 
69  for(i=6; i<n; i++)
70  q[i] += xm*(x--)*(q[i+1]-q[i]);
71 
72 M0: xm /= (1.+xm);
73  }
74 
75  return *q;
76 }
77 
78 // calculate significance for sign cross-correlation
79 inline double signPDF(const size_t m, const size_t k)
80 {
81  size_t i;
82  double pdf = 0.;
83  size_t n = m+k;
84  double rho = (double(m)-double(k))/n;
85 
86  if(n < 1) return 0.;
87  if(n < 100) {
88  for(i=1; i<k+1; i++){ pdf -= log(double(m+i)/double(i)); }
89  pdf -= log(double(n))-(n+1)*log(2.);
90  pdf -= log(sqrt(2.*PI/n)); // normalization from Gaussian approximation
91  }
92  else pdf = n*rho*rho/2.;
93  return pdf;
94 }
95 
96 
97 // survival probability for Gamma distribution
98 // calculates integral I=int(x*x^n*exp(-x)) from Y to infinity
99 // returns confidence = -log(I/Gamma(n))
100 // Y - low integration limit
101 // n - Gamma function parameter
102 inline double gammaCLa(double Y, int n){
103  double y = Y;
104  double s = 1.;
105  // if(Y<0.) return 0.;
106  for(int k=1; k<n; k++){
107  s += y;
108  if((y*=Y/(k+1))>1.e290) break;
109  }
110  return Y-log(s);
111 }
112 
113 inline double gammaCL(double x, double n){
114  return x>=n ? x-n + (2+(n-1)/sqrt(2.))/(n+1)-(n-sqrt(n/4.)-1)*log(x/n) :
115  pow(x/n,sqrt(n))*(2+(n-1)/sqrt(2.))/(n+1);
116 }
117 
118 // survival probability for LogNormal distribution
119 // calculates integral I=int(Pln(x)) from Y to infinity
120 // returns one side confidence = -log(I)
121 // Y - low integration limit
122 // p - x peak
123 // s - sigma
124 // a - asymmetry
125 
126 inline double logNormCL(double Y, double p=0., double s=1., double a=0.0001){
127  // if(a <= 0.) a = 0.0001;
128  double z = a*sqrt(log(4.));
129  z = sinh(z)/z;
130  z = 1+a*z*(Y-p)/s;
131  z = z>0 ? fabs(log(z)/a-a) : 0.;
132  return erfc(z/sqrt(2.))/2.;
133 }
134 
135 // calculate value of argument from LogNormal and Normal survival probability
136 // calculates inverse of integral I=int(P(x) dx) from Y to infinity
137 // returns value of the argument Y
138 // CL - single side probability
139 // p - x peak
140 // s - sigma
141 // a - asymmetry
142 
143 inline double logNormArg(double CL, double p=0., double s=1., double a=0.){
144  double z = fabs(sqrt(-2.14*log(2.*CL))-0.506);
145  if(a <= 0.) return p+z*s;
146  double f = a*sqrt(log(4.));
147  z = (exp((z+a)*a)-1)/a;
148  return p+z*s*f/sinh(f);
149 }
150 
151 
152 // Calculates polynomial interpolation coefficients using Lagrange formula.
153 // gap is allouded in the middle of interval, like
154 // n=13, m=5: x x x x o o o o o x x x x
155 inline void fLagrange(int n, int m, double* c)
156 {
157  if(!(n&1)) n++;
158  if(!(m&1)) m++;
159  int i,j;
160 
161  for(i=0; i<n; i++) c[i]=0.;
162 
163  for(i=0; i<n; i++) {
164  if(abs(n/2-i)<=m/2) continue;
165  c[i] = 1.;
166  for(j=0; j<n; j++) {
167  if(abs(n/2-j)<=m/2) continue;
168  if(j!=i) c[i] *= double(n/2-j)/(i-j);
169  }
170  }
171  return;
172 }
173 
174 // complete gamma function
175 inline double Gamma(double r)
176 {
177  return TMath::Gamma(r);
178 }
179 
180 // incomplete regularized gamma function
181 inline double Gamma(double r, double x)
182 {
183  return TMath::Gamma(r,x);
184 }
185 
186 // inverse incomplete regularized gamma function
187 inline double iGamma(double r, double p)
188 {
189  double x = 0;
190  double P = 1.;
191  while(P>p) { x += 1.e-5; P=1-TMath::Gamma(r,x); }
192  return x;
193 }
194 
195 // complete gamma function
196 // used by 1G - only for back compatibility
197 inline double Gamma1G(double r)
198 {
199  if(r<=0.) return 0.;
200  double p0 = 1.000000000190015;
201  double p1 = 76.18009172947146;
202  double p2 = -86.50532032941677;
203  double p3 = 24.01409824083091;
204  double p4 = -1.231739572450155;
205  double p5 = 1.208650973866179e-3;
206  double p6 = -5.395239384953e-6;
207  double g = p0+p1/(r+1)+p2/(r+2)+p3/(r+3)+p4/(r+4)+p5/(r+5)+p6/(r+6);
208  return g*pow(r+5.5,r+0.5)*exp(-(r+5.5))*sqrt(2*PI)/r;
209 }
210 
211 // incomplete regularized gamma function
212 // used by 1G - only for back compatibility
213 inline double Gamma1G(double r, double x)
214 {
215  double a = r>0 ? 1/r : 0;
216  double b = a;
217  int n = int(10*x-r);
218  if(n<100) n = 100;
219  for(int i=1; i<n; i++) {
220  a *= x/(r+i);
221  b += a;
222  }
223  return b*exp(log(x)*r-x)/Gamma1G(r);
224 }
225 
226 // inverse incomplete regularized gamma function (approximation)
227 // used by 1G - only for back compatibility
228 inline double iGamma1G(double r, double p)
229 {
230  double x = 700;
231  double P = 1.;
232  while(P>p) { x *= 0.999; P=Gamma1G(r,x); }
233  return 2*x;
234 }
235 
236 #endif // WATFUN_HH
double rho
double gammaCL(double x, double n)
Definition: watfun.hh:113
static double g(double e)
Definition: GNGen.cc:98
tuple f
Definition: cwb_online.py:91
wavearray< double > a(hp.size())
double Gamma(double r)
Definition: watfun.hh:175
int n
Definition: cwb_net.C:10
wavearray< double > z
Definition: Test10.C:32
double iGamma(double r, double p)
Definition: watfun.hh:187
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\t layers : "<< nLAYERS<< "\t dF(hz) : "<< dF<< "\t dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1)*itime+ifreq;double time=itime *dT;double freq=(ifreq >0)?ifreq *dF:dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
double Gamma1G(double r)
Definition: watfun.hh:197
TRandom3 P
Definition: compare_bkg.C:296
double logNormCL(double Y, double p=0., double s=1., double a=0.0001)
Definition: watfun.hh:126
double Nevill(const double x0, int n, DataType_t *p, double *q)
Definition: watfun.hh:38
watplot p1(const_cast< char * >("TFMap1"))
int m
Definition: cwb_net.C:10
int j
Definition: cwb_net.C:10
i drho i
#define PI
Definition: watfun.hh:14
double signPDF(const size_t m, const size_t k)
Definition: watfun.hh:79
i() int(T_cor *100))
bool log
Definition: WaveMDC.C:41
int k
double Lagrange(const int n, const int i, const double x)
Definition: watfun.hh:20
double logNormArg(double CL, double p=0., double s=1., double a=0.)
Definition: watfun.hh:143
void fLagrange(int n, int m, double *c)
Definition: watfun.hh:155
regression r
Definition: Regression_H1.C:44
Double_t x0
s s
Definition: cwb_net.C:137
double gammaCLa(double Y, int n)
Definition: watfun.hh:102
watplot p2(const_cast< char * >("TFMap2"))
double fabs(const Complex &x)
Definition: numpy.cc:37
double iGamma1G(double r, double p)
Definition: watfun.hh:228
wavearray< double > y
Definition: Test10.C:31