Stirling's Formula and a hypothesis test

We use Stirling's formula to approximate the probability of drawing $n$ red and $n$ blue balls from an urn with $2n$ red and $2n$ blue balls, assuming we draw $2n$ balls without replacement.

The percent error is quite small (less than 2%) even for modest values of $n$, like $n=10$.

In [1]:
n = 100

truevalue = convert(Float64,binomial(big(2n),n)^2 / binomial(big(4n),2n))
approximation = 2/(sqrt(2π*n))

truevalue, approximation, 100*(approximation/truevalue-1)
Out[1]:
(0.07963899366836436,0.07978845608028654,0.18767491280036186)
In [2]:
function b(n,p,k)
    # Computes (n choose k) * p^k * (1-p)^(n-k)
    result = 1.0
    for j = 0:k-1
        result *= (n-j)/((j+1))*p
    end
    result *= (1-p)^(n-k)
    return result
end

PowerCurve(n,p,m) = sum([b(n,p,k) for k=m:n])
Out[2]:
PowerCurve (generic function with 1 method)
In [3]:
PowerCurve(100,0.6,68)
Out[3]:
0.061503909592474384
In [4]:
using Gadfly
In [5]:
plot(x=[50:100],y=[PowerCurve(100,0.6,k) for k=50:100],Geom.line)
Out[5]:
x -10 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100 102 104 106 108 110 112 114 116 118 120 122 124 126 128 130 132 134 136 138 140 142 144 146 148 150 0 50 100 150 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 -1.5 -1.0 -0.5 0.0 0.5 1.0 1.5 2.0 2.5 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 1.55 1.60 1.65 1.70 1.75 1.80 1.85 1.90 1.95 2.00 -1 0 1 2 -1.0 -0.9 -0.8 -0.7 -0.6 -0.5 -0.4 -0.3 -0.2 -0.1 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2.0 y
In [ ]: