Calculate conditional probability in python
Show
Use python to calculate the conditional probability of a student getting an A in math given they missed 10 or more classes.This article has 2 parts: Part 1: Theory and formula behind conditional probabilityFor once, wikipedia has an approachable definition,
Translation: given B is true, what is the probability that A is also true. It’s easier to understand something with concrete examples. Below are a few random examples of conditional probabilities we could calculate. Examples:
Formula:The formula for conditional probability is The parts:
But why do we divide Because we want to exclude the probability of non-B cases. We’re scoping our probability to that
falling within Dividing by Part 2: Example with pythonWe’re going to calculate the probability a student gets an A (80%+) in math, given they miss 10 or more classes. Download the dataset from kaggle and inspect the data. import pandas as pd And check the number of records. len(df) We’re only concerned with the columns, Let’s create a couple new boolean columns based on these columns to make our lives easier. Add a boolean column called df['grade_A'] = np.where(df['G3']*5 >= 80, 1, 0) Make another boolean column called df['high_absenses'] = np.where(df['absences'] >= 10, 1, 0) Add one more column to make building a pivot table easier. df['count'] = 1 And drop all columns we don’t care about. df = df[['grade_A','high_absenses','count']] Nice. Now we’ll create a pivot table from this. pd.pivot_table( We now have all the data we need to do our calculation. Let’s start by calculating each individual part in the formula. In our case: Calculations of parts: And per the formula, P(A|B) = 0.012658227848101266/ 0.21012658227848102= 0.06 There we have it. The probability of getting at least an 80% final grade, given missing 10 or more classes is 6%. ConclusionWhile the learning from our specific example is clear - go to class if you want good grades, conditional probability can be applied to more serious circumstances. For example, the probability a person has a particular disease, given test results. An understanding is also essential before diving into more complicated probability estimations using Bayes’ theorem. How do you calculate conditional probability?How Do You Calculate Conditional Probability? Conditional probability is calculated by multiplying the probability of the preceding event by the probability of the succeeding or conditional event.
Can Python calculate probability?In fact, difficult probability problems can be solved in Python without needing to know a single math equation. Such an equation-free approach to probability requires a baseline understanding of what mathematicians call a sample space.
How do you find conditional probability with three variables?We can rearrange the formula for conditional probability to get the so-called product rule:. P(A,B) = p(A|B) p(B). We can extend this for three variables:. P(A,B,C) = P(A| B,C) P(B,C) = P(A|B,C) P(B|C) P(C). and in general to n variables:. P(A1, A2, ..., An) = P(A1| A2, ..., An) P(A2| A3, ..., An) P(An-1|An) P(An). |