Monday, February 19, 2018

From binomial distribution to Gaussian and Poisson distributions. Plus 2D random walk

Mainly for referencing.

1.Derivation of Gaussian distribution from binomial distribution

P(k,n)=\frac{n!}{k!(n-k)!}p^kq^{n-k}as n \rightarrow\infty and p remains finite.
  • Taking log:ln(p)=ln(n!)-ln(k!)-ln((n-k)!)+kln(p)+(n-k)ln(q)
  • Using Stirling approximation :
ln(n!) \approx nln(n)+n+\frac{1}{2}ln(2\pi n)
ln(p) \approx [nln(n)+n+\frac{1}{2}ln(2\pi n)]+[kln(k)+k+\frac{1}{2}ln(2\pi k)]+[(n-k)ln((n-k))+n+\frac{1}{2}ln(2\pi (n-k)]+kln(p)+(n-k)ln(q)
(1) For terms of order ln(n), we evaluate them at mean value for k=\langle k\rangle=np:\frac{1}{2}ln(2\pi n)-\frac{1}{2}ln(2\pi k)-\frac{1}{2}ln(2\pi (n-k))\implies \frac{1}{2}ln(2\pi n)-\frac{1}{2}ln(2\pi np)-\frac{1}{2}ln(2\pi np)\implies \frac{1}{2}ln(2\pi npq)which is ln(\frac{1}{\sqrt{2\pi \sigma^2} }) , where \sigma^2=npq.
(2) For terms kln(p) and (n-k)ln(n-k) again we plug in k=\langle k\rangle=np:
kln(p) +(n-k)ln(n-k) \implies np ln(p)+nqln(q)
(3) For terms nln(n)kln(p) and (n-k)ln(n-k) we can Taylor expand to 2nd order and plug in results from (2) to obtain:nln(n)+ kln(p)+(n-k)ln(n-k) \implies -kln(p)-nqln(q)+kln(q)-npln(q)-\frac{1}{2npq}(k-nq)^2which is -\frac{1}{2\sigma^2} (k-np)^2.
Thus from (1) and (3):ln(p)=ln(\frac{1}{\sqrt{2\pi \sigma^2} })+-\frac{1}{2\sigma^2} (k-np)^2p(k)=\frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{1}{2}(\frac{k-\langle k\rangle}{\sigma})^2}

2.Derivation of Poisson distribution from binomial distribution


 P(k,n)=\frac{n!}{k!(n-k)!}p^kq^{n-k}, with the assumption that as n\rightarrow \inftyp>>1 and np \rightarrow 0.
  • Let’s define \mu= np. Thus, p=\frac{\mu}{n}q=(1-\frac{\mu}{n}).
We can rewrite the binomial distribution:p(k,n)=\frac{n!}{k!(n-k)!}(\frac{\mu}{n})^k(1-\frac{\mu}{n})^{n-k}\implies \frac{n!}{(n-k)!k!} \frac{\mu^k}{n^k} (1-\frac{\mu}{n})^n (1-\frac{\mu}{n})^{-k}
  • Now we take the limit of the distribution as n\rightarrow \infty by looking at each term involving n:
    (0) Term \frac{\mu^k}{k!} is irrelevant as we take limit of n.
    (1)\lim_{n\to \infty} \frac{n!}{(n-k)!n^k}Since \frac{n!}{(n-k)!n^k}=\frac{n(n-1) \dots (n-k)(n-k-1)\dots1 }{(n-k)(n-k-1)\dots 1} \frac{1}{n^k}=\frac{n(n-1) \dots (n-k+1)}{n^k} \implies \frac{n}{n}\frac{n-1}{n} \dots\frac{n-k+1}{n}
    If we take limit as n \to \infty, each of the n terms above goes to 1. Thus \lim_{n\to \infty} \frac{n!}{(n-k)!n^k}=1.
    (2)\lim_{n\to \infty} (1-\frac{\mu}{n})^nUsing definition of e:e=\lim_{n\to \infty} (1+\frac{1}{x})^xWe letx=-\frac{n}{\mu}The limit of interest becomes\implies \lim_{x\to \infty} (1+\frac{1}{x})^{-\mu x}=e^{-\mu}
    (3)\lim_{n\to \infty} (1-\frac{\mu}{n})^{-k} =1^{-k}=1
Thusp(k,n)=\frac{\mu!e^{-\mu}}{k!}which is the Poisson distribution.

3. 2D random walk stimulation in Python:


import numpy as np
import random 
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
import math 

def two_d_randomwalk(x,y):
    theta = np.random.random()*2*math.pi #scaling the angle 2pi
    x+=math.cos(theta)
    y+=math.sin(theta)
    return (x,y)

#One random walk
x,y=0,0
steps = 10000
a=np.zeros((steps,2))
r=[0]

for i in range(steps):
    #position coordinates
    x,y=two_d_randomwalk(x,y)
    a[i:]=x,y 
    #final distance squared
    distance_sq=x**2+y**2
    r.append(distance_sq)

    
lc=LineCollection(zip(a[:-1],a[1:]),array=z, cmap=plt.cm.hsv)
fig, ax=plt.subplots(1,1)
ax.add_collection(lc)
ax.margins(0.1)
plt.show()


fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlabel('Number of steps')
plt.ylabel('Distance squared')
ax.plot(r)
plt.show()