Rana_093

Binomial

Sep 20th, 2020 (edited)
838
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import matplotlib.pyplot as plt
  2. n = 10
  3. p = 0.5
  4.  
  5. values = list(range(n+1))
  6. #print(values)
  7.  
  8. def fact(n):
  9.     mul = 1
  10.     for i in range(1,n+1):
  11.         mul*=i
  12.     return mul
  13.  
  14. def nCr(n,r):
  15.     return (int) (fact(n) ) / ( fact(r) * fact(n-r) )
  16.  
  17. #print(nCr(5,2))
  18.  
  19. def PMF(r, n):
  20.     cur = nCr(n,r) * pow(p,r) * pow(1-p,n-r)
  21.     return cur
  22.  
  23. dist = [PMF(r,n) for r in values]
  24. width = 0.8
  25. plt.bar(values,dist,width)
  26. plt.xlabel('Binomial')
  27. plt.ylabel('Frequency')
  28. plt.show()
  29.  
  30. cdfs = []
  31.  
  32. sum = 0.0
  33.  
  34. for i in range(0,n+1):
  35.     sum+=PMF(i,n)
  36.     cdfs.append(sum)
  37.  
  38. plt.scatter(values,cdfs)
  39. plt.plot(values,cdfs)
  40. plt.xlabel('Binomial')
  41. plt.ylabel('cdf val')
  42. plt.show()
  43.  
  44.  
RAW Paste Data