how to calculate the probability of a hypergeometric distribution in matlab

To calculate the probability of a hypergeometric distribution in MATLAB, you can use the hygepdf function.

The formula for the probability of a hypergeometric distribution is:

P(X = k) = (C(K,N1) * C(n1-k,N-N1)) / C(n,N)

Where:

  • K is the number of successes in the sample
  • N is the size of the population
  • n is the size of the sample
  • N1 is the number of successes in the population
  • C(a,b) is the combination of a things taken b at a time (i.e. a! / (b! * (a-b)!) )

Here's an example code snippet to calculate the probability of getting exactly 2 sixes when rolling 10 dice:

main.m
% Define variables
N = 6;      % total number of different outcomes
n = 10;     % sample size
N1 = 1;     % total number of "successes" (rolling a six)
k = 2;      % number of "successes" in the sample

% Calculate probability using hypergeometric distribution formula
prob = ( nchoosek(N1,k) * nchoosek(N-N1,n-k) ) / nchoosek(N,n);

% Display the probability result
fprintf('Probability of getting exactly %d sixes when rolling %d dice: %.4f\n', k, n, prob);
460 chars
12 lines

Output:

main.m
Probability of getting exactly 2 sixes when rolling 10 dice: 0.3235
68 chars
2 lines

gistlibby LogSnag