Categories &

Functions List

Class Definition: NegativeBinomialDistribution

statistics: NegativeBinomialDistribution

Negative binomial probability distribution object.

A NegativeBinomialDistribution object consists of parameters, a model description, and sample data for a negative binomial probability distribution.

The negative binomial distribution is a discrete probability distribution that models the number of failures in a sequence of independent and identically distributed Bernoulli trials before a specified (non-random) number of successes occurs. It is defined by the number of successes R and the probability of success P.

There are several ways to create a NegativeBinomialDistribution object.

  • Fit a distribution to data using the fitdist function.
  • Create a distribution with fixed parameter values using the makedist function.
  • Use the constructor NegativeBinomialDistribution (R, P) to create a negative binomial distribution with fixed parameter values R and P.
  • Use the static method NegativeBinomialDistribution.fit (x, freq, options) to fit a distribution to the data in x using the same input arguments as the nbinfit function.

It is highly recommended to use fitdist and makedist functions to create probability distribution objects, instead of the class constructor or the aforementioned static method.

Further information about the negative binomial distribution can be found at https://en.wikipedia.org/wiki/Negative_binomial_distribution

See also: fitdist, makedist, nbincdf, nbininv, nbinpdf, nbinrnd, nbinfit, nbinlike, nbinstat

Source Code: NegativeBinomialDistribution

Properties

A scalar value characterizing the number of successes in the negative binomial distribution. You can access the R property using dot name assignment.

Example: 1

 

 ## Create a Negative Binomial distribution with default parameters
 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");

 ## Query parameter 'R' (number of successes)
 pd.R

 ## Set parameter 'R'
 pd.R = 10

 ## Use this to initialize or modify the number of successes parameter in a
 ## Negative Binomial distribution. R must be a positive scalar, controlling
 ## the shape and often fixed based on the problem context, like successes in trials.

ans = 5.2135
pd =
  NegativeBinomialDistribution

  negative binomial distribution
        R =       10
        P = 0.508399

                    

Example: 2

 

 ## Create a Negative Binomial distribution object by calling its constructor
 pd = NegativeBinomialDistribution(10, 0.3)

 ## Query parameter 'R'
 pd.R

 ## This demonstrates direct construction with a specific number of successes,
 ## useful for modeling scenarios with a known fixed number of events, such as
 ## in reliability testing or count processes.

pd =
  NegativeBinomialDistribution

  negative binomial distribution
        R =  10
        P = 0.3

ans = 10
                    

A scalar value characterizing the probability of success in the negative binomial distribution. You can access the P property using dot name assignment.

Example: 1

 

 ## Create a Negative Binomial distribution with default parameters
 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");

 ## Query parameter 'P' (probability of success)
 pd.P

 ## Set parameter 'P'
 pd.P = 0.3

 ## Use this to initialize or modify the success probability in a Negative
 ## Binomial distribution. P must be a real scalar in (0,1], influencing the
 ## mean and variance of the distribution.

ans = 0.4926
pd =
  NegativeBinomialDistribution

  negative binomial distribution
        R = 4.87025
        P =     0.3

                    

Example: 2

 

 ## Create a Negative Binomial distribution object by calling its constructor
 pd = NegativeBinomialDistribution(5, 0.3)

 ## Query parameter 'P'
 pd.P

 ## This shows how to set the success probability directly via the constructor,
 ## ideal for modeling varying success rates in repeated trials.

pd =
  NegativeBinomialDistribution

  negative binomial distribution
        R =   5
        P = 0.3

ans = 0.3000
                    

A character vector specifying the name of the probability distribution object. This property is read-only.

A scalar integer value specifying the number of parameters characterizing the probability distribution. This property is read-only.

A 2×1 cell array of character vectors with each element containing the name of a distribution parameter. This property is read-only.

A 2×1 cell array of character vectors with each element containing a short description of a distribution parameter. This property is read-only.

A 2×1 numeric vector containing the values of the distribution parameters. This property is read-only. You can change the distribution parameters by assigning new values to the R and P properties.

A 2×2 numeric matrix containing the variance-covariance of the parameter estimates. Diagonal elements contain the variance of each estimated parameter, and non-diagonal elements contain the covariance between the parameter estimates. The covariance matrix is only meaningful when the distribution was fitted to data. If the distribution object was created with fixed parameters, or a parameter of a fitted distribution is modified, then all elements of the variance-covariance are zero. This property is read-only.

