Docsity

Finite Differencing: Approximating Derivatives when Analytic Expressions are Unavailable -, Study notes of Mathematics

Toggle sidebar
bg1
Non-Analytic Derivatives — Finite Differencing
Finite Differencing — Sparsity and Symmetry
Project...
Numerical Optimization
Lecture Notes #16
Calculating Derivatives — Finite Differencing
Peter Blomgren,
Department of Mathematics and Statistics
Dynamical Systems Group
Computational Sciences Research Center
San Diego State University
San Diego, CA 92182-7720
http://terminus.sdsu.edu/
Fall 2009
Peter Blomgren, h[email protected]iCalculating Derivatives — Finite Differencing — (1/18)
Non-Analytic Derivatives — Finite Differencing
Finite Differencing — Sparsity and Symmetry
Project...
Outline
1Non-Analytic Derivatives — Finite Differencing
Taylor’s Theorem ⇒Finite Differencing
Finite Difference Gradient
Finite Difference Hessian
2Finite Differencing — Sparsity and Symmetry
3Project...
Peter Blomgren, h[email protected]iCalculating Derivatives — Finite Differencing — (2/18)
Non-Analytic Derivatives — Finite Differencing
Finite Differencing — Sparsity and Symmetry
Project...
Taylor’s Theorem ⇒Finite Differencing
Finite Difference Gradient
Finite Difference Hessian
Derivatives Needed!!!
As we have seen (and will see), algorithms for nonlinear
optimization (and nonlinear equations) require knowledge of
derivatives:
Nonlinear Optimization Nonlinear Equations
Gradient, vector, 1st order Jacobian, matrix, 1st order
Hessian, matrix, 2nd order
Often it is quite trivial to provide the code which computes those
derivatives, but in some cases the analytic expression for the
derivatives are not available and/or not practical to evaluate.
In those cases we need some other way to compute or
approximate the derivatives.
Peter Blomgren, h[email protected]iCalculating Derivatives — Finite Differencing — (3/18)
Non-Analytic Derivatives — Finite Differencing
Finite Differencing — Sparsity and Symmetry
Project...
Taylor’s Theorem ⇒Finite Differencing
Finite Difference Gradient
Finite Difference Hessian
Finite Differences — The Return of Taylor’s Theorem
We can get an approximation of the gradient ∇f(˜x) by evaluating
the objective fat n+1points, using the forward difference
formula
∂f(˜x)
∂xi≈f(˜x +ǫ˜ei)−f(˜x)
ǫ,i= 1,2,...,n,
where ˜eiis the ith unit vector, and ǫ > 0 is small.
If fis twice continuously differentiable, then by Taylor’s Theorem
f(˜x +˜p) = f(˜x) + ∇f(˜x)T˜p +1
2˜pT∇2f(˜x +t˜p)˜p,t∈(0,1),
with ˜p =ǫ˜ei,i.e.
f(˜x+ǫ˜ei) = f(˜x)+ǫ∇f(˜x)T˜ei+1
2ǫ2˜eT
i∇2f(˜x+tǫ˜ei)˜ei,i= 1,2,...,n.
Peter Blomgren, h[email protected]iCalculating Derivatives — Finite Differencing — (4/18)
pf3
pf4
pf5

Partial preview of the text

Download Finite Differencing: Approximating Derivatives when Analytic Expressions are Unavailable - and more Study notes Mathematics in PDF only on Docsity!

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Numerical Optimization

Lecture Notes #

Calculating Derivatives — Finite Differencing

Peter Blomgren,

〈[email protected]

Department of Mathematics and Statistics

Dynamical Systems Group Computational Sciences Research Center^ San Diego State UniversitySan Diego, CA 92182-7720^ http://terminus.sdsu.edu/

