Skip to content

Latest commit

 

History

History
380 lines (298 loc) · 17 KB

set_theory-introduction.md

File metadata and controls

380 lines (298 loc) · 17 KB

Introduction to set theory

Erika Duan 3/5/23

Summary

This tutorial provides a framework for describing element belonging to a set, where a set is a collection of distinct elements. Working with sets is a prerequisite for understanding probability theory.

What is a set?

A set is a collection of distinct objects or elements (e).

We reference a set by listing all its elements. For example, S = \{cat, \;mouse, \;dog \} or S = \{1, 2, 3, 4, 5, 6 \} to describe all the possible distinct outcomes when we roll a dice and observe its upper face.

Set notation:

  • An example of a finite set is A = \{1, 3, 5 \}. A finite set is countable by definition.
  • An example of a countably infinite set is B = \{1, 2, \cdots \}. The set consists of integers which extend from 1 to infinity.
  • An example of an uncountably infinite set is C = (1, 0). The set is uncountable as it comprises all real numbers between 0 and 1.
  • The null set (\varnothing) does not contain any elements and is denoted by \varnothing = \{\}.
  • The universal set is denoted by S and is the set of all elements under consideration for a specific scenario.
  • The complement of set A is the set of elements which are in S but not in A. This is expressed in mathematical notation as \bar{A} = \{e : e\in S, e\notin A\}.
  • If all elements of set A are also in set B, then A is a subset of B and this is denoted as A \subseteq B. There are only two possibilities when this is true, that the number of elements in set A are fewer than those in set B or that set A and B contain the same elements.
  • If A \subseteq B and B \subseteq A, then A = B.

Working with two or more sets

Venn diagrams are useful for conceptually visualising set properties. However, we still want to use rigorous mathematical proofs when asserting set properties.

Consider set A and set B such that a subset of elements in A are also found in B:

  • The intersection of A and B contains the set of elements found in both A and B. This is denoted as A \cap B = \{e: e\in A, e \in B \}.
  • The union of A and B contains the set of elements found in A or B. This is denoted as A \cup B = \{e: e \in A \; or \; e\in B \}.
  • The set operation A - B contains the set of elements found in A that are not found in B. This is equivalent to A \cap \bar B and denoted as A \cap \bar B = \{e: e \in A, e \notin B\}.

Consider set A and set B such that no elements in A are found in B:

  • Sets A and B are mutually exclusive or disjoint if A \cap B = \varnothing.
  • Sets A, B and C are therefore disjoint if A \cap B = A \cap C = B \cap C = \varnothing.

R

In contrast to Python, R does not have a set data type. However, set operations union(), intersect(x, y), setdiff(x, y) and setequal(x, y) exist in base R.

# Perform set operations in R --------------------------------------------------
a = c(1, 2, 3)
b = c(1, 3, 6)

union(a, b)
#> [1] 1 2 3 6  

intersect(a, b)
#> [1] 1 3  

# setdiff(a, b) is equivalent to a - b 
setdiff(a, b)
#> [1] 2

setequal(a, b)
#> [1] FALSE  

Python

In Python, a set is an unordered data type comprising a collection of distinct data objects. Sets can be created directly using {1, 2, 3} or set([1, 2, 3]).

# Create a set in Python -------------------------------------------------------
list_a = [1, 2, 2, 3]
set_a = set(list_a)

print(set_a)
#> {1, 2, 3}

type(set_a)
#> <class 'set'>  
# Perform set operations in Python ---------------------------------------------
set_b = {1, 3, 6}
type(set_b)
#> <class 'set'>  

set_a.union(set_b)
#> {1, 2, 3, 6}  

set_a.union(set_b) == set_a | set_b
#> True  

set_a.intersection(set_b)
#> {1, 3}  

set_a.intersection(set_b) == set_a & set_b
#> True  

# a.difference(b) is equivalent to a - b  

set_a.difference(set_b)
#> {2}   