A 1×2 logical vector specifying which parameters are fixed and which are estimated. true values correspond to fixed parameters, false values correspond to parameter estimates. This property is read-only.

A 1×2 numeric vector specifying the truncation interval for the probability distribution. First element contains the lower boundary, second element contains the upper boundary. This property is read-only. You can only truncate a probability distribution with the truncate method.

A logical scalar value specifying whether a probability distribution is truncated or not. This property is read-only.

A scalar structure containing the following fields:

  • data: a numeric vector containing the data used for distribution fitting.
  • cens: a numeric vector of logical values indicating censoring information corresponding to the elements of the data used for distribution fitting. If no censoring vector was used for distribution fitting, then this field defaults to an empty array.
  • freq: a numeric vector of non-negative integer values containing the frequency information corresponding to the elements of the data used for distribution fitting. If no frequency vector was used for distribution fitting, then this field defaults to an empty array.

Methods

NegativeBinomialDistribution: p = cdf (pd, x)
NegativeBinomialDistribution: p = cdf (pd, x, "upper")

p = cdf (pd, x) computes the CDF of the probability distribution object, pd, evaluated at the values in x.

p = cdf (…, "upper") returns the complement of the CDF of the probability distribution object, pd, evaluated at the values in x.

Example: 1

 

 ## Plot various CDFs from the Negative Binomial distribution
 x = 0:20;
 data1 = nbinrnd(5, 0.3, 10000, 1);
 data2 = nbinrnd(5, 0.5, 10000, 1);
 data3 = nbinrnd(5, 0.7, 10000, 1);
 pd1 = fitdist (data1, "NegativeBinomial");
 pd2 = fitdist (data2, "NegativeBinomial");
 pd3 = fitdist (data3, "NegativeBinomial");
 p1 = cdf (pd1, x);
 p2 = cdf (pd2, x);
 p3 = cdf (pd3, x);
 plot (x, p1, "-b", x, p2, "-g", x, p3, "-r")
 grid on
 legend ({"R=5, P=0.3", "R=5, P=0.5", "R=5, P=0.7"}, "location", "southeast")
 title ("Negative Binomial CDF")
 xlabel ("values in x (number of failures)")
 ylabel ("Cumulative probability")

 ## Use this to compute and visualize the cumulative distribution function
 ## for different Negative Binomial distributions, showing how probability
 ## accumulates for count data, useful in modeling overdispersed counts.

                    
plotted figure

NegativeBinomialDistribution: x = icdf (pd, p)

x = icdf (pd, p) computes the quantile (the inverse of the CDF) of the probability distribution object, pd, evaluated at the values in p.

Example: 1

 

 ## Plot various iCDFs from the Negative Binomial distribution
 p = 0.001:0.001:0.999;
 data1 = nbinrnd(5, 0.3, 10000, 1);
 data2 = nbinrnd(5, 0.5, 10000, 1);
 data3 = nbinrnd(5, 0.7, 10000, 1);
 pd1 = fitdist (data1, "NegativeBinomial");
 pd2 = fitdist (data2, "NegativeBinomial");
 pd3 = fitdist (data3, "NegativeBinomial");
 x1 = icdf (pd1, p);
 x2 = icdf (pd2, p);
 x3 = icdf (pd3, p);
 plot (p, x1, "-b", p, x2, "-g", p, x3, "-r")
 grid on
 legend ({"R=5, P=0.3", "R=5, P=0.5", "R=5, P=0.7"}, "location", "northwest")
 title ("Negative Binomial iCDF")
 xlabel ("Probability")
 ylabel ("values in x (number of failures)")

 ## This demonstrates the inverse CDF (quantiles) for Negative Binomial
 ## distributions, useful for finding count values corresponding to given
 ## probabilities, such as in risk assessment or inventory thresholds.

                    
plotted figure

NegativeBinomialDistribution: r = iqr (pd)

r = iqr (pd) computes the interquartile range of the probability distribution object, pd.

Example: 1

 

 ## Compute the interquartile range for a Negative Binomial distribution
 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");
 iqr_value = iqr (pd)

 ## Use this to calculate the interquartile range, which measures the spread
 ## of the middle 50% of the distribution, helpful for understanding variability
 ## in count data.

iqr_value = 4
                    
