pub struct Binomial { /* private fields */ }Expand description
Binomial distribution with n trials and success probability p.
Models the number of successes in a sequence of n independent Bernoulli trials. The CDF reduces to the incomplete Β (Abramowitz–Stegun 26.5.24): Pr[S ≤ s] = I₁ ₋ ₚ(n − s, s + 1).
§Notes
Entropy is not implemented.
§Example
use cdflib::Binomial;
use cdflib::traits::{Discrete, DiscreteCdf};
let b = Binomial::new(10, 0.3);
// Probability of 3 or fewer successes in 10 trials
let cdf = b.cdf(3);
// Compute success probability given Pr[S ≤ 2] = 0.5 and n = 10
let pr = Binomial::search_pr(0.5, 0.5, 10, 2).unwrap();Implementations§
Source§impl Binomial
impl Binomial
Sourcepub fn try_new(n: u64, pr: f64) -> Result<Self, BinomialError>
pub fn try_new(n: u64, pr: f64) -> Result<Self, BinomialError>
Fallible counterpart of new returning a
BinomialError instead of panicking.
Returns PrOutOfRange if pr falls outside [0 . . 1] or is
non-finite.
Sourcepub fn search_trials(
p: f64,
q: f64,
pr: f64,
s: u64,
) -> Result<f64, BinomialError>
pub fn search_trials( p: f64, q: f64, pr: f64, s: u64, ) -> Result<f64, BinomialError>
Returns the (continuous) number of trials n satisfying Pr[S ≤ s] = p given the success probability. The search works on the continuous extension of the CDF.
Mirrors CDFLIB’s cdfbin with which = 3. Caller passes both
p and q = 1 − p; consistency is enforced within 3ε.
Sourcepub fn search_pr(p: f64, q: f64, n: u64, s: u64) -> Result<f64, BinomialError>
pub fn search_pr(p: f64, q: f64, n: u64, s: u64) -> Result<f64, BinomialError>
Returns the success probability pr satisfying Pr[S ≤ s] = p given n.
Mirrors CDFLIB’s cdfbin with which = 4 (cdflib.f90:3232-3265).
Caller passes both p and q = 1 − p; consistency is enforced
within 3ε. When p > q the search runs on ompr = 1 − pr
(F90’s variable-switch precision strategy) and returns pr = 1 − ompr.
Source§impl Binomial
impl Binomial
Sourcepub fn inverse_ccdf(&self, q: f64) -> Result<f64, BinomialError>
pub fn inverse_ccdf(&self, q: f64) -> Result<f64, BinomialError>
Returns the real-valued s such that cdf(s) = 1 − q on the smooth continuous extension via I₁₋ₚᵣ(n−s, s+1).
Mirrors CDFLIB’s cdfbin with which = 2 (cdflib.f90:3138-3185):
single dinvr loop, residual cum-p if p≤q else ccum-q.