set_a - set_b
#> {2}   

# Python also has an ^ operator which returns all elements in A or B but not AB 

set_a.symmetric_difference(set_b)
#> {2, 6}  

set_a.symmetric_difference(set_b) == set_a ^ set_b
#> True  
# Identify disjoint sets in Python ---------------------------------------------
set_c = {8, 9}
set_a.isdisjoint(set_c)
#> True
# Identify subsets in Python ---------------------------------------------------
set_d = {1, 2, 3, 4}  
set_a.issubset(set_d)
#> True 

Julia

In Julia, inequality statements are also outputted as Boolean values i.e. true or false.

# Create a set in Julia --------------------------------------------------------
a = Set([1, 2, 3]) 
b = Set([1, 3, 6])

typeof(a)
#> Set{Int64}  
print(a)
#> Set([2, 3, 1])  
# Perform set operations in Julia ----------------------------------------------
print(union(a, b))
#> Set([6, 2, 3, 1])  

print(intersect(a, b))
#> Set([3, 1])   

print(setdiff(a, b))
#> Set([2])  

# symdiff(a, b) is equivalent to a.symmetric_difference(b) in Python
print(symdiff(a, b))
#> Set([6, 2])

Set algebra

Commutative laws

The set order has no impact on the union or intersection of two sets. This is intuitive as changing the set order does not change contents of each individual set.

  • A \cup B = B \cup A
  • A \cap B = B \cap A

Associative laws

The set order also has no impact when only either an intersection or union is performed on more than two sets. This is similarly intuitive to the commutative laws, as introducing extra sets does not change contents of each individual set.

  • A \cup B \cup C = (A \cup B) \cup C = A \cup (B \cup C)
  • A \cap B \cup C = (A \cap B) \cap C = A \cup (B \cap C)

Distributive laws

The sequence of first performing the set operation inside the parenthesis matters when both an intersection and union are applied to multiple sets. This is similar to how the sequence of first performing the operation inside the parenthesis matters in elementary algebra. For example, 2+(3 \times 4) \neq (2 + 3) \times 4.

  • A \cup (B \cap C) \neq (A \cup B) \cap C
  • A \cup (B \cap C) = (A \cup B) \cap (A \cup C)

  • A \cap (B \cup C) \neq (A \cap B) \cup C)
  • A \cap (B \cup C) = (A \cap B) \cup (A \cap C)

De Morgan’s laws

De Morgan’s law is less intuitive and can be visualised by Venn diagrams or (more preferably) proven mathematically.

  • \overline{A\cup B} = \bar A \cap \bar B

Proof 1

Suppose that e \in \overline{A\cup B}
Then e \notin A \cup B
\implies e \notin A, \; e \notin B
\implies e \in \bar A, \; e \in \bar B
\implies e \in \bar A \cap \bar B
Therefore, \overline{A\cup B} \subseteq \bar A \cap \bar B

Similarly, suppose that e \in \bar A \cap \bar B
Then e \in \bar A, \; e \in \bar B
\implies e \notin A, \; e \notin B
\implies e \notin A \cup B
\implies e \in \overline{A\cup B}
Therefore, \bar A \cap \bar B \subseteq \overline{A\cup B}

Therefore, \overline{A\cup B} = \bar A \cap \bar B

  • \overline{A\cap B} = {\bar A} \cup {\bar B}

Proof 2

From proof 1, \overline{A\cup B} = \bar A \cap \bar B

If we substitute A with \bar A and B with \bar B
\implies \overline{\bar A \cup \bar B} = \bar {\bar A} \cap \bar {\bar B}
\implies \overline{\bar A \cup \bar B} = A \cap B
\implies {\bar A \cup \bar B} = \overline {A \cap B}

Therefore, \overline{A\cap B} = {\bar A} \cup {\bar B}

Resources

  • Wikipedia entry on set algebra.
  • A guide on Python set operations from Real Python.
  • A guide on Julia set operations from GeeksforGeeks.