Advertisement
karlakmkj

Cumulative Distribution Function (cdf)

Jul 26th, 2021
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import scipy.stats as stats
  2.  
  3. # Using Cumulative function as a faster way to calculate than pmf
  4.  
  5. # probability of observing 3 or fewer heads from 10 fair coin flips
  6. # P(3 or fewer heads)=P(0 to 3 heads)
  7. prob_1 = stats.binom.cdf(3, 10, 0.5)
  8. print(prob_1)
  9.  
  10. # compare to pmf code - add each from 0 to 3
  11. print(stats.binom.pmf(0, n=10, p=.5) + stats.binom.pmf(1, n=10, p=.5) + stats.binom.pmf(2, n=10, p=.5) + stats.binom.pmf(3, n=10, p=.5))
  12.  
  13. # probability of observing more than 5 heads from 10 fair coin flips (use 1 minus) - want from 6 to 10
  14. # P(more than 5 heads)= 1 − P(5 or fewer heads)
  15. prob_2 = 1- stats.binom.cdf(5, 10, 0.5)
  16. print(prob_2)
  17.  
  18. # probability of observing between 2 and 5 heads from 10 fair coin flips
  19. # P(2 to 5 Heads)=P(0 to 5 Heads)−P(0 to 1 Heads)
  20. prob_3 = stats.binom.cdf(5, 10, 0.5) - stats.binom.cdf(1, 10, 0.5)
  21. print(prob_3)
  22.  
  23. # compare to pmf code - add each from 2 to 5
  24. print(stats.binom.pmf(2, n=10, p=.5) + stats.binom.pmf(3, n=10, p=.5) + stats.binom.pmf(4, n=10, p=.5) + stats.binom.pmf(5, n=10, p=.5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement