Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm watching this video at 21:20 min. The guy gave an example for tossing a coin four times. At 23:49 min, he said we can plot the probability of tossing a coin (e.i. $n$) for a large number in MATLAB by using this formula $$ px(k) = \dbinom{n}{k} p^{k}(1-p)^{n-k} \qquad, k=0,1,...,n $$

Ok now I want to plot his example but I'm facing problems. For his example, $n=4 , k = 0,1,2,3,4$ (what does $k=0$ mean here?). This is my code in Matlab

clear all
outcome = 2; % outcome # of a coin (Head & Tail)
n = 4; % tossing #
k = 0:n; % # of getting head or tail
p = 1/n^outcome; % the total probability of all outcomes (1/16)
i = 0; % iterator for the loop

for i = 0:size(k,2)
    Px = nchoosek(n,k(i)) * p^k(i) * (1-p)^(n-k(i));

plot(k, Px)

What's wrong with my approach?

share|cite|improve this question
In the formula you gave (for Bernoulli trials), the parameter $p$ is the probability of a single outcome. For tossing a coin, this should be 1/2. In your code, it should be 1/outcome. – Hugh Denoncourt Feb 15 '14 at 15:54
@HughDenoncourt, I'm still getting error. How can I apply the formula in case $k =0$? – CroCo Feb 15 '14 at 16:02
up vote 1 down vote accepted

This is the binomial distribution (you can look it up just about anywhere). $k$ is the number of successes (heads in this case), and it is certainly possible to get no successes in four attempts. The $p$ is the probability of success for each trial (it's assumed that each coin-flip has the same probability of success as all the others) and should be 1/2.

share|cite|improve this answer
I can visualize the problem by drawing a tree of tossing the coin 4 times and one of the case is getting (TTTT), so in this case $k=0$ (I'm assuming $k$ to be the number of getting Head) but How can I apply the formula in case $k=0$? – CroCo Feb 15 '14 at 16:15
@CroCo plug it in? You get $\binom40\cdot p^0\cdot (1-p)^{4-0} = (1-p)^4$. In matlab, nchoosek(4,0)*p^0*(1-p)^(n-0). They're all well-defined. – tabstop Feb 15 '14 at 16:17
I found the problem. it was the first step of my loop. It should starts from 1 not 0. – CroCo Feb 15 '14 at 17:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.