Fall 2009

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (1/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Outline

1

Non-Analytic Derivatives — Finite Differencing^ Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference Gradient Finite Difference Hessian 2

Finite Differencing — Sparsity and Symmetry 3

Project...^ Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (2/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Derivatives Needed!!!

As we have seen (and will see), algorithms for nonlinearoptimization (and nonlinear equations) require knowledge ofderivatives:

Nonlinear Optimization

Nonlinear Equations

Gradient

, vector, 1st order

Jacobian

, matrix, 1st order

Hessian

, matrix, 2nd order

Often it is quite trivial to provide the code which computes thosederivatives, but in some cases the analytic expression for thederivatives are not available and/or not practical to evaluate.In those cases we need some other way to compute or approximate

the derivatives.

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (3/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Finite Differences — The Return of Taylor’s Theorem

We can get an approximation of the gradient

f^ (˜x

) by evaluating

the objective

f^

at^

n^ +

points, using the

forward difference

formula

∂f^

(˜x) ∂xi

≈^

f^ (˜x

+^

ǫ˜ei

)^ −

f^ (

˜x) ǫ^

,^

i^ = 1

,^2 ,... ,

n,

where

˜ei^

is the

i^ th unit vector, and

ǫ >

0 is small.

If^ f

is twice continuously differentiable, then by

Taylor’s Theorem

f^ (˜x

+^

˜p) =

f^ (

˜x) +

∇f

(˜x

T^ )˜p

1 ˜p 2

T^ ∇

2 f^ (

˜x^ +

t˜p

)˜p,

t^ ∈

,^ 1)

with

˜p^

=^ ǫ

˜e,i^

i.e.

f^ (˜x

+ǫ

˜e) =i^

f^ (

˜x)+

ǫ∇

f^ (˜x

T^ )˜

e+ i^

12 ǫ 2

T˜ei^

f^ (˜x

+t

ǫ˜ei

)˜e i

,^

i^ = 1

,^2 ,... ,

n.

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (4/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Forward Differences

Building the Gradient

With a bit of re-arrangement we see

∇f

(˜x

T^ )˜

ei

︸^

︷︷^

∂f^ (˜x

) ∂xi

=^

f^ (˜x

+^

ǫ˜ei

)^ −

f^ (

˜x) ǫ

︸^

︷︷^

Finite Difference Approximation

−^

1 ǫ^2

T˜ei^

f^ (˜x

+^

tǫ˜e

)˜ei i

︸^

︷︷^

Approximation Error

If the Hessian

2 f^ (

˜x) is bounded,

i.e.

2 f^ (

˜x)‖ ≤

L, then we have

∂f^

(˜x) ∂xi

≈^

f^ (˜x

+^

ǫ˜ei

)^ −

f^ (

˜x) ǫ

where the approximation error is bounded by

ǫL^2

Since the error is proportional to

ǫ, this is a

first-order

approximation

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (5/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Selecting

ǫ^

Machine Epsilon / Unit Roundoff

1 of 3

Clearly, the smaller the

ǫ^

the smaller the error. How small can we

set

ǫ^

in^ finite precision??? Let

ǫmach

denote value for

machine epsilon

, a.k.a.

unit roundoff

, it

is essentially the largest value for which

ǫmach

)^ −

,^

in finite precision

Currently,

ǫmach

−^16

in double-precision arithmetic (“C”

double

, and Matlab internals on typical intel-x86 based systems.) ǫmach

is a measure of how well (or badly) we can represent any number in finite precision, and in extension a measure of thequality of every computation.

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (6/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Selecting

ǫ^

2 of 3

If^ L

is a bound on the value off

f^ (

˜x), then in finite precision we

have (something like)

‖computed

(f^ (

˜x))

−^

f^ (˜x

)‖^

≤^

ǫmach

Lf

‖computed

(f^ (

˜x^ +

ǫ˜e

))^ i −^ f

(˜x

+^

ǫ˜ei

)‖^

≤^

ǫmach

L.f^

Now, if we recall our finite difference approximation (with a slightabuse of notation)

∂f^

(˜x) ∂xi

≈^

f^ (˜x

+^

ǫ˜ei

)^ −

f^ (

˜x) ǫ^

+^ error

(^ ǫ

)L 2

We find that the total error is

error

∼^

2 ǫmach

Lf ǫ

+^

ǫL^2

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (7/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Selecting

ǫ^

3 of 3

Now,

derror

dǫ^

2 ǫmach

Lf (^2) ǫ

+^

L= 0 2

⇒^

(^2) ǫ

=^

4 ǫmach

Lf, L

gives us the optimal value for epsilon. Since

Lf

and

L^

are unknown

in general, most software packages, tend to select

ǫ^ =

ǫmach

which is close to optimal in most cases.Hence, the error in the approximated gradient is

error

2 L

√f ǫmach

+^

√L 2

ǫmach

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (8/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Approximating the Hessian

4 of 5

The harder case: Analytic Gradient not given When the analytic gradient is not given we must use a finitedifference formula using only function values to approximate theHessian.The first order

forward difference

approximation is given by

(^2) ∂f (˜x

∂x

∂xi

≈j

f^ ( ˜x^ +

ǫ˜e

+i ǫ˜e

)^ −j

f^ (

˜x^ +

ǫ˜e

)^ −i

f^ (

˜x^ +

ǫ˜e

) +j

f^ (

˜x)

(^2) ǫ −

− +

+1^ i j

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (13/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Taylor’s Theorem

⇒^

Finite Differencing

Finite Difference GradientFinite Difference Hessian

Approximating the Hessian

5 of 5

At a price of

(^2) n additional function evaluations (an increase of

33%) we can use the second order

central difference

approximation^ ∂

2 f^ (˜x

) ∂x∂i^

≈xj f^ (˜x +^ ǫ

˜e+i^

ǫ˜ej^

)^ −^

f^ (˜x^

+^ ǫ

˜e−i^

ǫ˜ej^

)^ −

f^ (˜x

−^ ǫ

˜e+i^

ǫ˜ej^

) +

f^ (˜x

−^ ǫ

˜e−i^

ǫ˜ej^

)

(^24) ǫ

i

j −1 +^

+1 −

Figure:

The second order 4-point central difference approximation stencil for

(^2) ∂ f^ (˜x) ∂x∂xi^ j^

at

the central point in the stencil — note that the value in that point is not part of theevaluation!

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (14/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Sparsity and Symmetry

1 of 3

Now that we are paying

4 function evaluations per entry in the

Hessian matrix, it is worth taking sparsity and symmetry intoaccount.Ponder the extended Rosenbrock function:^ double function

rosenbrock( int n, double *x )

{ xxdouble f = 0.0; xxint

xxxx

i;

xxfor( i=0; i<n/2; i++ ) xxxxx

f += ( 10 * ( x[2i+1] - x[2i]x[2i] ) * xxxxxxxxxxxxxxxxx

( x[2i+1] - x[2i]x[2i] ) ) +

xxxxxxxxxx

( 1 - x[2i] ) * ( 1 - x[2i] ) ;

xxreturn(f); } Clearly, there is no “interaction” between coordinate-directions

˜ei

and

˜ej^

, where

|i^

−^ j

|^ >

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (15/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Sparsity and Symmetry

2 of 3

The fill-pattern of the Hessian of the extended Rosenbrockfunction consists of 2

×2-diagonal blocks:^0

1 2

3

4

5 6

7

8

9

0 1 2 3 4 5 6 7 8 9

nz = 16

There are a lot of zero-entries in this Hessian. If somehow we haveknowledge of the sparsity pattern, then we can exploit this by notcomputing the zeros.

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (16/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Sparsity and Symmetry

3 of 3

By using the fact that the Hessian is symmetric, we can save abouthalf of the work,

0

1

2

3

4

5

6

7

8

9

0 1 2 3 4 5 6 7 8 9

nz = 36

0

1

2

3

4

5

6

7

8

9

0 1 2 3 4 5 6 7 8 9

nz = 28

Figure:

The entries to the left

Hij^

,^ j^ ≤

i^ must be computed, but using symmetry

we can fill in the missing ones

Hij

=^ H,ji^

j^ >

i.

Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (17/18)

Non-Analytic Derivatives — Finite DifferencingFinite Differencing — Sparsity and Symmetry

Project...

Project Extensions

Milestone #2: with linesearch+

Add the following to your codebase: fdhessg

Finite difference approximation to the Hessian using analytic gradient.(Executed when

analgrad=TRUE, analhess=FALSE, cheapf=TRUE

.)

fdjac

The

core

call

from

fdhessg

,^ note

that

fvec

in^

the^

pseudo-code

corresponds to your analytic gradient.

fdgrad

Finite difference (forward) approximation to the gradient.

(Executed

when

analgrad=FALSE

.)

fdhessf

Finite

difference

approximation

to

the

Hessian

using

only

func-

tion

values.

(Executed

when

analgrad=FALSE, analhess=FALSE,

cheapf=TRUE

.)

Compare:

Performance of analytic everything (from before) / analytic gradient

(fdhessg+fdjac

) / finite difference everything (

fdhessf+fdgrad

). Try optimal and

non-optimal

ǫ. Use 2 test problems from Dennis-Schnabel, Appendix B. Peter Blomgren,

〈[email protected]

〉^

Calculating Derivatives — Finite Differencing

— (18/18)