Skip to main content

Binomial

Struct Binomial 

Source
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[Ss] = I₁ ₋ (ns, 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

Source

pub fn new(n: u64, pr: f64) -> Self

Construct a Binomial(n, pr) distribution with n trials and success probability pr ∈ [0 . . 1].

§Panics

Panics if pr is invalid; use try_new for a fallible variant.

Source

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.

Source

pub const fn n(&self) -> u64

Returns the number of trials n.

Source

pub const fn pr(&self) -> f64

Returns the success probability pr.

Source

pub fn search_trials( p: f64, q: f64, pr: f64, s: u64, ) -> Result<f64, BinomialError>

Returns the (continuous) number of trials n satisfying Pr[Ss] = 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ε.

Source

pub fn search_pr(p: f64, q: f64, n: u64, s: u64) -> Result<f64, BinomialError>

Returns the success probability pr satisfying Pr[Ss] = 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

Source

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₁₋ₚᵣ(ns, 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.

Trait Implementations§

Source§

impl Clone for Binomial

Source§

fn clone(&self) -> Binomial

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Binomial

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Discrete for Binomial

Source§

fn pmf(&self, s: u64) -> f64

Returns the mass Pr[X = x] at the support point x.
Source§

fn ln_pmf(&self, s: u64) -> f64

Returns the logarithm of the mass Pr[X = x]. Computing in log-space avoids underflow in the tails.
Source§

impl DiscreteCdf for Binomial

Source§

type Error = BinomialError

Domain-specific error type returned by the inverse routines.
Source§

fn cdf(&self, s: u64) -> f64

Returns Pr[Xx].
Source§

fn ccdf(&self, s: u64) -> f64

Returns Pr[X > x] = 1 − cdf(x). Read more
Source§

fn inverse_cdf(&self, p: f64) -> Result<u64, BinomialError>

Returns the smallest integer x such that cdf(x) ≥ p. At p = 0 returns 0; at p = 1 returns the supremum of support (the upper bound for distributions with finite support, u64::MAX for the unbounded ones).
Source§

impl Mean for Binomial

Source§

fn mean(&self) -> f64

Returns the expected value E[X]. Read more
Source§

impl PartialEq for Binomial

Source§

fn eq(&self, other: &Binomial) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Variance for Binomial

Source§

fn variance(&self) -> f64

Returns the variance Var(X) = E[(X − E[X])²]. Read more
Source§

fn std_dev(&self) -> f64

Returns the standard deviation (the square root of the variance).
Source§

impl Copy for Binomial

Source§

impl StructuralPartialEq for Binomial

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.