Probability theory is all around. A common example is in the game of poker or related card games, where players try to calculate the probability of winning a round given the cards they have in their hands. Also, in a business context, probabilities play an important role. Operating in a volatile economy, companies need to take uncertainty into account and this is exactly where probability theory plays a role.
As mentioned in the lesson before, a good understanding of probability starts with understanding of sets and set operations. That's exactly what you'll learn in this lab!
You will be able to:
- Use Python to perform set operations
- Use Python to demonstrate the inclusion/exclusion principle
Let's start with a pretty conceptual example. Let's consider the following sets:
-
$\Omega$ = positive integers between [1, 12] -
$A$ = even numbers between [1, 10] $B = {3,8,11,12}$ $C = {2,3,6,8,9,11}$
a. Illustrate all the sets in a Venn Diagram like the one below. The rectangular shape represents the universal set.
Do this work by hand (writing in the values of each set), then you will check your answers using Python code later!
For example, if the question was just asking for the values of None
with {3, 8, 11, 12}
typed out.
$ A \cap B$
ans1 = None
ans1
$ A \cup C$
ans2 = None
ans2
ans3 = None
ans3
The absolute complement of B
ans4 = None
ans4
ans5 = None
ans5
ans6 = None
ans6
ans7 = None
ans7
ans8 = None
ans8
ans9 = None
ans9
c. For the remainder of this exercise, let's create sets A, B and C and universal set U in Python and test out the results you came up with. Sets are easy to create in Python. For a guide to the syntax, follow some of the documentation here
# Create set A
A = None
'Type A: {}, A: {}'.format(type(A), A) # "Type A: <class 'set'>, A: {2, 4, 6, 8, 10}"
# Create set B
B = None
'Type B: {}, B: {}'.format(type(B), B) # "Type B: <class 'set'>, B: {8, 11, 3, 12}"
# Create set C
C = None
'Type C: {}, C: {}'.format(type(C), C) # "Type C: <class 'set'>, C: {2, 3, 6, 8, 9, 11}"
# Create universal set U
U = None
'Type U: {}, U: {}'.format(type(U), U) # "Type U: <class 'set'>, U: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}"
Now, verify your answers in section 1 by using the correct methods in Python. For example, if the question was just asking for the values of None
with B
.
To provide a little bit of help, you can find a table with common operations on sets below.
Method | Equivalent | Result |
---|---|---|
s.issubset(t) | s <= t | test whether every element in s is in t |
s.issuperset(t) | s >= t | test whether every element in t is in s |
s.union(t) | s |
new set with elements from both s and t |
s.intersection(t) | s & t | new set with elements common to s and t |
s.difference(t) | s - t | new set with elements in s but not in t |
s.symmetric_difference(t) | s ^ t | new set with elements in either s or t but not both |
Every cell should display True
if your original answer matches the answer you calculated with Python. If it displays False
, that means either your original answer or your Python code is incorrect.
A_inters_B = None
A_inters_B == ans1
A_union_C = None
A_union_C == ans2
A_comp = None
A_comp == ans3
B_comp = None
B_comp == ans4
A_union_B_comp = None
A_union_B_comp == ans5
B_inters_C_comp = None
B_inters_C_comp == ans6
compl_of_B = None
compl_of_B == ans7
C_compl_B_compl_A = None
C_compl_B_compl_A == ans8
C_inters_A_union_C_min_B = None
C_inters_A_union_C_min_B == ans9
Use A, B and C from exercise one to verify the inclusion exclusion principle in Python. You can use the sets A, B and C as used in the previous exercise.
Recall from the previous lesson that:
Combining these main commands:
Method | Equivalent | Result |
---|---|---|
a.union(b) | A |
new set with elements from both a and b |
a.intersection(b) | A & B | new set with elements common to a and b |
along with the len(x)
function to get to the cardinality of a given x ("|x|").
What you'll do is translate the left hand side of the equation for the inclusion principle in the object left_hand_eq
, and the right hand side in the object right_hand_eq
and see if the results are the same.
left_hand_eq = None
print(left_hand_eq) # 9 elements in the set
right_hand_eq = None
print(right_hand_eq) # 9 elements in the set
None # Use a comparison operator to compare `left_hand_eq` and `right_hand_eq`. Needs to say "True".
Mary is preparing for a road trip from her hometown, Boston, to Chicago. She has quite a few pets, yet luckily, so do her friends. They try to make sure that they take care of each other's pets while someone is away on a trip. A month ago, each respective person's pet collection was given by the following three sets:
Nina = set(["Cat","Dog","Rabbit","Donkey","Parrot", "Goldfish"])
Mary = set(["Dog","Chinchilla","Horse", "Chicken"])
Eve = set(["Rabbit", "Turtle", "Goldfish"])
In this exercise, you'll be able to use the following operations:
Operation | Equivalent | Result |
---|---|---|
s.update(t) | return set s with elements added from t | |
s.intersection_update(t) | s &= t | return set s keeping only elements also found in t |
s.difference_update(t) | s -= t | return set s after removing elements found in t |
s.symmetric_difference_update(t) | s ^= t | return set s with elements from s or t but not both |
s.add(x) | add element x to set s | |
s.remove(x) | remove x from set s | |
s.discard(x) | removes x from set s if present | |
s.pop() | remove and return an arbitrary element from s | |
s.clear() | remove all elements from set s |
Sadly, Eve's turtle passed away last week. Let's update her pet list accordingly.
None
Eve # should be {'Rabbit', 'Goldfish'}
This time around, Nina promised to take care of Mary's pets while she's away. But she also wants to make sure her pets are well taken care of. As Nina is already spending a considerable amount of time taking care of her own pets, adding a few more won't make that much of a difference. Nina does want to update her list while Mary is away.
None
Nina # {'Chicken', 'Horse', 'Chinchilla', 'Parrot', 'Rabbit', 'Donkey', 'Dog', 'Cat', 'Goldfish'}
Mary, on the other hand, wants to clear her list altogether while away:
None
Mary # set()
Look at how many species Nina is taking care of right now.
n_species_Nina = None
n_species_Nina # 9
Taking care of this many pets is weighing heavily on Nina. She remembered Eve had a smaller collection of pets lately, and that's why she asks Eve to take care of the common species. This way, the extra pets are not a huge effort on Eve's behalf. Let's update Nina's pet collection.
None
Nina # 7
Taking care of 7 species is something Nina feels comfortable doing!
Mary dropped off her pets at Nina's house and finally made her way to the highway. Awesome, her vacation has begun! She's approaching an exit. At the end of this particular highway exit, cars can either turn left (L), go straight (S) or turn right (R). It's pretty busy and there are two cars driving close to her. What you'll do now is create several sets. You won't be using Python here, it's sufficient to write the sets down on paper. A good notion of sets and subsets will help you calculate probabilities in the next lab!
Note: each set of action is what all three cars are doing at any given time
a. Create a set
b. Create a set
c. Create a set
d. Create a set
e. Write down the interpretation and give all possible outcomes for the sets denoted by:
- I.
$D'$ - II.
$C \cap D$ , - III.
$C \cup D$ .
Use set operations to determine which European countries are not in the European Union. Use the Country
column. You just might have to clean the data first with pandas.
Note that this data is from 2018, so EU membership may have changed.
import pandas as pd
# Load Europe and EU
europe = pd.read_excel('Europe_and_EU.xlsx', sheet_name = 'Europe')
eu = pd.read_excel('Europe_and_EU.xlsx', sheet_name = 'EU')
# Your code here to remove any whitespace from names
Preview data:
europe.head(3)
eu.head(3)
Your code comes here:
# Check to confirm that the EU countries are a subset of countries in Europe
# Find the set of countries that are in Europe but not the EU
In this lab, you practiced your knowledge on sets, such as common set operations, the use of Venn Diagrams, the inclusion exclusion principle, and how to use sets in Python!