Logic and Set Theory Fundamentals
Logic and Set Theory Fundamentals
In previous chapters, we introduced algebra, functions, and geometry, all dealing with "numbers" and "shapes."
In this chapter, we will deal with more fundamental things: how to describe "conditions" (logic) and how to describe "a collection of things" (set theory).
In machine learning, almost all content involving "probability" or "uncertainty" is fundamentally based on set theory; all content involving "conditional judgments" or "algorithmic flows" is fundamentally based on logic.
Basic Logical Operations: AND, OR, NOT, Implication
Logical AND (AND, symbol ∧)
True, if and only if p and qBoth are true.
Intuition: both conditions must be met; if one is missing, it doesn't count--just like "only if it's raining AND you didn't bring an umbrella will you get wet."
Logical OR (OR, symbol ∨)
is true as long as p and qat least one is true.
The "OR" in mathematics is "inclusive or": when both are true, the whole is also true. It is not the meaning of "either/or" in everyday speech.
Logical NOT (NOT, symbol ¬)
It reverses the truth value of p: if p is true, it is false; if p is false, it is true.
Logical IMPLIES (IMPLIES, symbol ⇒)
Read as "if p, then q," it is one of the most counter-intuitive:
| p (antecedent) | q (consequent) | Description | |
|---|---|---|---|
| True | True | True | Normal situation |
| True | False | False | The only false case is: "True then False" |
| False | True | True | If the premise is false, the whole is true |
| False | False | True | If the premise is false, the whole is true |
The two most counter-intuitive linesWhen p is false, it is judged to be true regardless of what q is.TrueAn example to help understanding: "If it rains tomorrow (p), I will bring an umbrella (q)."
If it does not rain tomorrow
(p is false), then no matter whether you bring an umbrella or not, you cannot say that this sentence is "false"—it does not make any commitment for the "no rain" situation.(p is false), then no matter whether you bring an umbrella or not, you cannot say that this sentence is "false"—it does not make any commitment for the "no rain" situation.Logical equivalence (IFF, symbol ⇔)
Logical equivalence (IFF, symbol ⇔)
Read as "p if and only if q", indicating that the truth values of p and q are always the same (either both true or both false).
Click the switches below to manually "turn on" each combination and observe the results of the four operations.
Interactive Demonstration: Logic Operation Truth Value Switches
Click the p and q switches to toggle the truth values, and the four cards below will display the results of each logical operation in real-time. Pay close attention to how p ⇒ q is always true when p is false.
p
True
q
True
p and q
True
p or q
True
not p
False
p implies q
True
Try setting p to "false" and q to remain "true"—p ⇒ q is still "true"; this is the most counter-intuitive aspect of implication.
Why are these symbols important for AI
When reading algorithm pseudocode or conditional statements in model training (such as the trigger condition for "early stopping"), they are all combinations of logical expressions.
Decision tree models are essentially composed of layers of "if... then..." (implication) judgments.
Many theorems in probability theory heavily use logical operations in their proofs to describe the relationship between events.
Example
Print the complete truth table using Python's boolean operations
Note: Python does not have an "implication" operator; p ⇒ q is equivalent to (not p) or q
(
'p q p∧q p∨q ¬p p⇒q'
)
for
p
in
[
True
,
False
]
:
for
q
in
[
True
,
False
]
:
implies
=
(
not
p
)
or
q
Implication: The only case where it is false is "p is true and q is false"
(
p
,
q
,
p
and
q
,
p
or
q
,
not
p
,
implies
)
The output result of the above code is:
p q p∧q p∨q ¬p p⇒q
True True True True False True
True False False True False False
False True False True True True
False False False False True True
The second line is the only "false": p true, q false, implication is not true.
Exercise:p = "Today is the weekend" is false, q = "I have to go to work" is true. Please judge the truth value of p implies q.
Click to view the answer
If p is false, according to the truth table, regardless of what q is,it is true.
Set concepts: intersection, union, subset
What is a set
A set is the whole of "a collection of definite things," for example, the set that contains the three elements 1, 2, and 3.
| Symbols | Meaning | Example |
|---|---|---|
| x is an element of set A (belongs to) | ||
| x is not an element of set A | ||
| Empty set, contains no elements | ||
| Universal set, "all things" within the scope of discussion | All possible outcomes of rolling a die |
Subset
If every element in set A is also an element in set B, then A is a subset of BSubset, denoted as .
For example, if , then .
Union, intersection, difference, complement
Let :
| Operation | Symbol | Meaning | Result |
|---|---|---|---|
| Union | The combination of elements in A and B (duplicates counted once) | ||
| Intersection | Elements that are in both A and B | ||
| Difference | Elements belonging to A but not to B | ||
| Complement | The part of the universal set U that is not in A, i.e. | Depends on U |
The correspondence between set operations and logical operations
Set theory and logical operations are actually "two languages for the same thing":
| Set language | Logic language |
|---|---|
This is why these two sections are in the same chapter: they are two expressions of the same underlying logic.
Click the button to switch set operations and see the illuminated area in the Venn diagram.
Interactive Demonstration: Venn Diagrams and Set Operations
Let U = {1..10}, A = {1,2,3,4,5}, B = {4,5,6,7}. Click the button to switch operations; the orange area is the selected result.
Example
Python Set Operations: One-to-one correspondence with mathematical symbols
A
=
{
1
,
2
,
3
,
4
,
5
}
B
=
{
4
,
5
,
6
,
7
}
U
=
set
(
range
(
1
,
11
)
)
Full set {1, 2, ..., 10}
(
A | B
)
Union A ∪ B
(
A & B
)
Intersection A ∩ B
(
A - B
)
Set difference A \ B
(
A ^ B
)
Symmetric difference A △ B (elements in only one of the sets)
(
U - A
)
Complement Ā = U \ A
The output of the code above is:
{1, 2, 3, 4, 5, 6, 7}
{4, 5}
{1, 2, 3}
{1, 2, 3, 6, 7}
{6, 7, 8, 9, 10}
Practice: , (within the range of 1~12). Find and .
Click to view answers
Why is probability theory built upon set theory
An event is a set
The "event" in probability theory is essentially a set: the set of all "possible outcomes" that satisfy a certain condition.
For example, when rolling a die, the set corresponding to the event "rolling an even number" is .
Probability operations directly correspond to set operations
| Probabilistic language | Set language | Meaning |
|---|---|---|
| A or B occurs | At least one of events A or B occurs | |
| Both A and B occur | Events A and B occur simultaneously | |
| A does not occur | Complement of A | |
| A and B are mutually exclusive | Two events cannot happen at the same time |
Addition formula: Principle of Inclusion-Exclusion
The geometric intuition of this formula: the overlapping part of two circles is counted twice, so it must be subtracted once.
This can be seen very intuitively using a Venn diagram (a graphical representation of a set).
Conditional probability
Read as "the probability of A occurring given that B has already occurred".
Intuition: Once B is known to have occurred, the "universe" of the problem discussion shrinks from the original U to B.
At this point, the probability of A occurring is naturally the proportion of the "intersection of A and B" within the "new universe B".
The embryo of Bayes' theorem
Conditional probability can also be written in reverse:
Organizing this yields the core formula of Bayes' theorem:
It is the theoretical foundation for many machine learning models (such as the Naive Bayes classifier), and its derivation starts from the simple set intuition that "conditional probability is the proportion of set intersection".
The classic disease detection problem best illustrates the power of Bayes: a "95% detection accuracy" does not equal a "95% probability of having the disease after testing positive". Drag the slider and calculate it yourself.
Interactive demonstration: Bayes' theorem and disease detection
100 dots represent 100 people. Adjust the prevalence, test sensitivity, and false positive rate to observe how the proportion of "truly sick after a positive test" changes.
Sick and positive
Sick but undetected
Healthy but falsely positive
Healthy and negative
Try setting the prevalence to 1%: even with a detection sensitivity of 90%, less than 20% of positive cases might actually be sick—a large number of false positives overwhelm the true positives.
Example
Bayes' theorem: classic calculation for disease detection
A = sick, B = test positive
p_a
=
0.01
Prevalence (Prior Probability)
p_b_given_a
=
0.95
Sensitivity: The probability that a sick person tests positive
p_b_given_na
=
0.05
False positive rate: The probability that a healthy person tests positive
Law of total probability: P(B) = P(B|A)P(A) + P(B|¬A)P(¬A)
p_b
=
p_b_given_a * p_a + p_b_given_na *
(
1
- p_a
)
Bayes' theorem: P(A|B) = P(B|A)P(A) / P(B)
p_a_given_b
=
p_b_given_a * p_a / p_b
(
p_b
)
The total probability of testing positive is about 5.9%
(
p_a_given_b
)
The proportion of truly sick people among the positive cases: only about 16.1%!
The output of the code above is:
0.059000000000000004
0.16101694915254236
The conclusion is counterintuitive: the test has an "accuracy of 95%", but the probability of actually being sick among positive results is only about 16%--because the prevalence itself is only 1%, and a large number of false positives drown out the true positives.
This is the value of Bayes' theorem: it tells youhow to update old beliefs (prevalence) with new evidence (test results), which is also the common ideological source for the Naive Bayes classifier and Bayesian optimization.
Exercise: In a deck of 52 cards, let event A = "drawing a heart" (13 cards), and event B = "drawing A" (4 cards, one of which is the Ace of Hearts). Calculate and .
Click to view the answer
Chapter Summary
| Topic | One-sentence core |
|---|---|
| Logical operations | ∧ requires both to be true, ∨ requires only one to be true, ⇒ is false only when "true before and false after" (most counter-intuitive) |
| Set operations | ∪ is union, ∩ is intersection, corresponding one-to-one with logical operations |
| Probability and sets | Events are sets, P(A∪B), P(A∩B), and conditional probability can be intuitively understood using the overlap relationship of sets |