\n", "

\n",
"## Small CheatSheet on matrix derivatives (click the triangle to extend)

\n",
"

\n",
"\n",
"\n",
"What we get is:\n",
"\n",
"$$\\Large \\begin{array}{rcl} \\frac{\\partial \\mathcal{L}}{\\partial \\textbf{w}} &=& \\frac{\\partial}{\\partial \\textbf{w}} \\frac{1}{2n} \\left( \\textbf{y}^{\\text{T}} \\textbf{y} -2\\textbf{y}^{\\text{T}} \\textbf{X} \\textbf{w} + \\textbf{w}^{\\text{T}} \\textbf{X}^{\\text{T}} \\textbf{X} \\textbf{w}\\right) \\\\\n",
"&=& \\frac{1}{2n} \\left(-2 \\textbf{X}^{\\text{T}} \\textbf{y} + 2\\textbf{X}^{\\text{T}} \\textbf{X} \\textbf{w}\\right)\n",
"\\end{array}$$\n",
"\n",
"$$\\Large \\begin{array}{rcl} \\frac{\\partial \\mathcal{L}}{\\partial \\textbf{w}} = 0 &\\Leftrightarrow& \\frac{1}{2n} \\left(-2 \\textbf{X}^{\\text{T}} \\textbf{y} + 2\\textbf{X}^{\\text{T}} \\textbf{X} \\textbf{w}\\right) = 0 \\\\\n",
"&\\Leftrightarrow& -\\textbf{X}^{\\text{T}} \\textbf{y} + \\textbf{X}^{\\text{T}} \\textbf{X} \\textbf{w} = 0 \\\\\n",
"&\\Leftrightarrow& \\textbf{X}^{\\text{T}} \\textbf{X} \\textbf{w} = \\textbf{X}^{\\text{T}} \\textbf{y} \\\\\n",
"&\\Leftrightarrow& \\textbf{w} = \\left(\\textbf{X}^{\\text{T}} \\textbf{X}\\right)^{-1} \\textbf{X}^{\\text{T}} \\textbf{y}\n",
"\\end{array}$$\n",
"\n",
"Bearing in mind all the definitions and conditions described above, we can say that, based on the Gauss–Markov theorem, OLS estimators of the model parameters are optimal among all linear and unbiased estimators, i.e. they give the lowest variance.\n",
"\n",
"## 2. Maximum Likelihood Estimation\n",
"\n",
"One could ask why we choose to minimize the mean square error instead of something else? After all, one can minimize the mean absolute value of the residual. The only thing that will happen, if we change the minimized value, is that we will exceed the Gauss-Markov theorem conditions, and our estimates will therefore cease to be the optimal over the linear and unbiased ones.\n",
"\n",
"Before we continue, let's digress to illustrate maximum likelihood estimation with a simple example.\n",
"\n",
"Many people probably remember the formula of ethyl alcohol, so I decided to do an experiment to determine whether people remember a simpler formula for methanol: $CH_3OH$. We surveyed 400 people to find that only 117 people remembered the formula. Then, it is reasonable to assume that the probability that the next respondent knows the formula of methyl alcohol is $\\frac{117}{400} \\approx 29\\%$. Let's show that this intuitive assessment is not only good but also a maximum likelihood estimate. Where this estimate come from? Recall the definition of the Bernoulli distribution: a random variable has a Bernoulli distribution if it takes only two values ($1$ and $0$ with probability $\\theta$ and $1 - \\theta$, respectively) and has the following probability distribution function:\n",
"\n",
"$$\\Large p\\left(\\theta, x\\right) = \\theta^x \\left(1 - \\theta\\right)^\\left(1 - x\\right), x \\in \\left\\{0, 1\\right\\}$$\n",
"\n",
"This distribution is exactly what we need, and the distribution parameter $\\theta$ is the estimate of the probability that a person knows the formula of methyl alcohol. In our $400$ *independent* experiments, let's denote their outcomes as $\\textbf{x} = \\left(x_1, x_2, \\ldots, x_{400}\\right)$. Let's write down the likelihood of our data (observations), i.e. the probability of observing exactly these 117 realizations of the random variable $x = 1$ and 283 realizations of $x = 0$:\n",
"\n",
"$$\\Large p(\\textbf{x}; \\theta) = \\prod_{i=1}^{400} \\theta^{x_i} \\left(1 - \\theta\\right)^{\\left(1 - x_i\\right)} = \\theta^{117} \\left(1 - \\theta\\right)^{283}$$\n",
"\n",
"Next, we will maximize the expression with respect to $\\theta$. Most often this is not done with the likelihood $p(\\textbf{x}; \\theta)$ but with its logarithm (the monotonic transformation does not change the solution but simplifies calculation greatly):\n",
"\n",
"$$\\Large \\log p(\\textbf{x}; \\theta) = \\log \\prod_{i=1}^{400} \\theta^{x_i} \\left(1 - \\theta\\right)^{\\left(1 - x_i\\right)} = $$\n",
"$$ \\large = \\log \\theta^{117} \\left(1 - \\theta\\right)^{283} = 117 \\log \\theta + 283 \\log \\left(1 - \\theta\\right)$$\n",
"\n",
"Now, we want to find such a value of $\\theta$ that will maximize the likelihood. For this purpose, we'll take the derivative with respect to $\\theta$, set it to zero, and solve the resulting equation:\n",
"\n",
"$$\\Large \\frac{\\partial \\log p(\\textbf{x}; \\theta)}{\\partial \\theta} = \\frac{\\partial}{\\partial \\theta} \\left(117 \\log \\theta + 283 \\log \\left(1 - \\theta\\right)\\right) = \\frac{117}{\\theta} - \\frac{283}{1 - \\theta};$$\n",
"\n",
"It turns out that our intuitive assessment is exactly the maximum likelihood estimate. Now let us apply the same reasoning to the linear regression problem and try to find out what lies beyond the mean squared error. To do this, we'll need to look at linear regression from a probabilistic perspective. Our model naturally remains the same:\n",
"\n",
"$$\\Large \\textbf y = \\textbf X \\textbf w + \\epsilon,$$\n",
"\n",
"but let us now assume that the random errors follow a centered [Normal distribution](https://en.wikipedia.org/wiki/Normal_distribution) (beware, it's an additional assumption, it's not a prerequisite of a Gauss-Markov theorem):\n",
"\n",
"$$\\Large \\epsilon_i \\sim \\mathcal{N}\\left(0, \\sigma^2\\right)$$\n",
"\n",
"Let's rewrite the model from a new perspective:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"y_i &=& \\sum_{j=1}^m w_j X_{ij} + \\epsilon_i \\\\\n",
"&\\sim& \\sum_{j=1}^m w_j X_{ij} + \\mathcal{N}\\left(0, \\sigma^2\\right) \\\\\n",
"p\\left(y_i \\mid \\textbf X; \\textbf{w}\\right) &=& \\mathcal{N}\\left(\\sum_{j=1}^m w_j X_{ij}, \\sigma^2\\right)\n",
"\\end{array}$$\n",
"\n",
"Since the examples are taken independently (uncorrelated errors is one of the conditions of the Gauss-Markov theorem), the full likelihood of the data will look like a product of the density functions $p\\left(y_i\\right)$. Let's consider the log-likelihood, which allows us to switch products to sums:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\log p\\left(\\textbf{y}\\mid \\textbf X; \\textbf{w}\\right) &=& \\log \\prod_{i=1}^n \\mathcal{N}\\left(\\sum_{j=1}^m w_j X_{ij}, \\sigma^2\\right) \\\\\n",
"&=& \\sum_{i=1}^n \\log \\mathcal{N}\\left(\\sum_{j=1}^m w_j X_{ij}, \\sigma^2\\right) \\\\\n",
"&=& -\\frac{n}{2}\\log 2\\pi\\sigma^2 -\\frac{1}{2\\sigma^2} \\sum_{i=1}^n \\left(y_i - \\textbf{w}^\\text{T} \\textbf{x}_i\\right)^2\n",
"\\end{array}$$\n",
"\n",
"We want to find the maximum likelihood hypothesis i.e. we need to maximize the expression $p\\left(\\textbf{y} \\mid \\textbf X; \\textbf{w}\\right)$ to get $\\textbf{w}_{\\text{ML}}$, which is the same as maximizing its logarithm. Note that, while maximizing the function over some parameter, you can throw away all the members that do not depend on this parameter:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\textbf{w}_{\\text{ML}} &=& \\arg \\max_{\\textbf w} p\\left(\\textbf{y}\\mid \\textbf X; \\textbf{w}\\right) = \\arg \\max_{\\textbf w} \\log p\\left(\\textbf{y}\\mid \\textbf X; \\textbf{w}\\right)\\\\\n",
"&=& \\arg \\max_{\\textbf w} -\\frac{n}{2}\\log 2\\pi\\sigma^2 -\\frac{1}{2\\sigma^2} \\sum_{i=1}^n \\left(y_i - \\textbf{w}^{\\text{T}} \\textbf{x}_i\\right)^2 \\\\\n",
"&=& \\arg \\max_{\\textbf w} -\\frac{1}{2\\sigma^2} \\sum_{i=1}^n \\left(y_i - \\textbf{w}^{\\text{T}} \\textbf{x}_i\\right)^2 \\\\\n",
"&=& \\arg \\min_{\\textbf w} \\mathcal{L}\\left(\\textbf X, \\textbf{y}, \\textbf{w} \\right)\n",
"\\end{array}$$\n",
"\n",
"Thus, we have seen that the maximization of the likelihood of data is the same as the minimization of the mean squared error (given the above assumptions). It turns out that such a cost function is a consequence of the fact that the errors are distributed normally.\n",
"\n",
"\n",
"## 3. Bias-Variance Decomposition\n",
"\n",
"Let's talk a little about the error properties of linear regression prediction (in fact, this discussion is valid for all machine learning algorithms). We just covered the following:\n",
"\n",
"- true value of the target variable is the sum of a deterministic function $f\\left(\\textbf{x}\\right)$ and random error $\\epsilon$: $y = f\\left(\\textbf{x}\\right) + \\epsilon$;\n",
"- error is normally distributed with zero mean and some variance: $\\epsilon \\sim \\mathcal{N}\\left(0, \\sigma^2\\right)$;\n",
"- true value of the target variable is also normally distributed: $y \\sim \\mathcal{N}\\left(f\\left(\\textbf{x}\\right), \\sigma^2\\right)$;\n",
"- we try to approximate a deterministic but unknown function $f\\left(\\textbf{x}\\right)$ using a linear function of the covariates $\\widehat{f}\\left(\\textbf{x}\\right)$, which, in turn, is a point estimate of the function $f$ in function space (specifically, the family of linear functions that we have limited our space to), i.e. a random variable that has mean and variance.\n",
"\n",
"So, the error at the point $\\textbf{x}$ decomposes as follows:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\text{Err}\\left(\\textbf{x}\\right) &=& \\mathbb{E}\\left[\\left(y - \\widehat{f}\\left(\\textbf{x}\\right)\\right)^2\\right] \\\\\n",
"&=& \\mathbb{E}\\left[y^2\\right] + \\mathbb{E}\\left[\\left(\\widehat{f}\\left(\\textbf{x}\\right)\\right)^2\\right] - 2\\mathbb{E}\\left[y\\widehat{f}\\left(\\textbf{x}\\right)\\right] \\\\\n",
"&=& \\mathbb{E}\\left[y^2\\right] + \\mathbb{E}\\left[\\widehat{f}^2\\right] - 2\\mathbb{E}\\left[y\\widehat{f}\\right] \\\\\n",
"\\end{array}$$\n",
"\n",
"For clarity, we will omit the notation of the argument of the functions. Let's consider each member separately. The first two are easily decomposed according to the formula $\\text{Var}\\left(z\\right) = \\mathbb{E}\\left[z^2\\right] - \\mathbb{E}\\left[z\\right]^2$:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\mathbb{E}\\left[y^2\\right] &=& \\text{Var}\\left(y\\right) + \\mathbb{E}\\left[y\\right]^2 = \\sigma^2 + f^2\\\\\n",
"\\mathbb{E}\\left[\\widehat{f}^2\\right] &=& \\text{Var}\\left(\\widehat{f}\\right) + \\mathbb{E}\\left[\\widehat{f}\\right]^2 \\\\\n",
"\\end{array}$$\n",
"\n",
"Note that:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\text{Var}\\left(y\\right) &=& \\mathbb{E}\\left[\\left(y - \\mathbb{E}\\left[y\\right]\\right)^2\\right] \\\\\n",
"&=& \\mathbb{E}\\left[\\left(y - f\\right)^2\\right] \\\\\n",
"&=& \\mathbb{E}\\left[\\left(f + \\epsilon - f\\right)^2\\right] \\\\\n",
"&=& \\mathbb{E}\\left[\\epsilon^2\\right] = \\sigma^2\n",
"\\end{array}$$\n",
"\n",
"$$\\Large \\mathbb{E}[y] = \\mathbb{E}[f + \\epsilon] = \\mathbb{E}[f] + \\mathbb{E}[\\epsilon] = f$$\n",
"\n",
"And finally, we get to the last term in the sum. Recall that the error and the target variable are independent of each other:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\mathbb{E}\\left[y\\widehat{f}\\right] &=& \\mathbb{E}\\left[\\left(f + \\epsilon\\right)\\widehat{f}\\right] \\\\\n",
"&=& \\mathbb{E}\\left[f\\widehat{f}\\right] + \\mathbb{E}\\left[\\epsilon\\widehat{f}\\right] \\\\\n",
"&=& f\\mathbb{E}\\left[\\widehat{f}\\right] + \\mathbb{E}\\left[\\epsilon\\right] \\mathbb{E}\\left[\\widehat{f}\\right] = f\\mathbb{E}\\left[\\widehat{f}\\right]\n",
"\\end{array}$$\n",
"\n",
"Finally, let's bring this all together:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\text{Err}\\left(\\textbf{x}\\right) &=& \\mathbb{E}\\left[\\left(y - \\widehat{f}\\left(\\textbf{x}\\right)\\right)^2\\right] \\\\\n",
"&=& \\sigma^2 + f^2 + \\text{Var}\\left(\\widehat{f}\\right) + \\mathbb{E}\\left[\\widehat{f}\\right]^2 - 2f\\mathbb{E}\\left[\\widehat{f}\\right] \\\\\n",
"&=& \\left(f - \\mathbb{E}\\left[\\widehat{f}\\right]\\right)^2 + \\text{Var}\\left(\\widehat{f}\\right) + \\sigma^2 \\\\\n",
"&=& \\text{Bias}\\left(\\widehat{f}\\right)^2 + \\text{Var}\\left(\\widehat{f}\\right) + \\sigma^2\n",
"\\end{array}$$\n",
"\n",
"With that, we have reached our ultimate goal -- the last formula tells us that the forecast error of any model of type $y = f\\left(\\textbf{x}\\right) + \\epsilon$ is composed of:\n",
"\n",
"- squared bias: $\\text{Bias}\\left(\\widehat{f}\\right)$ is the average error for all sets of data;\n",
"- variance: $\\text{Var}\\left(\\widehat{f}\\right)$ is error variability, or by how much error will vary if we train the model on different sets of data;\n",
"- irremovable error: $\\sigma^2$.\n",
"\n",
"While we cannot do anything with the $\\sigma^2$ term, we can influence the first two. Ideally, we'd like to negate both of these terms (upper left square of the picture), but, in practice, it is often necessary to balance between the biased and unstable estimates (high variance).\n",
"\n",
"```{figure} /_static/img/topic4_bias_variance.png\n",
":width: 480px\n",
"```\n",
"\n",
"Generally, as the model becomes more computational (e.g. when the number of free parameters grows), the variance (dispersion) of the estimate also increases, but bias decreases. Due to the fact that the training set is memorized completely instead of generalizing, small changes lead to unexpected results (overfitting). On the other side, if the model is too weak, it will not be able to learn the pattern, resulting in learning something different that is offset with respect to the right solution.\n",
"\n",
"```{figure} /_static/img/topic4_bias_variance2.png\n",
":width: 480px\n",
"```\n",
"\n",
"The Gauss-Markov theorem asserts that the OLS estimator of parameters of the linear model is the best for the class of linear unbiased estimator. This means that if there exists any other unbiased model $g$, from the same class of linear models, we can be sure that $Var\\left(\\widehat{f}\\right) \\leq Var\\left(g\\right)$.\n",
"\n",
"## 4. Regularization of Linear Regression\n",
"\n",
"There are situations where we might intentionally increase the bias of the model for the sake of stability i.e. to reduce the variance of the model $\\text{Var}\\left(\\widehat{f}\\right)$. One of the conditions of the Gauss-Markov theorem is the full column rank of matrix $\\textbf{X}$. Otherwise, the OLS solution $\\textbf{w} = \\left(\\textbf{X}^\\text{T} \\textbf{X}\\right)^{-1} \\textbf{X}^\\text{T} \\textbf{y}$ does not exist since the inverse matrix $\\left(\\textbf{X}^\\text{T} \\textbf{X}\\right)^{-1}$ does not exist. In other words, matrix $\\textbf{X}^\\text{T} \\textbf{X}$ will be singular or degenerate. This problem is called an ill-posed problem. Problems like this must be corrected, namely, matrix $\\textbf{X}^\\text{T} \\textbf{X}$ needs to become non-degenerate, or regular (which is why this process is called regularization). Often we observe the so-called multicollinearity in the data: when two or more features are strongly correlated, it is manifested in the matrix $\\textbf{X}$ in the form of \"almost\" linear dependence between the columns. For example, in the problem of predicting house prices by their parameters, attributes \"area with balcony\" and \"area without balcony\" will have an \"almost\" linear relationship. Formally, matrix $\\textbf{X}^\\text{T} \\textbf{X}$ for such data is reversible, but, due to multicollinearity, some of its eigenvalues will be close to zero. In the inverse matrix $\\textbf{X}^\\text{T} \\textbf{X}$, some extremely large eigenvalues will appear, as eigenvalues of the inverse matrix are $\\frac{1}{\\lambda_i}$. The result of this vacillation of eigenvalues is an unstable estimate of model parameters, i.e. adding a new set of observations to the training data will lead to a completely different solution.\n",
"One method of regularization is Tikhonov regularization, which generally looks like the addition of a new member to the mean squared error:\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\mathcal{L}\\left(\\textbf{X}, \\textbf{y}, \\textbf{w} \\right) &=& \\frac{1}{2n} \\left\\| \\textbf{y} - \\textbf{X} \\textbf{w} \\right\\|_2^2 + \\left\\| \\Gamma \\textbf{w}\\right\\|^2\\\\\n",
"\\end{array}$$\n",
"\n",
"The Tikhonov matrix is often expressed as the product of a number by the identity matrix: $\\Gamma = \\frac{\\lambda}{2} E$. In this case, the problem of minimizing the mean squared error becomes a problem with a restriction on the $L_2$ norm. If we differentiate the new cost function with respect to the model parameters, set the resulting function to zero, and rearrange for $\\textbf{w}$, we get the exact solution of the problem.\n",
"\n",
"$$\\Large \\begin{array}{rcl}\n",
"\\textbf{w} &=& \\left(\\textbf{X}^{\\text{T}} \\textbf{X} + \\lambda \\textbf{E}\\right)^{-1} \\textbf{X}^{\\text{T}} \\textbf{y}\n",
"\\end{array}$$\n",
"\n",
"This type of regression is called ridge regression. The ridge is the diagonal matrix that we add to the $\\textbf{X}^\\text{T} \\textbf{X}$ matrix to ensure that we get a regular matrix as a result.\n",
"\n",
"```{figure} /_static/img/topic4_ridge.png\n",
":width: 480px\n",
"```\n",
"\n",
"Such a solution reduces dispersion but becomes biased because the norm of the vector of parameters is also minimized, which makes the solution shift towards zero. On the figure below, the OLS solution is at the intersection of the white dotted lines. Blue dots represent different solutions of ridge regression. It can be seen that by increasing the regularization parameter $\\lambda$, we shift the solution towards zero.\n",
"\n",
"```{figure} /_static/img/topic4_l2_regul.png\n",
":width: 480px\n",
"```\n",
"## 5. Useful resources\n",
"- Medium [\"story\"](https://medium.com/open-machine-learning-course/open-machine-learning-course-topic-4-linear-classification-and-regression-44a41b9b5220) based on this notebook\n",
"- Main course [site](https://mlcourse.ai), [course repo](https://github.com/Yorko/mlcourse.ai), and YouTube [channel](https://www.youtube.com/watch?v=QKTuw4PNOsU&list=PLVlY_7IJCMJeRfZ68eVfEcu-UcN9BbwiX)\n",
"- Course materials as a [Kaggle Dataset](https://www.kaggle.com/kashnitsky/mlcourse)\n",
"- If you read Russian: an [article](https://habrahabr.ru/company/ods/blog/323890/) on Habrahabr with ~ the same material. And a [lecture](https://youtu.be/oTXGQ-_oqvI) on YouTube\n",
"- A nice and concise overview of linear models is given in the book [\"Deep Learning\"](http://www.deeplearningbook.org) (I. Goodfellow, Y. Bengio, and A. Courville).\n",
"- Linear models are covered practically in every ML book. We recommend \"Pattern Recognition and Machine Learning\" (C. Bishop) and \"Machine Learning: A Probabilistic Perspective\" (K. Murphy).\n",
"- If you prefer a thorough overview of linear model from a statistician's viewpoint, then look at \"The elements of statistical learning\" (T. Hastie, R. Tibshirani, and J. Friedman).\n",
"- The book \"Machine Learning in Action\" (P. Harrington) will walk you through implementations of classic ML algorithms in pure Python.\n",
"- [Scikit-learn](http://scikit-learn.org/stable/documentation.html) library. These guys work hard on writing really clear documentation.\n",
"- Scipy 2017 [scikit-learn tutorial](https://github.com/amueller/scipy-2017-sklearn) by Alex Gramfort and Andreas Mueller.\n",
"- One more [ML course](https://github.com/diefimov/MTH594_MachineLearning) with very good materials.\n",
"- [Implementations](https://github.com/rushter/MLAlgorithms) of many ML algorithms. Search for linear regression and logistic regression."
]
}
],
"metadata": {
"jupytext": {
"formats": "md:myst",
"text_representation": {
"extension": ".md",
"format_name": "myst"
}
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
},
"source_map": [
11
]
},
"nbformat": 4,
"nbformat_minor": 5
}\n", "$$\\large \\begin{array}{rcl}\n", "\\frac{\\partial}{\\partial \\textbf{X}} \\textbf{X}^{\\text{T}} \\textbf{A} &=& \\textbf{A} \\\\\n", "\\frac{\\partial}{\\partial \\textbf{X}} \\textbf{X}^{\\text{T}} \\textbf{A} \\textbf{X} &=& \\left(\\textbf{A} + \\textbf{A}^{\\text{T}}\\right)\\textbf{X} \\\\\n", "\\frac{\\partial}{\\partial \\textbf{A}} \\textbf{X}^{\\text{T}} \\textbf{A} \\textbf{y} &=& \\textbf{X}^{\\text{T}} \\textbf{y}\\\\\n", "\\frac{\\partial}{\\partial \\textbf{X}} \\textbf{A}^{-1} &=& -\\textbf{A}^{-1} \\frac{\\partial \\textbf{A}}{\\partial \\textbf{X}} \\textbf{A}^{-1}\n", "\\end{array}$$\n", "

\n", "