biomcmc-lib  0.1
low level library for phylogenetic analysis
prob_distribution.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 
29 #ifndef _biomcmc_prob_distribution_h_
30 #define _biomcmc_prob_distribution_h_
31 
32 #include "lowlevel.h"
33 #include "random_number.h"
34 
36 
37 /* \brief sampling from a discrete distribution in O(1) time (from GSL based on Walker's algorithm) */
39 {
40  size_t K; /* note that here we DO NOT use int, but size_t (which limits somehow the largest vector size, but practical) */
41  size_t *A;
42  double *F;
43 };
44 
45 // reminder: gamma is in E[x] = a/b , but poisson is E[x] = lambda (since lambda is related to alpha, not beta) */
46 
48 void biomcmc_discrete_gamma (double alpha, double beta, double *rate, int nrates);
49 
51 double biomcmc_dexp_dt (double d, double lambda, double m, bool log_p);
53 double biomcmc_pexp_dt (double d, double lambda, double m, bool log_p);
55 double biomcmc_qexp_dt (double p, double lambda, double m, bool log_p);
56 
58 double biomcmc_dgamma (double x, double alpha, double beta, bool log_p);
60 double biomcmc_qgamma (double p, double alpha, double beta, bool log_p);
63 double biomcmc_pgamma (double x, double alpha, double beta, bool log_p);
64 
65 double biomcmc_dnorm (double x, double mu, double sigma, bool log_p);
66 double biomcmc_qnorm (double p, double mu, double sigma, bool log_p);
67 double biomcmc_pnorm (double x, double mu, double sigma, bool log_p);
68 
69 double biomcmc_dlnorm (double x, double meanlog, double sdlog, bool log_p);
70 double biomcmc_qlnorm (double p, double meanlog, double sdlog, bool log_p);
71 double biomcmc_plnorm (double x, double meanlog, double sdlog, bool log_p);
72 
73 double biomcmc_dpois (double x, double lambda, bool log_p);
74 /*The quantile function of the Poisson distribution. */
75 double biomcmc_qpois (double p, double lambda, bool log_p);
76 double biomcmc_ppois (double x, double lambda, bool log_p);
77 
78 double biomcmc_rng_gamma (double alpha, double beta);
80 double biomcmc_rng_norm (double mu, double sigma);
81 double biomcmc_rng_lnorm (double meanlog, double sdlog);
82 double biomcmc_rng_pois (double mu);
83 
84 
85 /* Calculates log|gamma(x)| and returns the sign of the gamma function in sgn if *sgn is not NULL. */
86 double biomcmc_lgammafn (double x, int *sgn);
87 /* This function computes the value of the gamma function. */
88 double biomcmc_gammafn (double x);
89 
91 double biomcmc_log1p (double x);
93 double biomcmc_log1pmx (double x);
95 double biomcmc_expm1 (double x);
96 
97 /* create the struct for sampling from an arbitrary empirical frequency vector */
98 discrete_sample new_discrete_sample_from_frequencies (double *prob, size_t size);
99 /* free discrete_sample_struct */
100 void del_discrete_sample (discrete_sample g);
101 /* sample index from empirical discrete distribution (between zero and g->K which is the original vector size) */
102 size_t biomcmc_rng_discrete (discrete_sample g);
103 /* density from empirical frequency vector (that is, original values from vector) */
104 double biomcmc_discrete_sample_pdf (discrete_sample g, size_t k);
105 
106 /* Compute log (exp (logx) + exp (logy)) without overflow and without loss of accuracy. */
107 double biomcmc_logspace_add (double logx, double logy);
108 /* Compute log (exp (logx) - exp (logy)) without overflow and without loss of accuracy. */
109 double biomcmc_logspace_sub (double logx, double logy);
111 bool biomcmc_isfinite(double x);
112 
113 #endif
Random number generation, with algorithms from the Gnu Scientific Library (GSL) and motivation from t...
double biomcmc_pgamma(double x, double alpha, double beta, bool log_p)
computes the cummulative distribution function for the gamma distribution with shape parameter alpha ...
Definition: prob_distribution.c:201
Definition: prob_distribution.h:38
double biomcmc_log1pmx(double x)
accurate calculation of , particularly for small x
Definition: prob_distribution.c:872
double biomcmc_expm1(double x)
compute accurately also when x is close to zero, i.e.
Definition: prob_distribution.c:886
bool biomcmc_isfinite(double x)
check if number is between minus infinity and plus infinity, or NaN
Definition: prob_distribution.c:1102
Lowest level header file. Header file for lowlevel.c.
double biomcmc_pexp_dt(double d, double lambda, double m, bool log_p)
cdf of discrete truncated exponential (d is discrete, m is maximum value): calculates P(D <= d) ...
Definition: prob_distribution.c:65
double biomcmc_log1p(double x)
compute the relative error logarithm (C99 standard)
Definition: prob_distribution.c:827
double biomcmc_qgamma(double p, double alpha, double beta, bool log_p)
gamma quantile (inverse CDF)
Definition: prob_distribution.c:121
double biomcmc_dexp_dt(double d, double lambda, double m, bool log_p)
pdf of discrete truncated exponential (d is discrete, m is maximum value)
Definition: prob_distribution.c:53
double biomcmc_rng_norm(double mu, double sigma)
Returns a random number from a Normal distribution N(mu, sigma^2) using 52 bits of precision...
Definition: prob_distribution.c:522
void biomcmc_discrete_gamma(double alpha, double beta, double *rate, int nrates)
Ziheng Yang&#39;s gamma discretization of rates.
Definition: prob_distribution.c:34
double biomcmc_dgamma(double x, double alpha, double beta, bool log_p)
gamma density
Definition: prob_distribution.c:92
double biomcmc_qexp_dt(double p, double lambda, double m, bool log_p)
quantile of discrete truncated exponential, that is, finds d s.t. P(D <= d) >= p
Definition: prob_distribution.c:77