42
Designing Nonlinear Features Linear regression and beyond Marek Petrik 4/11/2017

Designing Nonlinear Features

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Designing Nonlinear Features

Designing Nonlinear FeaturesLinear regression and beyond

Marek Petrik

4/11/2017

Page 2: Designing Nonlinear Features

Linear Regression

I Can linear regression fit non-linear functions?

I Can logistic regression be used to compute non-linear decisionboundaries?

I What feature transformations do you know?I How is it related to kernels?

Page 3: Designing Nonlinear Features

Linear Regression

I Can linear regression fit non-linear functions?I Can logistic regression be used to compute non-linear decision

boundaries?

I What feature transformations do you know?I How is it related to kernels?

Page 4: Designing Nonlinear Features

Linear Regression

I Can linear regression fit non-linear functions?I Can logistic regression be used to compute non-linear decision

boundaries?I What feature transformations do you know?

I How is it related to kernels?

Page 5: Designing Nonlinear Features

Linear Regression

I Can linear regression fit non-linear functions?I Can logistic regression be used to compute non-linear decision

boundaries?I What feature transformations do you know?I How is it related to kernels?

Page 6: Designing Nonlinear Features

When to Fit Nonlinear Model?

I Residual plot

5 10 15 20 25 30

−15

−10

−5

05

10

15

20

Fitted values

Resid

uals

Residual Plot for Linear Fit

323

330

334

15 20 25 30 35

−15

−10

−5

05

10

15

Fitted values

Resid

uals

Residual Plot for Quadratic Fit

334323

155

Page 7: Designing Nonlinear Features

When to Fit Nonlinear Model?

I Residual plot

5 10 15 20 25 30

−15

−10

−5

05

10

15

20

Fitted values

Resid

uals

Residual Plot for Linear Fit

323

330

334

15 20 25 30 35−

15

−10

−5

05

10

15

Fitted values

Resid

uals

Residual Plot for Quadratic Fit

334323

155

Page 8: Designing Nonlinear Features

Approaches to Nonlinear Feature Relationship

Today: Problems with a single variable

I We will cover:1. Polynomial regression2. Step functions3. Regression splines4. Smoothing splines5. Local regression6. Generalized additive models

I Others significant ones:1. Fourier Analysis2. Wavelets

Page 9: Designing Nonlinear Features

Approaches to Nonlinear Feature Relationship

Today: Problems with a single variableI We will cover:

1. Polynomial regression2. Step functions3. Regression splines4. Smoothing splines5. Local regression6. Generalized additive models

I Others significant ones:1. Fourier Analysis2. Wavelets

Page 10: Designing Nonlinear Features

Polynomial Regression

I Standard linear model:

yi = β0 + β1xi + εi

I Polynomial function:

yi = β0 + β1xi + β2xi + β3xi + . . . βdxi + εi

Page 11: Designing Nonlinear Features

Example Polynomial Regression

I Linear regression:

mpg = β0 + β1 × power

I Degree 2 (�adratic):

mpg = β0 + β1 × power + β2 × power2

I Degree k:

mpg =

k∑i=0

βk × powerk

Page 12: Designing Nonlinear Features

Polynomial Functions

50 100 150 200

10

20

30

40

50

Horsepower

Mile

s p

er

gallo

n

Linear

Degree 2

Degree 5

Page 13: Designing Nonlinear Features

Polynomial Functions (Linear and Logistic)

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Degree−4 Polynomial

20 30 40 50 60 70 80

0.0

00

.05

0.1

00

.15

0.2

0

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Page 14: Designing Nonlinear Features

Why Polynomial Regression is Insu�icient?

I Does not account for local non-linearityI Limited a-priori knowledgeI Very unstable in extreme rangesI Di�erent problems require di�erent structure

Page 15: Designing Nonlinear Features

Step Functions

I Similar to dummy variables, but for quantitative featuresI Create cut points c1.c2. . . . , cKI ConstructK + 1 new features:

C0(X) = I(X < c1)

C1(X) = I(c1 ≤ X < c2)

...

I I(·) is an indicator function

Page 16: Designing Nonlinear Features

Step Functions Example

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Piecewise Constant

20 30 40 50 60 70 80

0.0

00

.05

0.1

00

.15

0.2

0

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Step functions are not continuous!

Page 17: Designing Nonlinear Features

Step Functions Example

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Piecewise Constant

20 30 40 50 60 70 80

0.0

00

