linear regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · linear regression (a...

41
Linear Regression Karl Stratos June 18, 2018 1 / 25

Upload: others

Post on 19-Aug-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Linear Regression

Karl Stratos

June 18, 2018

1 / 25

Page 2: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Regression Problem

I Problem. Find a desired input-output mapping f : X → Rwhere the output is a real value.

x = =⇒ y = 0.1◦

“How much should I turn my handle, given the environment?”

I Today’s focus: data-driven approach to regression

2 / 25

Page 3: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Regression Problem

I Problem. Find a desired input-output mapping f : X → Rwhere the output is a real value.

x = =⇒ y = 0.1◦

“How much should I turn my handle, given the environment?”

I Today’s focus: data-driven approach to regression

2 / 25

Page 4: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

3 / 25

Page 5: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Running Example: Predict Weight from Height

I Suppose we want a regression model f : X → R that predictsweight (in pounds) from height (in inches).

I What is the input space?

X = R

I Naive approach: stipulate rules.I If x ∈ [0, 30), then predict y = 50.I If x ∈ [30, 60), then predict y = 80.I If x ∈ [60, 70), then predict y = 150.I If x ≥ 70, then predict y = 200.

I Pro: Immediately programmable

I Cons: “Uninformed”, requires labor-intensive domain-specificrule engineering

I There is no learning from data (on the machine’s part).

4 / 25

Page 6: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Running Example: Predict Weight from Height

I Suppose we want a regression model f : X → R that predictsweight (in pounds) from height (in inches).

I What is the input space? X = R

I Naive approach: stipulate rules.I If x ∈ [0, 30), then predict y = 50.I If x ∈ [30, 60), then predict y = 80.I If x ∈ [60, 70), then predict y = 150.I If x ≥ 70, then predict y = 200.

I Pro: Immediately programmable

I Cons: “Uninformed”, requires labor-intensive domain-specificrule engineering

I There is no learning from data (on the machine’s part).

4 / 25

Page 7: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Running Example: Predict Weight from Height

I Suppose we want a regression model f : X → R that predictsweight (in pounds) from height (in inches).

I What is the input space? X = R

I Naive approach: stipulate rules.I If x ∈ [0, 30), then predict y = 50.I If x ∈ [30, 60), then predict y = 80.I If x ∈ [60, 70), then predict y = 150.I If x ≥ 70, then predict y = 200.

I Pro: Immediately programmable

I Cons: “Uninformed”, requires labor-intensive domain-specificrule engineering

I There is no learning from data (on the machine’s part).

4 / 25

Page 8: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Running Example: Predict Weight from Height

I Suppose we want a regression model f : X → R that predictsweight (in pounds) from height (in inches).

I What is the input space? X = R

I Naive approach: stipulate rules.I If x ∈ [0, 30), then predict y = 50.I If x ∈ [30, 60), then predict y = 80.I If x ∈ [60, 70), then predict y = 150.I If x ≥ 70, then predict y = 200.

I Pro: Immediately programmable

I Cons: “Uninformed”, requires labor-intensive domain-specificrule engineering

I There is no learning from data (on the machine’s part).

4 / 25

Page 9: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Running Example: Predict Weight from Height

I Suppose we want a regression model f : X → R that predictsweight (in pounds) from height (in inches).

I What is the input space? X = R

I Naive approach: stipulate rules.I If x ∈ [0, 30), then predict y = 50.I If x ∈ [30, 60), then predict y = 80.I If x ∈ [60, 70), then predict y = 150.I If x ≥ 70, then predict y = 200.

I Pro: Immediately programmable

I Cons: “Uninformed”, requires labor-intensive domain-specificrule engineering

I There is no learning from data (on the machine’s part).

4 / 25

Page 10: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Before We Move on to Data-Driven Approaches

Rule-based solutions can go surpringly far.

eliza: a conversation program from the 60s

5 / 25

Page 11: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

6 / 25

Page 12: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Data

I A set of n height-weight pairs(x(1), y(1)) . . . (x(n), y(n)) ∈ R× R

Q. How can we use this data to obtain a weight predictor?

7 / 25

Page 13: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Simple Data-Specific Rules

Store all n data points in a dictionary D(x(i)) = y(i).

1. Predict by memorization (“rote learning”):

f(x) =

{D(x) if x ∈ D

? otherwise

2. Or slightly better, predict by nearest neighbor search:

f(x) = D

(n

argmini=1

∣∣∣∣∣∣x− x(i)∣∣∣∣∣∣)

8 / 25

Page 14: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Nonparameteric Models

I These are simplest instances of nonparameteric models.I It just means that the model doesn’t have any associated

parameters before seeing the data.

I Pro: Adapts to data without assuming anything about a givenproblem, achieving better “coverage” with more data

I ConsI Not scalable: need to store the entire dataI Issues with “overfitting”: model excessively dependent on

data, generalizing to new instances can be difficult.

9 / 25

Page 15: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

10 / 25

Page 16: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Parameteric ModelsI Dominant approach in machine learning.

I Assumes a fixed form of model fθ defined by a set ofparameters θ.

I The parameters θ are learned from data S ={(x(i), y(i))

}ni=1

by optimizing a data-dependent “objective” or “loss”function JS(θ) ∈ R.

I Optimizing JS wrt. parameter θ is the learning problem!

θ∗ = argminθ

JS(θ)

I Today: Focus on a simplest parametric model called linearregression.

11 / 25

Page 17: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Parameteric ModelsI Dominant approach in machine learning.

I Assumes a fixed form of model fθ defined by a set ofparameters θ.

I The parameters θ are learned from data S ={(x(i), y(i))

}ni=1

by optimizing a data-dependent “objective” or “loss”function JS(θ) ∈ R.

I Optimizing JS wrt. parameter θ is the learning problem!

θ∗ = argminθ

JS(θ)

I Today: Focus on a simplest parametric model called linearregression.

11 / 25

Page 18: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Parameteric ModelsI Dominant approach in machine learning.

I Assumes a fixed form of model fθ defined by a set ofparameters θ.

I The parameters θ are learned from data S ={(x(i), y(i))

}ni=1

by optimizing a data-dependent “objective” or “loss”function JS(θ) ∈ R.

I Optimizing JS wrt. parameter θ is the learning problem!

θ∗ = argminθ

JS(θ)

I Today: Focus on a simplest parametric model called linearregression.

11 / 25

Page 19: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Parameteric ModelsI Dominant approach in machine learning.

I Assumes a fixed form of model fθ defined by a set ofparameters θ.

I The parameters θ are learned from data S ={(x(i), y(i))

}ni=1

by optimizing a data-dependent “objective” or “loss”function JS(θ) ∈ R.

I Optimizing JS wrt. parameter θ is the learning problem!

θ∗ = argminθ

JS(θ)

I Today: Focus on a simplest parametric model called linearregression.

11 / 25

Page 20: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

12 / 25

Page 21: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Linear Regression ModelI Model parameter: w ∈ R

I Model definition:

fw(x) := wx

I Defines a line with slope w

I Goal: learn w from data S ={(x(i), y(i))

}ni=1

I Need a data-dependent objective function JS(w)

13 / 25

Page 22: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Least Squares Objective

I Least squares objective: minimize

JLSS (w) :=

n∑i=1

(y(i) − wx(i)

)2I Idea: fit a line on the training data by reducing the sum of

squared residuals

14 / 25

Page 23: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

15 / 25

Page 24: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the scalar

wLSS = argmin

w∈R

n∑i=1

(y(i) − wx(i)

)2︸ ︷︷ ︸

JLSS (w)

I The objective JLSS (w) is strongly convex in w (unless all

x(i) = 0), thus the global minimum is uniquely achieved bywLSS satisfying

∂JLSS (w)

∂w

∣∣∣∣w=wLS

S

= 0

I Solving this system yields the close-form expression:

wLSS =

∑ni=1 x

(i)y(i)∑ni=1(x

(i))2

16 / 25

Page 25: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the scalar

wLSS = argmin

w∈R

n∑i=1

(y(i) − wx(i)

)2︸ ︷︷ ︸

JLSS (w)

I The objective JLSS (w) is strongly convex in w (unless all

x(i) = 0), thus the global minimum is uniquely achieved bywLSS satisfying

∂JLSS (w)

∂w

∣∣∣∣w=wLS

S

= 0

I Solving this system yields the close-form expression:

wLSS =

∑ni=1 x

(i)y(i)∑ni=1(x

(i))2

16 / 25

Page 26: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the scalar

wLSS = argmin

w∈R

n∑i=1

(y(i) − wx(i)

)2︸ ︷︷ ︸

JLSS (w)

I The objective JLSS (w) is strongly convex in w (unless all

x(i) = 0), thus the global minimum is uniquely achieved bywLSS satisfying

∂JLSS (w)

∂w

∣∣∣∣w=wLS

S

= 0

I Solving this system yields the close-form expression:

wLSS =

∑ni=1 x

(i)y(i)∑ni=1(x

(i))216 / 25

Page 27: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

17 / 25

Page 28: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Linear Regression with Multi-Dimensional InputI Input x ∈ Rd is now a vector of d features x1 . . . xd ∈ R.

x1 = 65 (height)x2 = 29 (age)x3 = 1 (male indicator)x4 = 0 (female indicator)

=⇒ y = 140 (pounds)

I Model: w ∈ Rd defining

fw(x) := w · x = w>x = 〈w,x〉= w1x1 + · · ·+ wdxd

I Least squares objective: exactly the same. Assume n ≥ d!

JLSS (w) =

n∑i=1

y(i)︸︷︷︸R

−w · x(i)︸ ︷︷ ︸R

2

18 / 25

Page 29: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Linear Regression with Multi-Dimensional InputI Input x ∈ Rd is now a vector of d features x1 . . . xd ∈ R.

x1 = 65 (height)x2 = 29 (age)x3 = 1 (male indicator)x4 = 0 (female indicator)

=⇒ y = 140 (pounds)

I Model: w ∈ Rd defining

fw(x) := w · x = w>x = 〈w,x〉= w1x1 + · · ·+ wdxd

I Least squares objective: exactly the same. Assume n ≥ d!

JLSS (w) =

n∑i=1

y(i)︸︷︷︸R

−w · x(i)︸ ︷︷ ︸R

2

18 / 25

Page 30: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Linear Regression with Multi-Dimensional InputI Input x ∈ Rd is now a vector of d features x1 . . . xd ∈ R.

x1 = 65 (height)x2 = 29 (age)x3 = 1 (male indicator)x4 = 0 (female indicator)

=⇒ y = 140 (pounds)

I Model: w ∈ Rd defining

fw(x) := w · x = w>x = 〈w,x〉= w1x1 + · · ·+ wdxd

I Least squares objective: exactly the same. Assume n ≥ d!

JLSS (w) =

n∑i=1

y(i)︸︷︷︸R

−w · x(i)︸ ︷︷ ︸R

2

18 / 25

Page 31: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the vector

wLSS = argmin

w∈Rd

n∑i=1

(y(i) −w · x(i)

)2= argmin

w∈Rd

||y −Xw||22

where yi = y(i) ∈ R and X ∈ Rn×d has rows x(i).

I ||y −Xw||22 is strongly convex in w (unless rank(X) < d),thus the global minimum is uniquely achieved by wLS

S

satisfying

∂ ||y −Xw||22∂w

∣∣∣∣w=wLS

S

= 0d×1

I Solving this system yields the close-form expression:

wLSS = (X>X)−1X>y = X+y

19 / 25

Page 32: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the vector

wLSS = argmin

w∈Rd

n∑i=1

(y(i) −w · x(i)

)2= argmin

w∈Rd

||y −Xw||22

where yi = y(i) ∈ R and X ∈ Rn×d has rows x(i).

I ||y −Xw||22 is strongly convex in w (unless rank(X) < d),thus the global minimum is uniquely achieved by wLS

S

satisfying

∂ ||y −Xw||22∂w

∣∣∣∣w=wLS

S

= 0d×1

I Solving this system yields the close-form expression:

wLSS = (X>X)−1X>y = X+y

19 / 25

Page 33: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

The Learning ProblemI Solve for the vector

wLSS = argmin

w∈Rd

n∑i=1

(y(i) −w · x(i)

)2= argmin

w∈Rd

||y −Xw||22

where yi = y(i) ∈ R and X ∈ Rn×d has rows x(i).

I ||y −Xw||22 is strongly convex in w (unless rank(X) < d),thus the global minimum is uniquely achieved by wLS

S

satisfying

∂ ||y −Xw||22∂w

∣∣∣∣w=wLS

S

= 0d×1

I Solving this system yields the close-form expression:

wLSS = (X>X)−1X>y = X+y

19 / 25

Page 34: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Overview

Approaches to the Regression ProblemNot Data-DrivenData-Driven: NonparametericData-Driven: Parameteric

Linear Regression (a Parameteric Approach)Model and ObjectiveParameter EstimationGeneralization to Multi-Dimensional Input

Polynomial Regression

20 / 25

Page 35: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Fitting a Polynomial: 1-Dimensional InputI In linear regression with scalar input, we learn the slope w ∈ R

of a line such that y ≈ wx.

I In polynomial regression, we learn the coefficientsw1 . . . wp, wp+1 ∈ R of a polynomial of degree p such that

y ≈ w1xp + · · ·+ wpx+ wp+1︸ ︷︷ ︸

bias term

I How? Upon receiving input x, apply polynomial featureexpansion to calculate a new representation of x:

x 7→

xp

...x1

Follow by linear regression with (p+ 1)-dimensional input.

21 / 25

Page 36: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Fitting a Polynomial: 1-Dimensional InputI In linear regression with scalar input, we learn the slope w ∈ R

of a line such that y ≈ wx.

I In polynomial regression, we learn the coefficientsw1 . . . wp, wp+1 ∈ R of a polynomial of degree p such that

y ≈ w1xp + · · ·+ wpx+ wp+1︸ ︷︷ ︸

bias term

I How? Upon receiving input x, apply polynomial featureexpansion to calculate a new representation of x:

x 7→

xp

...x1

Follow by linear regression with (p+ 1)-dimensional input.

21 / 25

Page 37: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Fitting a Polynomial: 1-Dimensional InputI In linear regression with scalar input, we learn the slope w ∈ R

of a line such that y ≈ wx.

I In polynomial regression, we learn the coefficientsw1 . . . wp, wp+1 ∈ R of a polynomial of degree p such that

y ≈ w1xp + · · ·+ wpx+ wp+1︸ ︷︷ ︸

bias term

I How? Upon receiving input x, apply polynomial featureexpansion to calculate a new representation of x:

x 7→

xp

...x1

Follow by linear regression with (p+ 1)-dimensional input.

21 / 25

Page 38: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Degree of Polynomial = Model ComplexityI p = 0: Fit a bias termI p = 1: Fit a slope and a bias term (i.e., an affine function)I p = 2: Learn a quadratic functionI . . .

https://machinelearningac.wordpress.com/2011/09/15/model-selection-and-the-triple-tradeoff/22 / 25

Page 39: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Polynomial Regression with Multi-Dimensional InputExample: p = 2

x1...xd

7→

x21...x2dx1x2

...xdxd−1x1...xd1

In general: time to calculate feature expansion O(dp) isexponential in p.

23 / 25

Page 40: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Summary

I Regression is the problem of learning a real-valued mappingf : X → R.

I Linear regressor is a simplest parametric model that usesparameter w ∈ Rd to define fw(x) = w · x.

I Fitting a linear regressor on a dataset by a least squaresobjective so easy that it has a closed-form solution.

I Polynomial regression: feature expansion followed by linearregression

24 / 25

Page 41: Linear Regression - ttic.uchicago.edusuriya/website-intromlss2018/course... · Linear Regression (a Parameteric Approach) Model and Objective Parameter Estimation Generalization to

Last Remarks

I What if we have a model/objective such that training doesn’thave a closed-form solution?

I Instead of manually fixing dictating the input representation(e.g., a polynomial of degree 3), can we automatically learn agood representation function φ(x) as part of optimization?

I We will answer these questions later in the course (hint:gradient descent, neural networks).

25 / 25