Skip to main content

ln_binomial_pmf

Function ln_binomial_pmf 

Source
pub fn ln_binomial_pmf(n: usize, p: f64) -> Vec<f64>
Expand description

The logarithms of the binomial probabilities $\ln \binom{n}{k} + k \ln p + (n - k) \ln (1 - p)$, $k = 0, \dots, n$: the law of the number of successes among $n$ trials of probability $p \in [0, 1]$ (the coefficients by the running product $\binom{n}{k} = \binom{n}{k-1} (n + 1 - k)/k$; at $p = 0$ or 1 the impossible counts are $-\infty$).

§Examples

use comnum_math::ln_binomial_pmf;
let l = ln_binomial_pmf(20, 0.3);
assert!((l[7].exp() - 0.164_261_985_217_236_67).abs() < 1e-15);
assert!((l.iter().map(|v| v.exp()).sum::<f64>() - 1.0).abs() < 1e-14);
assert_eq!(ln_binomial_pmf(3, 0.0), [0.0, f64::NEG_INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY]);