{"id":7071,"date":"2026-09-12T15:30:10","date_gmt":"2026-09-12T20:30:10","guid":{"rendered":"https:\/\/ykim.synology.me\/wordpress\/ordinary-least-squares-7071\/"},"modified":"2026-11-25T16:55:50","modified_gmt":"2026-11-25T22:55:50","slug":"ordinary-least-squares","status":"publish","type":"post","link":"https:\/\/ykim.synology.me\/wordpress\/ordinary-least-squares-7071\/","title":{"rendered":"Ordinary Least Squares"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" src=\"https:\/\/github.com\/yBarn1\/Images\/blob\/main\/coupons\/cvs-vaccination-5-off-20-coupon-single.jpg?raw=true\" alt=\"\" style=\"width:auto;height:500px\"\/><\/figure>\n\n\n<p><div class=\"github-readme-container markdown-body\"><div id=\"file\" class=\"md\" data-path=\"Regression\/OLS\/ordinary-least-squares-ko.md\"><article class=\"markdown-body entry-content container-lg\" itemprop=\"text\"><div class=\"markdown-heading\" dir=\"auto\"><h1 class=\"heading-element\" dir=\"auto\">Ordinary Least Squares<\/h1><a id=\"user-content-ordinary-least-squares\" class=\"anchor\" aria-label=\"Permalink: Ordinary Least Squares\" href=\"#ordinary-least-squares\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Version 16 | Created: 2026-09-12 | Updated: 2026-11-07 13:46 UTC<\/p>\n<ul dir=\"auto\">\n<li><a href=\"#1-purpose\">1. Purpose<\/a><\/li>\n<li><a href=\"#2-summary\">2. Summary<\/a><\/li>\n<li><a href=\"#3-design-matrix\">3. Design Matrix<\/a><\/li>\n<li><a href=\"#4-least-squares-criterion\">4. Least Squares Criterion<\/a><\/li>\n<li><a href=\"#5-normal-equation\">5. Normal Equation<\/a><\/li>\n<li><a href=\"#6-fitting\">6. Fitting<\/a><\/li>\n<li><a href=\"#appendix-a-terminology\">Appendix A. Terminology<\/a><\/li>\n<li><a href=\"#appendix-b-computation\">Appendix B. Computation<\/a><\/li>\n<li><a href=\"#appendix-c-self-inner-product-of-a-vector\">Appendix C. Self Inner Product of a Vector<\/a><\/li>\n<li><a href=\"#appendix-d-expanding-the-residual-sum-of-squares\">Appendix D. Expanding the Residual Sum of Squares<\/a><\/li>\n<li><a href=\"#appendix-e-vector-derivatives-for-the-gradient\">Appendix E. Vector Derivatives for the Gradient<\/a><\/li>\n<\/ul>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">1. Purpose<\/h2><a id=\"user-content-1-purpose\" class=\"anchor\" aria-label=\"Permalink: 1. Purpose\" href=\"#1-purpose\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<ul dir=\"auto\">\n<li>\n<strong>Problem Statement<\/strong>: \uc120\ud615 \ud68c\uadc0\uc758 \uacc4\uc218\ub294 \uc790\ub8cc\uac00 \uc815\ud558\uc9c0\ub9cc, \uacc4\uc218\ub97c \uad00\uce21\uce58\ub9c8\ub2e4 \ub530\ub85c \ub9de\ucd94\uba74 \uad00\uce21\uce58 \uc218\ub9cc\ud07c\uc758 \uc2dd\uc774 \ub0a8\ub294\ub2e4. \uc790\ub8cc\uc640 \ud56d\uc744 \ud558\ub098\uc758 \ud589\ub82c\ub85c \uc138\uc6cc \ucd5c\uc18c\ud654 \ubb38\uc81c\ub97c \ud55c \ubc88\uc5d0 \ud480\uc5b4\uc57c \uacc4\uc218\uac00 \uc720\uc77c\ud558\uac8c \uc815\ud574\uc9c4\ub2e4.<\/li>\n<li>\n<strong>Goal<\/strong>: design matrix <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer> \uc640 \uc751\ub2f5 \ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uc5d0\uc11c normal equation \uc744 \uc720\ub3c4\ud558\uc5ec \ud68c\uadc0\uacc4\uc218 \ucd94\uc815\uac12 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\hat{\\beta}$<\/math-renderer> \ub97c \ub2eb\ud78c \ud615\ud0dc\ub85c \uad6c\ud558\uace0, \uadf8 \uacc4\uc0b0\uc744 NumPy \ub85c \uc7ac\ud604\ud55c\ub2e4.<\/li>\n<li>\n<strong>Non-Goal<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X$<\/math-renderer> \uc758 \uc5ed\ud589\ub82c\uc774 \uc5c6\uac70\ub098 \ubd88\uc548\uc815\ud560 \ub54c \uc4f0\ub294 regularization \uc740 \ub2e4\ub8e8\uc9c0 \uc54a\ub294\ub2e4.<\/li>\n<\/ul>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">2. Summary<\/h2><a id=\"user-content-2-summary\" class=\"anchor\" aria-label=\"Permalink: 2. Summary\" href=\"#2-summary\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\ud68c\uadc0 model \uc744 \uc801\ud569 (fitting) \ud55c\ub2e4\ub294 \uac83\uc740 design matrix <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer> \uc640 \uc751\ub2f5 \ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \ub85c\ubd80\ud130 \uc2dd (7) \uc758 \ud589\ub82c \uc5f0\uc0b0\uc73c\ub85c \ud68c\uadc0\uacc4\uc218 \ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\hat{\\beta}$<\/math-renderer> \ub97c \uc0b0\ucd9c\ud558\ub294 \uacfc\uc815\uc774\ub2e4. \uadf8 \uacc4\uc218\ub294 \uc794\ucc28 \uc81c\uacf1\ud569 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS$<\/math-renderer> \ub97c \uac00\uc7a5 \uc791\uac8c \ub9cc\ub4dc\ub294 \uac12\uc774\uba70, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS$<\/math-renderer> \ub97c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uc5d0 \ub300\ud574 \ubbf8\ubd84\ud558\uc5ec 0 \uc73c\ub85c \ub450\uba74 normal equation \uc774 \ub0a8\ub294\ub2e4. \ud574\ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X$<\/math-renderer> \uc758 \uc5ed\ud589\ub82c\uc774 \uc874\uc7ac\ud560 \ub54c \uc720\uc77c\ud558\uac8c \uacb0\uc815\ub41c\ub2e4.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">3. Design Matrix<\/h2><a id=\"user-content-3-design-matrix\" class=\"anchor\" aria-label=\"Permalink: 3. Design Matrix\" href=\"#3-design-matrix\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Design matrix <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer> \ub294 \uad00\uce21 \ub370\uc774\ud130\uc640 model \uc758 \ud56d (terms) \uc744 \uc120\ud615\ub300\uc218 \ud615\ud0dc\ub85c \ud45c\ud604\ud55c \ud589\ub82c\uc774\ub2e4.<\/p>\n<ul dir=\"auto\">\n<li>\n<strong>\ud589 (Rows)<\/strong>: \uac1c\ubcc4 \uad00\uce21\uce58 (observation) <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n$<\/math-renderer> \uac1c<\/li>\n<li>\n<strong>\uc5f4 (Columns)<\/strong>: model \uc774 \uc0ac\uc6a9\ud558\ub294 \uac01 \ud56d, \uc808\ud3b8 \ubc0f \ud2b9\uc131 (feature) <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$p + 1$<\/math-renderer> \uac1c<\/li>\n<\/ul>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$X =\n\\begin{bmatrix}\n1 &amp; x_{11} &amp; x_{12} &amp; \\dots &amp; x_{1p} \\\\\\\n1 &amp; x_{21} &amp; x_{22} &amp; \\dots &amp; x_{2p} \\\\\\\n\\vdots &amp; \\vdots &amp; \\vdots &amp; \\ddots &amp; \\vdots \\\\\\\n1 &amp; x_{n1} &amp; x_{n2} &amp; \\dots &amp; x_{np}\n\\end{bmatrix}\n\\hspace{19em} (1)$$<\/math-renderer>\n<p dir=\"auto\">\uccab \ubc88\uc9f8 \uc5f4\uc758 1 \uc740 \uc808\ud3b8 (intercept, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta_0$<\/math-renderer>) \uc5d0 \ub300\uc751\ud558\ub294 \ud56d\uc774\ub2e4.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">4. Least Squares Criterion<\/h2><a id=\"user-content-4-least-squares-criterion\" class=\"anchor\" aria-label=\"Permalink: 4. Least Squares Criterion\" href=\"#4-least-squares-criterion\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\uc120\ud615 \ud68c\uadc0 model \uc740 \ud589\ub82c\uc2dd\uc73c\ub85c \ud45c\ud604\ub41c\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$y = X\\beta + \\epsilon\n\\hspace{19em} (2)$$<\/math-renderer>\n<p dir=\"auto\">Table 1. Symbols of the matrix form<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Symbol<\/th>\n<th>Meaning<\/th>\n<th>Shape<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer><\/td>\n<td>\uc751\ub2f5 \ubcc0\uc218 \ubca1\ud130<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n \\times 1$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer><\/td>\n<td>Design matrix<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n \\times (p+1)$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer><\/td>\n<td>\ud68c\uadc0\uacc4\uc218 \ubca1\ud130<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(p+1) \\times 1$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\epsilon$<\/math-renderer><\/td>\n<td>\uc794\ucc28 (\uc624\ucc28) \ubca1\ud130<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n \\times 1$<\/math-renderer><\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<p dir=\"auto\">\ucd5c\uc18c\uc81c\uacf1\ubc95 (Ordinary Least Squares, OLS) \uc758 \ubaa9\uc801\uc740 \uc794\ucc28 \uc81c\uacf1\ud569 (Residual Sum of Squares, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS$<\/math-renderer>) \uc744 \uac00\uc7a5 \uc791\uac8c \ub9cc\ub4dc\ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \ub97c \ucc3e\ub294 \uac83\uc774\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$RSS(\\beta) = \\sum_{i=1}^{n} e_i^2 = \\epsilon^\\top \\epsilon = (y - X\\beta)^\\top (y - X\\beta)\n\\hspace{19em} (3)$$<\/math-renderer>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\epsilon^\\top \\epsilon$<\/math-renderer> \uc774 \uc131\ubd84\uc758 \uc81c\uacf1\ud569\uc774 \ub418\ub294 \uae4c\ub2ed\uc740 <a href=\"#appendix-c-self-inner-product-of-a-vector\">Appendix C<\/a> \uc5d0 \uc788\ub2e4.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">5. Normal Equation<\/h2><a id=\"user-content-5-normal-equation\" class=\"anchor\" aria-label=\"Permalink: 5. Normal Equation\" href=\"#5-normal-equation\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS(\\beta)$<\/math-renderer> \ub97c \ucd5c\uc18c\ud654\ud558\ub294 \uc810\uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uc5d0 \ub300\ud55c \ubbf8\ubd84\uc774 0 \uc774 \ub418\ub294 \uc9c0\uc810\uc774\ub2e4. \uc2dd (3) \uc744 \uc804\uac1c\ud558\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uc758 \uc774\ucc28\uc2dd\uc774 \ub418\uace0, \uadf8 \uacbd\uc0ac\ub3c4\ub97c 0 \uc73c\ub85c \ub450\uba74 \uc5f0\ub9bd\ubc29\uc815\uc2dd \ud558\ub098\uac00 \ub0a8\ub294\ub2e4.<\/p>\n<p dir=\"auto\">\uc2dd (3) \uc758 \uc804\uac1c\ub294 \ub2e4\uc74c\uacfc \uac19\uc73c\uba70, \uadf8 \uacfc\uc815\uc740 <a href=\"#appendix-d-expanding-the-residual-sum-of-squares\">Appendix D<\/a> \uc5d0 \uc788\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$RSS(\\beta) = y^\\top y - 2\\beta^\\top X^\\top y + \\beta^\\top X^\\top X \\beta\n\\hspace{19em} (4)$$<\/math-renderer>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uc5d0 \ub300\ud574 \ubbf8\ubd84\ud558\uc5ec 0 \uc73c\ub85c \uc124\uc815\ud55c\ub2e4. \uc5ec\uae30\uc5d0 \uc4f4 \ubca1\ud130 \ubbf8\ubd84 \uaddc\uce59\uc740 <a href=\"#appendix-e-vector-derivatives-for-the-gradient\">Appendix E<\/a> \uc5d0 \uc788\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\frac{\\partial RSS}{\\partial \\beta} = -2X^\\top y + 2X^\\top X\\beta = 0\n\\hspace{19em} (5)$$<\/math-renderer>\n<p dir=\"auto\">\uc2dd (5) \ub97c \uc815\ub9ac\ud55c \uac83\uc774 normal equation \uc774\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$X^\\top X \\beta = X^\\top y\n\\hspace{19em} (6)$$<\/math-renderer>\n<p dir=\"auto\">\uc774\ub984\uc758 normal \uc740 \uc9c1\uad50\ub97c \ub73b\ud558\uba70, \uc2dd (6) \uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top (y - X\\beta) = 0$<\/math-renderer> \uacfc \uac19\uc740 \ub9d0, \uace7 \uc794\ucc28\uac00 design matrix \uc758 \ubaa8\ub4e0 \uc5f4\uacfc \uc9c1\uad50\ud55c\ub2e4\ub294 \uc870\uac74\uc774\ub2e4. \uc9c1\uad50\ud558\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X\\beta$<\/math-renderer> \ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer> \uc758 \uc5f4\ub4e4\uc774 \ub9cc\ub4dc\ub294 \uacf5\uac04 (column space) \uc548\uc5d0\uc11c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uc5d0 \uac00\uc7a5 \uac00\uae4c\uc6b4 \uc810, \uace7 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uc5d0\uc11c \uadf8 \uacf5\uac04\uc5d0 \ub0b4\ub9b0 \uc218\uc120 (perpendicular) \uc758 \ubc1c (\uc815\uc0ac\uc601, orthogonal projection) \uc774 \ub418\uace0, \ub2e4\ub978 \uc5b4\ub5a4 \uacc4\uc218\ub97c \ub123\uc5b4\ub3c4 \uc794\ucc28\ub294 \uadf8\ubcf4\ub2e4 \uae38\uc5b4\uc9c4\ub2e4. \uc794\ucc28 \uc81c\uacf1\ud569\uc774 \ucd5c\uc18c\uac00 \ub418\ub294 \uc790\ub9ac\uac00 \ubc14\ub85c \uc774 \uc9c0\uc810\uc774\ub2e4.<\/p>\n<p dir=\"auto\">\uc774 \uc870\uac74\uc744 \uadf8\ub9bc\uc73c\ub85c \uc62e\uae34 \uac83\uc774 Fig 1 \uc774\ub2e4.<\/p>\n<div class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"                            y\n                           \/|\n                          \/ |\n                         \/  |\n                        \/   |  residual\n                       \/    |  (perpendicular to the column space)\n                      \/     |\n        O -----------+------+----------------------  column space\n                            X beta\n                            (foot of the perpendicular)\"><pre lang=\"text\" class=\"notranslate\"><code>                            y\n                           \/|\n                          \/ |\n                         \/  |\n                        \/   |  residual\n                       \/    |  (perpendicular to the column space)\n                      \/     |\n        O -----------+------+----------------------  column space\n                            X beta\n                            (foot of the perpendicular)\n<\/code><\/pre><\/div>\n<p dir=\"auto\">Fig 1. The residual as the perpendicular from y to the column space<\/p>\n<ul dir=\"auto\">\n<li>\n<strong>\ud3c9\uba74<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X\\beta$<\/math-renderer> \ub85c \ub9cc\ub4e4 \uc218 \uc788\ub294 \ubaa8\ub4e0 \ubca1\ud130\uac00 \ub193\uc774\ub294 \uc790\ub9ac\uc778 \uc5f4\uacf5\uac04.<\/li>\n<li>\n<strong>\uc218\uc120 (perpendicular)<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uc5d0\uc11c \uadf8 \ud3c9\uba74\uc5d0 90\u00b0 \ub85c \ub0b4\ub9b0 \uc120\ubd84. \uadf8 \uae38\uc774\uc758 \uc81c\uacf1\uc774 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS$<\/math-renderer> \uc774\ub2e4.<\/li>\n<li>\n<strong>\uc218\uc120\uc758 \ubc1c<\/strong>: \ud3c9\uba74 \uc704\uc5d0\uc11c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uc5d0 \uac00\uc7a5 \uac00\uae4c\uc6b4 \uc810 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X\\beta$<\/math-renderer>. \ud3c9\uba74 \uc704\uc758 \ub2e4\ub978 \uc810\uc744 \uace0\ub974\uba74 \ube57\ubcc0\uc774 \ub418\uc5b4 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y$<\/math-renderer> \uae4c\uc9c0\uc758 \uac70\ub9ac\uac00 \uae38\uc5b4\uc9c4\ub2e4.<\/li>\n<\/ul>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">6. Fitting<\/h2><a id=\"user-content-6-fitting\" class=\"anchor\" aria-label=\"Permalink: 6. Fitting\" href=\"#6-fitting\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">Model \uc744 \uc801\ud569 (fit) \ud55c\ub2e4\ub294 \uac83\uc740 \uc790\ub8cc\uac00 \uc9c0\uc2dc\ud558\ub294 \ucd5c\uc801\uc758 \uacc4\uc218 \uac12 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\hat{\\beta}$<\/math-renderer> \ub97c \uad6c\ud558\ub294 \uacfc\uc815\uc774\uba70, \uc774\ub294 \uc2dd (6) \uc758 normal equation \uc744 \ud478\ub294 \ubb38\uc81c\uc640 \uac19\ub2e4.<\/p>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X$<\/math-renderer> \uc758 \uc5ed\ud589\ub82c\uc774 \uc874\uc7ac\ud560 \ub54c \ud574 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\hat{\\beta}$<\/math-renderer> \ub294 \uc720\uc77c\ud558\uac8c \uacb0\uc815\ub418\uba70, \uadf8 \ub2eb\ud78c \ud615\ud0dc\uc778 \uc2dd (7) \uc774 \uc774 \ubb38\uc11c\uc758 \uacb0\ub860\uc774\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\hat{\\beta} = (X^\\top X)^{-1} X^\\top y\n\\hspace{19em} (7)$$<\/math-renderer>\n<p dir=\"auto\">\uc2dd (7) \uc758 \uacc4\uc0b0\uc740 <a href=\"#appendix-b-computation\">Appendix B<\/a> \uc5d0 \uc788\ub2e4.<\/p>\n<hr>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix A. Terminology<\/h2><a id=\"user-content-appendix-a-terminology\" class=\"anchor\" aria-label=\"Permalink: Appendix A. Terminology\" href=\"#appendix-a-terminology\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<ul dir=\"auto\">\n<li>\n<strong>column space<\/strong>: Design matrix \uc758 \uc5f4\ub4e4\uc774 \ub9cc\ub4dc\ub294 \ubca1\ud130 \uacf5\uac04.<\/li>\n<li>\n<strong>design matrix<\/strong>: \uad00\uce21\uce58\ub97c \ud589\uc73c\ub85c, model \uc758 \ud56d\uc744 \uc5f4\ub85c \ub193\uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n \\times (p+1)$<\/math-renderer> \ud589\ub82c.<\/li>\n<li>\n<strong>fitting<\/strong>: \uc790\ub8cc\uac00 \uc9c0\uc2dc\ud558\ub294 \ucd5c\uc801\uc758 \uacc4\uc218 \uac12\uc744 \uad6c\ud558\ub294 \uacfc\uc815.<\/li>\n<li>\n<strong>intercept<\/strong>: Design matrix \uc758 1 \ub85c \ucc44\uc6cc\uc9c4 \uccab \uc5f4\uc5d0 \ub300\uc751\ud558\ub294 \uacc4\uc218 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta_0$<\/math-renderer>.<\/li>\n<li>\n<strong>L2 norm<\/strong>: \ubca1\ud130 \uc6d0\uc18c\uc758 \uc81c\uacf1\ud569\uc758 \uc81c\uacf1\uadfc.<\/li>\n<li>\n<strong>linear form<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta^\\top a$<\/math-renderer> \ucc98\ub7fc \uacc4\uc218\uc758 1 \ucc28 \ud56d\ub9cc\uc73c\ub85c \uc774\ub8e8\uc5b4\uc9c4 \uc2a4\uce7c\ub77c \ud568\uc218.<\/li>\n<li>\n<strong>normal equation<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$RSS$<\/math-renderer> \uc758 \uacbd\uc0ac\ub3c4\ub97c 0 \uc73c\ub85c \ub450\uc5b4 \uc5bb\uc740 \uc5f0\ub9bd\ubc29\uc815\uc2dd <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X \\beta = X^\\top y$<\/math-renderer>.<\/li>\n<li>\n<strong>orthogonal projection<\/strong>: \ud55c \ubca1\ud130\uc5d0\uc11c \uc5b4\ub5a4 \uacf5\uac04\uc5d0 \ub0b4\ub9b0 \uc218\uc120\uc758 \ubc1c. \uadf8 \uacf5\uac04 \uc548\uc5d0\uc11c \uc6d0\ub798 \ubca1\ud130\uc5d0 \uac00\uc7a5 \uac00\uae4c\uc6b4 \uc810\uc774\ub2e4.<\/li>\n<li>\n<strong>perpendicular<\/strong>: \ud55c \uc810\uc5d0\uc11c \uc9c1\uc120\uc774\ub098 \ud3c9\uba74\uc5d0 90\u00b0 \ub85c \ub0b4\ub9ac\uadf8\uc740 \uc120\ubd84. \uadf8\uac83\uc774 \ub2ff\ub294 \uc810\uc774 \uc218\uc120\uc758 \ubc1c\uc774\ub2e4.<\/li>\n<li>\n<strong>quadratic form<\/strong>: <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta^\\top A \\beta$<\/math-renderer> \ucc98\ub7fc \uacc4\uc218\uc758 2 \ucc28 \ud56d\uc73c\ub85c \uc774\ub8e8\uc5b4\uc9c4 \uc2a4\uce7c\ub77c \ud568\uc218.<\/li>\n<li>\n<strong>residual<\/strong>: \uad00\uce21\uac12\uacfc model \uc608\uce21\uac12\uc758 \ucc28\uc774.<\/li>\n<li>\n<strong>RSS<\/strong>: Residual Sum of Squares. \uc794\ucc28\uc758 \uc81c\uacf1\ud569.<\/li>\n<li>\n<strong>transpose<\/strong>: \ud589\uacfc \uc5f4\uc744 \ub9de\ubc14\uafbc \ud589\ub82c. \ud569\uc5d0\uc11c\ub294 \uadf8\ub300\ub85c, \uacf1\uc5d0\uc11c\ub294 \ucc28\ub840\uac00 \ub4a4\uc9d1\ud78c\ub2e4.<\/li>\n<\/ul>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix B. Computation<\/h2><a id=\"user-content-appendix-b-computation\" class=\"anchor\" aria-label=\"Permalink: Appendix B. Computation\" href=\"#appendix-b-computation\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\uc2dd (7) \uc740 NumPy \uc758 \ud589\ub82c \uc5f0\uc0b0\uc73c\ub85c \uadf8\ub300\ub85c \uc62e\uaca8\uc9c4\ub2e4. \uc544\ub798 code \ub294 \uacc4\uc218\ub97c \uc54c\uace0 \uc788\ub294 \uac00\uc0c1 \ub370\uc774\ud130\ub97c \ub9cc\ub4e4\uace0, design matrix \ub97c \uc138\uc6b0\uace0, normal equation \uc73c\ub85c \uad6c\ud55c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\hat{\\beta}$<\/math-renderer> \ub97c scikit-learn \uc758 <code>LinearRegression<\/code> \uacb0\uacfc\uc640 \ub300\uc870\ud55c\ub2e4.<\/p>\n<div class=\"highlight highlight-source-python notranslate position-relative overflow-auto\" dir=\"auto\" data-snippet-clipboard-copy-content=\"# Python\nimport numpy as np\nfrom sklearn.linear_model import LinearRegression\n\n# 1. Generate synthetic data (y = 3 + 2*x1 + 5*x2 + noise)\nnp.random.seed(42)\nn_samples = 100\n\nx1 = np.random.rand(n_samples, 1) * 10\nx2 = np.random.rand(n_samples, 1) * 5\nnoise = np.random.randn(n_samples, 1)\n\n# True coefficients: beta_0 = 3, beta_1 = 2, beta_2 = 5\ny = 3 + 2 * x1 + 5 * x2 + noise\n\n# 2. Build the design matrix X\n# Prepend a column of ones for the intercept (bias) term\nX_raw = np.hstack([x1, x2])\nones = np.ones((n_samples, 1))\nX = np.hstack([ones, X_raw])\n\nprint(f&quot;Shape of design matrix X: {X.shape}&quot;)  # (100, 3)\nprint(&quot;First 3 rows of the design matrix:\\n&quot;, X[:3])\nprint(&quot;-&quot; * 50)\n\n# 3. Solve for beta with the normal equation\n# beta_hat = (X^T * X)^(-1) * X^T * y\n\n# Method A: direct inverse with np.linalg.inv\nX_transpose = X.T\nbeta_hat_inv = np.linalg.inv(X_transpose @ X) @ X_transpose @ y\n\n# Method B: linear solve, preferred for numerical stability\n# (X^T * X) * beta = X^T * y\nbeta_hat_solve = np.linalg.solve(X_transpose @ X, X_transpose @ y)\n\nprint(&quot;Normal equation (direct inverse):&quot;)\nprint(f&quot;  intercept (beta_0): {beta_hat_inv[0][0]:.4f}&quot;)\nprint(f&quot;  coefficient (beta_1): {beta_hat_inv[1][0]:.4f}&quot;)\nprint(f&quot;  coefficient (beta_2): {beta_hat_inv[2][0]:.4f}&quot;)\nprint(&quot;-&quot; * 50)\n\n# 4. Cross-check against scikit-learn LinearRegression\nmodel = LinearRegression()\nmodel.fit(X_raw, y)\n\nprint(&quot;scikit-learn fit:&quot;)\nprint(f&quot;  intercept: {model.intercept_[0]:.4f}&quot;)\nprint(f&quot;  coefficients: {model.coef_[0]}&quot;)\"><pre><span class=\"pl-c\"># Python<\/span>\n<span class=\"pl-k\">import<\/span> <span class=\"pl-s1\">numpy<\/span> <span class=\"pl-k\">as<\/span> <span class=\"pl-s1\">np<\/span>\n<span class=\"pl-k\">from<\/span> <span class=\"pl-s1\">sklearn<\/span>.<span class=\"pl-s1\">linear_model<\/span> <span class=\"pl-k\">import<\/span> <span class=\"pl-v\">LinearRegression<\/span>\n\n<span class=\"pl-c\"># 1. Generate synthetic data (y = 3 + 2*x1 + 5*x2 + noise)<\/span>\n<span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">random<\/span>.<span class=\"pl-c1\">seed<\/span>(<span class=\"pl-c1\">42<\/span>)\n<span class=\"pl-s1\">n_samples<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-c1\">100<\/span>\n\n<span class=\"pl-s1\">x1<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">random<\/span>.<span class=\"pl-c1\">rand<\/span>(<span class=\"pl-s1\">n_samples<\/span>, <span class=\"pl-c1\">1<\/span>) <span class=\"pl-c1\">*<\/span> <span class=\"pl-c1\">10<\/span>\n<span class=\"pl-s1\">x2<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">random<\/span>.<span class=\"pl-c1\">rand<\/span>(<span class=\"pl-s1\">n_samples<\/span>, <span class=\"pl-c1\">1<\/span>) <span class=\"pl-c1\">*<\/span> <span class=\"pl-c1\">5<\/span>\n<span class=\"pl-s1\">noise<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">random<\/span>.<span class=\"pl-c1\">randn<\/span>(<span class=\"pl-s1\">n_samples<\/span>, <span class=\"pl-c1\">1<\/span>)\n\n<span class=\"pl-c\"># True coefficients: beta_0 = 3, beta_1 = 2, beta_2 = 5<\/span>\n<span class=\"pl-s1\">y<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-c1\">3<\/span> <span class=\"pl-c1\">+<\/span> <span class=\"pl-c1\">2<\/span> <span class=\"pl-c1\">*<\/span> <span class=\"pl-s1\">x1<\/span> <span class=\"pl-c1\">+<\/span> <span class=\"pl-c1\">5<\/span> <span class=\"pl-c1\">*<\/span> <span class=\"pl-s1\">x2<\/span> <span class=\"pl-c1\">+<\/span> <span class=\"pl-s1\">noise<\/span>\n\n<span class=\"pl-c\"># 2. Build the design matrix X<\/span>\n<span class=\"pl-c\"># Prepend a column of ones for the intercept (bias) term<\/span>\n<span class=\"pl-v\">X_raw<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">hstack<\/span>(&#91;<span class=\"pl-s1\">x1<\/span>, <span class=\"pl-s1\">x2<\/span>&#93;)\n<span class=\"pl-s1\">ones<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">ones<\/span>((<span class=\"pl-s1\">n_samples<\/span>, <span class=\"pl-c1\">1<\/span>))\n<span class=\"pl-c1\">X<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">hstack<\/span>(&#91;<span class=\"pl-s1\">ones<\/span>, <span class=\"pl-v\">X_raw<\/span>&#93;)\n\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"Shape of design matrix X: <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-c1\">X<\/span>.<span class=\"pl-c1\">shape<\/span><span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)  <span class=\"pl-c\"># (100, 3)<\/span>\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">\"First 3 rows of the design matrix:<span class=\"pl-cce\">\\n<\/span>\"<\/span>, <span class=\"pl-c1\">X<\/span>&#91;:<span class=\"pl-c1\">3<\/span>&#93;)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">\"-\"<\/span> <span class=\"pl-c1\">*<\/span> <span class=\"pl-c1\">50<\/span>)\n\n<span class=\"pl-c\"># 3. Solve for beta with the normal equation<\/span>\n<span class=\"pl-c\"># beta_hat = (X^T * X)^(-1) * X^T * y<\/span>\n\n<span class=\"pl-c\"># Method A: direct inverse with np.linalg.inv<\/span>\n<span class=\"pl-v\">X_transpose<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-c1\">X<\/span>.<span class=\"pl-c1\">T<\/span>\n<span class=\"pl-s1\">beta_hat_inv<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">linalg<\/span>.<span class=\"pl-c1\">inv<\/span>(<span class=\"pl-v\">X_transpose<\/span> @ <span class=\"pl-c1\">X<\/span>) @ <span class=\"pl-v\">X_transpose<\/span> @ <span class=\"pl-s1\">y<\/span>\n\n<span class=\"pl-c\"># Method B: linear solve, preferred for numerical stability<\/span>\n<span class=\"pl-c\"># (X^T * X) * beta = X^T * y<\/span>\n<span class=\"pl-s1\">beta_hat_solve<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-s1\">np<\/span>.<span class=\"pl-c1\">linalg<\/span>.<span class=\"pl-c1\">solve<\/span>(<span class=\"pl-v\">X_transpose<\/span> @ <span class=\"pl-c1\">X<\/span>, <span class=\"pl-v\">X_transpose<\/span> @ <span class=\"pl-s1\">y<\/span>)\n\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">\"Normal equation (direct inverse):\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"  intercept (beta_0): <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-s1\">beta_hat_inv<\/span>&#91;<span class=\"pl-c1\">0<\/span>&#93;&#91;<span class=\"pl-c1\">0<\/span>&#93;:.4f<span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"  coefficient (beta_1): <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-s1\">beta_hat_inv<\/span>&#91;<span class=\"pl-c1\">1<\/span>&#93;&#91;<span class=\"pl-c1\">0<\/span>&#93;:.4f<span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"  coefficient (beta_2): <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-s1\">beta_hat_inv<\/span>&#91;<span class=\"pl-c1\">2<\/span>&#93;&#91;<span class=\"pl-c1\">0<\/span>&#93;:.4f<span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">\"-\"<\/span> <span class=\"pl-c1\">*<\/span> <span class=\"pl-c1\">50<\/span>)\n\n<span class=\"pl-c\"># 4. Cross-check against scikit-learn LinearRegression<\/span>\n<span class=\"pl-s1\">model<\/span> <span class=\"pl-c1\">=<\/span> <span class=\"pl-en\">LinearRegression<\/span>()\n<span class=\"pl-s1\">model<\/span>.<span class=\"pl-c1\">fit<\/span>(<span class=\"pl-v\">X_raw<\/span>, <span class=\"pl-s1\">y<\/span>)\n\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">\"scikit-learn fit:\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"  intercept: <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-s1\">model<\/span>.<span class=\"pl-c1\">intercept_<\/span>&#91;<span class=\"pl-c1\">0<\/span>&#93;:.4f<span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)\n<span class=\"pl-en\">print<\/span>(<span class=\"pl-s\">f\"  coefficients: <span class=\"pl-s1\"><span class=\"pl-kos\">{<\/span><span class=\"pl-s1\">model<\/span>.<span class=\"pl-c1\">coef_<\/span>&#91;<span class=\"pl-c1\">0<\/span>&#93;<span class=\"pl-kos\">}<\/span><\/span>\"<\/span>)<\/pre><\/div>\n<p dir=\"auto\">NumPy 2.4.6 \uacfc scikit-learn 1.9.1 \uc5d0\uc11c \uc2e4\ud589\ud55c \uacb0\uacfc\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<div class=\"snippet-clipboard-content notranslate position-relative overflow-auto\" data-snippet-clipboard-copy-content=\"Shape of design matrix X: (100, 3)\nFirst 3 rows of the design matrix:\n [[1.         3.74540119 0.15714593]\n [1.         9.50714306 3.18205206]\n [1.         7.31993942 1.57177991]]\n--------------------------------------------------\nNormal equation (direct inverse):\n  intercept (beta_0): 2.9106\n  coefficient (beta_1): 1.9658\n  coefficient (beta_2): 5.1439\n--------------------------------------------------\nscikit-learn fit:\n  intercept: 2.9106\n  coefficients: [1.96582747 5.14386228]\"><pre lang=\"text\" class=\"notranslate\"><code>Shape of design matrix X: (100, 3)\nFirst 3 rows of the design matrix:\n &#91;&#91;1.         3.74540119 0.15714593&#93;\n &#91;1.         9.50714306 3.18205206&#93;\n &#91;1.         7.31993942 1.57177991&#93;&#93;\n--------------------------------------------------\nNormal equation (direct inverse):\n  intercept (beta_0): 2.9106\n  coefficient (beta_1): 1.9658\n  coefficient (beta_2): 5.1439\n--------------------------------------------------\nscikit-learn fit:\n  intercept: 2.9106\n  coefficients: &#91;1.96582747 5.14386228&#93;\n<\/code><\/pre><\/div>\n<p dir=\"auto\">\uc815\uaddc\ubc29\uc815\uc2dd\uc73c\ub85c \uad6c\ud55c \uacc4\uc218\uc640 <code>LinearRegression<\/code> \uc774 \uc801\ud569\ud55c \uacc4\uc218\ub294 \uc18c\uc218 \ub137\uc9f8 \uc790\ub9ac\uae4c\uc9c0 \uac19\ub2e4. \ub458 \ub2e4 \ucc38\uac12 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(3, 2, 5)$<\/math-renderer> \uc5d0\uc11c \uc870\uae08 \ubc97\uc5b4\ub098 \uc788\ub294\ub370, code \uac00 \uc751\ub2f5\uc5d0 \ub354\ud55c noise \ub54c\ubb38\uc774\ub2e4.<\/p>\n<p dir=\"auto\">Code \uc5d0\uc11c \uc9da\uc744 \uacf3\uc740 \uc138 \uad70\ub370\ub2e4.<\/p>\n<ul dir=\"auto\">\n<li>\n<code>np.hstack(&#91;ones, X_raw&#93;)<\/code>: \uc6d0\ubcf8 \ud2b9\uc131 \ub370\uc774\ud130 \uc55e\uc5d0 1 \ub85c \ucc44\uc6cc\uc9c4 \uc5f4\uc744 \ubd99\uc5ec \ub9cc\ub4e0 design matrix <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X$<\/math-renderer>. \uadf8 \uccab \ubc88\uc9f8 \uc5f4\uc774 \uc808\ud3b8 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta_0$<\/math-renderer> \uc640 \uacf1\ud574\uc9c0\ub294 \ud56d.<\/li>\n<li>\n<code>@<\/code> \uc5f0\uc0b0\uc790: NumPy \uc758 \ud589\ub82c \uacf1\uc148 (matrix multiplication). <code>X.T @ X<\/code> \ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X$<\/math-renderer>.<\/li>\n<li>\n<code>np.linalg.solve<\/code> \uc640 <code>np.linalg.inv<\/code>: \uc5f0\ub9bd\ubc29\uc815\uc2dd\uc744 \ud478\ub294 \uacc4\uc0b0\uacfc <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(X^\\top X)^{-1}$<\/math-renderer> \uc5ed\ud589\ub82c\uc744 \uc9c1\uc811 \uad6c\ud558\ub294 \uacc4\uc0b0. Numerical \uc624\ucc28\uc640 \uc18d\ub3c4 \uce21\uba74\uc5d0\uc11c <code>np.linalg.solve(A, b)<\/code> \ud615\ud0dc\uac00 \uc548\uc815\uc801.<\/li>\n<\/ul>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix C. Self Inner Product of a Vector<\/h2><a id=\"user-content-appendix-c-self-inner-product-of-a-vector\" class=\"anchor\" aria-label=\"Permalink: Appendix C. Self Inner Product of a Vector\" href=\"#appendix-c-self-inner-product-of-a-vector\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\uc5f4\ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v$<\/math-renderer> \ub97c \uc790\uae30 \uc790\uc2e0\uacfc \uacf1\ud55c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v^\\top v$<\/math-renderer> \ub294 \uc131\ubd84 \uc81c\uacf1\uc758 \ud569\uc778 \uc2a4\uce7c\ub77c\ub2e4.<\/p>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v$<\/math-renderer> \uac00 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$n \\times 1$<\/math-renderer> \uc5f4\ubca1\ud130\uc774\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v^\\top$<\/math-renderer> \ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$1 \\times n$<\/math-renderer> \ud589\ubca1\ud130\uc774\uace0, \ud589\ub82c \uacf1 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(1 \\times n)(n \\times 1)$<\/math-renderer> \uc758 \uacb0\uacfc\ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$1 \\times 1$<\/math-renderer> \uc774\ub2e4. \ud589\ub82c \uacf1\uc758 \uc815\uc758\uc5d0 \ub530\ub77c \uadf8 \ud558\ub098\ubfd0\uc778 \uc131\ubd84\uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v^\\top$<\/math-renderer> \uc758 \uac01 \uc131\ubd84\uacfc <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v$<\/math-renderer> \uc758 \uac19\uc740 \uc790\ub9ac \uc131\ubd84\uc744 \uacf1\ud574 \ub354\ud55c \uac12\uc774\uba70, \ub450 \ubca1\ud130\uac00 \uac19\uc73c\ubbc0\ub85c \uac01 \ud56d\uc774 \uc81c\uacf1\uc774 \ub41c\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$v^\\top v =\n\\begin{bmatrix} v_1 &amp; v_2 &amp; \\dots &amp; v_n \\end{bmatrix}\n\\begin{bmatrix} v_1 \\\\ v_2 \\\\ \\vdots \\\\ v_n \\end{bmatrix}\n= v_1^2 + v_2^2 + \\dots + v_n^2 = \\sum_{i=1}^{n} v_i^2\n\\hspace{19em} (8)$$<\/math-renderer>\n<p dir=\"auto\">\uc774 \uac12\uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v$<\/math-renderer> \uc758 L2 norm \uc758 \uc81c\uacf1 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$|v|_2^2$<\/math-renderer> \uc774\uba70, \ubaa8\ub4e0 \uc131\ubd84\uc774 0 \uc77c \ub54c\ub9cc 0 \uc774\uace0 \uadf8 \ubc16\uc5d0\ub294 \uc5b8\uc81c\ub098 \uc591\uc218\ub2e4.<\/p>\n<p dir=\"auto\">\uc2dd (3) \uc758 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\epsilon^\\top \\epsilon$<\/math-renderer> \uc740 \uc2dd (8) \uc758 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$v$<\/math-renderer> \uc790\ub9ac\uc5d0 \uc794\ucc28 \ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\epsilon$<\/math-renderer> \uc744 \ub123\uc740 \uac83\uc774\ubbc0\ub85c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\sum e_i^2$<\/math-renderer> \uacfc \uac19\ub2e4.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix D. Expanding the Residual Sum of Squares<\/h2><a id=\"user-content-appendix-d-expanding-the-residual-sum-of-squares\" class=\"anchor\" aria-label=\"Permalink: Appendix D. Expanding the Residual Sum of Squares\" href=\"#appendix-d-expanding-the-residual-sum-of-squares\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\uc2dd (4) \ub294 \uc2dd (3) \uc758 \uacf1\uc744 \ud480\uc5b4 \uc4f4 \uac83\uc774\uba70, \uc804\uce58\uc758 \ub450 \uaddc\uce59\uacfc \uc2a4\uce7c\ub77c\uc758 \uc131\uc9c8 \ud558\ub098\ub97c \uc4f4\ub2e4.<\/p>\n<p dir=\"auto\">\uc804\uce58\ub294 \ud569\uc744 \uadf8\ub300\ub85c \ub530\ub77c\uac00\uace0 (<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(A + B)^\\top = A^\\top + B^\\top$<\/math-renderer>), \uacf1\uc5d0\uc11c\ub294 \ucc28\ub840\ub97c \ub4a4\uc9d1\ub294\ub2e4 (<math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(AB)^\\top = B^\\top A^\\top$<\/math-renderer>). \uc774\ub97c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(y - X\\beta)$<\/math-renderer> \uc5d0 \uc801\uc6a9\ud55c\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$(y - X\\beta)^\\top = y^\\top - \\beta^\\top X^\\top\n\\hspace{19em} (9)$$<\/math-renderer>\n<p dir=\"auto\">\uc2dd (9) \ub97c \uc2dd (3) \uc5d0 \ub123\uace0 \ub450 \uad04\ud638\ub97c \ubd84\ubc30\ud558\uba74 \ud56d\uc774 \ub137 \ub098\uc628\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$(y - X\\beta)^\\top (y - X\\beta) = y^\\top y - y^\\top X\\beta - \\beta^\\top X^\\top y + \\beta^\\top X^\\top X \\beta\n\\hspace{19em} (10)$$<\/math-renderer>\n<p dir=\"auto\">\uac00\uc6b4\ub370 \ub450 \ud56d\uc740 \uac19\uc740 \uac12\uc774\ub2e4. <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y^\\top X\\beta$<\/math-renderer> \ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(1 \\times n)(n \\times (p+1))((p+1) \\times 1)$<\/math-renderer> \uc758 \uacf1\uc774\ub77c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$1 \\times 1$<\/math-renderer> \uc2a4\uce7c\ub77c\uc774\uace0, \uc2a4\uce7c\ub77c\ub294 \uc804\uce58\ud574\ub3c4 \uc790\uae30 \uc790\uc2e0\uc774\ubbc0\ub85c \uc804\uce58\ub97c \ucde8\ud574 \uc21c\uc11c\ub97c \ub4a4\uc9d1\uc5b4\ub3c4 \uac12\uc774 \ubc14\ub00c\uc9c0 \uc54a\ub294\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$y^\\top X\\beta = (y^\\top X\\beta)^\\top = \\beta^\\top X^\\top y\n\\hspace{19em} (11)$$<\/math-renderer>\n<p dir=\"auto\">\uc2dd (11) \ub85c \uac00\uc6b4\ub370 \ub450 \ud56d\uc744 \ud569\uce58\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$-2\\beta^\\top X^\\top y$<\/math-renderer> \uac00 \ub418\uace0, \uc2dd (10) \uc740 \uc2dd (4) \uac00 \ub41c\ub2e4.<\/p>\n<div class=\"markdown-heading\" dir=\"auto\"><h2 class=\"heading-element\" dir=\"auto\">Appendix E. Vector Derivatives for the Gradient<\/h2><a id=\"user-content-appendix-e-vector-derivatives-for-the-gradient\" class=\"anchor\" aria-label=\"Permalink: Appendix E. Vector Derivatives for the Gradient\" href=\"#appendix-e-vector-derivatives-for-the-gradient\"><svg data-component=\"Octicon\" class=\"octicon octicon-link\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" height=\"16\" aria-hidden=\"true\"><path d=\"m7.775 3.275 1.25-1.25a3.5 3.5 0 1 1 4.95 4.95l-2.5 2.5a3.5 3.5 0 0 1-4.95 0 .751.751 0 0 1 .018-1.042.751.751 0 0 1 1.042-.018 1.998 1.998 0 0 0 2.83 0l2.5-2.5a2.002 2.002 0 0 0-2.83-2.83l-1.25 1.25a.751.751 0 0 1-1.042-.018.751.751 0 0 1-.018-1.042Zm-4.69 9.64a1.998 1.998 0 0 0 2.83 0l1.25-1.25a.751.751 0 0 1 1.042.018.751.751 0 0 1 .018 1.042l-1.25 1.25a3.5 3.5 0 1 1-4.95-4.95l2.5-2.5a3.5 3.5 0 0 1 4.95 0 .751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018 1.998 1.998 0 0 0-2.83 0l-2.5 2.5a1.998 1.998 0 0 0 0 2.83Z\"><\/path><\/svg><\/a><\/div>\n<p dir=\"auto\">\uc2dd (5) \ub294 \uc138 \uac00\uc9c0 \uaddc\uce59\uc5d0\uc11c \ub098\uc628\ub2e4. \uc0c1\uc218\ud56d\uc758 \ubbf8\ubd84, \uc77c\ucc28\ud615 (linear form) \uc758 \ubbf8\ubd84, \uc774\ucc28\ud615 (quadratic form) \uc758 \ubbf8\ubd84\uc774\uba70, \uc5ec\uae30\uc11c \ubbf8\ubd84\uc740 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uc758 \uac01 \uc131\ubd84\uc5d0 \ub300\ud55c \ud3b8\ubbf8\ubd84\uc744 \ubaa8\uc544 \ub193\uc740 \uc5f4\ubca1\ud130\ub97c \ub73b\ud55c\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\frac{\\partial f}{\\partial \\beta} =\n\\begin{bmatrix}\n\\partial f \/ \\partial \\beta_0 \\\\\\\n\\vdots \\\\\\\n\\partial f \/ \\partial \\beta_p\n\\end{bmatrix}\n\\hspace{19em} (12)$$<\/math-renderer>\n<p dir=\"auto\">\uccab\uc9f8, \uc0c1\uc218\uc758 \ubbf8\ubd84\uc740 0 \uc774\ub2e4. <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y^\\top y$<\/math-renderer> \uc5d0\ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta$<\/math-renderer> \uac00 \ub4e4\uc5b4 \uc788\uc9c0 \uc54a\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\frac{\\partial (y^\\top y)}{\\partial \\beta} = 0\n\\hspace{19em} (13)$$<\/math-renderer>\n<p dir=\"auto\">\ub458\uc9f8, \uc77c\ucc28\ud615 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta^\\top a$<\/math-renderer> \ub294 \uc131\ubd84\uc73c\ub85c \uc4f0\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\sum_j \\beta_j a_j$<\/math-renderer> \uc774\ubbc0\ub85c, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta_k$<\/math-renderer> \ub85c \ubbf8\ubd84\ud558\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$a_k$<\/math-renderer> \ud558\ub098\ub9cc \ub0a8\ub294\ub2e4. \uc131\ubd84\uc744 \ub2e4\uc2dc \ubaa8\uc73c\uba74 \ubca1\ud130 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$a$<\/math-renderer> \uac00 \ub41c\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\frac{\\partial (\\beta^\\top a)}{\\partial \\beta} = a\n\\hspace{19em} (14)$$<\/math-renderer>\n<p dir=\"auto\">\uc14b\uc9f8, \uc774\ucc28\ud615 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta^\\top A \\beta$<\/math-renderer> \ub294 \uc131\ubd84\uc73c\ub85c \uc4f0\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\sum_i \\sum_j \\beta_i A_{ij} \\beta_j$<\/math-renderer> \uc774\ub2e4. <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta_k$<\/math-renderer> \ub85c \ubbf8\ubd84\ud558\uba74 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$i = k$<\/math-renderer> \uc778 \ud56d\uc5d0\uc11c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\sum_j A_{kj} \\beta_j$<\/math-renderer> \uac00, <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$j = k$<\/math-renderer> \uc778 \ud56d\uc5d0\uc11c <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\sum_i \\beta_i A_{ik}$<\/math-renderer> \uac00 \ub0a8\uc73c\uba70, \uc774\ub294 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(A + A^\\top)\\beta$<\/math-renderer> \uc758 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$k$<\/math-renderer> \ubc88\uc9f8 \uc131\ubd84\uc774\ub2e4.<\/p>\n<math-renderer class=\"js-display-math\" style=\"display: block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$$\\frac{\\partial (\\beta^\\top A \\beta)}{\\partial \\beta} = (A + A^\\top)\\beta\n\\hspace{19em} (15)$$<\/math-renderer>\n<p dir=\"auto\"><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$A$<\/math-renderer> \uac00 \ub300\uce6d\uc774\uba74 \ub450 \ud56d\uc774 \uac19\uc544 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$2A\\beta$<\/math-renderer> \uac00 \ub41c\ub2e4. <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$X^\\top X$<\/math-renderer> \ub294 \uc804\uce58\uac00 <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$(X^\\top X)^\\top = X^\\top (X^\\top)^\\top = X^\\top X$<\/math-renderer> \ub85c \uc790\uae30 \uc790\uc2e0\uc774\ubbc0\ub85c \ub300\uce6d\uc774\ub2e4.<\/p>\n<p dir=\"auto\">\uc2dd (4) \uc758 \uc138 \ud56d\uc5d0 \uc774 \uaddc\uce59\uc744 \ucc28\ub840\ub85c \ub123\uc740 \uac83\uc774 \uc2dd (5) \uc774\ub2e4.<\/p>\n<p dir=\"auto\">Table 2. The three terms of equation (4) under the rules<\/p>\n<markdown-accessiblity-table><table>\n<thead>\n<tr>\n<th>Term of equation (4)<\/th>\n<th>Rule<\/th>\n<th>Derivative<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$y^\\top y$<\/math-renderer><\/td>\n<td>\uc2dd (13)<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$0$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$-2\\beta^\\top X^\\top y$<\/math-renderer><\/td>\n<td>\uc2dd (14), <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$a = X^\\top y$<\/math-renderer>\n<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$-2X^\\top y$<\/math-renderer><\/td>\n<\/tr>\n<tr>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$\\beta^\\top X^\\top X \\beta$<\/math-renderer><\/td>\n<td>\uc2dd (15), <math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$A = X^\\top X$<\/math-renderer> \ub294 \ub300\uce6d<\/td>\n<td><math-renderer class=\"js-inline-math\" style=\"display: inline-block\" data-run-id=\"c6ecff363d2b818815c36fdeed93b9b0\">$2X^\\top X \\beta$<\/math-renderer><\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/markdown-accessiblity-table>\n<\/article><\/div><\/div><\/p>\n<div style='text-align:center' class='yasr-auto-insert-overall'><\/div><div style='text-align:center' class='yasr-auto-insert-visitor'><\/div>","protected":false},"excerpt":{"rendered":"<p>Fitting a linear regression means solving one matrix equation, not one equation per observation. Stacking the data into a design matrix and minimizing the residual sum of squares gives the normal equation, whose closed-form solution fixes the coefficients uniquely whenever the cross-product matrix can be inverted. NumPy reproduces the computation.<\/p>\n","protected":false},"author":4,"featured_media":7072,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","yasr_overall_rating":0,"yasr_post_is_review":"","yasr_auto_insert_disabled":"","yasr_review_type":"","fifu_image_url":"","fifu_image_alt":"","iawp_total_views":11,"footnotes":""},"categories":[56,321,431],"tags":[386,428],"class_list":["post-7071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science-slug","category-math-slug","category-regression-slug","tag-github-hosted","tag-regression"],"yasr_visitor_votes":{"stars_attributes":{"read_only":false,"span_bottom":false},"number_of_votes":1,"sum_votes":3},"jetpack_featured_media_url":"https:\/\/ykim.synology.me\/wordpress\/wp-content\/uploads\/2026\/09\/cvs-vaccination-5-off-20-coupon-single.jpg","_links":{"self":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/7071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/comments?post=7071"}],"version-history":[{"count":2,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/7071\/revisions"}],"predecessor-version":[{"id":7368,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/posts\/7071\/revisions\/7368"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media\/7072"}],"wp:attachment":[{"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/media?parent=7071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/categories?post=7071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykim.synology.me\/wordpress\/wp-json\/wp\/v2\/tags?post=7071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}