BCMath Namespace Reference

Some useful mathematic functions. More...


Functions

double LogGaus (double x, double mean=0, double sigma=1, bool norm=false)
double LogPoisson (double x, double par)
double ApproxBinomial (int n, int k, double p)
double LogApproxBinomial (int n, int k, double p)
double LogBinomFactor (int n, int k)
double ApproxLogFact (double x)
double LogNoverK (int n, int k)
double LogFact (int n)
int Max (int x, int y)
double Max (double x, double y)
int Min (int x, int y)
double Min (double x, double y)
int Nint (double x)
double rms (int n, const double *a)
double LogBreitWignerNonRel (double x, double mean, double Gamma, bool norm=false)
double LogBreitWignerRel (double x, double mean, double Gamma)
double LogChi2 (double x, int n)
double LogVoigtian (double x, double sigma, double gamma)
void RandomChi2 (std::vector< double > &randoms, int K)
double LogGaus (double x, double mean, double sigma, bool norm)
double LogPoisson (double x, double par)
double ApproxBinomial (int n, int k, double p)
double LogApproxBinomial (int n, int k, double p)
double LogBinomFactor (int n, int k)
double ApproxLogFact (double x)
double LogFact (int n)
double LogNoverK (int n, int k)
int Nint (double x)
double rms (int n, const double *a)
double LogBreitWignerNonRel (double x, double mean, double Gamma, bool norm)
double LogBreitWignerRel (double x, double mean, double Gamma)
double LogChi2 (double x, int n)
double LogVoigtian (double x, double sigma, double gamma)
void RandomChi2 (std::vector< double > &randoms, int K)


Detailed Description

Some useful mathematic functions.

Author:
Daniel Kollar

Kevin Kröninger

Jing Liu

Version:
1.0
Date:
08.2008 A namespace which encapsulates some mathematical functions necessary for BAT.


Function Documentation

double BCMath::ApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 62 of file BCMath.cxx.

00063 {
00064    return exp( BCMath::LogApproxBinomial(n, k, p) );
00065 }

double BCMath::ApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 62 of file BCMath.cxx.

00063 {
00064    return exp( BCMath::LogApproxBinomial(n, k, p) );
00065 }

double BCMath::ApproxLogFact ( double  x  ) 

Calculates natural logarithm of the n-factorial (n!) using Srinivasa Ramanujan approximation log(n!) = n*log(n) - n + log(n*(1.+4.*n*(1.+2.*n)))/6. + log(PI)/2. if n > 20. If n <= 20 it uses BCMath::LogFact to calculate it exactly.

Definition at line 113 of file BCMath.cxx.

00114 {
00115    if(x>BCMATH_NFACT_ALIMIT)
00116       return x*log(x) - x + log(x*(1.+4.*x*(1.+2.*x)))/6. + log(M_PI)/2.;
00117 
00118    else
00119       return BCMath::LogFact((int)x);
00120 }

double BCMath::ApproxLogFact ( double  x  ) 

Calculates natural logarithm of the n-factorial (n!) using Srinivasa Ramanujan approximation log(n!) = n*log(n) - n + log(n*(1.+4.*n*(1.+2.*n)))/6. + log(PI)/2. if n > 20. If n <= 20 it uses BCMath::LogFact to calculate it exactly.

Definition at line 113 of file BCMath.cxx.

00114 {
00115    if(x>BCMATH_NFACT_ALIMIT)
00116       return x*log(x) - x + log(x*(1.+4.*x*(1.+2.*x)))/6. + log(M_PI)/2.;
00117 
00118    else
00119       return BCMath::LogFact((int)x);
00120 }

double BCMath::LogApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates natural logarithm of the Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 69 of file BCMath.cxx.

00070 {
00071    // check p
00072    if (p == 0)
00073       return -1e99;
00074 
00075    else if (p == 1)
00076       return 0;
00077 
00078    // switch parameters if n < k
00079    if(n<k)
00080    {
00081       int a=n;
00082       n=k;
00083       k=a;
00084    }
00085 
00086    return BCMath::LogBinomFactor(n,k) + (double)k*log(p) + (double)(n-k)*log(1.-p);
00087 }

double BCMath::LogApproxBinomial ( int  n,
int  k,
double  p 
)

