Binomial distribution

From Chapter 1 of Information Theory, Inference and Learning Algorithms

Question

A coin has probability f of coming up heads, tossed N times. Probability of getting r heads? Mean and variance of r?

$$P(r|f,N) = {N \choose r} f^r(1-f)^{N-r}$$

Factorial

$${N \choose r} = \frac{N!}{(N-r)!r!}$$

Stirling's approximation for the factorial function $$ x! \simeq x^x e^{-x} $$


In [10]:
-- a quick graphing function

import Graphics.Rendering.Chart.Renderable
import Graphics.Rendering.Chart.Simple
-- for example plot [1,1.1..10] sin
qplot range function = toRenderable $ plotLayout $ plot range function

Some initial functions from the definitions


In [20]:
-- factorial
fact n = product[1..n]

-- stirling's approximation of factorial
stirling n1 = (n**n) * e ** (-n)
    where
        e = 2.71828
        n = n1+1

-- number of permutations given n tosses, and seeking r heads
choices n r = fact(n) / (fact(n-r) * fact(r))

--  the probbility of r heads, given n coins tossed, and f probability of each toss being a head
bin n r f = nr * (f**r) * (1-f)**(n-r)
    where 
        nr = choices n r

Varying across number of coins thrown


In [23]:
qplot [0..10] (\x -> bin 10 x 0.5)


Varying probability of one coin being head


In [26]:
qplot [0,0.05..1] (\x -> bin 10 4 x)


What if I want to calculate the probability of getting five heads or more?


In [42]:
-- probability of getting r or more heads from n tosses, given that the probability of each toss being head is f
cumbin n r f = sum([bin n x f | x <- [r..n] ])

qplot [0..10] (\x -> cumbin 10 x 0.5)