<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Conditional Probability Activity & Exercise
Below is some code to create some fake data on how much stuff people purchase given their age range.
It generates 100,000 random "people" and randomly assigns them as being in their 20's, 30's, 40's, 50's, 60's, or 70's.
It then assigns a lower probability for young people to buy stuff.
In the end, we have two Python dictionaries
Step1:  Let's play with conditional probability.
First let's compute P(E|F), where E is "purchase" and F is "you're in your 30's". The probability of someone in their 30's buying something is just the percentage of how many 30-year-olds bought something
Step2:  P(F) is just the probability of being 30 in this data set
Step3:  And P(E) is the overall probability of buying something, regardless of your age
Step4:  If E and F were independent, then we would expect P(E | F) to be about the same as P(E). But they're not; PE is 0.45, and P(E|F) is 0.3. So, that tells us that E and F are dependent (which we know they are in this example.)
What is P(E)P(F)?
Step5:  P(E,F) is different from P(E|F). P(E,F) would be the probability of both being in your 30's and buying something, out of the total population - not just the population of people in their 30's
Step6:  P(E,F) = P(E)P(F), and they are pretty close in this example. But because E and F are actually dependent on each other, and the randomness of the data we're working with, it's not quite the same.
We can also check that P(E|F) = P(E,F)/P(F) and sure enough, it is
<ASSISTANT_TASK:>
Python Code:
from numpy import random
random.seed(0)
totals = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}
purchases = {20:0, 30:0, 40:0, 50:0, 60:0, 70:0}
totalPurchases = 0
for _ in range(100000):
    ageDecade = random.choice([20, 30, 40, 50, 60, 70])
    purchaseProbability = float(ageDecade) / 100.0
    totals[ageDecade] += 1
    if (random.random() < purchaseProbability):
        totalPurchases += 1
        purchases[ageDecade] += 1
totals
purchases
totalPurchases
Explanation: Conditional Probability Activity & Exercise
Below is some code to create some fake data on how much stuff people purchase given their age range.
It generates 100,000 random "people" and randomly assigns them as being in their 20's, 30's, 40's, 50's, 60's, or 70's.
It then assigns a lower probability for young people to buy stuff.
In the end, we have two Python dictionaries:
"totals" contains the total number of people in each age group.
"purchases" contains the total number of things purchased by people in each age group.
The grand total of purchases is in totalPurchases, and we know the total number of people is 100,000.
Let's run it and have a look:
End of explanation
PEF = float(purchases[30]) / float(totals[30])
print('P(purchase | 30s): ' + str(PEF)
Explanation: Let's play with conditional probability.
First let's compute P(E|F), where E is "purchase" and F is "you're in your 30's". The probability of someone in their 30's buying something is just the percentage of how many 30-year-olds bought something:
End of explanation
PF = float(totals[30]) / 100000.0
print("P(30's): " +  str(PF))
Explanation: P(F) is just the probability of being 30 in this data set:
End of explanation
PE = float(totalPurchases) / 100000.0
print("P(Purchase):" + str(PE))
Explanation: And P(E) is the overall probability of buying something, regardless of your age:
End of explanation
print("P(30's)P(Purchase)" + str(PE * PF))
Explanation: If E and F were independent, then we would expect P(E | F) to be about the same as P(E). But they're not; PE is 0.45, and P(E|F) is 0.3. So, that tells us that E and F are dependent (which we know they are in this example.)
What is P(E)P(F)?
End of explanation
print("P(30's, Purchase)" + str(float(purchases[30]) / 100000.0))
Explanation: P(E,F) is different from P(E|F). P(E,F) would be the probability of both being in your 30's and buying something, out of the total population - not just the population of people in their 30's:
End of explanation
print((purchases[30] / 100000.0) / PF)
Explanation: P(E,F) = P(E)P(F), and they are pretty close in this example. But because E and F are actually dependent on each other, and the randomness of the data we're working with, it's not quite the same.
We can also check that P(E|F) = P(E,F)/P(F) and sure enough, it is:
End of explanation
<END_TASK>