🤖 AI Math Unit 6 — Linear Models & OLSApplied STEM · Simple linear regression: y = mx + b · Matrix formulation Y = Xw + ε · Ordinary Least Squares (OLS) closed form · Normal equation w = (XᵀX)⁻¹ Xᵀ y · Residuals, MSE and R² coefficient of determination
Linear regression is the foundational predictive model in data science. We explore both algebraic least-squares minimization and the closed-form matrix Normal Equation used across analytics libraries.
▲ مبرهنة 6.1 — The المصفوفة Normal المعادلة
For a linear model y = Xwy=Xw where X \in \mathbb{R}^{N times (d+1)}X∈RN×(d+1) is the design matrix (including a column of ones for intercept), the weight vector \( \mathbf{w} \) that minimizes the sum of squared errors \( \|\mathbf{y} - \mathbf{X}\mathbf{w}\|^2 \) satisfies: \[ \mathbf{X}^T \mathbf{X} \mathbf{w} = \mathbf{X}^T \mathbf{y} \implies \mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{y} \] provided \( \mathbf{X}^T \mathbf{X} \) is invertible (non-singular).
● الفهم المفاهيمي العميق — الإسقاط العمودي على فضاء الأعمدة والمعادلات الطبيعية
التعامد الهندسي لبواقي المربعات الصغرى: يختار نموذج \(\hat{\mathbf{y}} = X\hat{\mathbf{w}}\) أقرب متجه داخل فضاء الأعمدة \(\operatorname{Col}(X)\) إلى المتجه \(\mathbf{y}\). لذا يكون متجه الباقي \(\mathbf{e} = \mathbf{y} - X\hat{\mathbf{w}}\) عمودياً على جميع أعمدة \(X\)، أي \(X^T(\mathbf{y} - X\hat{\mathbf{w}}) = \mathbf{0}\) ومنها المعادلة الطبيعية \(X^T X\hat{\mathbf{w}} = X^T\mathbf{y}\).
تختار طريقة المربعات الصغرى العادية المعلمتين \((w_0, w_1)\) اللتين تصغّران مجموع مربعات البواقي الرأسية \(\operatorname{SSE} = \sum_{i=1}^n (y_i - \hat{y}_i)^2\).
Worked Example \(B\) — Linear Regression & Ordinary Least Squares (Hard Application)
المسألة. A simple linear regression model has been fitted to a dataset. The model equation is given by \( \hat{y} = ax + b \). The regression line passes through the points \( (2, 10) \) and \( (5, 22) \) in the feature-target space. Determine the value of the intercept \( b \).
▸ عرض خطوات الحل بالتفصيل
The problem states that the linear regression line \( \hat{y} = ax + b \) passes through two points: \( (x_1, y_1) = (2, 10) \) and \( (x_2, y_2) = (5, 22) \).
We can use these two points to form a system of linear equations to solve for the unknown parameters \( a \) (the slope) and \( b \) (the intercept).
Substituting the first point \( (2, 10) \) into the equation \( \hat{y} = ax + b \) gives:
\( 10 = a(2) + b \) (Equation 1)
Substituting the second point \( (5, 22) \) into the equation \( \hat{y} = ax + b \) gives:
\( 22 = a(5) + b \) (Equation 2)
To find the slope \( a \), we can subtract Equation 1 from Equation 2:
\( (22 - 10) = (5a - 2a) + (b - b) \)
\( 12 = 3a \)
\( a = \frac{12}{3} = 4 \)
Now that we have the value of \( a \), we can substitute it back into either Equation 1 or Equation 2 to solve for \( b \). Let's use Equation 1:
\( 10 = 4(2) + b \)
\( 10 = 8 + b \)
\( b = 10 - 8 \)
\( b = 2 \)
Let's verify with Equation 2:
\( 22 = 4(5) + b \)
\( 22 = 20 + b \)
\( b = 22 - 20 \)
\( b = 2 \)
Both equations yield \( b = 2 \). Therefore, the intercept \( b \) is 2.
Common algebraic traps include incorrectly calculating the slope or making sign errors when substituting values back into the equations. For instance, if one incorrectly calculated the slope as \( \frac{3}{12} \) or made a sign error in \( 10 - 8 \), they might arrive at an incorrect intercept.
Worked Example \(C\) — Linear Regression & Ordinary Least Squares (Easy Application)
المسألة. In gradient descent on loss \( f(x) \), parameter \( x_0 = 30 \) is updated with learning rate \( \eta = 0.1 \) and gradient \( \nabla f(x_0) = 20 \). Determine the updated parameter \( x_1 = x_0 - \eta \nabla f(x_0) \).
▸ عرض خطوات الحل بالتفصيل
Computing \( x_1 = 30 - 0.1(20) = 28 \).
Worked Example \(D\) — Linear Regression & Ordinary Least Squares (Medium Application)
المسألة. Consider a simple linear regression model where the predicted value \( \hat{y} \) is related to the independent variable \( x \) by the equation \( \hat{y} = mx + c \). If the line of best fit passes through the points \( (1, 5) \) and \( (3, 11) \), what is the slope \( m \) of this line?
▸ عرض خطوات الحل بالتفصيل
The slope \( m \) of a line passing through two points \( (x_1, y_1) \) and \( (x_2, y_2) \) is given by the formula \( m = \frac{y_2 - y_1}{x_2 - x_1} \). Given the points \( (1, 5) \) and \( (3, 11) \), we have \( x_1 = 1 \), \( y_1 = 5 \), \( x_2 = 3 \), and \( y_2 = 11 \). Plugging these values into the formula, we get \( m = \frac{11 - 5}{3 - 1} = \frac{6}{2} = 3 \). Therefore, the slope of the line of best fit is 3.
1. إغفال عمود الآحاد في مصفوفة التصميم \(X\): بدون إضافة العمود \(\mathbf{1}_n\) يُجبر خط الانحدار على المرور بنقطة الأصل \((b_0 = 0)\) ولا يكون مجموع البواقي صفراً بالضرورة. 2. شذوذ \(X^T X\) عند الارتباط الخطي التام: إذا ارتبطت ميزتان خطياً تصبح \(X^T X\) غير قابلة للقلب، ويعالج انحدار ريدج \((X^T X + \lambda I)^{-1}X^T\mathbf{y}\) ذلك لأي \(\lambda > 0\).
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\)
يؤدي إلى فقدان جذور صحيحة أو إدخال جذور دخيلة لا تحقق المعادلة الأصلية.
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}\)
يؤدي إلى فقدان جذور صحيحة أو إدخال جذور دخيلة لا تحقق المعادلة الأصلية.
Use total distance over total time: \(v_{\text{avg}} = \frac{2v_1 v_2}{v_1 + v_2}\).
▲ استراتيجية الحل والتحقق السريع — الانحدار الخطي والمربعات الصغرى
اختبار مركز البيانات ومجموع البواقي الصفري: كل خط انحدار بسيط يتضمن قاطعاً يمر حتماً بالنقطة المتوسطة \((\bar{x}, \bar{y})\) ويحقق \(\sum_{i=1}^n e_i = 0\) و\(\sum_{i=1}^n x_i e_i = 0\)، فتحقق منهما فوراً!
[Practice 1 · Hard] A 2D loss surface is defined as \( L(w_1, w_2) = 4w_1^2 + 7w_2^2 \). Find the gradient vector \( \nabla L \) at the point \( (w_1, w_2) = (2, -1) \).
[Practice 2 · Easy] A fully-connected dense layer maps an input \( \mathbf{x} \in \mathbb{R}^{50} \) to \( \mathbf{y} \in \mathbb{R}^{25} \) using \( \mathbf{y} = \mathbf{W}\mathbf{x} + \mathbf{b} \). How many total learnable parameters are in this layer?
💡 Hint 1 of 3
The weight matrix \( \mathbf{W} \) has shape \( 25 \times 50 \), giving \( 1250 \) weights.
💡 Hint 2 of 3
The bias vector \( \mathbf{b} \) has \( 25 \) parameters.
💡 Hint 3 of 3
Substitute the result from the previous step into the governing relation and verify the problem condition to find the final answer.
The weight matrix \( \mathbf{W} \) has shape \( 25 \times 50 \), giving \( 1250 \) weights. The bias vector \( \mathbf{b} \) has \( 25 \) parameters. Total = \( 1250 + 25 = 1275 \).
[Practice 3 · Easy] In a dataset, the prior probability of class \( C_1 \) is \( P(C_1) = 0.10 \). What is the probability of the complement class \( P(\neg C_1) \)?
💡 Hint 1 of 3
By the complement rule of probability: \(P( eg C_1) = 1 - P(C_1)\).
💡 Hint 2 of 3
Substitute and simplify: \(P( eg C_1) = 1 - 0.10\).
💡 Hint 3 of 3
Compute the final value from: \(1 - 0.10\).
By the complement rule of probability: \( P(\neg C_1) = 1 - P(C_1) = 1 - 0.10 = 0.90 \).
[Practice 4 · Medium] Consider a simple linear regression model where a dependent variable \( y \) is predicted from an independent variable \( x \). The model is given by the equation \( y = \beta_0 + \beta_1 x + \epsilon \), where \( \epsilon \) is the error term. Suppose we have estimated the regression line to be \( \hat{y} = 3 + 2x \). If we observe an independent variable value of \( x=4 \), what is the predicted value \( \hat{y} \) for the dependent variable?
💡 Hint 1 of 3
The problem asks for the predicted value \( \hat{y} \) given an independent variable value \( x=4 \) and a linear regression equation \( \hat{y} = 3 + 2x \). We substitute the value of \( x \) into the equation: \( \hat{y} = 3 + 2(4) \).
💡 Hint 2 of 3
Rewrite and simplify to obtain: \( \hat{y} = 3 + 8 \) \( \hat{y} = 11 \).
💡 Hint 3 of 3
Therefore, the predicted value for the dependent variable is 11.
The problem asks for the predicted value \( \hat{y} \) given an independent variable value \( x=4 \) and a linear regression equation \( \hat{y} = 3 + 2x \). We substitute the value of \( x \) into the equation:
\( \hat{y} = 3 + 2(4) \)
\( \hat{y} = 3 + 8 \)
\( \hat{y} = 11 \)
Therefore, the predicted value for the dependent variable is 11. Common errors might involve incorrectly multiplying 2 by 4 or incorrectly adding 3 to 8.
[Practice 5 · Hard] In anomaly detection, anomaly prior \( P(A) = 0.10 \), detection sensitivity \( P(+|A) = 0.95 \), and false alarm rate \( P(+|\neg A) = 0.05 \). By Bayes' theorem, what is the posterior probability \( P(A|+) \) that an alarm is a true anomaly?
● Master Atlas of the 9 Main Parent Functions & Universal Transformation Law
Every algebraic and elementary transcendental model is built by transforming one of the nine core parent functions via \(g(x) = a\,f(x - h) + k\), where \(|a|\) scales vertically (reflecting across the \(x\)-axis if \(a < 0\)), \(h\) shifts horizontally, and \(k\) shifts vertically:
مقارنة قيم الدالة الأم \(f(x)\) والدالة المحوّلة \(g(x) = a\,f(x - h) + k\) عند مدخلات صحيحة.
\(x\)
الدالة الأم \(f(x)\)
الدالة المحوّلة \(g(x)\)
المعادلة النشطةf(x) = x
المجال(−∞, ∞)
المدى(−∞, ∞)
الرأس / خطوط التقاربAnchor (0, 0), Slope m = 1
نقاط التقاطع مع المحورينy-int: (0, 0) · x-int: (0, 0)
يبدأ على خطية عائلة \(f(x) = a(x - h) + k\) مع ميل \(a\) و الارتكاز نقطة \((h, k)\). اسحب \((h, k)\) مباشرة على الإحداثي المستوى أو بدّل إلى أي من 9 الأم عائلات.
الانحدار الخطي وطريقة المربعات الصغرى (OLS) — مسألة مهيكلة من ٦ درجات
6 نقاط
الآلة الحاسبة مسموحة
في موضوع الانحدار الخطي وطريقة المربعات الصغرى (OLS)، ادرس الدالة \(f(x) = 3x^2 - 3x + 1\) على الفترة \([0, 3]\).
(a)2 نقطة
احسب قيمتي \(f(0)\) و \(f(2)\).
معيار التصحيح والإجابة النموذجية
[M1]يعوّض القيمتين في الدالة(1 نقطة)
[A1]يحصل على 1 و 7(1 نقطة)
(b)2 نقطة
أوجد المشتقة \(f'(x)\) والنقطة الحرجة \(x_c\).
معيار التصحيح والإجابة النموذجية
[M1]يشتق الدالة التربيعية(1 نقطة)
[A1]يحل لإيجاد النقطة الحرجة(1 نقطة)
(c)2 نقطة
احسب متوسط معدل التغيّر من \(x = 0\) إلى \(x = 2\).
معيار التصحيح والإجابة النموذجية
[M1]يطبّق قانون متوسط معدل التغيّر(1 نقطة)
[A1]يبسّط الناتج إلى 3(1 نقطة)
الحل الكامل التفصيلي
(أ) \(f(0)=1\) و \(f(2)=7\). (ب) \(f'(x)=6x-3\). (ج) متوسط معدل التغيّر يساوي \(3\).