NegativeBinomialDistribution: m = mean (pd)

m = mean (pd) computes the mean of the probability distribution object, pd.

Example: 1

 

 ## Compute the mean for different Negative Binomial distributions
 data1 = nbinrnd(5, 0.3, 10000, 1);
 data2 = nbinrnd(5, 0.5, 10000, 1);
 pd1 = fitdist (data1, "NegativeBinomial");
 pd2 = fitdist (data2, "NegativeBinomial");
 mean1 = mean (pd1)
 mean2 = mean (pd2)

 ## This shows how to compute the expected value for Negative Binomial
 ## distributions with different success probabilities, representing average
 ## number of failures.

mean1 = 11.684
mean2 = 5.0440
                    
NegativeBinomialDistribution: m = median (pd)

m = median (pd) computes the median of the probability distribution object, pd.

Example: 1

 

 ## Compute the median for different Negative Binomial distributions
 data1 = nbinrnd(5, 0.3, 10000, 1);
 data2 = nbinrnd(5, 0.5, 10000, 1);
 pd1 = fitdist (data1, "NegativeBinomial");
 pd2 = fitdist (data2, "NegativeBinomial");
 median1 = median (pd1)
 median2 = median (pd2)

 ## Use this to find the median value, which splits the distribution
 ## into two equal probability halves, robust to skewness in count data.

median1 = 11
median2 = 4
                    
NegativeBinomialDistribution: nlogL = negloglik (pd)

nlogL = negloglik (pd) computes the negative loglikelihood of the probability distribution object, pd.

Example: 1

 

 ## Compute the negative loglikelihood for a fitted Negative Binomial distribution
 rand ("seed", 21);
 data = nbinrnd(5, 0.5, 100, 1);
 pd_fitted = fitdist (data, "NegativeBinomial");
 params = [pd_fitted.R, pd_fitted.P];
 nlogL_nbinlike = nbinlike (params, data)

 ## This is useful for assessing the fit of a Negative Binomial distribution to
 ## data, with lower values indicating a better fit, often used in model comparison.

nlogL_nbinlike = 246.98
                    
NegativeBinomialDistribution: ci = paramci (pd)
NegativeBinomialDistribution: ci = paramci (pd, Name, Value)

ci = paramci (pd) computes the lower and upper boundaries of the 95% confidence interval for each parameter of the probability distribution object, pd.

ci = paramci (pd, Name, Value) computes the confidence intervals with additional options specified by Name-Value pair arguments listed below.

NameValue
"Alpha"A scalar value in the range (0,1) specifying the significance level for the confidence interval. The default value 0.05 corresponds to a 95% confidence interval.
"Parameter"A character vector or a cell array of character vectors specifying the parameter names for which to compute confidence intervals. By default, paramci computes confidence intervals for all distribution parameters.

paramci is meaningful only when pd is fitted to data, otherwise an empty array, [], is returned.

Example: 1

 

 ## Compute confidence intervals for parameters of a fitted Negative Binomial
 ## distribution
 rand ("seed", 21);
 data = nbinrnd(5, 0.5, 1000, 1);
 pd_fitted = fitdist (data, "NegativeBinomial");
 ci = paramci (pd_fitted, "Alpha", 0.05)

 ## Use this to obtain confidence intervals for the estimated parameters (R and P),
 ## providing a range of plausible values given the data.

ci =

   4.2483   0.4637
   6.2122   0.5595

                    
NegativeBinomialDistribution: y = pdf (pd, x)

y = pdf (pd, x) computes the PDF of the probability distribution object, pd, evaluated at the values in x.

Example: 1

 

 ## Plot various PDFs from the Negative Binomial distribution
 x = 0:20;
 data1 = nbinrnd(5, 0.3, 10000, 1);
 data2 = nbinrnd(5, 0.5, 10000, 1);
 data3 = nbinrnd(5, 0.7, 10000, 1);
 pd1 = fitdist (data1, "NegativeBinomial");
 pd2 = fitdist (data2, "NegativeBinomial");
 pd3 = fitdist (data3, "NegativeBinomial");
 y1 = pdf (pd1, x);
 y2 = pdf (pd2, x);
 y3 = pdf (pd3, x);
 plot (x, y1, "-b", x, y2, "-g", x, y3, "-r")
 grid on
 legend ({"R=5, P=0.3", "R=5, P=0.5", "R=5, P=0.7"}, "location", "northeast")
 title ("Negative Binomial PDF")
 xlabel ("values in x (number of failures)")
 ylabel ("Probability density")

 ## This visualizes the probability mass function for Negative Binomial
 ## distributions, showing the likelihood for discrete count values.

                    
