# HIDDEN
from datascience import *
from prob140 import *
import numpy as np
import warnings
import matplotlib.cbook
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
%matplotlib inline
from scipy import stats
The equation of the regression line for predicting Y based on X can be written in several equivalent ways. The regression equation, and the error in the regression estimate, are best understood in standard units. All the other representations follow by straightforward algebra.
Let X and Y be bivariate normal with parameters (μX,μY,σX2,σY2,ρ). Then, as we have seen, the best predictor E(Y∣X) is a linear function of X and hence the formula for E(Y∣X) is also the equation of the regression line.
Let Xsu be X in standard units and Ysu be Y in standard units. The regression equation is
E(Ysu∣Xsu)=ρXsu
and the amount of error in the prediction is measured by
SD(Ysu∣Xsu)=1−ρ2
The conditional SD is in the same units as the prediction. The conditional variance is
Var(Ysu∣Xsu)=1−ρ2
We know more than just the conditional expectation and conditional variance. We know that the conditional distribution of Ysu given Xsu is normal. This allows us to find conditional probabilities given Xsu, by the usual normal curve methods. For example,
P(Ysu<ysu∣Xsu=xsu)=Φ(1−ρ2ysu−ρxsu)
In one of Galton’s famous data sets, the distribution of the heights of father-son pairs was roughly bivariate normal with a correlation of 0.5. Of the fathers whose heights were two SDs above average, about what percent had sons whose heights were more than 2 SDs above average?
By the regression effect, you know this answer has to be less than 50%. If Ysu denotes the height of a randomly picked son in standard units, and Xsu the height of his father in standard units, then the proportion is approximately
P(Ysu>2∣Xsu=2)=1−Φ(1−0.522−0.5×2)
which is approximately 12.4%.
1 - stats.norm.cdf(2, 0.5*2, np.sqrt(1-0.5**2))
0.12410653949496186
from IPython.display import YouTubeVideo
YouTubeVideo('hlAaKwSAXp8')
Usually, you want to make predictions in the units in which the data were measured. Before changing units in the formulas above, keep in mind that conditioning on X is equivalent to conditioning on Xsu. If you know the value of either of X or Xsu, you also know the other.
which is the same as the equation of the least squares line we had derived earlier without any assumptions about the joint distribution of X and Y. This confirms our observation that if X and Y are bivariate normal, the best linear predictor is the best among all predictors.
The amount of error in the prediction is measured by SD(Y∣X) which is the same as
Regardless of the joint distribution of X and Y, the regression equation is
Y^=a∗X+b∗where a∗=ρσXσY and b∗=μY−a∗μX
This is equivalent to
Y^=a∗(X−μX)+μY
This form shows that the regression line passes through the point (μX,μY) and that E(Y^)=μY. The predicted values and the actual values are the same on average.
When there are just two variables, matrix formulations are hardly necessary. But it is worth writing the regression estimate and the conditional variance using only the mean vector and covariance matrix, and replacing division with multipliciation by an inverse. This effort will be rewarded in the next chapter because exactly analogous formulas will work for multiple regression.
Define σX,Y=Cov(X,Y)=σY,X. Then X and Y have mean vector [μX,μY]T and covariance matrix
[σX2σX,YσY,XσY2]
We know that
ρ=σXσYσX,Y
The regression equation can therefore be written as