MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot...

27
MSML 605 - Lecture 5

Transcript of MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot...

Page 1: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

MSML 605 - Lecture 5

Page 2: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Least Squares Optimization

Page 3: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Example

160014002100 … …. 2400

Area(sq. ft.)y

220180350…….500

Price (in 1000$) x

Page 4: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

SCATTER PLOTPlot all (Xi, Yi) pairs, and plot your learned model

4

0204060

0 20 40 60X

Y

[WF]

Page 5: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

QUESTIONHow would you draw a line through the points? How do you determine which line “fits the best” …? ?????????

5

0204060

0 20 40 60X

Y

[WF]

Page 6: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

QUESTIONHow would you draw a line through the points? How do you determine which line “fits the best” ?????????

6

0204060

0 20 40 60X

Y

Slope changed

Intercept unchanged

[WF]

Page 7: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

QUESTIONHow would you draw a line through the points? How do you determine which line “fits the best” ?????????

7

0204060

0 20 40 60X

Y

Slope unchanged

Intercept changed[WF]

Page 8: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

QUESTIONHow would you draw a line through the points? How do you determine which line “fits the best” ?????????

8

0204060

0 20 40 60X

Y

Slope changed

Intercept changed

[WF]

Page 9: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

LEAST SQUARESBest fit: difference between the true (observed) Y-values and the estimated Y-values is minimized: • Positive errors offset negative errors … • … square the error!

Least squares minimizes the sum of the squared errors

9[WF]

n

∑i=1

(yi − yi)2 =n

∑i=1

ϵ2i

Page 10: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

10

LEAST SQUARES, GRAPHICALLY

ε2

Y

X

ε1 ε3

ε4

^^

^^

[WF]

LS Minimizes n

∑i=1

ϵ2i = ϵ2

1 + ϵ22 + … + ϵ2

n

y2 = θ0 + θ1x2 + ϵ2

yi = θ0 + θ1xi

Page 11: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Example

■ Single Variable Linear Regression estimate yi = θ0 + θ1xi

160014002100 … …. 2400

Area(sq. ft.)y

220180350…….500

Price (in 1000$) x

Page 12: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Multivariate Regression

■ Multi Linear Regression

Price (in 1000$)

220180350…….500

Area(sq. ft.)

160014002100 … ….

2400

# Bathrooms # Bedrooms

2.51.53.5……4

334……5

yi = θ0 + θ1xi1 + θ2xi2 + … + θnxin

y x1 x2 x3

Page 13: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Multivariate Regression

■ Multi Linear Regression

Price (in 1000$)

220180350…….500

Area(sq. ft.)

160014002100 … ….

2400

# Bathrooms # Bedrooms

2.51.53.5……4

334……5 1400

1.53

yi

yi = θ0 + θ1xi1 + θ2xi2 + … + θnxin

xi

xi1

xi2

xi3

Page 14: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Multivariate Regression

■ Multi Linear Regression

Price (in 1000$)

220180350…….500

Area(sq. ft.)

160014002100 … ….

2400

# Bathrooms # Bedrooms

2.51.53.5……4

334……5

yi

14001.53

1

yi = θ0xi0 + θ1xi1 + θ2xi2 + … + θnxin

xi

xi1

xi2

xi3

xi0

111…….1

y x1 x2 x3x0

Page 15: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Multivariate Regression Model

■ Model:

feature 1 = x0 …. (constant, 1)feature 2 = x1 …. (area, sq. ft.)feature 3 = x2 …. (# of bedrooms)feature 4 = x3 …. (# of bathrooms)….….feature n = xn

yi = θ0xi0 + θ1xi1 + θ2xi2 + … + θnxin

yi =n

∑j=0

θijxij

Page 16: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Single Variable Linear Regression

Cost

($)

Area (sq. ft)

1 sq. ft.

predicted change in $

base price

yi = θ0 + θ1xi

Page 17: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Interpreting Coefficients■ Two Linear Features

pric

e ($

)

Area (in sq. ft.)# o

f bed

rooms

fix

y = θ0 + θ1x1 + θ2x2

Page 18: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Single Variable Linear Regression

Cost

($)

,

# of Bedrooms,

1 bedroom

predicted change in $

fix

y = θ0 + θ1x1 + θ2x2

for fixed area (sq. ft.),

x2

x1

y

Page 19: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

Interpreting Coefficients■ Multiple Features

pric

e ($

)

Area (in sq. ft.)# o

f bed

rooms

fix fix fix fix

y = θ0 + θ1x1 + θ2x2 + … + θjxj + … + θmxm

Page 20: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

One Observation Model

■ Matrix Notation For observation i

yi =m

∑j=0

θijxij

yi =xio xi1 xi2 . . . …. xim

θ0. . . …

.θ2

θmθ1

yi = XTi θ

Page 21: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

All Observation Model

■ Matrix Notation For all observations

y1

y2

y3

.

.

.

yn

=

x10

x20

x30

.

.

.xn0

x11

x21

x31

.

.

.xn1

x12

x22

x32

.

.

.xn2

..

..

...

.

.

..

..

..

...

.

.

..

x1m

x2m

x3m

.

.

.

xnm

.θm

θ0

θ1

θ2

Y = Xθ

Page 22: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

LEAST SQUARES OPTIMIZATION

Rewrite inputs:

Rewrite optimization problem:

Each row is a feature vector paired with a label for a single input

n labeled inputs

m features

X =

(x(1))T

(x(2))T

⋯(x(n))T

∈ ℝn×m, y =

y(1)

y(2)

⋯y(n)

∈ ℝn

*Recall  | |z | |22 = zT z = ∑

i

z2i

Page 23: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

LEAST SQUARES OPTIMIZATION

Rewrite inputs:

Rewrite optimization problem:

Each row is a feature vector paired with a label for a single input

n labeled inputs

m features

X =

(x(1))T

(x(2))T

⋯(x(n))T

∈ ℝn×m, y =

y(1)

y(2)

⋯y(n)

∈ ℝn

*Recall  | |z | |22 = zT z = ∑

i

z2i

⟹ minimize n

∑i=1

(yi − yi)2 =n

∑i=1

ϵ2i

Page 24: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

ERROR FUNCTION

24

θ

n

∑i=1

(yi − yi)2 =n

∑i=1

ϵ2i

ϵ

Page 25: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

GRADIENTSMinimizing a multivariate function involves finding a point where the gradient is zero:

Points where the gradient is zero are local minima • If the function is convex, also a global minimum Let’s solve the least squares problem! • Chain rule:

• Gradient of squared ℓ2 norm:

25

Page 26: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

LEAST SQUARESRecall the least squares optimization problem:

What is the gradient of the optimization objective ????????

26

Chain rule:

Gradient of norm:

Page 27: MSML 605 - Lecture 5nayeem/courses/MSML605/files/05_Least... · 2020-04-03 · SCATTER PLOT Plot all (X i, Y i) pairs, and plot your learned model 4 0 20 40 60 0 20 40 60 X Y [WF]

LEAST SQUARESRecall: points where the gradient equals zero are minima.

So where do we go from here?????????

27

Solve for model parameters θ