lecture2:interpolation - wordpress.com...polynomialapproximation...

31
Lecture 2: Interpolation Fatih Guvenen Spring 2019 Fatih Guvenen Lecture 2: Interpolation 1 / 29

Upload: others

Post on 04-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Lecture 2: Interpolation

Fatih GuvenenSpring 2019

Fatih Guvenen Lecture 2: Interpolation 1 / 29

Page 2: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation

Page 3: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

General Idea

▶ Suppose you are given a grid (x1, x2,..., xn) and the functionvalues (y1, y2, ..., yn) at corresponding points generated byfunction f(x).

▶ Q: How to find values off the grid points that provides a “goodapproximation” to f(x)?

▶ A good approximation is often taken to mean to minimize∥∥∥f(x)− f̂(x)∥∥∥ according to some norm (Lp, sup-, etc).

▶ In economics another very important concern is to preserve theshape—ie, concavity or convexity—of the interpolated (e.g,utility or value) function

▶ Popular methods are linear-, Chebyshev-, and Spline-interpolations. I will spend most of my time on Splines.

Fatih Guvenen Lecture 2: Interpolation 2 / 29

Page 4: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Polynomial Approximation

▶ Weierstrass Approximation Theorem. Suppose f is acontinuous real-valued function defined on the real interval [a,b]. For a given ε > 0, there exists a polynomial of order n, Pn(x),such that for all x in [a, b], we have ∥f(x)− Pn(x)∥∞ < ε. In thelimit, limn→∞ ∥f(x)− Pn(x)∥∞ = 0.

▶ Runge. Let f(x) = 1/(1 + x2) on [-5,5], and let Lmf be the uniquepolynomial of order m that interpolates f at m equally-spacedpoints. Then:

lim supm→∞

|f(x)− Lmf| ={0 if |x| < 3.633...,

∞ if |x| > 3.633...

▶ Wait, what? How can Runge example not contradict theWeierstrass theorem?

▶ Weierstrass does not provide a way of finding the right Pn(x)and the second one shows a naive approach can failspectacularly. (Who said equally-spaced points, right?)

Fatih Guvenen Lecture 2: Interpolation 3 / 29

Page 5: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Runge Example

Page 6: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Splines: Three Objectives

1 Match the function values at grid points (y1, y2, ..., yn) exactly.

2 Generate first derivatives that are continuous anddifferentiable for all x ∈ [x1, xn].

3 Generate second derivatives that are continuous for allx ∈ [x1, xn]

Fatih Guvenen Lecture 2: Interpolation 5 / 29

Page 7: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Splines: Building from Ground Up

▶ Begin with the interval between two generic knots, xi and xi+1.If we were to construct a linear interpolant:

y = Ayj + Byj+1

A(x) ≡xj+1 − xxj+1 − xj

B(x) ≡ 1− A =x− xjxj+1 − xj

(1)

▶ Although linear interpolation is sometimes useful, ashortcoming is that

First derivative changes abruptly at the knot points, i.e., theinterpolants have as many kinks as the knot points.Second derivative does not exist at knot points.

▶ This can create many problems, such as difficulties withderivative based minimization algorithms.

▶ Question: How can we modify (1) to allow differentiable firstderivatives and continuous second derivatives at all points?

Fatih Guvenen Lecture 2: Interpolation 6 / 29

Page 8: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Splines: Building from Ground Up

▶ Generalize (1) using second derivative values at knot points:

y = A(x)yj + B(x)yj+1 + C(x)y′′

j + D(x)y′′

j+1 (2)

where C(x) = 16 (A3(x)− A(x))(xj+1 − xj)2 and

D(x) = 16 (B3(x)− B(x))(xj+1 − xj)2

▶ Note that you only need to know A and B to calculateeverything.

▶ Verify that d2ydx2 = A(x)y′′

j + B(x)y′′

j+1

▶ Since A(xj) = 1 and B(xj+1) = 1− A(xj+1) = 1, the secondderivative agrees with y′′at end points.

▶ But how to find y′′

j and y′′

j+1 ?

Fatih Guvenen Lecture 2: Interpolation 7 / 29

Page 9: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Splines: Building from Ground Up

▶ Differentiate (2) to obtain an expression for dydx that will involve

y′′

j and y′′

j+1.▶ Then impose the condition that dy

dx calculated using (xj, xj+1)

and using (xj+1, xj+2) equal each other at xj+1. We get:xj − xj−1

6︸ ︷︷ ︸cj−1

y′′

j−1 +xj+1 − xj−1

3︸ ︷︷ ︸dj−1

y′′

j +xj+1 − xj

6︸ ︷︷ ︸cj

y′′

j+1 =yj+1 − yjxj+1 − xj

−yj − yj−1

xj − xj−1︸ ︷︷ ︸ .sj−sj−1

(3)▶ For interior knot points, j = 2, ...,N− 1 we have an equation like

this. But we have N unknowns (y”j for j = 1, ...,N).▶ Impose two boundary conditions. Some common choices:

Set y′1 and y′N to specified values, orSet y′′1 and y′′N to zero (natural spline) (can help with extrapolation)

▶ Caution: no condition is imposed for y′ or y′′ to agree with f′and f′′ , since these are unknown. This can create problems aswe will see.

Fatih Guvenen Lecture 2: Interpolation 8 / 29

Page 10: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

A Tridiagonal System of Equations

First and last lines are dy/dx at end points:

2c1 −c1c1 2d1 c2

. . .cj−1 2dj−1 cj

. . .cn−2 2dn−2 cn−1

−cn−1 2cn−1

.

y′′1y′′2...y′′j...

y′′n−1

y′′n

=

s1 − a∗1s2 − s1

...sj − sj−1

...sn−1 − sn−2

sn − a∗n

.

(4)

Fatih Guvenen Lecture 2: Interpolation 9 / 29

Page 11: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Fatih Guvenen Lecture 2: Interpolation 10 / 29

Page 12: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Fatih Guvenen Lecture 2: Interpolation 11 / 29

Page 13: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Comparing InterpolationMethods for U(C)

Page 14: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Comparing Boundary Conditions

Figure 1: N = 500 pts

0.04 0.06 0.08 0.1 0.12 0.14 0.16

Log

Axis

-1011

-1010

-109

-108

-107

-106

y′

1 = U′

(0.05)

y′

1 = 0Natural SplineTrue Function

Page 15: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Comparing Boundary Conditions

Figure 2: N = 100 pts

0.05 0.1 0.15 0.2 0.25

LinearAxis

×1010

-10

-5

0

5

y′

1 = U′

(0.05)

y′

1 = 0Natural splineTrue Function

Page 16: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation: What Can Go Wrong

0 0.5 1 1.5 2−1

−0.5

0

0.5

1

1.5

2x 10

8

CRRA function with γ = 10

8th-order polynomial

Cubic spline

Shumaker quadratic spline

Fatih Guvenen Lecture 2: Interpolation 15 / 29

Page 17: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation: What Can Go Wrong

0 1 2 3 4 5 6 7 8 9 10−6

−5

−4

−3

−2

−1

0

1x 10

8

CRRA function with γ = 10

Cubic spline

Shumaker shape-preserving spline

8th-order polynomial

Fatih Guvenen Lecture 2: Interpolation 16 / 29

Page 18: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation: What Can Go Wrong

0 1 2 3 4 5 6 7 8 9 10−600

−500

−400

−300

−200

−100

0

100

Fatih Guvenen Lecture 2: Interpolation 17 / 29

Page 19: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation: What Can Go Wrong

0 0.5 1 1.5 2−60

−50

−40

−30

−20

−10

0

10

Fatih Guvenen Lecture 2: Interpolation 18 / 29

Page 20: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Interpolation: What Can Go Wrong

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1−6

−5

−4

−3

−2

−1

0

1

Fatih Guvenen Lecture 2: Interpolation 19 / 29

Page 21: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Standard Spline vs Shape-Preserving

−5 −4 −3 −2 −1 0 1 2 3 4 5−1.5

−1

−0.5

0

0.5

1

1.5

y=tanh(x) at x=−5,−4,−3,...

Cubic spline

Shumaker’s spline

y=tanh(x) continuous

Fatih Guvenen Lecture 2: Interpolation 20 / 29

Page 22: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Spacing of Grid Points: Crucial!!

▶ One heuristic: put more grid points where f has more curvature.▶ Another one: put more points near the parts of the function

that are more relevant.e.g., in an RBC style model, put more points near K, even thoughthe functions of interest may have more curvature at low K values.

▶ In incomp. mkts models, V′′(ω) is largest for very low ω. So

should put more points there.▶ Is this true if there are not many individuals near the

constraint? (Answer: Typically, Yes. But why?)▶ In some DP problems, with max operator on the RHS, the value

function may have a kink or significant curvature somewhere inthe middle of the state space.

Linear interpolation maybe your best choice.

Fatih Guvenen Lecture 2: Interpolation 21 / 29

Page 23: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Figure 3: Grid Point Locations: 51-Point Expanding Grid From 0 to 250

(a) Low End of Grid

Wealth

0 1 2 3 4 5

θ = 1 θ = 2 θ = 3 θ = 4

(b) High End of Grid

Wealth

220 225 230 235 240 245 250

θ = 1 θ = 2 θ = 3 θ = 4

Note: The number of grid points between 0 and 4.99 is 1, 8, 14, and 19 when θ is equalto 1, 2, 3, and 4, respectively.

Page 24: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Algorithmus 1 : Creating A Polynomially-Expanding Grid

Step 1. First, create an equally-spaced [0,1] grid:{x̃j : x̃j = j−1

N−1 , j = 1, ...,N}.

Step 2. Shift and expand the grid: x = {xj : xj = a+(b−a)x̃jθ},where θ > 1 is the expansion factor.

Fatih Guvenen Lecture 2: Interpolation 22 / 29

Page 25: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Spline w/ Expanding Grid(1000 pts)

Figure 4: N = 1000 pts

0 0.2 0.4 0.6 0.8 1-100

-50

0

50

100

Equally-spaced, θ =1.0

θ = 1.05

θ = 1.10

Fatih Guvenen Lecture 2: Interpolation 23 / 29

Page 26: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Spline w/ Expanding Grid (100 pts)

Figure 5: N = 100 pts

0 1 2 3 4 5-10

-5

0

5

10

U(C)

θ = 1.0

θ = 2.0

θ = 3.0

Fatih Guvenen Lecture 2: Interpolation 24 / 29

Page 27: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

A Word about B-Splines

A fundamental theorem about B-splines (see de Boor (1978)):

▶ Constructing a cubic spline interpolation with (m,n) points in(x, y) direction requires:

O((mn)2) operations using regular splines.

O(m3 +m2n+mn2 + n3) operations with B-splines.

▶ Suppose m = n = 10:

Regular spline requires ∼ 1, 000, 000 operations

B-splines require: ∼ 4, 000 operations!

▶ ∴ Learn about B-splines!

Fatih Guvenen Lecture 2: Interpolation 25 / 29

Page 28: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

A Trick to Reduce the Curvature of V(w)

▶ Samuelson (1969) showed that in a standard portfolio choiceproblem with CRRA preferences and a linear budget set, thevalue function inherits the curvature of U:

U(c0, c1, ...) =∞∑t=1

βtc1−γt

1− γ⇒ V(ω,A) = ϕ(A)× ω1−γ

▶ The same result holds approximately true in a variety ofdifferent problems.

▶ With incomplete markets V(w) will typically have even morecurvature than U(c) especially at low wealth levels.

▶ As we have seen so far, this high curvature creates a lot ofheadache when you try to interpolate the value function.

▶ Fortunately, there is a way out!

Fatih Guvenen Lecture 2: Interpolation 26 / 29

Page 29: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

A Trick to Reduce the Curvature of V(w)

▶ There is an alternative formulation of CRRA preferences:

U(c0, c1, ...) =( ∞∑t=1

βtc(1−γ)t

)1/(1−γ)

▶ This is a special case of Epstein-Zin (1989, E’trica) utility andrepresents the same preferences as CRRA utility with RRA = ρ.

▶ Now the value function is linear: V(ω,A) = ϕ(A)× ω

▶ Although incomplete markets introduces some curvature, thisvalue function is much easier to interpolate than the oneabove.

▶ In fact, I once solved a GE model with asset pricing and a riskaversion of 4 using only 30 points in the wealth grid and linearinterpolation!

Fatih Guvenen Lecture 2: Interpolation 27 / 29

Page 30: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

0 1 2 3 4 5-10

15

-1010

-105

-100

-10-5

-10-10

0 1 2 3 4 5

0.19

0.20

0.21

Epstein-Zin Value Func.(right axis)

Standard CRRA Utility(left axis)

Figure 6: Which Function Would You Rather Interpolate?

Fatih Guvenen Lecture 2: Interpolation 28 / 29

Page 31: Lecture2:Interpolation - WordPress.com...PolynomialApproximation WeierstrassApproximationTheorem.Suppose f isa continuousreal-valuedfunctiondefinedontherealinterval[a, b].Foragiven"

Final Thoughts

▶ For any model that you solve, you MUST eventually re-solve iton a much finer grid and confirm that your main results are notchanging (much).

▶ This is the only realistic way to check if approximation errorscoming from interpolations are important.

▶ You will be surprised to find that some bad-lookinginterpolations actually yield the same results as much moreaccurate (and more costly to compute) interpolations.

▶ And vice versa..▶ Some problems are especially sensitive to any kind of

approximation errors. We will see examples.

Fatih Guvenen Lecture 2: Interpolation 29 / 29