Calculates natural logarithm of the Binomial probability using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function.

Definition at line 69 of file BCMath.cxx.

00070 {
00071    // check p
00072    if (p == 0)
00073       return -1e99;
00074 
00075    else if (p == 1)
00076       return 0;
00077 
00078    // switch parameters if n < k
00079    if(n<k)
00080    {
00081       int a=n;
00082       n=k;
00083       k=a;
00084    }
00085 
00086    return BCMath::LogBinomFactor(n,k) + (double)k*log(p) + (double)(n-k)*log(1.-p);
00087 }

double BCMath::LogBinomFactor ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k" using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function. Even for large numbers the calculation is performed precisely, if n-k < 5

Definition at line 91 of file BCMath.cxx.

00092 {
00093    // switch parameters if n < k
00094    if(n<k)
00095    {
00096       int a=n;
00097       n=k;
00098       k=a;
00099    }
00100 
00101    if(n==k || k==0) return 0.;
00102    if(k==1 || k==n-1) return log((double)n);
00103 
00104    // if no approximation needed
00105    if(n<BCMATH_NFACT_ALIMIT || (n-k)<5) return BCMath::LogNoverK(n,k);
00106 
00107    // calculate final log(n over k) using approximations if necessary
00108    return BCMath::ApproxLogFact((double)n) - BCMath::ApproxLogFact((double)k) - BCMath::ApproxLogFact((double)(n-k));
00109 }

double BCMath::LogBinomFactor ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k" using approximations for factorial calculations if calculation for number greater than 20 required using the BCMath::ApproxLogFact function. Even for large numbers the calculation is performed precisely, if n-k < 5

Definition at line 91 of file BCMath.cxx.

00092 {
00093    // switch parameters if n < k
00094    if(n<k)
00095    {
00096       int a=n;
00097       n=k;
00098       k=a;
00099    }
00100 
00101    if(n==k || k==0) return 0.;
00102    if(k==1 || k==n-1) return log((double)n);
00103 
00104    // if no approximation needed
00105    if(n<BCMATH_NFACT_ALIMIT || (n-k)<5) return BCMath::LogNoverK(n,k);
00106 
00107    // calculate final log(n over k) using approximations if necessary
00108    return BCMath::ApproxLogFact((double)n) - BCMath::ApproxLogFact((double)k) - BCMath::ApproxLogFact((double)(n-k));
00109 }

double BCMath::LogBreitWignerNonRel ( double  x,
double  mean,
double  Gamma,
bool  norm = false 
)

Calculates the logarithm of the non-relativistic Breit-Wigner distribution.

Definition at line 209 of file BCMath.cxx.

00210 {
00211    double bw = log(Gamma) - log( (x - mean) * (x - mean) + Gamma * Gamma / 4.0);
00212 
00213    if (norm)
00214       bw -= log(2. * M_PI);
00215 
00216    return bw;
00217 }

double BCMath::LogBreitWignerNonRel ( double  x,
double  mean,
double  Gamma,
bool  norm = false 
)

Calculates the logarithm of the non-relativistic Breit-Wigner distribution.

Definition at line 209 of file BCMath.cxx.

00210 {
00211    double bw = log(Gamma) - log( (x - mean) * (x - mean) + Gamma * Gamma / 4.0);
00212 
00213    if (norm)
00214       bw -= log(2. * M_PI);
00215 
00216    return bw;
00217 }

double BCMath::LogBreitWignerRel ( double  x,
double  mean,
double  Gamma 
)

Definition at line 221 of file BCMath.cxx.

00222 {
00223    return -log( (x*x - mean*mean) * (x*x - mean*mean) + mean*mean*Gamma*Gamma );
00224 }

double BCMath::LogBreitWignerRel ( double  x,
double  mean,
double  Gamma 
)

Definition at line 221 of file BCMath.cxx.

00222 {
00223    return -log( (x*x - mean*mean) * (x*x - mean*mean) + mean*mean*Gamma*Gamma );
00224 }

double BCMath::LogChi2 ( double  x,
int  n 
)

Calculates the logarithm of chi square function: chi2(double x; size_t n)

Definition at line 228 of file BCMath.cxx.

