{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove_cell"
]
},
"outputs": [],
"source": [
"# HIDDEN\n",
"from datascience import *\n",
"from prob140 import *\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('fivethirtyeight')\n",
"%matplotlib inline\n",
"from scipy import stats\n",
"from myst_nb import glue"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Least Squares Linear Predictor ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section we are going to see if we can identify the best among all linear predictors of one numerical variable based on another, regardless of the joint distribution of the two variables.\n",
"\n",
"For jointly distributed random variables $X$ and $Y$, you know that $E(Y \\mid X)$ is the least squares predictor of $Y$ based on functions of $X$. We will now *restrict the allowed functions to linear functions* and see if we can find the best among those. In later sections we will see the connection between this best linear predictor, the best among all predictors, and the bivariate normal distribution."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove-cell"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" VIDEO"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# VIDEO: Simple Regression\n",
"from IPython.display import YouTubeVideo\n",
"\n",
"vid_simple_reg = YouTubeVideo('p-Dmvjh7MP4')\n",
"glue(\"vid_simple_reg\", vid_simple_reg)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{dropdown} See More\n",
":icon: video\n",
"{glue:}`vid_simple_reg`\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Minimizing Mean Squared Error ###\n",
"Let $h(X) = aX + b$ for constants $a$ and $b$, and let $MSE(a, b)$ denote $MSE(h)$.\n",
"\n",
"$$\n",
"MSE(a, b) ~ = ~ E\\big{(} (Y - (aX + b))^2 \\big{)} \n",
"$$\n",
"\n",
"To find the *least squares linear predictor*, we have to minimize this MSE over all $a$ and $b$. We will do this using calculus, in two steps:\n",
"- Fix $a$ and find the value $b_a^*$ that minimizes $MSE(a, b)$ for that fixed value of $a$.\n",
"- Then plug in the minimizing value $b_a^*$ in place of $b$ and minimize $MSE(a, b_a^*)$ with respect to $a$.\n",
"\n",
"#### Step 1 ####\n",
"Fix $a$ and minimize $MSE(a, b)$ with respect to $b$.\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"MSE(a, b) ~ &= ~ E\\big{(} ((Y-aX) - b)^2\\big{)}\\\\\n",
"&= ~ E((Y-aX)^2) -2bE(Y-aX) + b^2\n",
"\\end{align*}\n",
"$$\n",
"\n",
"Differentiate this with respect to $b$.\n",
"\n",
"$$\n",
"\\frac{d}{db} MSE(a, b) ~ = ~ -2E(Y-aX) + 2b\n",
"$$\n",
"\n",
"Set this equal to 0 and solve to see that the minimizing value of $b$ for the fixed value of $a$ is\n",
"\n",
"$$\n",
"b_a^* ~ = ~ E(Y-aX) ~ = ~ E(Y) - aE(X)\n",
"$$\n",
"\n",
"#### Step 2 ####\n",
"Now we have to minimize the following function with respect to $a$:\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"E\\big{(} (Y - (aX + b_a^*))^2 \\big{)} ~ &= ~\n",
"E\\big{(} (Y - (aX + E(Y) - aE(X)))^2 \\big{)} \\\\\n",
"&= ~ E\\Big{(} \\big{(} (Y - E(Y)) - a(X - E(X))\\big{)}^2 \\Big{)} \\\\\n",
"&= ~ E\\big{(} (Y - E(Y))^2 \\big{)} - 2aE\\big{(} (Y - E(Y))(X - E(X)) \\big{)} + a^2E\\big{(} (X - E(X))^2 \\big{)} \\\\\n",
"&= ~ Var(Y) - 2aCov(X, Y) + a^2Var(X) \\\\\n",
"\\end{align*}\n",
"$$\n",
"\n",
"The derivative with respect to $a$ is $-2Cov(X, Y) + 2aVar(X)$. Thus the minimizing value of $a$ is\n",
"\n",
"$$\n",
"a^* ~ = ~ \\frac{Cov(X, Y)}{Var(X)} \n",
"$$\n",
"\n",
"At this point we should check that what we have is a minimum, not a maximum, but based on your experience with prediction you might just be willing to accept that we have a minimum. If you're not, then differentiate again and look at the sign of the resulting function."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Slope and Intercept of the Regression Line ###\n",
"The least squares straight line is called the *regression line*.You now have a proof of its equation, familiar to you from Data 8. Let $r_{X,Y}$ be the correlation between $X$ and $Y$ and let $\\sigma_X$ and $\\sigma_Y$ be the standard deviations of $X$ and $Y$ respectively. As you know, $r_{X,Y} = \\frac{Cov(X,Y)}{\\sigma_X\\sigma_Y}$. So the slope and intercept are given by\n",
"\n",
"$$\n",
"\\begin{align*} \n",
"\\text{slope of regression line} ~ &= ~ \\frac{Cov(X,Y)}{Var(X)} ~ = ~ r_{X,Y} \\frac{\\sigma_Y}{\\sigma_X} \\\\ \\\\\n",
"\\text{intercept of regression line} ~ &= ~ E(Y) - \\text{slope} \\cdot E(X)\n",
"\\end{align*}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Regression in Standard Units ###\n",
"If both $X$ and $Y$ are measured in standard units, then the slope of the regression line is the correlation $r_{X,Y}$ and the intercept is 0. \n",
"\n",
"In other words, given that $X = x$ standard units, the predicted value of $Y$ is $r_{X,Y}x$ standard units. When $r_{X,Y}$ is positive but not 1, this result is called the *regression effect*: the predicted value of $Y$ is closer to 0 than the given value of $X$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Line and the Joint Distribution ###\n",
"\n",
"The calculations above show that *regardless of the joint distribution of $X$ and $Y$*, that is, regardless of the relation between $X$ and $Y$,\n",
"\n",
"- The equation of the regression line holds.\n",
"\n",
"- The regression line goes through the point $(E(X), E(Y))$.\n",
"\n",
"- There is a unique best straight line predictor among all straight lines. If the relation between $X$ and $Y$ isn't roughly linear then you won't want to use the best straight line for predictions, because the best straight line is only the best among a bad class of predictors. But it exists."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Regression Line for Data ###\n",
"\n",
"In Data 8, the setting for simple linear regression was that we had a deterministic set of points $\\{(x_i, y_i): 1 \\le i \\le n\\}$ and we were using a line of the from $y = ax+b$ as our predictor. \n",
"\n",
"The equation of the regression line based on the data is a special case of the random variable calculations of this section. The mean squared error of the prediction is easily seen to be equal to $MSE(a, b)$ as defined in this section for a randomly picked point:\n",
"\n",
"$$\n",
"\\frac{1}{n} \\sum_{i=1}^n (y_i - (ax_i + b))^2 ~ = ~ \\sum_{i=1}^n (y_i - (ax_i + b))^2 \\frac{1}{n} ~ = ~ MSE(a, b)\n",
"$$\n",
"\n",
"for $(X, Y)$ picked uniformly at random from the set $\\{(x_i, y_i): 1 \\le i \\le n\\}$. \n",
"\n",
"We have already found the minimizing values of $a$ and $b$. The least-squares slope and intercept are\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"a^* ~ &= ~ \\frac{Cov(X,Y)}{Var(X)} ~ = ~ r_{X,Y} \\frac{\\sigma_Y}{\\sigma_X} \\\\\n",
"b^* ~ &= ~ E(Y) - b^*E(X)\n",
"\\end{align*}\n",
"$$\n",
"\n",
"where the quantities on the right are calculated based on the uniform distribution. For example, \n",
"\n",
"$$\n",
"E(Y) ~ = ~ \\sum_{i=1}^n y_i\\cdot\\frac{1}{n} ~ = ~ \\frac{1}{n}\\sum_{i=1}^n y_i ~ = ~ \\bar{y}\n",
"$$\n",
"\n",
"That's the average of the $y$-values. The variance is \n",
"\n",
"$$\n",
"\\sigma_Y^2 = E((Y - E(Y))^2) = \\frac{1}{n} \\sum_{i=1}^n (y_i - \\bar{y})^2\n",
"$$ \n",
"\n",
"by plugging in $E(Y) = \\bar{y}$ and using the uniform distribution again. So also $\\sigma_X^2 = \\frac{1}{n} \\sum_{i=1}^n (x_i - \\bar{x})^2$ and\n",
"\n",
"$$\n",
"Cov(X, Y) = E((X-E(X))(Y-E(Y)) = \\frac{1}{n} \\sum_{i=1}^n (x_i - \\bar{x})(y_i - \\bar{y})\n",
"$$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Tags",
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}