plotted figure

NegativeBinomialDistribution: plot (pd)
NegativeBinomialDistribution: plot (pd, Name, Value)
NegativeBinomialDistribution: h = plot (…)

plot (pd) plots a probability density function (PDF) of the probability distribution object pd. If pd contains data, which have been fitted by fitdist, the PDF is superimposed over a histogram of the data.

plot (pd, Name, Value) specifies additional options with the Name-Value pair arguments listed below.

NameValue
"PlotType"A character vector specifying the plot type. "pdf" plots the probability density function (PDF). When pd is fit to data, the PDF is superimposed on a histogram of the data. "cdf" plots the cumulative density function (CDF). When pd is fit to data, the CDF is superimposed over an empirical CDF. "probability" plots a probability plot using a CDF of the data and a CDF of the fitted probability distribution. This option is available only when pd is fitted to data.
"Discrete"A logical scalar to specify whether to plot the PDF or CDF of a discrete distribution object as a line plot or a stem plot, by specifying false or true, respectively. By default, it is true for discrete distributions and false for continuous distributions. When pd is a continuous distribution object, option is ignored.
"Parent"An axes graphics object for plot. If not specified, the plot function plots into the current axes or creates a new axes object if one does not exist.

h = plot (…) returns a graphics handle to the plotted objects.

Example: 1

 

 ## Create a Negative Binomial distribution with fixed parameters R=5 and
 ## P=0.5 and plot its PDF.

 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");
 plot (pd)
 title ("Fixed Negative Binomial distribution with R=5 and P=0.5")

                    
plotted figure

Example: 2

 

 ## Generate a data set of 100 random samples from a Negative Binomial
 ## distribution with parameters R=5 and P=0.5. Fit a Negative Binomial
 ## distribution to this data and plot its CDF superimposed over an empirical
 ## CDF.

 rand ("seed", 21);
 data = nbinrnd(5, 0.5, 100, 1);
 pd_fitted = fitdist (data, "NegativeBinomial");
 plot (pd_fitted, "PlotType", "cdf")
 txt = "Fitted Negative Binomial distribution with R=%0.2f and P=%0.2f";
 title (sprintf (txt, pd_fitted.R, pd_fitted.P))
 legend ({"empirical CDF", "fitted CDF"}, "location", "southeast")

 ## Use this to visualize the fitted CDF compared to the empirical CDF of the
 ## data, useful for assessing model fit in count data.

                    
plotted figure

Example: 3

 

 ## Generate a data set of 200 random samples from a Negative Binomial
 ## distribution with parameters R=5 and P=0.5. Display a probability
 ## plot for the Negative Binomial distribution fit to the data.

 rand ("seed", 21);
 data = nbinrnd(5, 0.5, 200, 1);
 pd_fitted = fitdist (data, "NegativeBinomial");
 plot (pd_fitted, "PlotType", "probability")
 txt = strcat ("Probability plot of fitted Negative Binomial", ...
               " distribution with R=%0.2f and P=%0.2f");
 title (sprintf (txt, pd_fitted.R, pd_fitted.P))
 legend ({"empirical CDF", "fitted CDF"}, "location", "southeast")

 ## This creates a probability plot to compare the fitted distribution to the
 ## data, useful for checking if the Negative Binomial model is appropriate.

                    
plotted figure

NegativeBinomialDistribution: [nlogL, param] = proflik (pd, pnum)
NegativeBinomialDistribution: [nlogL, param] = proflik (pd, pnum, "Display", display)
NegativeBinomialDistribution: [nlogL, param] = proflik (pd, pnum, setparam)
NegativeBinomialDistribution: [nlogL, param] = proflik (pd, pnum, setparam, "Display", display)

[nlogL, param] = proflik (pd, pnum) returns a vector nlogL of negative loglikelihood values and a vector param of corresponding parameter values for the parameter in the position indicated by pnum. By default, proflik uses the lower and upper bounds of the 95% confidence interval and computes 100 equispaced values for the selected parameter. pd must be fitted to data.

[nlogL, param] = proflik (pd, pnum, "Display", "on") also plots the profile likelihood against the default range of the selected parameter.

[nlogL, param] = proflik (pd, pnum, setparam) defines a user-defined range of the selected parameter.

[nlogL, param] = proflik (pd, pnum, setparam, "Display", "on") also plots the profile likelihood against the user-defined range of the selected parameter.

For the negative binomial distribution, pnum = 1 selects the parameter R and pnum = 2 selects the parameter P.

When opted to display the profile likelihood plot, proflik also plots the baseline loglikelihood computed at the lower bound of the 95% confidence interval and estimated maximum likelihood. The latter might not be observable if it is outside of the used-defined range of parameter values.

Example: 1

 

 ## Compute and plot the profile likelihood for the success probability parameter
 ## of a fitted Negative Binomial distribution
 rand ("seed", 21);
 data = nbinrnd(5, 0.5, 1000, 1);
 pd_fitted = fitdist (data, "NegativeBinomial");
 [nlogL, param] = proflik (pd_fitted, 2, "Display", "on");

 ## Use this to analyze the profile likelihood of the success probability (P),
 ## helping to understand the uncertainty in parameter estimates.

                    
plotted figure

NegativeBinomialDistribution: r = random (pd)
NegativeBinomialDistribution: r = random (pd, rows)
NegativeBinomialDistribution: r = random (pd, rows, cols, …)
NegativeBinomialDistribution: r = random (pd, [sz])

r = random (pd) returns a random number from the distribution object pd.

When called with a single size argument, nbindrnd returns a square matrix with the dimension specified. When called with more than one scalar argument, the first two arguments are taken as the number of rows and columns and any further arguments specify additional matrix dimensions. The size may also be specified with a row vector of dimensions, sz.

Example: 1

 

 ## Generate random samples from a Negative Binomial distribution
 rand ("seed", 21);
 samples = random (NegativeBinomialDistribution(5, 0.5), 500, 1);
 hist (samples, 50)
 title ("Histogram of 500 random samples from NegativeBinomial(R=5, P=0.5)")
 xlabel ("values in x (number of failures)")
 ylabel ("Frequency")

 ## This generates random samples from a Negative Binomial distribution, useful
 ## for simulating count data like failures in trials or overdispersed Poisson processes.

                    
plotted figure

NegativeBinomialDistribution: s = std (pd)

s = std (pd) computes the standard deviation of the probability distribution object, pd.

Example: 1

 

 ## Compute the standard deviation for a Negative Binomial distribution
 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");
 std_value = std (pd)

 ## Use this to calculate the standard deviation, which measures the variability
 ## in the count values of the distribution.

std_value = 3.1134
                    
NegativeBinomialDistribution: t = truncate (pd, lower, upper)

t = truncate (pd, lower, upper) returns a probability distribution t, which is the probability distribution pd truncated to the specified interval with lower limit, lower, and upper limit, upper. If pd is fitted to data with fitdist, the returned probability distribution t is not fitted, does not contain any data or estimated values, and it is as it has been created with the makedist function, but it includes the truncation interval.

NegativeBinomialDistribution: v = var (pd)

v = var (pd) computes the variance of the probability distribution object, pd.

Example: 1

 

 ## Compute the variance for a Negative Binomial distribution
 data = nbinrnd(5, 0.5, 10000, 1);
 pd = fitdist (data, "NegativeBinomial");
 var_value = var (pd)

 ## Use this to calculate the variance, which quantifies the spread of the
 ## count values in the distribution.

var_value = 10.269
                    

Examples

 
 pd_fixed = makedist ("NegativeBinomial", "R", 5, "P", 0.5)
 rand ("seed", 2);
 data = random (pd_fixed, 5000, 1);
 pd_fitted = fitdist (data, "NegativeBinomial")
 plot (pd_fitted)
 msg = "Fitted Negative Binomial distribution with R = %0.2f and P = %0.2f";
 title (sprintf (msg, pd_fitted.R, pd_fitted.P))
 
pd_fixed =
  NegativeBinomialDistribution

  negative binomial distribution
        R =   5
        P = 0.5

pd_fitted =
  NegativeBinomialDistribution

  negative binomial distribution
        R =  5.01545   [4.60677, 5.42414]
        P = 0.499147   [0.478312, 0.519982]
plotted figure