00229 {
00230    if (x<0) {
00231       BCLog::OutWarning("BCMath::LogChi2 : parameter cannot be negative!");
00232       return -1e99;
00233    }
00234 
00235    if (x==0 && n==1) {
00236       BCLog::OutWarning("BCMath::LogChi2 : returned value is infinity!");
00237       return 1e99;
00238    }
00239 
00240    double nOver2 = ((double) n)/2.;
00241 
00242    return (nOver2-1.)*log(x) - x/2. - nOver2*log(2) - log(TMath::Gamma(nOver2));
00243 }

double BCMath::LogChi2 ( double  x,
int  n 
)

Calculates the logarithm of chi square function: chi2(double x; size_t n)

Definition at line 228 of file BCMath.cxx.

00229 {
00230    if (x<0) {
00231       BCLog::OutWarning("BCMath::LogChi2 : parameter cannot be negative!");
00232       return -1e99;
00233    }
00234 
00235    if (x==0 && n==1) {
00236       BCLog::OutWarning("BCMath::LogChi2 : returned value is infinity!");
00237       return 1e99;
00238    }
00239 
00240    double nOver2 = ((double) n)/2.;
00241 
00242    return (nOver2-1.)*log(x) - x/2. - nOver2*log(2) - log(TMath::Gamma(nOver2));
00243 }

double BCMath::LogFact ( int  n  ) 

Calculates natural logarithm of the n-factorial (n!)

Definition at line 123 of file BCMath.cxx.

00124 {
00125    double ln = 0.;
00126 
00127    for(int i=1;i<=n;i++)
00128       ln += log((double)i);
00129 
00130    return ln;
00131 }

double BCMath::LogFact ( int  n  ) 

Calculates natural logarithm of the n-factorial (n!)

Definition at line 123 of file BCMath.cxx.

00124 {
00125    double ln = 0.;
00126 
00127    for(int i=1;i<=n;i++)
00128       ln += log((double)i);
00129 
00130    return ln;
00131 }

double BCMath::LogGaus ( double  x,
double  mean = 0,
double  sigma = 1,
bool  norm = false 
)

Calculate the natural logarithm of a gaussian function with mean and sigma. If norm=true (default is false) the result is multiplied by the normalization constant, i.e. divided by sqrt(2*Pi)*sigma.

Definition at line 22 of file BCMath.cxx.

00023 {
00024    // if we have a delta function, return fixed value
00025    if(sigma==0.)
00026       return 0;
00027 
00028    // if sigma is negative use absolute value
00029    if(sigma<0.)
00030       sigma *= -1.;
00031 
00032    double arg = (x-mean)/sigma;
00033    double result = -.5 * arg * arg;
00034 
00035    // check if we should add the normalization constant
00036    if(!norm)
00037       return result;
00038 
00039    // subtract the log of the denominator of the normalization constant
00040    // and return
00041    return result - log( sqrt( 2.* M_PI ) * sigma );
00042 }

double BCMath::LogGaus ( double  x,
double  mean = 0,
double  sigma = 1,
bool  norm = false 
)

Calculate the natural logarithm of a gaussian function with mean and sigma. If norm=true (default is false) the result is multiplied by the normalization constant, i.e. divided by sqrt(2*Pi)*sigma.

Definition at line 22 of file BCMath.cxx.

00023 {
00024    // if we have a delta function, return fixed value
00025    if(sigma==0.)
00026       return 0;
00027 
00028    // if sigma is negative use absolute value
00029    if(sigma<0.)
00030       sigma *= -1.;
00031 
00032    double arg = (x-mean)/sigma;
00033    double result = -.5 * arg * arg;
00034 
00035    // check if we should add the normalization constant
00036    if(!norm)
00037       return result;
00038 
00039    // subtract the log of the denominator of the normalization constant
00040    // and return
00041    return result - log( sqrt( 2.* M_PI ) * sigma );
00042 }

double BCMath::LogNoverK ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k".

Definition at line 135 of file BCMath.cxx.

00136 {
00137    // switch parameters if n < k
00138    if(n<k)
00139    {
00140       int a = n;
00141       n = k;
00142       k = a;
00143    }
00144 
00145    if(n==k || k==0) return 0.;
00146    if(k==1 || k==n-1) return log((double)n);
00147 
00148    int lmax = BCMath::Max(k,n-k);
00149    int lmin = BCMath::Min(k,n-k);
00150 
00151    double ln = 0.;
00152 
00153    for(int i=n;i>lmax;i--)
00154       ln += log((double)i);
00155    ln -= BCMath::LogFact(lmin);
00156 
00157    return ln;
00158 }

