Coin throwing simulator

In [192]:
%matplotlib inline 
import numpy as np
import matplotlib.pyplot as plt

# n_samples of throwing a coin 4 times. counting head = 1 and tail = 0

n_sample = 10000

# initializing
ht_sample=[]

for i in range(0, n_sample):

    # initializing
    ht_sum = 0
    # realisation of the 5 tosses
    for j in range(0, 4):
        ht_sum = ht_sum + np.random.random_integers(0,1)
    ht_sample.append(ht_sum)
  
# plotting the histogram
plt.hist(ht_sample,5,[-0.5,4.5],normed=1)

# making the plot more meaningfull
plt.xlabel("heads per four tosses")
plt.ylabel("occupancy")
plt.xlim([-0.5,4.5])
plt.ylim([0,1])

label = r"#samples: "+ str(n_sample)
plt.gca().figure.text(0.7,0.8,label)

plt.show()


Example Poisson distribution

In [196]:
from scipy import stats
# mean mu
mu = 5
x=np.arange(0,10)

#plt.plot(x, stats.poisson.pmf(x,mu))
plt.plot(x, stats.poisson.pmf(x,mu),'-ro')

plt.xlabel("r")
plt.ylabel("P(r;$\lambda$)")
label = r"$\lambda =$"+ str(mu)
plt.gca().figure.text(0.8,0.8,label)


Out[196]:
<matplotlib.text.Text at 0x10dfe5ba8>

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: