🤖 AI Math Unit 4 — Probabilistic Machine LearningApplied STEM · Joint and conditional probability P(A|B) · Bayes' Theorem: Prior, Likelihood, Posterior · Total probability theorem · Naive Bayes independence assumption · Text spam filtering & medical diagnostics
Uncertainty is omnipresent in real-world data. Bayes' Theorem provides the mathematical framework for updating beliefs when observing new evidence, forming the core of Bayesian machine learning and Naive Bayes classifiers.
▲ Theorem 4.1 — Bayes' Theorem in Machine Learning
For hypothesis (class) C C and observed evidence (features) xx: \[ P(C | \mathbf{x}) = \frac{P(\mathbf{x} | C) P(C)}{P(\mathbf{x})} \] Where: • \( P(C) \) is the Prior probability of class \( C \). • \( P(\mathbf{x} | C) \) is the Likelihood of observing features \( \mathbf{x} \) given class \( C \). • \( P(\mathbf{x}) = \sum_k P(\mathbf{x}|C_k)P(C_k) \) is the Marginal probability (Evidence). • \( P(C | \mathbf{x}) \) is the Posterior probability.
With fixed sensitivity \(P(+ | C_{1}) = 0.90\), see how a low base rate \(P(C_{1})\) or high false-positive rate \(P(+ | C_{0})\) dramatically reduces the posterior probability \(P(C_{1} | +)\).
Empirical contingency counts across \(N = 1000\) emails yielding class-conditional likelihoods \(P(\text{'free'}\mid \text{Spam}) = 0.80\) vs \(P(\text{'free'}\mid \text{Ham}) = 0.10\).
Prevents zero probability: \( \frac{\text{count} + 1}{\text{total} + |V|} \)
◆ Why It Works — Derivation from Joint Probability & Total Probability
Symmetry of Joint Probability: By definition of conditional probability, \(P(A \cap B) = P(A \mid B)P(B) = P(B \mid A)P(A)\). Dividing by \(P(B) > 0\) and expanding the partition \(B = (B \cap A) \cup (B \cap \neg A)\) yields \[ P(A \mid B) = \frac{P(B \mid A)P(A)}{P(B \mid A)P(A) + P(B \mid \neg A)P(\neg A)}. \]
Worked Example 4.A — Bayesian Spam Classification
Problem. An email filter processes emails where \( 20\% \) are spam (\( P(S) = 0.20 \)) and \( 80\% \) are legitimate (\( P(L) = 0.80 \)). The word 'lottery' appears in \( 60\% \) of spam emails (\( P(W|S) = 0.60 \)) and in \( 1\% \) of legitimate emails (\( P(W|L) = 0.01 \)). If an incoming email contains the word 'lottery', what is the probability that it is spam?
▸ Show step-by-step solution
By Bayes' Theorem: \[ P(S|W) = \frac{P(W|S)P(S)}{P(W)} \] Calculate total probability \( P(W) \): \[ P(W) = P(W|S)P(S) + P(W|L)P(L) = (0.60)(0.20) + (0.01)(0.80) = 0.12 + 0.008 = 0.128 \] Compute posterior: \[ P(S|W) = \frac{0.12}{0.128} = \frac{120}{128} = \frac{15}{16} = 0.9375 \]. Despite only 20% of all emails being spam initially, observing the word 'lottery' raises the probability to \( 93.75\% \). Answer: \( 93.75\% \) (or \( 0.938 \)).
Worked Example \(B\) — Conditional Probability, Bayes' Rule & Classifiers (Medium Application)
Problem. Given \( n = 4 \) independent observations from a normal distribution with sum \( \sum_{i=1}^{4} x_i = 12 \), what is the Maximum Likelihood Estimate (MLE) \( \hat{\mu}_{\text{MLE}} \)?
Worked Example \(C\) — Conditional Probability, Bayes' Rule & Classifiers (Hard Application)
Problem. A logistic regression model produces a class probability \( p = P(y=1|\mathbf{x}) = 0.40 \). Compute the corresponding log-odds (logit) \( \ln\left(\frac{p}{1-p}\right) \).
Worked Example \(D\) — Conditional Probability, Bayes' Rule & Classifiers (Easy Application)
Problem. A medical diagnostic test for a rare disease is designed to be highly sensitive and specific. The disease affects \( 1 \) in every \( 1000 \) individuals in the population. The test has a sensitivity of \( 99 \)\% (meaning it correctly identifies \( 99 \)\% of individuals who have the disease) and a specificity of \( 95 \)\% (meaning it correctly identifies \( 95 \)\% of individuals who do not have the disease). If a randomly selected individual from the population tests positive, what is the probability that they actually have the disease?
▸ Show step-by-step solution
Let D be the event that an individual has the disease, and P be the event that the individual tests positive.
We are given the following probabilities:
- Prior probability of having the disease: \(P(D) = \frac{1}{1000} = 0.001\)
- Prior probability of not having the disease: \(P(D^c) = 1 - P(D) = 1 - 0.001 = 0.999\)
- Sensitivity (true positive rate): \(P(P|D) = 0.99\)
- Specificity (true negative rate): \(P(P^c|D^c) = 0.95\)
From specificity, we can derive the false positive rate: \(P(P|D^c) = 1 - P(P^c|D^c) = 1 - 0.95 = 0.05\).
We want to find the probability that an individual actually has the disease given a positive test result, which is \(P(D|P)\). We can use Bayes' Theorem:
\[P(D|P) = \frac{P(P|D) P(D)}{P(P)}\]
To find \(P(P)\), the overall probability of testing positive, we can use the law of total probability:
\[P(P) = P(P|D) P(D) + P(P|D^c) P(D^c)\]
Substitute the given values:
\[P(P) = (0.99)(0.001) + (0.05)(0.999)\]
\[P(P) = 0.00099 + 0.04995\]
\[P(P) = 0.05094\]
Now, substitute \(P(P)\) back into Bayes' Theorem:
\[P(D|P) = \frac{(0.99)(0.001)}{0.05094}\]
\[P(D|P) = \frac{0.00099}{0.05094}\]
\[P(D|P) \approx 0.0194346\]
Therefore, the probability that a person who tests positive actually has the disease is approximately \( 0.0194 \), which is closest to \( 0.019 \).
⚠️ Common Exam Traps — Base-Rate Neglect & Conditional Reversal
1. Confusing \(P(+ \mid D)\) with \(P(D \mid +)\): A sensitivity of \(99\%\) does not mean a positive test implies \(99\%\) disease probability when \(P(D)\) is small (base-rate fallacy). 2. Forgetting False Positives in the Denominator: Always compute \(P(+) = P(+\mid D)P(D) + P(+\mid \neg D)(1 - P(D))\), noting \(P(+\mid \neg D) = 1 - \text{Specificity}\).
Error Diagnosis — Common Student Traps vs. Exam-Board Method (Conditional Probability, Bayes' Rule & Classifiers)
Common Student Trap
Why It Fails Mathematically
Correct Exam-Board Method
Claiming \(+25\%\) followed by \(-25\%\) returns to \(100\%\)
The \(25\%\) reduction applies to the larger base \(1.25 Q_0\), not \(Q_0\).
Multiply scale factors: \(1.25 \times 0.75 = 0.9375\) (a \(6.25\%\) net decrease).
Solving \(-3x > 12\) as \(x > -4\)
Dividing by a negative number reverses order on the real number line.
Flip the inequality symbol whenever multiplying or dividing by a negative: \(x < -4\).
Computing round-trip average speed as \(\frac{v_1 + v_2}{2}\)
Time spent at the slower speed is longer than time spent at the faster speed.
Use total distance over total time: \(v_{\text{avg}} = \frac{2v_1 v_2}{v_1 + v_2}\).
Natural Frequency Shortcut (10,000-Sample Table): Convert percentages into counts out of \(10{,}000\) samples first: compute \(\text{TP}\) and \(\text{FP}\) as integers, then form \(P(D \mid +) = \frac{\text{TP}}{\text{TP} + \text{FP}}\) to eliminate decimal arithmetic slips.
[Practice 1 · Medium] A spam classification model identifies \( TP = 30 \) genuine spam emails and \( FP = 10 \) legitimate emails falsely flagged as spam. What is the precision of this model?
💡 Hint 1 of 3
Precision is the fraction of predicted positive instances that are truly positive: \(\text{Precision} = \frac{TP}{TP + FP}\).
💡 Hint 2 of 3
Substitute and simplify: \(\text{Precision} = \frac{30}{30 + 10}\).
💡 Hint 3 of 3
Compute the final value from: \(\frac{30}{30 + 10}\).
Precision is the fraction of predicted positive instances that are truly positive: \( \text{Precision} = \frac{TP}{TP + FP} = \frac{30}{30 + 10} = \frac{30}{40} = 75.0\% \).
[Practice 2 · Hard] Compute the Manhattan (\( L_1 \)) norm and Euclidean (\( L_2 \)) norm of the word vector \( \mathbf{w} = [10, -15]^T \).
💡 Hint 1 of 3
Set up the initial relation: \( L_1 \) norm is the sum of absolute values: \( |10| + |-15| = 25 \).
💡 Hint 2 of 3
Rewrite and simplify to obtain: \( L_2 \) norm is the square root of sum of squares: \(\sqrt{(10)^2 + (-15)^2}\). Rewrite and substitute: \(\sqrt{(10)^2 + (-15)^2} = \sqrt{100 + 225}\).
💡 Hint 3 of 3
Evaluate \(\sqrt{100 + 225}\) to obtain the final answer.
\( L_1 \) norm is the sum of absolute values: \( |10| + |-15| = 25 \). \( L_2 \) norm is the square root of sum of squares: \( \sqrt{(10)^2 + (-15)^2} = \sqrt{100 + 225} = \sqrt{325} \).
[Practice 3 · Easy] Consider two vectors, \( \mathbf{u} = \begin{bmatrix} 3 \\ 4 \end{bmatrix} \) and \( \mathbf{v} = \begin{bmatrix} 1 \\ -2 \end{bmatrix} \). What is the vector representing \( 2\mathbf{u} - \mathbf{v} \)?
💡 Hint 1 of 3
To find the vector \( 2\mathbf{u} - \mathbf{v} \), we first compute \( 2\mathbf{u} \) and then subtract \( \mathbf{v} \) from the result.
Therefore, the resulting vector is \(\begin{bmatrix} 5 \\ 10 \end{bmatrix}\).
To find the vector \( 2\mathbf{u} - \mathbf{v} \), we first compute \( 2\mathbf{u} \) and then subtract \( \mathbf{v} \) from the result.
\( 2\mathbf{u} = 2 \times \begin{bmatrix} 3 \\ 4 \end{bmatrix} = \begin{bmatrix} 2 \times 3 \\ 2 \times 4 \end{bmatrix} = \begin{bmatrix} 6 \\ 8 \end{bmatrix} \).
Now, we subtract \( \mathbf{v} \) from \( 2\mathbf{u} \):
\( 2\mathbf{u} - \mathbf{v} = \begin{bmatrix} 6 \\ 8 \end{bmatrix} - \begin{bmatrix} 1 \\ -2 \end{bmatrix} = \begin{bmatrix} 6 - 1 \\ 8 - (-2) \end{bmatrix} = \begin{bmatrix} 5 \\ 8 + 2 \end{bmatrix} = \begin{bmatrix} 5 \\ 10 \end{bmatrix} \).
Therefore, the resulting vector is \( \begin{bmatrix} 5 \\ 10 \end{bmatrix} \).
[Practice 4 · Medium] Consider a dataset of \( n \) observations \( (x_i, y_i) \) for \( i = 1, \dots, n \), where \( x_i \) are scalar predictor variables and \( y_i \) are scalar response variables. The ordinary least squares (OLS) estimates for the intercept \( \beta_0 \) and the slope \( \beta_1 \) of the linear model \( y = \beta_0 + \beta_1 x + \epsilon \) are given by \( \hat{\beta}_1 = \frac{\sum_{i=1}^n (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^n (x_i - \bar{x})^2} \) and \( \hat{\beta}_0 = \bar{y} - \hat{\beta}_1 \bar{x} \). Suppose we have a dataset where \( n = 5 \), \( \sum x_i = 15 \), \( \sum y_i = 30 \), \( \sum x_i^2 = 55 \), \( \sum y_i^2 = 210 \), and \( \sum x_i y_i = 105 \). A new data point \( (x_6, y_6) \) is added to the dataset. If the new regression line that minimizes the sum of squared errors for the \( n+1 = 6 \) points is parallel to the original regression line (i.e., \( \hat{\beta}_1^{\text{new}} = \hat{\beta}_1^{\text{old}} \)), what is the value of \( y_6 \) in terms of \( x_6 \)?
💡 Hint 1 of 3
For the original \( n = 5 \) observations, \( \bar{x} = \frac{15}{5} = 3 \) and \(\bar{y} = \frac{30}{5}\).
💡 Hint 2 of 3
The sums of squares are \( S_{xx} = 55 - 5(3^2) = 10 \) and \( S_{xy} = 105 - 5(3)(6) = 15 \), giving slope \( \hat{\beta}_1 = \frac{15}{10} = 1.5 \) and intercept \(\hat{\beta}_0 = 6 - 1.5(3)\).
💡 Hint 3 of 3
When a 6th point \((x_6, y_6)\) lying on the existing regression line \(y_6 = 1.5x_6 + 1.5\) is added, the residual is zero and the OLS slope remains unchanged at \(1.5\).
For the original \( n = 5 \) observations, \( \bar{x} = \frac{15}{5} = 3 \) and \( \bar{y} = \frac{30}{5} = 6 \). The sums of squares are \( S_{xx} = 55 - 5(3^2) = 10 \) and \( S_{xy} = 105 - 5(3)(6) = 15 \), giving slope \( \hat{\beta}_1 = \frac{15}{10} = 1.5 \) and intercept \( \hat{\beta}_0 = 6 - 1.5(3) = 1.5 \). When a 6th point \( (x_6, y_6) \) lying on the existing regression line \( y_6 = 1.5x_6 + 1.5 \) is added, the residual is zero and the OLS slope remains unchanged at \( 1.5 \).
[Practice 5 · Hard] Consider a dataset of \( n \) data points, where each data point is a vector in \( \mathbb{R}^{D} \). A non-linear dimensionality reduction technique maps these data points to a lower-dimensional space \( \mathbb{R}^{d} \). The mapping function for a single data point \( \mathbf{x} \in \mathbb{R}^{D} \) is given by \( f(\mathbf{x}) = \phi(\mathbf{x}) \), where \( \phi: \mathbb{R}^{D} \to \mathbb{R}^{d} \) is a function designed to preserve local neighborhood structures. Suppose the mapping \( \phi \) is defined by an explicit feature map \( \phi(\mathbf{x}) = [\mathbf{x}^T \mathbf{x}, \mathbf{x}^T \mathbf{x}, \dots, \mathbf{x}^T \mathbf{x}]^T \) where the vector \( \mathbf{x} \in \mathbb{R}^{10} \) and \( d \) is the dimension of the embedded space. If the goal is to embed these points into a space such that pairwise Euclidean distances are preserved as closely as possible under a specific distortion, and it is found that \( d=15 \) yields optimal results for this mapping under a certain constraint, determine the effective rank of the feature map's image space.
💡 Hint 1 of 3
The feature map is defined as \( \phi(\mathbf{x}) = [\mathbf{x}^T \mathbf{x}, \mathbf{x}^T \mathbf{x}, \dots, \mathbf{x}^T \mathbf{x}]^T \), where \( \mathbf{x} \in \mathbb{R}^{10} \).
💡 Hint 2 of 3
The term \( \mathbf{x}^T \mathbf{x} \) is the squared Euclidean norm of \( \mathbf{x} \), denoted as \( \|\mathbf{x}\|^2 \). This is a scalar value.
💡 Hint 3 of 3
The problem states that \(d\), but the effective rank of the image of this specific feature map is always 1, as all output vectors lie on a single line passing through the origin.
The feature map is defined as \( \phi(\mathbf{x}) = [\mathbf{x}^T \mathbf{x}, \mathbf{x}^T \mathbf{x}, \dots, \mathbf{x}^T \mathbf{x}]^T \), where \( \mathbf{x} \in \mathbb{R}^{10} \). The term \( \mathbf{x}^T \mathbf{x} \) is the squared Euclidean norm of \( \mathbf{x} \), denoted as \( \|\mathbf{x}\|^2 \). This is a scalar value. Thus, the mapping \( \phi \) produces a vector where each component is the same scalar value, \( \|\mathbf{x}\|^2 \). Let \( k = \|\mathbf{x}\|^2 \). Then \( \phi(\mathbf{x}) = [k, k, \dots, k]^T \) in \( \mathbb{R}^{d} \). This means that the image of the mapping \( \phi \) is a subspace spanned by a single vector. For example, if \( d=15 \), then \( \phi(\mathbf{x}) = [k, k, \dots, k]^T \) which can be written as \( k \cdot [1, 1, \dots, 1]^T \). The vector \( [1, 1, \dots, 1]^T \) forms a basis for the image space. Therefore, the dimension of the image space, which is equivalent to its rank, is 1, regardless of the value of \( d \) (as long as \( d \ge 1 \)). The problem states that \( d=15 \), but the effective rank of the image of this specific feature map is always 1, as all output vectors lie on a single line passing through the origin.
Conditional Probability, Bayes' Rule & Classifiers — 6-Mark Structured Problem
6 pts
Calculator Active
For Conditional Probability, Bayes' Rule & Classifiers, examine the quadratic-rate model \(f(x) = 5x^2 - 4x + 1\) on \(x \in [0, 3]\).
(a)2 pts
Compute \(f(0)\) and \(f(2)\).
Scoring Guideline & Justification
[M1]Substitutes x=0 and x=2(1 pt)
[A1]Obtains 1 and 13(1 pt)
(b)2 pts
Find the derivative/rate \(f'(x)\) and critical point \(x_c\).
Scoring Guideline & Justification
[M1]Differentiates to 10x - 4(1 pt)
[A1]Solves x_c = 4/10(1 pt)
(c)2 pts
Calculate the average rate of change from \(x = 0\) to \(x = 2\).