how to calculate the probability of a negative binomial distribution in matlab

To calculate the probability of a negative binomial distribution in Matlab, you can use the nbinpdf function. The nbinpdf function calculates the probability density function (PDF) for the negative binomial distribution with parameters r (number of failures) and p (probability of success on each trial) at the values in x.

Here's an example code block that generates a random sample from a negative binomial distribution with r = 5 and p = 0.3, calculates the PDF of this distribution at the value x = 10, and displays the result:

main.m
% Set the parameters of the negative binomial distribution
r = 5;
p = 0.3;

% Generate a random sample from the negative binomial distribution
sample = nbinrnd(r,p,1,1000);

% Calculate the PDF of the negative binomial distribution at x = 10
x = 10;
pdf = nbinpdf(x,r,p);

% Display the result
fprintf('The probability of %d successes before %d failures in a negative binomial distribution with r = %d and p = %.2f is %.4f.\n',x,r,p,pdf);
439 chars
14 lines

This code block generates a random sample of 1000 values from a negative binomial distribution with r = 5 and p = 0.3. It then calculates the PDF of this distribution at the value x = 10. Finally, it displays the result as a formatted string.

Note that the nbinpdf function requires the Statistics and Machine Learning Toolbox. If you do not have this toolbox installed, you can use the nbincdf function instead to calculate the cumulative distribution function (CDF) of the negative binomial distribution.

gistlibby LogSnag