From Chapter 1 of Information Theory, Inference and Learning Algorithms
A coin has probability f
of coming up heads, tossed N
times. Probability of getting r
heads? Mean and variance of 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
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
In [23]:
qplot [0..10] (\x -> bin 10 x 0.5)
In [26]:
qplot [0,0.05..1] (\x -> bin 10 4 x)
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)