To calculate the negative binomial distribution in MATLAB, you can use the nbinpdf
and nbinrnd
functions from the Statistics and Machine Learning Toolbox.
To calculate the probability mass function for a given set of parameters, use the nbinpdf
function:
main.m143 chars6 lines
This will return a vector prob_mass
with the probability mass function for the negative binomial distribution with parameters r
and p
evaluated at the values in k
.
To generate a random sample from the negative binomial distribution with given parameters, use the nbinrnd
function:
main.m180 chars7 lines
This will return a vector samples
with n_samples
random samples from the negative binomial distribution with parameters r
and p
. The rng
function is used to set the seed for the random number generator, for reproducibility.
gistlibby LogSnag