double BCMath::LogNoverK ( int  n,
int  k 
)

Calculates natural logarithm of the Binomial factor "n over k".

Definition at line 135 of file BCMath.cxx.

00136 {
00137    // switch parameters if n < k
00138    if(n<k)
00139    {
00140       int a = n;
00141       n = k;
00142       k = a;
00143    }
00144 
00145    if(n==k || k==0) return 0.;
00146    if(k==1 || k==n-1) return log((double)n);
00147 
00148    int lmax = BCMath::Max(k,n-k);
00149    int lmin = BCMath::Min(k,n-k);
00150 
00151    double ln = 0.;
00152 
00153    for(int i=n;i>lmax;i--)
00154       ln += log((double)i);
00155    ln -= BCMath::LogFact(lmin);
00156 
00157    return ln;
00158 }

double BCMath::LogPoisson ( double  x,
double  par 
)

Calculate the natural logarithm of a poisson distribution.

Definition at line 46 of file BCMath.cxx.

00047 {
00048    if (par > 899)
00049       return BCMath::LogGaus(x,par,sqrt(par),true);
00050 
00051    if (x<0)
00052       return 0;
00053 
00054    if (x == 0.)
00055       return  -par;
00056 
00057    return x*log(par)-par-BCMath::ApproxLogFact(x);
00058 }

double BCMath::LogPoisson ( double  x,
double  par 
)

Calculate the natural logarithm of a poisson distribution.

Definition at line 46 of file BCMath.cxx.

00047 {
00048    if (par > 899)
00049       return BCMath::LogGaus(x,par,sqrt(par),true);
00050 
00051    if (x<0)
00052       return 0;
00053 
00054    if (x == 0.)
00055       return  -par;
00056 
00057    return x*log(par)-par-BCMath::ApproxLogFact(x);
00058 }

double BCMath::LogVoigtian ( double  x,
double  sigma,
double  gamma 
)

Calculates the logarithm of normalized voigtian function: voigtian(double x, double sigma, double gamma)

