pub struct Multinomial {
pub n: u64,
pub p: Array1<f64>,
}Expand description
Multinomial distribution structure
The multinomial distribution is a generalization of the binomial distribution. It models the probability of counts for each side of a k-sided die rolled n times.
Fields§
§n: u64Number of trials
p: Array1<f64>Probability of each outcome (must sum to 1)
Implementations§
Source§impl Multinomial
impl Multinomial
Sourcepub fn new<D>(n: u64, p: ArrayBase<D, Ix1>) -> StatsResult<Self>
pub fn new<D>(n: u64, p: ArrayBase<D, Ix1>) -> StatsResult<Self>
Create a new Multinomial distribution with given parameters
§Arguments
n- Number of trialsp- Probability of each outcome (must sum to 1)
§Returns
- A new Multinomial distribution instance
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
// Create a multinomial distribution for a 3-sided die rolled 10 times
let n = 10;
let p = array![0.2, 0.3, 0.5]; // Probabilities for each outcome
let multinomial = Multinomial::new(n, p).expect("Operation failed");Sourcepub fn pmf<D>(&self, x: &ArrayBase<D, Ix1>) -> f64
pub fn pmf<D>(&self, x: &ArrayBase<D, Ix1>) -> f64
Calculate the probability mass function (PMF) at a given point
§Arguments
x- The point at which to evaluate the PMF (must be a vector of non-negative integers that sum to n)
§Returns
- The value of the PMF at the given point
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
// Calculate PMF at x = [2, 3, 5]
let x = array![2.0, 3.0, 5.0];
let pmf_value = multinomial.pmf(&x);Sourcepub fn logpmf<D>(&self, x: &ArrayBase<D, Ix1>) -> f64
pub fn logpmf<D>(&self, x: &ArrayBase<D, Ix1>) -> f64
Calculate the log probability mass function (log PMF) at a given point
§Arguments
x- The point at which to evaluate the log PMF (must be a vector of non-negative integers that sum to n)
§Returns
- The value of the log PMF at the given point
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
// Calculate log PMF at x = [2, 3, 5]
let x = array![2.0, 3.0, 5.0];
let logpmf_value = multinomial.logpmf(&x);Sourcepub fn rvs(&self, size: usize) -> StatsResult<Vec<Array1<f64>>>
pub fn rvs(&self, size: usize) -> StatsResult<Vec<Array1<f64>>>
Generate random samples from the distribution
§Arguments
size- Number of samples to generate
§Returns
- Vector of random samples (each sample is a vector of counts)
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
// Generate 5 random samples
let samples = multinomial.rvs(5).expect("Operation failed");
assert_eq!(samples.len(), 5);
assert_eq!(samples[0].len(), 3);Sourcepub fn rvs_single(&self) -> StatsResult<Array1<f64>>
pub fn rvs_single(&self) -> StatsResult<Array1<f64>>
Generate a single random sample from the distribution
§Returns
- A random sample (a vector of counts)
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
// Generate a single random sample
let sample = multinomial.rvs_single().expect("Operation failed");
assert_eq!(sample.len(), 3);Sourcepub fn mean(&self) -> Array1<f64>
pub fn mean(&self) -> Array1<f64>
Calculate the mean of the distribution
§Returns
- Mean vector (n * p)
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
let mean = multinomial.mean();
// Mean should be [2.0, 3.0, 5.0]Sourcepub fn cov(&self) -> Array2<f64>
pub fn cov(&self) -> Array2<f64>
Calculate the covariance matrix of the distribution
§Returns
- Covariance matrix
§Examples
use scirs2_core::ndarray::array;
use scirs2_stats::distributions::multivariate::multinomial::Multinomial;
let n = 10;
let p = array![0.2, 0.3, 0.5];
let multinomial = Multinomial::new(n, p).expect("Operation failed");
let cov = multinomial.cov();Trait Implementations§
Source§impl Clone for Multinomial
impl Clone for Multinomial
Source§fn clone(&self) -> Multinomial
fn clone(&self) -> Multinomial
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Multinomial
impl Debug for Multinomial
Auto Trait Implementations§
impl Freeze for Multinomial
impl RefUnwindSafe for Multinomial
impl Send for Multinomial
impl Sync for Multinomial
impl Unpin for Multinomial
impl UnsafeUnpin for Multinomial
impl UnwindSafe for Multinomial
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.