FAQ: Introduction to Data Science - Probability


#1

This community-built FAQ covers the “Probability” exercise from the lesson “Introduction to Data Science”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Code Foundations

FAQs on the exercise Probability

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

I have zero knowledge of scientific probability so it may be just me, but on a purely common-sense level the result of this script feels completely counter-intuitive:
If there are 365 possible unique birth dates and there are 90 people in the room (i.e. 1/4 of the number of unique birthdates), how can the probability of two of them having the same birthday be “nearly 100%”?

Moreover, why does the script return an error if I enter a number of people higher than 120?


#3

This is a well known problem called the birthday paradox.

When there are 23 people in a room, the probability of two having the same birthday is roughly 0.5. That number goes up quickly as more and more people enter the room. With 90 people in the room the chance of no two having the same birthday is very small, about 0.00002. So the probability that at least two people have the same birthday is 1 - 0.00002 which is still very close to 1.

>>> 90 * 89 / 2
4005.0
>>> (364/365) ** 4005
1.690905614940199e-05
>>> 1 - (364/365) ** 4005
0.9999830909438506
>>> 

#4

I was wondering, in the code for this exercise the “simulate” function comes earlier in the code than the “calculate_probability” and the “find_duplicates” functions even though “simulate” uses these functions in its body. Does python care about the order of the function definitions in its code?


#5

Hi @laggedinertia,

See the following:

def func_one(m):
    return func_two(m) * 2

def func_two(n):
    return n * 7

print(func_one(4))

It is fine for func_one to include a call to func_two, even though func_two was not yet defined, so long as func_two gets defined before that calling statement actually executes.

Neither of the above functions executes until this statement calls func_one:

print(func_one(4))

Therefore, by the time the statement in func_one that calls func_two gets executed, func_two has been defined.

By contrast, this raises a NameError because the call to func_two attempts to execute before func_two has been defined:

def func_one(m):
    return func_two(m) * 2

print(func_one(4))

def func_two(n):
    return n * 7