voigtian is a convolution of the following two functions: gaussian(x) = 1/(sqrt(2*pi)*sigma) * exp(x*x/(2*sigma*sigma) and lorentz(x) = (1/pi)*(gamma/2) / (x*x + (gamma/2)*(gamma/2))

it is singly peaked at x=0. The width of the peak is decided by sigma and gamma, so they should be positive.

Definition at line 247 of file BCMath.cxx.

00248 {
00249    if (sigma<=0 || gamma<=0) {
00250       BCLog::OutWarning("BCMath::LogVoigtian : widths are negative or zero!");
00251       return -1e99;
00252    }
00253 
00254    return log(TMath::Voigt(x,sigma,gamma));
00255 }

double BCMath::LogVoigtian ( double  x,
double  sigma,
double  gamma 
)

Calculates the logarithm of normalized voigtian function: voigtian(double x, double sigma, double gamma)

voigtian is a convolution of the following two functions: gaussian(x) = 1/(sqrt(2*pi)*sigma) * exp(x*x/(2*sigma*sigma) and lorentz(x) = (1/pi)*(gamma/2) / (x*x + (gamma/2)*(gamma/2))

it is singly peaked at x=0. The width of the peak is decided by sigma and gamma, so they should be positive.

Definition at line 247 of file BCMath.cxx.

00248 {
00249    if (sigma<=0 || gamma<=0) {
00250       BCLog::OutWarning("BCMath::LogVoigtian : widths are negative or zero!");
00251       return -1e99;
00252    }
00253 
00254    return log(TMath::Voigt(x,sigma,gamma));
00255 }

double BCMath::Max ( double  x,
double  y 
) [inline]

Definition at line 96 of file BCMath.h.

00097       { return x >= y ? x : y; }

int BCMath::Max ( int  x,
int  y 
) [inline]

Returns the "greater or equal" of two numbers

Definition at line 93 of file BCMath.h.

00094       { return x >= y ? x : y; }

double BCMath::Min ( double  x,
double  y 
) [inline]

Definition at line 105 of file BCMath.h.

00106    { return x <= y ? x : y; }

int BCMath::Min ( int  x,
int  y 
) [inline]

Returns the "less or equal" of two numbers

Definition at line 102 of file BCMath.h.

00103       { return x <= y ? x : y; }

int BCMath::Nint ( double  x  ) 

Returns the nearest integer of a double number.

Definition at line 162 of file BCMath.cxx.

00163 {
00164    // round to integer
00165    int i;
00166 
00167    if (x >= 0)
00168    {
00169       i = (int)(x + .5);
00170 
00171       if (x + .5 == (double)i && i & 1)
00172          i--;
00173    }
00174 
00175    else
00176    {
00177       i = int(x - 0.5);
00178 
00179       if (x - 0.5 == double(i) && i & 1)
00180          i++;
00181    }
00182 
00183    return i;
00184 }

int BCMath::Nint ( double  x  ) 

Returns the nearest integer of a double number.

Definition at line 162 of file BCMath.cxx.

00163 {
00164    // round to integer
00165    int i;
00166 
00167    if (x >= 0)
00168    {
00169       i = (int)(x + .5);
00170 
00171       if (x + .5 == (double)i && i & 1)
00172          i--;
00173    }
00174 
00175    else
00176    {
00177       i = int(x - 0.5);
00178 
00179       if (x - 0.5 == double(i) && i & 1)
00180          i++;
00181    }
00182 
00183    return i;
00184 }

void BCMath::RandomChi2 ( std::vector< double > &  randoms,
int  K 
)

Get N random numbers distributed according to chi square function with K degrees of freedom

Definition at line 264 of file BCMath.cxx.

00264                                                         {
00265 
00266    //fixed upper cutoff to 1000, might be too small
00267    TF1 *f = new TF1("chi2", chi2, 0.0, 1000, 1);
00268    f->SetParameter(0, K);
00269    f->SetNpx(500);
00270    //uses inverse-transform method
00271    //fortunately CDF only built once
00272    for (unsigned int i = 0; i < randoms.size(); i++)
00273       randoms.at(i) = f->GetRandom();
00274    delete f;
00275 }

void BCMath::RandomChi2 ( std::vector< double > &  randoms,
int  K 
)

Get N random numbers distributed according to chi square function with K degrees of freedom

Definition at line 264 of file BCMath.cxx.

00264                                                         {
00265 
00266    //fixed upper cutoff to 1000, might be too small
00267    TF1 *f = new TF1("chi2", chi2, 0.0, 1000, 1);
00268    f->SetParameter(0, K);
00269    f->SetNpx(500);
00270    //uses inverse-transform method
00271    //fortunately CDF only built once
00272    for (unsigned int i = 0; i < randoms.size(); i++)
00273       randoms.at(i) = f->GetRandom();
00274    delete f;
00275 }

double BCMath::rms ( int  n,
const double *  a 
)

Returns the rms of an array.

Definition at line 188 of file BCMath.cxx.

00189 {
00190    if (n <= 0 || !a)
00191       return 0;
00192 
00193    double sum = 0., sum2 = 0.;
00194 
00195    for (int i = 0; i < n; i++)
00196    {
00197       sum  += a[i];
00198       sum2 += a[i] * a[i];
00199    }
00200 
00201    double n1 = 1./(double)n;
00202    double mean = sum * n1;
00203 
00204    return sqrt(fabs(sum2 * n1 - mean * mean));
00205 }

double BCMath::rms ( int  n,
const double *  a 
)

Returns the rms of an array.

Definition at line 188 of file BCMath.cxx.

00189 {
00190    if (n <= 0 || !a)
00191       return 0;
00192 
00193    double sum = 0., sum2 = 0.;
00194 
00195    for (int i = 0; i < n; i++)
00196    {
00197       sum  += a[i];
00198       sum2 += a[i] * a[i];
00199    }
00200 
00201    double n1 = 1./(double)n;
00202    double mean = sum * n1;
00203 
00204    return sqrt(fabs(sum2 * n1 - mean * mean));
00205 }


Generated on Thu Feb 11 11:29:38 2010 for BayesianAnalysisToolkit by  doxygen 1.5.1