English 中文(简体)
How do I compute a PMF and CDF for a binomial distribution in MATLAB?
原标题:

I need to calculate the probability mass function, and cumulative distribution function, of the binomial distribution. I would like to use MATLAB to do this (raw MATLAB, no toolboxes). I can calculate these myself, but was hoping to use a predefined function and can t find any. Is there something out there?

function x = homebrew_binomial_pmf(N,p)
    x = [1];
    for i = 1:N
       x = [0 x]*p + [x 0]*(1-p);
    end
最佳回答

You can use the function NCHOOSEK to compute the binomial coefficient. With that, you can create a function that computes the value of the probability mass function for a set of k values for a given N and p:

function pmf = binom_dist(N,p,k)
  nValues = numel(k);
  pmf = zeros(1,nValues);
  for i = 1:nValues
    pmf(i) = nchoosek(N,k(i))*p^k(i)*(1-p)^(N-k(i));
  end
end

To plot the probability mass function, you would do the following:

k = 0:40;
pmf = binom_dist(40,0.5,k);
plot(k,pmf, r. );

and the cumulative distribution function can be found from the probability mass function using CUMSUM:

cummDist = cumsum(pmf);
plot(k,cummDist, r. );

NOTE: When the binomial coefficient returned from NCHOOSEK is large you can end up losing precision. A very nice alternative is to use the submission Variable Precision Integer Arithmetic from John D Errico on the MathWorks File Exchange. By converting your numbers to his vpi type, you can avoid the precision loss.

问题回答

octave provides a good collection of distribution pdf, cdf, quantile; they have to be translated from octave, but this is relatively trivial (convert endif to end, convert != to ~=, etc;) see e.g. octave binocdf for the binomial cdf function.

looks like for the CDF of the binomial distribution, my best bet is the incomplete beta function betainc.

For PDF

x=1:15
p=.45

c=binopdf(x,15,p)

plot(x,c)

Similarly CDF

D=binocdf(x,15,p) 

plot(x,D)




相关问题
Generating N numbers that sum to 1

Given an array of size n I want to generate random probabilities for each index such that Sigma(a[0]..a[n-1])=1 One possible result might be: 0 1 2 3 4 0.15 0.2 0.18 0.22 0.25 ...

为什么我随意 du弄。 粉碎吗?

10个帐篷的清单有10个。 可能的命令或变更。 为什么是任意的。 只剩下5 000个工件后,便可复制件?

Estimating/forecasting download completion time

We ve all poked fun at the X minutes remaining dialog which seems to be too simplistic, but how can we improve it? Effectively, the input is the set of download speeds up to the current time, and ...

probability of deck of cards [closed]

for(i=1;i<=n;i++) { pick a random index j between 1 and n inclusive; swap card[i] and card[j]; } for the above code am trying to find the probability of original card[k] winding up in slot ...

Nth Combination

Is there a direct way of getting the Nth combination of an ordered set of all combinations of nCr? Example: I have four elements: [6, 4, 2, 1]. All the possible combinations by taking three at a time ...

热门标签