Module2 Linear RegressionLectureSlide
Module2 Linear RegressionLectureSlide
𝑦ො = 𝛽0 + 𝛽1 𝑥
This line should be the best line - meaning the prediction errors
should be as small as possible.
[Link], SCOPE, VIT CHENNAI
LINEAR REGRESSION
Input X – Independent variable: Input or predictor variable is the
variable that helps predict the value of the output variable. It is
commonly referred to as X.
Output Y – Dependent variable: The output or target variable is
the variable that we want to predict. It is commonly referred to
as y.
y = mx + c
𝑦ො = 𝛽0 + 𝛽1 𝑥
This line should be the best line - meaning the prediction errors
should be as small as possible.
55
Simple Linear Regression
• For example, X may represent TV
advertising and Y may represent
sales.
• Then we can regress sales onto TV
by fitting the mode
66
Simple Linear Regression
77
LINEAR REGRESSION
18
18
SIMPLE LINEAR REGRESSION
In the equation, you can see that two entities can have changeable
values (regression coefficients / variables), 𝛽0 which is the point at
which the line intercepts the x-axis, and 𝛽1 , which is how steep the line
will be, or slope.
At first, if these two regression coefficients are not properly
optimized, you get a line that might not properly fit the model.
As you optimize the values of the model, for some variables, you will
get the perfect fit.
The perfect fit will be a straight line running through most of the data
points while ignoring the noise and outliers.
23
23
Model Estimation and Evaluation
Question-1:
Consider the following five training examples
X = [2 3 4 5 6]
Y = [12.8978 17.7586 23.3192 28.3129 32.1351]
We want to learn a function f(x) of the form f(x) = ax + b which is
parameterized by (a, b). Find the best linear fit
24
24
Simple Linear Regression
Solution:
25
25
Simple Linear Regression
Matrix Method
Numerical Example
26
Matrix approach for solving Regression numericals
Augment for w0
regression
coefficient
27
27
Matrix approach for solving Regression numericals
28
28
Matrix approach for solving Regression numericals
29
29
[Link], SCOPE, VIT CHENNAI
Matrix approach for solving Regression numericals
31
31
[Link], SCOPE, VIT CHENNAI
SIMPLE LINEAR REGRESSION (3 CASES)
35
35
QUIZ
List out real time examples of all three cases
Create different dataset for X and Y for all three cases
41
Simple Linear Regression
Question-2:
Consider the following five training examples
X = [2 3 4 5 6]
Y = [12.8978 17.7586 23.3192 28.3129 32.1351]
(a)Find the best linear fit
(b)Determine the minimum SSE
(c) Draw the residual plot for the best linear fit and comment on the
suitability of the linear model to this training data.
42
42
Simple Linear Regression
Solution:
(a) To find the best fit, calculate the model coefficients using the formula
43
43
Simple Linear Regression
Solution:
44
44
Simple Linear Regression
Solution:
45
45
Simple Linear Regression
Solution:
(b) To determine RSS
SSE𝒎𝒊𝒏
SSE𝒎𝒊𝒏 = 0.8558
Simple Linear Regression
Solution: Residual Plot
(c) Residual plot for the best linear fit
Residual
X Y Ypredicted (Y-YPredicted)
2 12.8978 13.0789 -0.1811
3 17.7586 17.9818 -0.2232
4 23.3192 22.8847 0.4345
5 28.3129 27.7876 0.5253
6 32.1351 32.6905 -0.5554
The random pattern in it is an indication that a linear model is suitable for this data
47
47
Simple Linear Regression
Question:
Consider the following five training examples
X = [2 3 4 5 6]
Y = [12.8978 17.7586 23.3192 28.3129 32.1351]
We want to learn a function f(x) of the form f(x) = ax + b which is
parameterized by (a, b). Using squared error as the loss function, which of
the following parameters would you use to model this function.
(a) (4 3)
(b) (5 3)
(c) (5 1)
(d) (1 5) 48
48
Simple Linear Regression
Solution:
1. Calculate Ypredicted for the given X using the given (a, b) values
2. For each (a, b) value, calculate the SSE
3. The best set of parameters is the one that gives minimum SSE
SSE
where
49
49
Simple Linear Regression
Solution:
There are four options given.
1. Lets start with first option, for a = 4 and b = 3
2. Calculate Ypredicted for the given X using the given (a, b) values
f(x) = Ypredicted = aX + b
Y1predicted = aX1 + b = 4(2)+3 =11
3. Calculate ( Y1 - Y1predicted)2
( Y1 - Y1predicted)2 = (12.8978 – 11)2 = 3.6016
X Y Ypredicted (Y-YPredicted)2
2 12.8978 11 3.6016
3 17.7586 15 7.6099
4 23.3192 19 18.6555
5 28.3129 23 28.2269
6 32.1351 27 26.3693 50
50
SSE = 84.4632
Simple Linear Regression
Solution:
There are four options given.
Lets start with first option, for a = 4 and b = 3
X Y Ypredicted (Y-YPredicted)2
2 12.8978 11 3.6016
3 17.7586 15 7.6099
4 23.3192 19 18.6555
5 28.3129 23 28.2269
6 32.1351 27 26.3693
SSE = 84.4632
Formula 51
51
SSE
sum of squares error
Simple Linear Regression
Solution:
There are four options given.
Lets start with first option, for a = 4 and b = 3
X Y Ypredicted (Y-YPredicted)2
2 12.8978 11 3.6016
3 17.7586 15 7.6099
4 23.3192 19 18.6555
5 28.3129 23 28.2269
6 32.1351 27 26.3693
SSE = 84.4632
Formula 52
52
SSE
53
53
Simple Linear Regression
Solution: Formula
The given third option, SSE
For a = 5 and b = 1
sum of squares error
X Y Ypredicted (Y-YPredicted)2
2 12.8978 11 3.6016
3 17.7586 16 3.0927
4 23.3192 21 5.3787
5 28.3129 26 5.3495
6 32.1351 31 1.2885
SSE= 18.7110
54
54
Simple Linear Regression
Solution: Formula
The given fourth option,
SSE
For a = 1 and b = 5
sum of squares error
X Y Ypredicted (Y-YPredicted
)2
2 12.8978 7 34.7840
3 17.7586 8 95.2303
4 23.3192 9 205.0395
5 28.3129 10 335.3623
6 32.1351 11 446.6925
SSE= 1117.1086
55
55
SSE
(a) (4 3) 84.4632
(b) (5 3) 1.0166
(c) (5 1) 18.7110
(d) (1 5) 1117.1086
Answer: The parameter (5,3) which gives least SSE (1.016). Hence
(5,3) second option is optimally used to model this function
[Link], SCOPE, VIT CHENNAI
Correlation
Numerical Example
57
Correlation
• A correlation is a relationship between two variables.
• Is there a relationship between the number of employee training
hours and the number of jobs produced?
• Is there a relationship between the number of hours a student spends
studying for a Mathematics test and the student’s score on that test?
• Let x to be the independent variable and y to be the dependent
variable. Data is represented by a collection of ordered pairs (x, y)
• Mathematically, the strength and direction of a linear relationship
between two variables is represented by the correlation coefficient.
58
58
Correlation
➢ The correlation coefficient r is given by
59
59
Correlation
Question:
➢ The time x in years that an employee spent at a company and the
employee’s hourly pay, y, for 5 employees are listed in the table below.
Calculate and interpret the correlation coefficient r
60
60
Correlation
61
61
Correlation
62
62
Correlation
63
63
Correlation
1
2
3
4
5
64
64
Correlation
65
65
Correlation
66
66
[Link], SCOPE, VIT CHENNAI
R2 metric / score
Numerical Example
68
[Link], SCOPE, VIT CHENNAI
Model Estimation and Evaluation
Error/Difference between
Original and Predicted
target
71
71
Simple Linear Regression
72
72
Model Estimation and Evaluation
73
73
Regression Modelling
74
Model Estimation and Evaluation
• We assume that the true relationship between X and Y takes the form
Y = f(X) + for some unknown function f, where is a mean-zero
random error term
• If f is to be approximated by a linear function, then we can write this
relationship as
75
75
Model Estimation and Evaluation
76
76
Model Estimation and Evaluation
78
78
Model Estimation and Evaluation
• Figure shows a simulated data set
• The population regression line is
shown in red, and the least squares
line in dark blue
• In light blue, ten least squares lines
are shown, each computed on the
basis of a separate random set of
observations from f(X)=2+3X +
• Each least squares line is different,
but on average, the least squares
lines are quite close to the
population regression line
79
79
Model Estimation and Evaluation
• The standard error tells us the average amount that this estimate ෝ differs
from the actual value of . The standard error equation tells us how this
deviation shrinks with n – the more observations we have, the smaller the
standard error of
ෝ 80
80
Model Estimation and Evaluation
Standard error
82
82
Model Estimation and Evaluation
• For linear regression, the 95% confidence interval for β0
approximately takes the form
Standard error
83
83
Model Estimation and Evaluation
• Similarly, a confidence interval for β1 approximately takes the form
Standard error
84
84
Model Estimation and Evaluation
85
85
Model Estimation and Evaluation
• The RSE provides an absolute measure of lack of fit of the model to the
data. A small RSE indicates that the model fits the data well whereas a
large RSE indicates that the model doesn’t fit the data well. But since it
is measured in the units of Y, it is not always clear what constitutes a
good RSE
• The R2 statistic provides an alternative measure of fit. It takes the
form of a proportion of variance, expressed as
SSE
TSS - SSE
89
89
Simple Linear Regression
Solution:
SSE = SSE
SE(a) = 0.1689
91
91
Model Estimation and Evaluation
92
92
Model Estimation and Evaluation
93
93
PRACTICE PROBLEMS
[Link], SCOPE, VIT CHENNAI
Also calculate the R2 score metric for the linear regression model
Loss function =
Low Variance
High Variance
[Link], SCOPE, VIT CHENNAI
Training Testing
Training Testing Error
Error Testing Error Training
Error Error Error
What is a Variance?
Variance is the variability of model prediction for a given data point or a
value that tells us the spread of our data. A model with high variance pays
a lot of attention to training data and does not generalize on the data
which it hasn’t seen before.
Simple definition: “Resulted Error from Test Data!”
BIAS VARIANCE TRADEOFF
▪If you reduce bias, variance
usually increases
Reduce Bias (Underfitting)
▪If you reduce variance, bias •Use more complex models
usually increases •Add more features
▪Goal: Find a balance where •Reduce regularization
total prediction error is Reduce Variance (Overfitting)
minimum. •Use simpler models
•Get more training data
•Apply regularization (L1/L2, dropout)
•Use cross-validation
Refer to :
[Link]
[Link], SCOPE, VIT CHENNAI
BAIS VS VARIANCE
“Most Generalized Model”.
[Link], SCOPE, VIT CHENNAI
BIAS AND VARIANCE
Testing phase
1. Lasso Regularization
2. Ridge Regularization
Ridge regression shrinks coefficients, but never makes them exactly zero.
1 No correlation
Moderate correlation
1–5
(acceptable)
The Model Performance Might Be Little Poor On The Training Set But It Will Perform
Consistently Well On Both The Training And Testing Dataset.
And Therefore The Model Becomes Less Sensitive To Change In The Independent Variable
Ridge Regression
VARIABLE
Case 3: 𝜆 = 3
Case 2: 𝜆 = 2