.05

0.1

00

.15

0.2

0

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Step functions are not continuous!

Page 18: Designing Nonlinear Features

Basis Functions

I Polynomial functions are new basis functionsI Step functions are new basis functionsI Basis Functions: Span linear spaceI Linear algebra detour

Page 19: Designing Nonlinear Features

Basis of Vector Space

I Vectors X1, X2, . . . , XK

I Span of vectors (space):

α1X1 + α2X2 + . . .+ αKXK

I Basis: smallest set of vectors that spans a space

Page 20: Designing Nonlinear Features

Column View of Linear Regression

I Linear regression:minβ‖y −Xβ‖22

I Treat vectors as columns:

minβ‖y −X1β1 − . . .−XKβK‖22

I Interpretation: closest point to y in space spanned byX1, . . . , XK

I Features are the basis!

Page 21: Designing Nonlinear Features

Column View of Linear Regression

I Linear regression:minβ‖y −Xβ‖22

I Treat vectors as columns:

minβ‖y −X1β1 − . . .−XKβK‖22

I Interpretation: closest point to y in space spanned byX1, . . . , XK

I Features are the basis!

Page 22: Designing Nonlinear Features

Regression Splines

I Polynomials are not localI Step functions are not continuous or smooth

I Regression splines are local and smoothI Derivation in several steps

Page 23: Designing Nonlinear Features

Regression Splines

I Polynomials are not localI Step functions are not continuous or smooth

I Regression splines are local and smooth

I Derivation in several steps

Page 24: Designing Nonlinear Features

Regression Splines

I Polynomials are not localI Step functions are not continuous or smooth

I Regression splines are local and smoothI Derivation in several steps

Page 25: Designing Nonlinear Features

Step 1: Step Function as Piecewise Polynomials

I Step functions (minor change in ≤):

C0(X) = I(X < c1)

C1(X) = I(c1 < X ≤ c2)...

I Step-functions are piece-wise polynomials of degree 0

Ci(X) =

