logo4 Evolution is progress—                          
progress is creativity.        
vline

Binomial distribution

view blog view wiki view wiki view wiki

Though this article tackles binomial distribution, a type of discrete probability distribution, it is rather focused on practical questions how to use matplotlib to construct such distributions.

Numpy offers four parameters that effect graphical appearance:

parametermeaningusage
pprobabilityessential, to construct a probability distribution
nsample sizeessential, to construct a probability distribution
locoriginoptional, the origin of the probability distribution default = 0
sizevector lengthoptional, size of the one dimensional array of random variables default=0 => no array but a single random variable is returned
def set_figure(fig):    
   import numpy as np
   from scipy.stats import binom
   ax=fig.add_subplot(3,1,1)
   bx=fig.add_subplot(3,1,2)
   cx=fig.add_subplot(3,1,3)

   fig.subplots_adjust(hspace=.5)

   p = [0.2, 0.3, 0.5, 0.7]
   c = ['r','b','y','g']
   shift = [0, 0.1, 0.2, 0.3]
   n = 30
    
   x = np.arange(0,n+1,1)
   for i in range(4):
       ax.vlines(x+shift[i], 0, binom(n,p[i]).pmf(x), lw=2, color=c[i])

   ax.set_ylabel("Probability")
   ax.set_title("Binomial probability mass funtion")
   
   for i in range(4):
       bx.vlines(x+shift[i], 0, binom(n,p[i]).cdf(x), lw=2, color=c[i])
   
   bx.set_title("Binomial cumulative probability density funtion")
   
   size = 100
   R = []
   for i in range(4):
       R.append(binom.rvs(n, p[i], size=size))

   string = ''
   j, k = 0, 0
   line = 1
   
   for i in range(4):
       for r in R[i]:
           j = j + 1
           string = string + str(r) + ','
           if k >= 49 or j == size:
               line = line - 0.06
               cx.text(0.05,line,string,color=c[i],size='5')
               string = ''
               k = 0
               
           else:
               k = k + 1
   cx.set_title("Binomial type "+str(size) +" random numbers" )
   cx.set_xticklabels([])
   cx.set_yticklabels([])
   cx.set_xticks([])
   cx.set_yticks([])
Binomial Distribution

The probability mass function can be written:

f(x) = \Bigl\{_\textrm{elsewhere}^{{{m^x e^{-m} } \over {x! }} \; x= 1,2,3, \cdots}

The cumulative probability function can be described by the equation:

F(x) = \sum_{k=0}^{x}{{n \choose k} p^k (1-p)^{n-k}}


Tags: Statistics


Categories: Mathematics Software

 
   

(c) Mato Nagel, Weißwasser 2004-2013, Disclaimer