(theoretical) Negative Binomial Distribution using Matlab

798 Views Asked by At

I was trying to solve some exercises on Matlab in order to improve my skills and I stumbled upon this question:

For the (theoretical) Negative Binomial distribution with parameters r = 5, p = 0.4, use Matlab to find the mean, μ, and variance, $\sigma^2$.

I have found the answers to be μ = 7.500 and $\sigma^2$ = 18.7500. Now, the second part of the question is a bit tough for me:

For each sample size n = 1, 5, 10:

 1) Generate 1,000 random samples from this distribution (as a 1000 x n or an n x 1000 matrix)
 2) Find the 1,000 sample means (Hint: store them for further calculation)
 3) Compute the mean and variance of the 1,000 sample means.

I have tried to use the function RND = nbinrnd(R,P,m,n), where R = 5, P = 0.4, m = 1000, and n = 1. If that's correct, I assume that would be for the sample size 1? How can I adjust this in order to calculate for size 5 and 10? I have tried to find an answer on the web, but I've had no luck so far.

1

There are 1 best solutions below

2
On

From the MATLAB documentation:

RND = nbinrnd(R,P) is a matrix of random numbers chosen from a negative binomial distribution with corresponding number of successes, R and probability of success in a single trial, P. R and P can be vectors, matrices, or multidimensional arrays that have the same size, which is also the size of RND. A scalar input for R or P is expanded to a constant array with the same dimensions as the other input.

RND = nbinrnd(R,P,m,n,...) or RND = nbinrnd(R,P,[m,n,...]) generates an m-by-n-by-... array. The R, P parameters can each be scalars or arrays of the same size as R.

The simplest motivation for the negative binomial is the case of successive random trials, each having a constant probability P of success. The number of extra trials you must perform in order to observe a given number R of successes has a negative binomial distribution. However, consistent with a more general interpretation of the negative binomial, nbinrnd allows R to be any positive value, including nonintegers.

Short version: try it and find out.

Long version, $m$ and $n$ give you an $m$ by $n$ matrix. So if $m = 5$ and $n = 1$, then you get a 5 by 1 matrix of negative binomial random variables.

If $m = 5$ and $n = 10$, you get a 5 by 10 matrix.

If you just need to generate $N$ samples, set $m = 1$ and $n = N$, or vice versa.