{1 if X > ci and X ≤ ci+1

0 otherwise

I Di�erent representation (basis spans the same space!):

Ci(X) =

{1 if X > ci−1

0 otherwise

Page 26: Designing Nonlinear Features

Step 2: Piecewise Polynomials

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Piecewise polynomials of degree 2:

Pi(X) =

{X2 if X > ci and X ≤ ci+1

0 otherwise

I Piecewise polynomials of degree 3:

Pi(X) =

{X3 if X > ci and X ≤ ci+1

0 otherwise

I Local but not continuous!

Page 27: Designing Nonlinear Features

Step 2: Piecewise Polynomials

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Piecewise polynomials of degree 2:

Pi(X) =

{X2 if X > ci and X ≤ ci+1

0 otherwise

I Piecewise polynomials of degree 3:

Pi(X) =

{X3 if X > ci and X ≤ ci+1

0 otherwise

I Local but not continuous!

Page 28: Designing Nonlinear Features

Step 3: Continuity and Regression Splines

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Must prevent discontinuity in knotsI Di�erent representation:

Hi(X) =

{X − ci−1 if X > ci−1

0 otherwise

I Each feature is 0 in its knot

Page 29: Designing Nonlinear Features

Step 3: Continuity and Regression Splines

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Must prevent discontinuity in knots

I Di�erent representation:

Hi(X) =

{X − ci−1 if X > ci−1

0 otherwise

I Each feature is 0 in its knot

Page 30: Designing Nonlinear Features

Step 3: Continuity and Regression Splines

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Must prevent discontinuity in knotsI Di�erent representation:

Hi(X) =

{X − ci−1 if X > ci−1

0 otherwise

I Each feature is 0 in its knot

Page 31: Designing Nonlinear Features

Step 3: Continuity and Regression Splines

I Piecewise polynomials of degree 1:

Pi(X) =

{X if X > ci and X ≤ ci+1

0 otherwise

I Must prevent discontinuity in knotsI Di�erent representation:

Hi(X) =

{X − ci−1 if X > ci−1

0 otherwise

I Each feature is 0 in its knot

Page 32: Designing Nonlinear Features

General Regression Splines

I Regression splines of degree d:

Hi(X) =

{(X − ci−1)d if X > ci−1

0 otherwise

I Compact representation:

h(x, ξ) = ([x− ξ]+)d = (max{x− ξ, 0})d

I Most common are cubic splines: continuous and continuouslydi�erentiable

Page 33: Designing Nonlinear Features

General Regression Splines

I Regression splines of degree d:

Hi(X) =

{(X − ci−1)d if X > ci−1

0 otherwise

I Compact representation:

h(x, ξ) = ([x− ξ]+)d = (max{x− ξ, 0})d

I Most common are cubic splines: continuous and continuouslydi�erentiable

Page 34: Designing Nonlinear Features

General Regression Splines

I Regression splines of degree d:

Hi(X) =

{(X − ci−1)d if X > ci−1

0 otherwise

I Compact representation:

h(x, ξ) = ([x− ξ]+)d = (max{x− ξ, 0})d

I Most common are cubic splines: continuous and continuouslydi�erentiable

Page 35: Designing Nonlinear Features

Example Splines

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Continuous Piecewise Cubic

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Cubic Spline

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Linear Spline

Page 36: Designing Nonlinear Features

Natural SplinesBoundary segments are linear

20 30 40 50 60 70

50

100

150

200

250

Age

Wage

Natural Cubic Spline

Cubic Spline

Page 37: Designing Nonlinear Features

Natural Splines and Logistic Regression

20 30 40 50 60 70 80

50

10

01

50

20

02

50

30

0

Age

Wa

ge

Natural Cubic Spline

20 30 40 50 60 70 80

0.0

00

.05

0.1

00

.15

0.2

0

Age

| | || | ||| | ||| | | ||| | || | | |||

|

|| || ||| | | | || || || | || |

|

|| | | |

|

| || || || | | || | ||| || ||| | | |

|

| || | ||| || | || |||| || || ||| || || ||| |||| || | | | ||

|

|| || ||| ||| || || ||| || ||| | || ||| |||| ||| || | | | ||| || |||| |||| || || | ||||| | || || || | ||| | || ||| || | || ||

|

||| | || | || || | || | ||| || || | || ||| |

|

| | |||| ||| | || | |||| ||| || || ||| | | || || |||||| || | || || | || || | | || || || | || ||| || | || || ||||| ||||| || || || || | |||| || || ||| | || || || |

|

| |||| ||| || || || ||| | | ||

|

|| |

|

| || || || ||| || || | || || || | || ||| || | ||| || | || || || | |||| || | |||| | |||| || | | | ||||

|

| || || || || || |

|

| |||| || || |||| | || || || ||| | || |||| || |

|

| | |||| || || || |

|

|| |||| ||| ||

|

||| || |||| | || || | | |||

|

||| || | || | || | || || ||||| | | ||| |

|

| | || || ||| ||| | || |

|

|| | || || | |||| || | || || | ||| || || || || |||| || | ||||| | | |||| | || ||| || ||| |

|

| ||| | || || | | || |

|

| | | ||| |||| || || | | || || | |||| | | | ||| | || | |||| ||| | |

|

|| ||||| ||| | | || || || || || || || |

|

| || || || | ||| || || | || || |||| |||| |

|

| || || ||| || | | ||| || || | ||| ||| || || |

|

||| || || || || || | | ||| | || ||| || || | |||| || | |

|

|| || ||||| || | || || ||| | ||| | || ||| ||||| || ||||| ||| | ||| ||| | || || || ||| || || | | || |

|

| || |||| ||| | |||

|

| | | | || | ||| | | || | |||| || ||| || | ||| || | ||| ||

|

|| || |||| | ||| | || | | ||| |||| || ||| || || || | | || | || | || || || || | | || || | |

|

|| ||| ||||| ||| ||| || ||||| || || | ||| || | | || | ||| | | ||| || || || || | ||| ||| || || |||

|

| || || ||| | | ||| | |||| | || || ||||

|

| | || | || | || | |||

|

| || || ||| | | ||| ||| | || ||| || || ||| | |||| | ||| | ||| | || | || | || | | || || || || || |||| || | | || | | | |||| || | ||| | || ||| || || ||| ||

|

||| ||| | || || || | | || | || || || || || || | || || | || || |

|

| || ||| || |

| |

| ||| | || || |

|

| |||| ||| | |||| ||

|

| ||| ||| ||| |||| |

|

| || || || || ||| | | | || || | ||| || | || | || | |||| | ||| ||| ||

|

| | ||||| ||| | | || || | | |||| | |||| ||| ||| | || | || || || | || | || || ||| | || ||| | || || ||| | | | |||| | || | | ||| ||| |||| | | ||| | |||| | || | || || | ||

|

| || ||||| || ||| ||| || | | ||||| || |||| || | | ||| | || || || ||| |||| |||| | | || || || | ||| | || || || | | || || || |||| || ||| || ||| || |

|

| || || |||| || | ||| | ||| || | || |||| |||| ||| | | | || ||| | || | | ||

|

|| |||| ||| ||| || | | |||| ||| |||| || |||| || || ||| |||| | ||| | |

|

|| | || || || | ||| | || ||| || ||| | || || ||| | || || || | || ||| | || || |||| || || | || ||| ||

|

|| || | || || || | || | ||| | ||| || | || || ||| || ||| ||| || | || || | | || || || ||| || || || | ||| || | |||

|

|| | |

|

||| | | | || ||| || | ||||| | | || || || | | || || || | | || ||| | |||| |

|

||||| | | | || || | | ||| || | | || | || | ||| || |||| | ||| | || || ||||| | || ||| ||| | || || || || || ||| | ||||| || || ||| ||| || | | || || || ||

|

| || | | || | || || | || || || | |||| | | | ||| | | ||

|

| | || ||

|

|| | | ||| || ||| || || | || || || || | | || ||| || ||| || || || ||| | ||| || ||| || ||| | ||| | | | || || | ||| ||| || | ||

|

|||| |

|

|| | |||| ||| | || || ||| || ||| | |||| || |

|

|| ||| ||| | ||| | || | | | ||| || | || || ||| | | | ||| || || ||| || | ||| | || |||| | |||| | ||| || || || || || | ||| || || | | ||| || || |||| ||| || | || ||| || | ||| |

|

| || | |||

|

| | || || | ||| || |

|

| | ||| || || || | | || | ||| | | ||| || | | || | | || ||||| || || |||| | ||| | | || || | | || | | |

|

|| || |||| | || |||| |

||

| | | ||||| |||

|

|| |||| | |||| || |

|

| | || ||||| ||||| | || || || | || ||| ||| | || ||| || ||| || | || || ||| || | | | || || ||| | || || | || || |

|

| || ||

|

|| || ||| || | | | || |||| || |||| ||| || |||| || || | ||| | |||

|

|| ||| | |

| |

|| || | ||| || ||| | | |||| | ||| | |||| || ||| || || | ||| | ||| | |||| || | || |||| | ||||| ||| | | ||| | ||| || ||| || | ||| || ||| | ||| || | ||| | | || || || || | ||| || || || |||| ||| | ||| || || |||| || |||

|

| |||

|

| ||

|

| |

|

|

|

| | | || || |||

|

|||| ||

|

|| || || || || || | | ||||| | ||| || | ||| ||| || ||| || | | || || | || | || ||| |||| || || ||| |||| ||| ||| ||| | | || |

|

| ||| || || || ||| ||| | ||| | || || ||| || || ||| ||

|

| ||| | || | || || |||| || ||| || | | ||| || | || ||| || || | || ||

|

| | ||| || | | | ||

|

| | || | | ||| | || | || | ||| || || ||| | | || |

|

|| ||| || || | || || |||| || || || | || || | || ||| | || ||| | || ||| || || | | || || ||| || || || ||| |||| |

Pr(Wage>

250|A

ge)

Page 38: Designing Nonlinear Features

Natural Splines vs Polynomials

20 30 40 50 60 70 80

50

100

150

200

250

300

Age

Wage

Natural Cubic Spline

Polynomial

Page 39: Designing Nonlinear Features

Choosing Knots

I Domain dependentI Change of mode (retirement?)I �antiles of data is generally a good choiceI Number of knots = degrees of freedom

Page 40: Designing Nonlinear Features

Smoothing Splines

I Extreme version of regression splinesI Knot in every data point

I Must have regularization to generalize

n∑i=1

(yi − g(xi))2 + λ

∫g′′(t)2dt

I Smoothing parameter λ chosen by LOOCVI E�ective degrees of freedom: technical, but not very important

Page 41: Designing Nonlinear Features

Smoothing Splines

I Extreme version of regression splinesI Knot in every data pointI Must have regularization to generalize

n∑i=1

(yi − g(xi))2 + λ

∫g′′(t)2dt

I Smoothing parameter λ chosen by LOOCVI E�ective degrees of freedom: technical, but not very important

Page 42: Designing Nonlinear Features

Finishing the Book

Read also 7.6 and 7.7:I Local regressionI General Additive Models