Advertisement
karlakmkj

Calculate Probabilities (pmf)

Jul 26th, 2021 (edited)
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import scipy.stats as stats # import library
  2.  
  3. # full formula: stats.binom.pmf(x, n, p)
  4. # binom stands for binomial distribution, pmf stands for probability mass function
  5.  
  6. # value of interest
  7. x = 3
  8.  
  9. # sample size/no. of trials
  10. n = 10
  11.  
  12. # calculate probability
  13. prob_1 = stats.binom.pmf(x, n, 0.5) # last parameter represents probability of success (p)
  14. print(prob_1)
  15.  
  16. # To calculate probability over a range
  17. # for observing between 4 to 6 heads from 10 coin flips - add them together
  18. prob_1 = stats.binom.pmf(4, 10, 0.5) + stats.binom.pmf(5, 10, 0.5) + stats.binom.pmf(6, 10, 0.5)
  19. print(prob_1)
  20.  
  21. # for observing more than 2 heads from 10 coin flips (ie. from 3 to 10)
  22. # use the 1 minus the sum of some values as a shortcut for unwanted 0 to 2 heads
  23. prob_2 = 1 - (stats.binom.pmf(0, 10, 0.5) + stats.binom.pmf(1, 10, 0.5) + stats.binom.pmf(2, 10, 0.5))
  24. print(prob_2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement