✓ تم التحقق من المنهج الرسميمعايير الجودة والصرامة ↗
6

الانحدار الخطي وطريقة المربعات الصغرى (OLS)

🤖 AI Math Unit 6 — Linear Models & OLS Applied 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 = Xw where X \in \mathbb{R}^{N times (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\).
⊙ مستكشف تفاعلي · الانحدار الخطي والمربعات الصغرى الاعتيادية (OLS) ومربعات البواقي
تفاعلي
المتوسط الحسابي مربع خطأ MSE\((w, b)\)MSE = 0.124
Optimal العمودي على المماس-Eq \((w*, b*)\)\(w* = 1.04, b* = 0.50\)
معامل من Determination \(R^{2}\)\(R^{2} = 0.943\)

رأسي كهرماني segments بيّن residuals \(e_i = y_i -(w \cdot x_i + b)\) لكل training نقطة. Minimizing \(\Sigma\) e_\(i^{2}\) ينتج متقطع ordinary المربعات الصغرى مستقيم.

سحابة البيانات ثنائية المتغير مع خط الانحدار الخطي بالمربعات الصغرى الذي يلتقط الاتجاه العام للعلاقة.
الميل الصيغة
\( m = \frac{\sum (x_i - \bar{x})(y_i - \bar{y})}{\sum (x_i - \bar{x})^2} = \frac{S_{xy}}{S_{xx}} \)
المقطع
\( b = \bar{y} - m\bar{x} \)
R² Score (الانحدار الخطي وطريقة المربعات الصغرى (OLS))
\( R^2 = 1 - \frac{\text{SS}_{\text{res}}}{\text{SS}_{\text{tot}}} \in [0, 1] \)
Residual المتجه
\( \mathbf{e} = \mathbf{y} - \hat{\mathbf{y}} \perp \text{col}(\mathbf{X}) \)
◆ لماذا تعمل القاعدة — اشتقاق المعادلة الطبيعية بحساب تفاضل المصفوفات

فك دالة الهدف التربيعية: نكتب \(J(\mathbf{w}) = \|\mathbf{y} - X\mathbf{w}\|_2^2 = \mathbf{y}^T\mathbf{y} - 2\mathbf{w}^T X^T\mathbf{y} + \mathbf{w}^T X^T X\mathbf{w}\). بأخذ التدرج بالنسبة إلى \(\mathbf{w}\) ومساواته بالصفر نجد \(\nabla_{\mathbf{w}} J = -2X^T\mathbf{y} + 2X^T X\mathbf{w} = \mathbf{0}\)، وعندما تكون أعمدة \(X\) مستقلة خطياً تكون \(X^T X\) قابلة للقلب ويكون \(\hat{\mathbf{w}} = (X^T X)^{-1}X^T\mathbf{y}\).

Worked Example 6.A — Closed-Form Matrix Normal Equation
المسألة. Fit a line \( y = w_1 x + w_0 \) to three points: \( (1, 2) \), \( (2, 3) \), and \( (3, 5) \) using the Normal Equation.
▸ عرض خطوات الحل بالتفصيل


1. Set up design matrix \( \mathbf{X} \) (with bias column) and target \( \mathbf{y} \):
\[ \mathbf{X} = \begin{pmatrix} 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{pmatrix}, \quad \mathbf{y} = \begin{pmatrix} 2 \\ 3 \\ 5 \end{pmatrix} \]
2. Compute \( \mathbf{X}^T \mathbf{X} \):
\[ \mathbf{X}^T \mathbf{X} = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 1 & 1 \end{pmatrix} \begin{pmatrix} 1 & 1 \\ 2 & 1 \\ 3 & 1 \end{pmatrix} = \begin{pmatrix} 1+4+9 & 1+2+3 \\ 1+2+3 & 1+1+1 \end{pmatrix} = \begin{pmatrix} 14 & 6 \\ 6 & 3 \end{pmatrix} \]
3. Invert \( \mathbf{X}^T \mathbf{X} \):
\( \det = (14)(3) - (6)(6) = 42 - 36 = 6 \).
\[ (\mathbf{X}^T \mathbf{X})^{-1} = \frac{1}{6} \begin{pmatrix} 3 & -6 \\ -6 & 14 \end{pmatrix} \]
4. Compute \( \mathbf{X}^T \mathbf{y} \):
\[ \mathbf{X}^T \mathbf{y} = \begin{pmatrix} 1 & 2 & 3 \\ 1 & 1 & 1 \end{pmatrix} \begin{pmatrix} 2 \\ 3 \\ 5 \end{pmatrix} = \begin{pmatrix} 2 + 6 + 15 \\ 2 + 3 + 5 \end{pmatrix} = \begin{pmatrix} 23 \\ 10 \end{pmatrix} \]
5. Compute \( \mathbf{w} = (\mathbf{X}^T \mathbf{X})^{-1} (\mathbf{X}^T \mathbf{y}) \):
\[ \mathbf{w} = \frac{1}{6} \begin{pmatrix} 3(23) - 6(10) \\ -6(23) + 14(10) \end{pmatrix} = \frac{1}{6} \begin{pmatrix} 69 - 60 \\ -138 + 140 \end{pmatrix} = \frac{1}{6} \begin{pmatrix} 9 \\ 2 \end{pmatrix} = \begin{pmatrix} 1.5 \\ 0.333 \end{pmatrix} \]
Line equation: \( \hat{y} = 1.5x + \frac{1}{3} \).
Answer: Slope \( w_1 = 1.5 \), Intercept \( w_0 = \frac{1}{3} \).

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\).

