biomcmc-lib  0.1
low level library for phylogenetic analysis
prob_distribution_aux.h
Go to the documentation of this file.
1 /*
2  * This file is part of biomcmc-lib, a low-level library for phylogenomic analysis.
3  * Copyright (C) 2019-today Leonardo de Oliveira Martins [ leomrtns at gmail.com; http://www.leomartins.org ]
4  *
5  * biomcmc is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
6  * License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
10  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11  * details (file "COPYING" or http://www.gnu.org/copyleft/gpl.html).
12  */
13 
18 #ifndef _biomcmc_prob_distribution_aux_h_
19 #define _biomcmc_prob_distribution_aux_h_
20 
21 #include "prob_distribution.h"
22 
23 #define R_LN2 0.693147180559945309417232121458 /* ln(2) */
24 #define R_PI 3.141592653589793238462643383280 /* pi */
25 #define R_2PI 6.283185307179586476925286766559 /* 2*pi */
26 #define R_EXP_M1 0.367879441171442321595523770161 /* exp(-1) = 1/e */
27 #define R_SQRT_32 5.656854249492380195206754896838 /* sqrt(32) */
28 #define R_1_SQRT_2PI 0.398942280401432677939946059934 /* 1/sqrt(2*pi) */
29 #define R_LN_SQRT_2PI 0.918938533204672741780329736406 /* log(sqrt(2*pi)) = log(2*pi)/2 */
30 #define R_LN_SQRT_PId2 0.225791352644727432363097614947 /* log(sqrt(pi/2)) */
31 
32 const double pInf = 1./0., mInf = -1./0., NaN = 0./0.;
33 /* Scalefactor:= (2^32)^8 = 2^256 = 1.157921e+77 */
34 const double scalefactor = 115792089237316195423570985008687907853269984665640564039457584007913129639936.;
35 /* If |x| > |k| * M_cutoff, then log[ exp(-x) * k^x ] =~= -x */
36 const double M_cutoff = R_LN2 * DBL_MAX_EXP / DBL_EPSILON;/*=3.196577e18*/
37 
38 /* log gamma correction factor for x >= 10 so that log(gamma(x)) = .5*log(2*pi) + (x-.5)*log(x) -x + lgammacor(x) */
39 double lgammacor (double x);
40 /* determines the number of terms for the double precision orthogonal series "dos" needed to insure
41  * the error is no larger than "eta". Ordinarily eta will be chosen to be one-tenth machine precision. */
42 int chebyshev_init (const double *dos, int nos, double eta);
43 /* evaluates the n-term Chebyshev series "a" at "x" */
44 double chebyshev_eval (double x, const double *a, const int n);
45 /* This function calculates the minimum and maximum legal bounds for x in biomcmc_gammafn(x). These are not the only
46  * bounds, but they are the only non-trivial ones to calculate. */
47 void gammalims (double *xmin, double *xmax);
48 /* Continued fraction for calculation of 1/i + x/(i+d) + x^2/(i+2*d) + x^3/(i+3*d) + ... = sum_{k=0}^Inf x^k/(i+k*d)
49  * auxilary in biomcmc_log1pmx() and lgamma1p(); eps = relative tolerance */
50 double logcf (double x, double i, double d, double eps);
51 /* Compute log(gamma(a+1)) accurately also for small a (0 < a < 0.5). */
52 double lgamma1p (double a);
53 /* wrapper over poisson density (== dpois (x_P_1 - 1, lambda, log_p)) */
54 double dpois_wrap (double x_plus_1, double lambda, bool log_p);
55 /* lowlevel poisson density (assuming E[X] = lambda, which is the scale and not rate) */
56 double dpois_raw(double x, double lambda, bool log_p);
57 /* computes the log of the error term in Stirling's formula. */
58 double stirlerr(double n);
59 /* Evaluates the "deviance part" bd0(x,M) = M * D0(x/M) = M*[ x/M * log(x/M) + 1 - (x/M) ] = x * log(x/M) + M - x
60  * where M = E[X] = n*p (or = lambda), for x, M > 0 */
61 double bd0(double x, double np);
62 /* auxiliary function for pgamma() */
63 double pgamma_smallx (double x, double alph, bool log_p);
64 /* sum of upper terms of series (auxiliar to pgamma()) */
65 double pd_upper_series (double x, double y, bool log_p);
66 /* sum of lower terms of series (auxiliar to pgamma()) */
67 double pd_lower_series (double lambda, double y);
68 /* Continued fraction for calculation of (i/d) + o(i/d) ( auxiliar to pgamma())*/
69 double pd_lower_cf (double i, double d);
70 /* Compute \f$\frac{dnorm (x, 0, 1, FALSE)}{pnorm (x, 0, 1, lower_tail = T, FALSE)} \f$ accurately */
71 double dpnorm (double x, double lp);
72 /* Asymptotic expansion to calculate the probability that Poisson variate has value <= x. */
73 double ppois_asymp (double x, double lambda, bool log_p);
74 /* lowlevel pgamma() calculation (without variable checking etc) */
75 double pgamma_raw (double x, double alph, bool log_p);
76 /* chi-squared approximation to qgamma, g = log Gamma (nu/2) and tol = EPS1 */
77 double qchisq_appr (double p, double nu, double g, bool log_p, double tol);
78 /* returns both cummulative points from the normal */
79 void pnorm_both(double x, double *cum, double *ccum, int i_tail, bool log_p);
80 /* find quantile for poisson distribution */
81 double do_poisson_search (double y, double *z, double p, double lambda, double incr);
82 
83 #endif
Probability distribution functions and auxiliary mathematical functions from statistical package R...