% ============================================================================== % Machine Learning & Statistical Theory Notes % OLS in Matrix Form % Author: Noted by Md. Inzamamul Lohani (Software Engineering, IIT, DU) % Architecture: Article-based Notes with Section / Subsection Hierarchy % Compile: pdflatex ols_matrix_notes.tex && pdflatex ols_matrix_notes.tex % ============================================================================== \documentclass[11pt,a4paper]{article} % ==================== Core Packages ==================== \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage[a4paper,margin=25mm,headheight=15pt]{geometry} \usepackage{microtype} \usepackage{amsmath,amssymb,mathtools,bm} \usepackage{graphicx} \usepackage{booktabs,tabularx,array} \usepackage{enumitem} \usepackage{float} \usepackage[section]{placeins} \usepackage{xcolor} \usepackage[most]{tcolorbox} \usepackage{listings} \usepackage{fancyhdr} \usepackage{titlesec} \usepackage{hyperref} \usepackage[nameinlink,noabbrev]{cleveref} \usepackage{caption} % Number equations per section (e.g., (1.1), (1.2)) \numberwithin{equation}{section} % ==================== Color Palette ==================== \definecolor{Navy}{HTML}{17324D} % Primary headings, main emphasis \definecolor{Blue}{HTML}{2878B5} % Secondary headings, key idea frame \definecolor{Teal}{HTML}{168477} % Sub-headings, intuition frame \definecolor{Gold}{HTML}{D18B26} % Warnings, alerts frame \definecolor{PaleBlue}{HTML}{EDF5FB} % Key idea background \definecolor{PaleTeal}{HTML}{EAF6F3} % Intuition background \definecolor{PaleGold}{HTML}{FFF6E6} % Warning background \definecolor{Ink}{HTML}{263238} % Body text \definecolor{Muted}{HTML}{61717D} % Headers, footers, meta text \definecolor{CodeBg}{HTML}{F7F8FA} % Code block background \definecolor{CodeGreen}{HTML}{236E41} % Code comments \definecolor{CodePurple}{HTML}{73378C} % Code strings % ==================== Topic & Metadata Definition ==================== \newcommand{\topicname}{OLS in Matrix Form} % ==================== Hyperlink Setup ==================== \hypersetup{ colorlinks=true, linkcolor=Navy, urlcolor=Blue, citecolor=Teal, pdftitle={\topicname: Study Notes}, pdfauthor={Md. Inzamamul Lohani} } % Paragraph & list spacing \setlength{\parskip}{5pt} \setlength{\parindent}{0pt} \setlist[itemize]{leftmargin=1.5em,itemsep=2pt,topsep=3pt} \setlist[enumerate]{leftmargin=1.7em,itemsep=3pt,topsep=3pt} \captionsetup{font=small,labelfont=bf} % ==================== Styled Callout Boxes ==================== \newtcolorbox{keyidea}[1][]{ enhanced,breakable, colback=PaleBlue,colframe=Blue, boxrule=.7pt,arc=2mm, left=3.5mm,right=3.5mm,top=2.5mm,bottom=2.5mm, title=Key idea,fonttitle=\bfseries\color{Navy}, #1 } \newtcolorbox{intuition}[1][]{ enhanced,breakable, colback=PaleTeal,colframe=Teal, boxrule=.7pt,arc=2mm, left=3.5mm,right=3.5mm,top=2.5mm,bottom=2.5mm, title=Intuition,fonttitle=\bfseries\color{Navy}, #1 } \newtcolorbox{warningbox}[1][]{ enhanced,breakable, colback=PaleGold,colframe=Gold, boxrule=.7pt,arc=2mm, left=3.5mm,right=3.5mm,top=2.5mm,bottom=2.5mm, title=Important note,fonttitle=\bfseries\color{Navy}, #1 } \newtcolorbox{worked}[1][]{ enhanced,breakable, colback=white,colframe=Muted!65, boxrule=.6pt,arc=2mm, left=3.5mm,right=3.5mm,top=2.5mm,bottom=2.5mm, title=Worked example,fonttitle=\bfseries\color{Navy}, #1 } % ==================== Code Block Styling ==================== \lstdefinestyle{pythonstyle}{ language=Python, basicstyle=\ttfamily\footnotesize, keywordstyle=\color{Navy}\bfseries, commentstyle=\color{CodeGreen}\itshape, stringstyle=\color{CodePurple}, backgroundcolor=\color{CodeBg}, frame=single, rulecolor=\color{black!15}, breaklines=true, showstringspaces=false, numbers=left, numberstyle=\tiny\color{Muted}, xleftmargin=1.5em, framexleftmargin=1.2em } \lstset{style=pythonstyle} % ==================== Header & Footer Setup ==================== \pagestyle{fancy} \fancyhf{} \fancyhead[L]{Machine Learning} \fancyhead[R]{\topicname} \fancyfoot[C]{\thepage} \renewcommand{\headrulewidth}{0.4pt} \renewcommand{\footrulewidth}{0.4pt} % ==================== Section Headings Styling ==================== \titleformat{\section} {\Large\bfseries\color{Navy}}{\thesection\quad}{0pt}{} \titlespacing*{\section}{0pt}{14pt}{8pt} \titleformat{\subsection} {\large\bfseries\color{Navy}}{\thesubsection\quad}{0pt}{} \titlespacing*{\subsection}{0pt}{10pt}{5pt} \titleformat{\subsubsection} {\normalsize\bfseries\color{Teal}}{\thesubsubsection\quad}{0pt}{} \titlespacing*{\subsubsection}{0pt}{8pt}{4pt} % ==================== Helpful Mathematical Notation ==================== \newcommand{\bhat}{\hat{\boldsymbol{\beta}}} \newcommand{\eps}{\boldsymbol{\varepsilon}} \newcommand{\ee}{\mathbf{e}} \newcommand{\XX}{\mathbf{X}} \newcommand{\yy}{\mathbf{y}} \newcommand{\RSS}{\operatorname{RSS}} % ==================== Document Body ==================== \begin{document} % ==================== Title Page ==================== \begin{titlepage} \centering \vspace*{\fill} {\Huge\bfseries \topicname\par} \vspace{2.5em} {\large Noted by Md. Inzamamul Lohani\par} \vspace{0.4em} {\large Software Engineering, IIT, DU\par} \vspace{2em} {\large \today\par} \vspace*{\fill} \end{titlepage} \setcounter{page}{2} \tableofcontents \newpage \section{The True Model} The population regression model in matrix notation is \begin{equation} \boxed{\yy=\XX\boldsymbol{\beta}+\eps.} \label{eq:true-model} \end{equation} Here: \begin{itemize} \item $\XX$ is an $n\times k$ matrix containing observations on $k$ independent variables for $n$ observations. If the model includes a constant (intercept), one column of $\XX$ consists entirely of ones. \item $\yy$ is an $n\times 1$ vector of observations on the dependent variable. \item $\eps$ is an $n\times 1$ vector of disturbances (unobserved influences). \item $\boldsymbol{\beta}$ is a $k\times 1$ vector of unknown population parameters. \end{itemize} The model has a \textbf{systematic component}, $\XX\boldsymbol{\beta}$, and a \textbf{stochastic component}, $\eps$. The objective is to estimate the unknown population parameters in $\boldsymbol{\beta}$. \subsection{Dimensions} The dimensions are chosen so that the matrix multiplication is valid: \[ (n\times k)(k\times 1)=n\times 1. \] Thus $\XX\boldsymbol{\beta}$ has the same dimension as $\yy$ and $\eps$. \begin{figure}[H] \centering \includegraphics[width=0.92\textwidth]{images/matrix_dimensions.png} \caption{Dimension check for the scalar product $\yy'\XX\bhat$.} \end{figure} \section{Estimates, Fitted Values, and Residuals} The estimated parameter vector is denoted by $\bhat$. The fitted (predicted) values are \[ \widehat{\yy}=\XX\bhat. \] The residual vector is the difference between observed and fitted values: \begin{equation} \boxed{\ee=\yy-\XX\bhat.} \label{eq:residual} \end{equation} A residual is observable because it can be calculated from the sample. \subsection{Disturbances versus residuals} These terms must be distinguished: \begin{center} \begin{tabular}{p{0.18\textwidth}p{0.34\textwidth}p{0.34\textwidth}} \toprule & \textbf{Disturbance $\eps$} & \textbf{Residual $\ee$}\\ \midrule Meaning & Unobserved part of the population model & Observed minus fitted value\\ Observable? & No & Yes\\ Role & Part of the true model & Computed after estimating the model\\ \bottomrule \end{tabular} \end{center} In general, $\eps\ne\ee$. Do not infer properties of the unobserved disturbances merely from properties of the residuals. \section{The OLS Criterion: Minimize the Sum of Squared Residuals} The sum of squared residuals (RSS) is \begin{equation} \RSS=\ee'\ee. \end{equation} Since $\ee=\yy-\XX\bhat$, \begin{equation} \RSS=(\yy-\XX\bhat)'(\yy-\XX\bhat). \label{eq:rss-start} \end{equation} The transpose of a product reverses the order: \[ (AB)'=B'A', \qquad (A-B)'=A'-B'. \] Therefore, \[ (\yy-\XX\bhat)'=\yy'-(\XX\bhat)'=\yy'-\bhat'\XX'. \] Substituting into \eqref{eq:rss-start} gives \begin{align} \RSS &=(\yy'-\bhat'\XX')(\yy-\XX\bhat)\\ &=\yy'\yy-\yy'\XX\bhat-\bhat'\XX'\yy+\bhat'\XX'\XX\bhat. \end{align} The middle expressions are scalars. A scalar is equal to its transpose, so \[ \yy'\XX\bhat=(\yy'\XX\bhat)'=\bhat'\XX'\yy. \] Hence the two middle terms are equal, and \begin{equation} \boxed{\RSS=\yy'\yy-2\bhat'\XX'\yy+\bhat'\XX'\XX\bhat.} \label{eq:rss-expanded} \end{equation} \subsection{Why is $\yy'\XX\bhat$ a scalar?} The dimensions are \[ \underbrace{\yy'}_{1\times n} \underbrace{\XX}_{n\times k} \underbrace{\bhat}_{k\times 1}. \] Multiplying from left to right, \[ (1\times n)(n\times k)=1\times k, \qquad (1\times k)(k\times 1)=1\times 1. \] A $1\times 1$ matrix contains one number, so it is a scalar. \begin{figure}[H] \centering \includegraphics[width=0.92\textwidth]{images/matrix_dimensions.png} \caption{The final dimension of $\yy'\XX\bhat$ is $1\times 1$.} \end{figure} \section{Differentiating RSS with Respect to the Coefficient Vector} We choose $\bhat$ to minimize RSS. Differentiate each term in \eqref{eq:rss-expanded} with respect to the vector $\bhat$. \subsection{Useful matrix differentiation rules} For vectors $\mathbf{a}$ and $\mathbf{b}$ of compatible dimensions, \[ \frac{\partial(\mathbf{b}'\mathbf{a})}{\partial\mathbf{b}}=\mathbf{a}. \] For a square matrix $A$, \begin{equation} \frac{\partial(\boldsymbol{\beta}'A\boldsymbol{\beta})} {\partial\boldsymbol{\beta}} =(A+A')\boldsymbol{\beta}. \label{eq:quadratic-rule} \end{equation} If $A$ is symmetric ($A'=A$), this becomes \[ \frac{\partial(\boldsymbol{\beta}'A\boldsymbol{\beta})} {\partial\boldsymbol{\beta}}=2A\boldsymbol{\beta}. \] \subsection{Deriving the quadratic-form rule} Let $f(\boldsymbol{\beta})=\boldsymbol{\beta}'A\boldsymbol{\beta}$. Using the product rule in differential form, \[ df=d\boldsymbol{\beta}'A\boldsymbol{\beta} +\boldsymbol{\beta}'A\,d\boldsymbol{\beta}. \] The second term is a scalar, so it equals its transpose: \[ \boldsymbol{\beta}'A\,d\boldsymbol{\beta} =(\boldsymbol{\beta}'A\,d\boldsymbol{\beta})' =d\boldsymbol{\beta}'A'\boldsymbol{\beta}. \] Thus \[ df=d\boldsymbol{\beta}'A\boldsymbol{\beta} +d\boldsymbol{\beta}'A'\boldsymbol{\beta} =d\boldsymbol{\beta}'(A+A')\boldsymbol{\beta}. \] Therefore the gradient is $(A+A')\boldsymbol{\beta}$, proving \eqref{eq:quadratic-rule}. \subsection{Differentiate the three RSS terms} Starting from \[ \RSS=\underbrace{\yy'\yy}_{\text{Term 1}} -\underbrace{2\bhat'\XX'\yy}_{\text{Term 2}} +\underbrace{\bhat'\XX'\XX\bhat}_{\text{Term 3}}, \] we obtain: \begin{align*} \frac{\partial(\yy'\yy)}{\partial\bhat}&=0, &&\text{because $\yy$ is fixed with respect to $\bhat$},\\ \frac{\partial(-2\bhat'\XX'\yy)}{\partial\bhat}&=-2\XX'\yy,\\ \frac{\partial(\bhat'\XX'\XX\bhat)}{\partial\bhat} &=\bigl(\XX'\XX+(\XX'\XX)'\bigr)\bhat\\ &=2\XX'\XX\bhat, &&\text{because $\XX'\XX$ is symmetric.} \end{align*} Combining the three derivatives gives \begin{equation} \boxed{\frac{\partial\RSS}{\partial\bhat} =-2\XX'\yy+2\XX'\XX\bhat.} \label{eq:rss-gradient} \end{equation} \section{Normal Equations and the OLS Estimator} At an interior minimum, the first derivative (gradient) is zero: \[ -2\XX'\yy+2\XX'\XX\bhat=0. \] Divide by $2$ and rearrange: \begin{equation} \boxed{\XX'\XX\bhat=\XX'\yy.} \label{eq:normal} \end{equation} These are the \textbf{normal equations}. The matrix $\XX'\XX$ is $k\times k$, square, and symmetric. If it is invertible, pre-multiply both sides of \eqref{eq:normal} by $(\XX'\XX)^{-1}$: \[ (\XX'\XX)^{-1}\XX'\XX\bhat=(\XX'\XX)^{-1}\XX'\yy. \] Since $(\XX'\XX)^{-1}\XX'\XX=I_k$, \begin{equation} \boxed{\bhat=(\XX'\XX)^{-1}\XX'\yy.} \label{eq:ols-estimator} \end{equation} This is the OLS estimator in matrix form. \subsection{When might the inverse fail to exist?} The inverse of $\XX'\XX$ may not exist if: \begin{itemize} \item $n