A summary of "Probability and Statistics in Data Science using Python", offered from UCSD DSE210x
{specify elements}
Sets | Notation | |
---|---|---|
Integers | {$\dots$, -2, -1, 0, 1, 2, $\dots$} | $\mathbb{Z}$ |
Naturals | {0, 1, 2, $\dots$ } | $\mathbb{N}$ |
Positives | {1, 2, 3, $\dots$} | $\mathbb{P}$ |
Rationals | {interger ratio m / n, n $\neq 0$} | $\mathbb{Q}$ |
Reals | { whole number } | $\mathbb{R}$ |
Note: $\mathbb{Z}$ comes from german word
Zahl
, meaning numbers
Usually, Sets are expressed with Upper case (A, B, etc), and Elements are expressed with Lower case (a, b, etc), as a convention.
$$ 0 \in \{0, 1\} \qquad 1 \in \{0, 1\} \qquad \pi \in \mathbb{R} $$
$$ \{0, 1\} \ni 0 \qquad \{0, 1\} \ni 1 \qquad \mathbb{R} \in \pi $$
If $x$ is not in $A$, then $x$ is not a member, or does not belong to $A$, denoted $x$ $\notin$ $A$.
Equivalently, $A$ does not contain $x$, $A$ $\not\owns$ $x$.
If you want to consider:
Note: $\forall$ means 'all', or 'every'
Universal set: all possible elements ($\Omega$, $\forall x, x \in \Omega$)
$\Omega$ lets us consider only relevant elements. $\Omega$ can be $\mathbb{Z}$ (the integer) or "prime number"
$\Omega$ depends on application (temperature, text, etc...)
$\emptyset$ is only one in whole case, this is the set with no elements.
{...} or set({...})
set() or set({})
Note: In python,
{}
is not a empty set, it is dictionary.
in
not in
# Empty set
S = set()
not S
True
# Set
T = {1, 2}
not T
False
len(S)
0
len(T)
2
Specify a set within a universe, or any other set, $$ \{x \in A \vert \dots \} $$ means elements $x$ in $A$ such that. Sometimes it expresses like this, $$ \{x \in A : \dots \} $$
For example,
$$ \mathbb{N} = \{x \in \mathbb{Z} \vert x \geq 0 \} $$$$ \mathbb{P} = \{x \in \mathbb{N} \vert x \gt 0 \} $$It usually express the solution to equations,
$$ \{ x \in \mathbb{R} \vert x^2 \geq 0\} = \mathbb{R} $$$$ \{ x \in \mathbb{R} : x^2 = 1 \} = \{-1, 1\} $$$$ \{ x \in \mathbb{R} \vert x^2 = 0 \} = \{0\} $$$$ \{ x \in \mathbb{R} \vert x^2 = -1 \} = \emptyset $$$$ \{ x \in \mathbb{C} \vert x^2 = -1 \} = \{i, -i\} $$Note: a single-element set is called singleton
It is a set of integers from $m$ to $n$, inclusively.
$$ \{3, \dots, 5\} = \{i \in \mathbb{Z} \vert 3 \leq i \leq 5 \} = \{3, 4, 5\} $$$$ \{3, \dots, 4\} = \{i \in \mathbb{Z} \vert 3 \leq i \leq 4 \} = \{3, 4\} $$ $$ \{3, \dots, 3\} = \{i \in \mathbb{Z} \vert 3 \leq i \leq 3 \} = \{3\} $$ $$ \{3, \dots, 2\} = \{i \in \mathbb{Z} \vert 3 \leq i \leq 2 \} = \emptyset $$
For convention, $[n] = \{1, \dots, n\}$
In $m, n \in \mathbb{Z}$, if $n = c \dot m$ for some $c \in \mathbb{Z}$, we say that n is a multiple of m, or $m$ divides $n$, and write $m \vert n$
If no such $c$ exists, $m$ does not divide $n$, or $n$ is not a multiple of $m$ denoted $m \not\vert n$.
For example,
$$\text{There is no } c \in \mathbb{Z} \quad \text{such that} \quad 4 = c \cdot 3 \quad \rightarrow 3 \not\vert 4 $$$$ 0 \not\vert n \quad \text{for any } n \neq 0 $$- Example
$$ \begin{aligned} {}_2\mathbb{Z} &= \{\dots, -4, -2, 0, 2, 4, \dots \} \overset{\underset{\mathrm{def}}{}}{=} \mathbb{E} \quad \rightarrow \text{even number} \\
{}_1\mathbb{Z} &= \{\dots , -2, -1, 0, 1, 2, \dots \} = \mathbb{Z} \\
{}_0\mathbb{Z} &= \{0\} \end{aligned} $$- Example
$$ \begin{aligned} {}_3[13] &= \{i \in \{1, \dots, 13\} : 3 \vert i \} = \{3, 6, 9, 12\} \\
{}_7[13] &= \{7\} \\ {}_1[13] &= [13] \\ {}_{14}[13] &= {}_0[13] = \emptyset \end{aligned} $$
$\{0,\dots, n-1\} \quad \rightarrow$ range(n)
$\{m, \dots, n-1\} \quad \rightarrow$ range(m, n)
$\{m, m+d, m+2d, \dots \} \leq n - 1 \quad \rightarrow$ range(m, n, d)
set(range(3))
{0, 1, 2}
set(range(2, 5))
{2, 3, 4}
set(range(2, 12, 3))
{2, 5, 8, 11}
!pip install matplotlib_venn
import matplotlib.pyplot as plt
import matplotlib_venn as venn
S = {1, 2, 3}
T = {0, 2, -1, 5}
venn.venn2([S, T], set_labels=('S', 'T'));
U = { 10, 8, 0, 2, -1}
venn.venn3([S, T, U], set_labels=('S', 'T', 'U'));
$\emptyset$ disjoint from any set
Non-empty $\Omega$ intersects every set
A set intersects itself if and only if it is not-empty.
Several sets
It generalizes $\leq$
If every element in $A$ is also in $B$, then $A$ is a subset of $B$, denoted $A \subseteq B$
$\mathbb{P} \subseteq \mathbb{N} \subseteq \mathbb{Z} \subseteq \mathbb{Q} \subseteq \mathbb{R}$
$\emptyset \subseteq A \subseteq A \subseteq \Omega$
(transitivity) $A \subseteq B$ and $B \subseteq C \rightarrow A \subseteq C$
Note: $\subseteq$ is called transitive
It generalizes $\gt$
If $A \subseteq B$ and $A \neq B$, $A$ is a strict subset of $B$, denoted $A \subset B$ and $B$ is a strict superset of $A$, denoted $B \supset A$.
S1 = {0, 1}
S2 = set({0, 1})
S3 = {1, 0, 1}
T = {0, 2}
print(S1 == T)
print(S1 == S2)
print(S1 == S3)
False True True
print(S1 != S2)
print(S1 != T)
False True
S1.isdisjoint(T)
False
S1.isdisjoint({2})
True
zero = {0}
zplus = {0, 1}
zminus = {0, -1}
zminus <= zplus
False
zero.issubset(zminus)
True
zplus < zminus
False
zero < zminus
True
zplus >= zminus
False
zplus.issuperset(zero)
True
zminus > zminus
False
zplus > zero
True
$A \times A = A^2$
$\mathbb{R}^2 = \{(x, y): x, y \in \mathbb{R}\} \quad \rightarrow$ Cartesian Plane
$A, B \subseteq \mathbb{R} \quad \rightarrow A \times B \subseteq \mathbb{R}^2$
$A \times \emptyset = \emptyset \times A = \emptyset$
$A \times (B \cup C) = A \times B \cup A \times C$
$A \times (B \cap C) = A \times B \cap A \times C$
$A \times (B - C) = A \times B - A \times C$
Use product function in itertools library
from itertools import product
Faces = set({'J', 'Q', 'K'})
Suits = {'\u2666\uFE0F', '\u2660\uFE0F'}
for i in product(Faces, Suits):
print(i)
('K', '♠️') ('K', '♦️') ('Q', '♠️') ('Q', '♦️') ('J', '♠️') ('J', '♦️')
Can a set belong to (be an element of) itself? $ \rightarrow S \in S$
Typically, sets do not belong to themselves $\quad \{0\} \not \in \{0\} , \emptyset \not \in \emptyset $
But some sets do belong to themselves! (infinite recursion)
Some sets $\in$ themselves, others don't ($\{0\}$)
Define a set that cannot exist
For example,
If
If R existed, then both $R \in R$ and $R \not \in R$ would hold
R defined but cannot exist!!
ex) The set that contains only the empty set $\emptyset$ is not empty
De Morgan's first law states the following for any two sets $A$ and $B$ $$(A\cup B)^c = A^c\cap B^c$$
In the following two exercises we calculate $(A\cup B)^c$ in two different ways. Both functions must take $A$, $B$ and the universal set $U$ as their inputs.
Write the function complement_of_union that first determines $A\cup B$ and then evaluates the complement of this set. Output the tuple: $\begin{pmatrix}A\cup B,\, (A\cup B)^c\end{pmatrix}$.
Code
A = {1, 2, 3}
B = {3, -6, 2, 0}
U = {-10, -9, -8, -7, -6, 0, 1, 2, 3, 4}
complement_of_union(A, B, U)
Output
({-6, 0, 1, 2, 3}, {-10, -9, -8, -7, 4})
def complement_of_union(A, B, U):
# inputs: A, B and U are of type 'set'
# output: a tuple of the type (set, set)
union = A.union(B)
complement_union = U.difference(union)
return (union, complement_union)
# Check Function
A = {1, 2, 3, 4, 5}
B = {0, 2, -6, 5, 8, 9}
U = A|B|{-3, 7, 10, -4}
assert( complement_of_union(A, B, U) == ({-6, 0, 1, 2, 3, 4, 5, 8, 9}, {-4, -3, 7, 10}) )
Write the function intersection_of_complements that first determines $A^c$ and $B^c$ and then evaluates the intersection of their complements. Output the tuple: $\begin{pmatrix}A^c, \, A^c\cap B^c\end{pmatrix}$
Code
A = {1, 2, 3}
B = {3, -6, 2, 0}
U = {-10, -9, -8, -7, -6, 0, 1, 2, 3, 4}
intersection_of_complements(A, B, U)
Output
({-10, -9, -8, -7, -6, 0, 4}, {-10, -9, -8, -7, 4})
def intersection_of_complements(A, B, U):
# inputs: A, B and U are of type 'set'
# output: a tuple of the form (set, set)
complement_a = U.difference(A)
complement_b = U.difference(B)
complement_intersect = complement_a.intersection(complement_b)
return (complement_a, complement_intersect)
# Check Function
A = {1, 2, 3, 4, 5}
B = {0, 2, -6, 5, 8, 9}
U = A|B|{-3, 7, 10, -4}
assert( intersection_of_complements(A, B, U) == ({-6, -4, -3, 0, 7, 8, 9, 10}, {-4, -3, 7, 10}) )
This problem illustrates a property of cartesian products of unions of two or more sets. For four sets $A$, $B$, $S$ and $T$, the following holds:
$$(A\cup B)\times(S\cup T) = (A\times S)\cup(A\times T)\cup(B\times S)\cup(B\times T)$$Write the following functions to determine $(A\cup B)\times(S\cup T)$ in two different ways.
Write function product_of_unions that first determines $(A\cup B)$ and $(S\cup T)$ and then evaluates the cartesian products of these unions. Output the tuple $\begin{pmatrix}(A\cup B),\, (A\cup B)\times(S\cup T)\end{pmatrix}$.
Code
A = {1, 2}
B = {1, 3}
S = {-1, 0}
T = {0, 10}
product_of_unions(A, B, S, T)
Output
({1, 2, 3},
{(1, -1),
(1, 0),
(1, 10),
(2, -1),
(2, 0),
(2, 10),
(3, -1),
(3, 0),
(3, 10)})
def product_of_unions(A, B, S, T):
# inputs: A, B, S and T are sets
# output: a tuple of the type (set, set)
union_a_b = A.union(B)
union_s_t = S.union(T)
product_a_b_s_t = set()
for i in product(union_a_b, union_s_t):
product_a_b_s_t.add(i)
return (union_a_b, product_a_b_s_t)
# Check Function
A = {5}
B = {5, 6}
S = {-1, 0, 1}
T = {1, 2}
assert( product_of_unions(A, B, S, T) == \
({5, 6}, {(5, -1), (5, 0), (5, 1), (5, 2), (6, -1), (6, 0), (6, 1), (6, 2)}) )
Write a function union_of_products that first determines $(A\times S)$ and the other three cartesian products that appear on the right hand side of the identity above, then evaluates the union of these cartesian products. Output the tuple $\begin{pmatrix}(A\times S),\, (A\times S)\cup(A\times T)\cup(B\times S)\cup(B\times T)\end{pmatrix}$.
Code
A = {1, 2}
B = {1, 3}
S = {-1, 0}
T = {0, 10}
union_of_products(A, B, S, T)
Output
({(1, -1), (1, 0), (2, -1), (2, 0)},
{(1, -1),
(1, 0),
(1, 10),
(2, -1),
(2, 0),
(2, 10),
(3, -1),
(3, 0),
(3, 10)})
def union_of_products(A, B, S, T):
# inputs: A, B, S and T are sets
# output: a tuple of the type (set, set)
product_a_s = set(x for x in product(A, S))
product_a_t = set(x for x in product(A, T))
product_b_s = set(x for x in product(B, S))
product_b_t = set(x for x in product(B, T))
union_all = product_a_s.union(product_a_t).union(product_b_s).union(product_b_t)
return (product_a_s, union_all)
# Check Function
A = {5}
B = {5, 6}
S = {-1, 0, 1}
T = {1, 2}
assert( union_of_products(A, B, S, T) == \
({(5, -1), (5, 0), (5, 1)}, \
{(5, -1), (5, 0), (5, 1), (5, 2), (6, -1), (6, 0), (6, 1), (6, 2)}) \
)