Python Tutorial : Probability mass and distribution functions

Learn the probability mass function and cumulative distribution function for the binomial distribution. The tutorial covers the binomial formula with coin-flip examples (2 heads out of 10 flips is about 4%, 5 heads is about 25%) and shows how to use SciPy's binom.pmf, binom.cdf, and binom.sf functions to calculate probabilities.
Chapters
After conducting many random experiments, you will notice that some outcomes are more likely than others. This is called a probability distribution. There are two important functions that are useful for probability the probability mass function and the cumulative distribution function. A discrete random variable has a finite number of possible outcomes. The probability mass function allows you to calculate the probability of getting a particular outcome. For a discrete random variable. The binomial probability mass function allows you to calculate the probability of getting k heads from n coin flips with p probability of getting hits. The formula multiplies the number of different ways you can get k successes and out of n coin flips by the probability of success raised to the number of successes k by the probability of failure 1p raised to the number of failures n minus k. It's okay if you don't understand the formula right now. With practice, your intuition about this will grow. If we plot the probability mass function of getting k heads out of ten fair coin flips, you can see that five is the most likely outcome. With the SCIPY STATS library, we can use the Binum PMF function to calculate this probability. If you use binom PMF with parameters k2, n10 and p 0.5, you get the probability of getting two heads from 10 flips of a fair coin, I.e. 4%. The probability of getting five heads from 10 coin flips is almost 25%. The probability of getting 50 heads out of 100 flips of a bias coin with 30% probability of getting heads is extremely small, not even a 1% chance. If instead you calculate the probability of getting 65 heads from 100 flips of a biased coin with 70% probability of getting heads, you see that it's almost 5%. As n gets larger, the probability of getting k heads becomes smaller for the same p. If you instead want to calculate the probability of getting k or fewer heads from nth rows, you use the binomial probability distribution function, which adds the probability of getting zero heads out of n flips, getting heads once out of n flips, and getting all the way up to k heads out of n flips. The binomial probability distribution function allows us to calculate the cumulative probability of getting k heads or fewer from n coin flips with p probability of getting heads. In Python, we use the binom CDF function with parameters k, n and p. Adding the probabilities from the MATHS function, we get the cumulative distribution function cdf. This is a way of getting a range of probabilities rather than the probability of a single event. With the SCIPY stats library we can use the binom CDF function to get such a probability in using the same parameters. If you use the binom CDF with parameters k5, n10 and p 0.5, you get the probability of getting heads 5 times or fewer out of 10 flips, which is 62%. The probability of getting heads 50 times or fewer out of 100 flips of a biased coin with 30% probability of getting hits is near 1, 100%. It's almost guaranteed the probability of getting heads more than 59 times from 100 flips of a Bytecoin with P 70% is 99%. Again, it's almost certain what if we want the probability of getting heads more than k times? This is called the complement and we get it by subtracting the CDF from 1. Alternatively, we can calculate the complement using the function binom SF with the same parameters. SF stands for survival function which allows you to get tail probabilities or the complement. In this case we've had some fun calculating probabilities. Now let's practice.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







