elena1234

Binomial Distribution in Python ( cdf and pmf )

Jul 1st, 2022 (edited)
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.93 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as pl
  3. import seaborn as sn
  4. from scipy.stats import binom
  5.  
  6. # A manifactures has 12% defects rate in production. The buyer decides to test 20 random pieces and will accept the supplier if there # are 2 or less defectives. What is the probability of getting accepted?
  7.  
  8. production = np.random.binomial(20,0.12,1000)
  9. production
  10.  
  11. sn.countplot(x=production)
  12. sn.set_theme(style = 'darkgrid')
  13. pl.title("Binomial Probability")
  14. pl.xlabel("Number of Defectives")
  15. pl.show()
  16.  
  17.  
  18. exact_two = binom.pmf(2,20,0.12)
  19. exact_two
  20.  
  21. P_0_1_2 = binom.cdf(2,20,0.12)
  22. P_0_1_2
  23.  
  24. more_than_two = 1 - binom.cdf(2,20,0.12)
  25. more_than_two
  26.  
  27. binom.mean(20,0.12)
  28.  
  29. binom.std(20,0.12)
  30.  
  31. binom.var(20,0.12)
  32.  
  33. # Plotting for exact number
  34. x_ax = np.arange(0,21)
  35. y_ax = binom.pmf(x_ax,20,0.12)
  36. sns.barplot(x = x_ax, y = y_ax)
  37.  
  38. # Plotting with cumulative func
  39. y_ax2 = binom.cdf(x_ax,20,0.12)
  40. sns.barplot(x = x_ax, y= y_ax2)
Add Comment
Please, Sign In to add comment