تشخيص الأخطاء — مقارنة الخطأ الشائع بالطريقة الصحيحة (الانحدار الخطي وطريقة المربعات الصغرى (OLS))
الخطأ الشائع للطالبالسبب الرياضي للخطأالطريقة الصحيحة المعتمدة
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) \).
💡 Hint 1 of 3
Compute partial derivatives: \(\frac{\partial L}{\partial w_1} = 2(4)w_1 = 8(2)\).
💡 Hint 2 of 3
Rewrite and simplify to obtain: \(\frac{\partial L}{\partial w_2} = 2(7)w_2 = 14(-1)\).
💡 Hint 3 of 3
Therefore, \(\nabla L = [16, -14]^T\).
Compute partial derivatives: \( \frac{\partial L}{\partial w_1} = 2(4)w_1 = 8(2) = 16 \); \( \frac{\partial L}{\partial w_2} = 2(7)w_2 = 14(-1) = -14 \). Therefore, \( \nabla L = [16, -14]^T \).
[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?
💡 Hint 1 of 3
By Bayes' Rule: \(P(A|+) = \frac{P(+|A)P(A)}{P(+|A)P(A) + P(+| eg A)P( eg A)}\).
💡 Hint 2 of 3
Substitute and simplify: \(P(A|+) = \frac{0.95 \times 0.10}{0.95(0.10) + 0.05(0.90)}\).
💡 Hint 3 of 3
Compute the final value from: \(\frac{0.0950}{0.1400}\).
By Bayes' Rule: \( P(A|+) = \frac{P(+|A)P(A)}{P(+|A)P(A) + P(+|\neg A)P(\neg A)} = \frac{0.95 \times 0.10}{0.95(0.10) + 0.05(0.90)} = \frac{0.0950}{0.1400} = 0.679 \).
● 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:

FamilyTransformed Form \(g(x)\)DomainRange (\(a > 0\))Key Geometric Feature
Constant\(f(x) = c\)\((-\infty, \infty)\)\(\{c\}\)Horizontal line, slope \(0\)
Linear\(a(x - h) + k\)\((-\infty, \infty)\)\((-\infty, \infty)\)Constant slope \(m = a\), pivot \((h, k)\)
Quadratic\(a(x - h)^2 + k\)\((-\infty, \infty)\)\([k, \infty)\)Parabola vertex \((h, k)\), axis \(x = h\)
Cubic\(a(x - h)^3 + k\)\((-\infty, \infty)\)\((-\infty, \infty)\)Inflection center \((h, k)\), S-curve
Absolute Value\(a|x - h| + k\)\((-\infty, \infty)\)\([k, \infty)\)V-corner vertex \((h, k)\), slopes \(\pm a\)
Radical\(a\sqrt{x - h} + k\)\([h, \infty)\)\([k, \infty)\)Endpoint \((h, k)\), half-parabola
Rational\(\dfrac{a}{x - h} + k\)\(x \neq h\)\(y \neq k\)Vertical asymptote \(x = h\), horizontal \(y = k\)
Exponential\(a \cdot 2^{x - h} + k\)\((-\infty, \infty)\)\((k, \infty)\)Horizontal asymptote \(y = k\), anchor \((h, a+k)\)
Logarithmic\(a\ln(x - h) + k\)\((h, \infty)\)\((-\infty, \infty)\)Vertical asymptote \(x = h\), anchor \((h+1, k)\)
⊙ مستكشف تفاعلي · مختبر الدالة الخطية والميل وتحويلات الدوال الأم
تفاعلي
المعادلة النشطة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]\).
  1. (a)2 نقطة
    احسب قيمتي \(f(0)\) و \(f(2)\).
    معيار التصحيح والإجابة النموذجية
    • [M1] يعوّض القيمتين في الدالة (1 نقطة)
    • [A1] يحصل على 1 و 7 (1 نقطة)
  2. (b)2 نقطة
    أوجد المشتقة \(f'(x)\) والنقطة الحرجة \(x_c\).
    معيار التصحيح والإجابة النموذجية
    • [M1] يشتق الدالة التربيعية (1 نقطة)
    • [A1] يحل لإيجاد النقطة الحرجة (1 نقطة)
  3. (c)2 نقطة
    احسب متوسط معدل التغيّر من \(x = 0\) إلى \(x = 2\).
    معيار التصحيح والإجابة النموذجية
    • [M1] يطبّق قانون متوسط معدل التغيّر (1 نقطة)
    • [A1] يبسّط الناتج إلى 3 (1 نقطة)
الحل الكامل التفصيلي
(أ) \(f(0)=1\) و \(f(2)=7\). (ب) \(f'(x)=6x-3\). (ج) متوسط معدل التغيّر يساوي \(3\).
📝 اختبار الفصل