Introduction to Numerical Optimization Biostatistics 615/815 - - PowerPoint PPT Presentation

introduction to numerical optimization
SMART_READER_LITE
LIVE PREVIEW

Introduction to Numerical Optimization Biostatistics 615/815 - - PowerPoint PPT Presentation

Introduction to Numerical Optimization Biostatistics 615/815 Lecture 14 Lecture 14 Course is More Than Half Done! If you have comments they are very welcome y y Lectures Lecture notes Weekly Homework Midterm


slide-1
SLIDE 1

Introduction to Numerical Optimization

Biostatistics 615/815 Lecture 14 Lecture 14

slide-2
SLIDE 2

Course is More Than Half Done!

If you have comments… … they are very welcome

y y

  • Lectures
  • Lecture notes
  • Weekly Homework
  • Midterm
  • Content
slide-3
SLIDE 3

Last Lecture

Computer generated “random” numbers Linear congruential generators

  • Improvements through shuffling summing

Improvements through shuffling, summing

Importance of using validated generators Importance of using validated generators

  • Beware of problems with the default rand()

function u c o

slide-4
SLIDE 4

Today …

Root finding Minimization for functions of one variable Ideas:

  • Li

it

  • Limits on accuracy
  • Local approximations
slide-5
SLIDE 5

Numerical Optimization

Consider some function f(x)

  • e.g. Likelihood for some model …

Find the value of x for which f takes a

maximum or minimum value maximum or minimum value

Maximization and minimization are equivalent Maximization and minimization are equivalent

  • Replace f(x) with –f(x)
slide-6
SLIDE 6

Algorithmic Objectives

Solve problem…

  • Conserve CPU time
  • Conserve memory

Most often, the CPU time is dominated

by the cost of evaluating f(x)

  • Minimize the number of evaluations
slide-7
SLIDE 7

The Minimization Problem

slide-8
SLIDE 8

Specific Objectives

Finding global minimum

  • The lowest possible value of the function
  • Extremely hard problem

Finding local minimum

  • Smallest value within finite neighborhood
slide-9
SLIDE 9

Typical Quality Checks

When solving an optimization problem it

is good practice to check the quality of the solution

Try different starting values … Perturb solution and repeat …

slide-10
SLIDE 10

A Quick Detour

Consider the problem of finding zeros for f(x) Assume that you know:

  • Point a where f(a) is positive
  • P i t b

h f(b) i ti

  • Point b where f(b) is negative
  • f(x) is continuous between a and b

How would you proceed to find x such that

f(x)=0?

slide-11
SLIDE 11

Root Finding in C

double zero(double (*func)(double), double lo, double hi, double e) { while (true) { double d = hi – lo; double d = hi – lo; double point = lo + d * 0.5; double fpoint = (*func)(point); if (fpoint < 0.0) ( po t . ) { d = lo – point; lo = point; } else { d = point – hi; hi = point; } if (fabs(d) < e || fpoint == 0.0) return point; } }

slide-12
SLIDE 12

Improvements to Root Finding

Consider the following approximation:

a b a f b f a x a f x f − − − + = ) ( ) ( ) ( ) ( ) (

*

Select new trial point such that f*(x) is zero.

slide-13
SLIDE 13

Improved Root Finding in C

double zero (double (*func)(double), double lo, double hi, double e) { double flo = (*func)(lo); double fhi = (*func)(hi); while (1) while (1) { double d = hi – lo; double point = lo + d * flo / (flo – fhi); double fpoint = (*func)(point); doub e po t ( u c)(po t); if (fpoint < 0.0) { d = lo – point; lo = point; flo = fpoint; } else { d = point – hi; hi = point; fhi = fpoint; } if (fabs(d) < e || fpoint == 0.0) return point; } }

slide-14
SLIDE 14

Performance Comparison

Find the zero for sin(x)

  • In the interval -π/4 to π/2
  • Accuracy parameter set to 10-5

Bi ti th d d 17 ll t i ( )

Bisection method used 17 calls to sin(x) Approximation used 5 calls to sin(x)

slide-15
SLIDE 15

Program That Uses Root Finding

double zero (double (*func)(double), double lo, double hi, double e);

double my_function(double x) { t (4* 3) return (4*x – 3); } int main(int argc, char ** argv) { double solution = zero(my_function, -5, +5, 1e-5); printf(“Zero for my function is %.3f at %.3f\n”, f ti ( l ti ) l ti ) my_function(solution), solution); }

slide-16
SLIDE 16

Notes on Root Finding

The 2nd method we implemented is the

False Position Method

In the bisection method, the bracketing

i t l i h l d t h t interval is halved at each step

For well-behaved functions, the False

Position Method will converge faster, but there is no performance guarantee there is no performance guarantee

slide-17
SLIDE 17

Questions on Root Finding

What care is required in setting precision? How to set starting brackets for minimum?

  • If the function was monotonic?

If the function was monotonic?

  • If there is a specific target interval?

What would happen for a function such as

f(x) = 1 / (x – c) f(x) 1 / (x c)

slide-18
SLIDE 18

Back to Numerical Optimization

Consider some function f(x)

  • e.g. Likelihood for some model …

Find the value of x for which f takes a

maximum or minimum value maximum or minimum value

Maximization and minimization are equivalent Maximization and minimization are equivalent

  • Replace f(x) with –f(x)
slide-19
SLIDE 19

Notes from Root Finding

Introduces two useful ideas that we’ll apply

to function minimization

Bracketing Bracketing

  • Keep track of interval containing solution

Accuracy

  • Recognize that solution has limited precision

Recognize that solution has limited precision

slide-20
SLIDE 20

Note on Accuracy

When estimating minima and bracketing

intervals, floating point accuracy must be considered

In general, if the machine precision is ε

the achievable accuracy is no more than y sqrt(ε)

slide-21
SLIDE 21

Note on Accuracy II

The error results from the second term in the

Taylor approximation:

2 2 1

) )( ( ) ( ) ( b x b f b f x f − ′ ′ + ≈

For functions where higher order terms are

i t t ld b l important, accuracy could be even lower.

  • For example, the minimum for f(x) = 1 + x4 is only

estimated to about ε1/4

slide-22
SLIDE 22

Outline of Minimization Strategy

Part I

  • Bracket minimum

Part II

  • Successively tighten bracketing interval

Successively tighten bracketing interval

slide-23
SLIDE 23

Detailed Minimization Strategy

Find 3 points such that

  • a < b < c
  • f(b) < f(a) and f(b) < f(c)

Then search for minimum by

  • Selecting trial point in interval

Selecting trial point in interval

  • Keep minimum and flanking points
slide-24
SLIDE 24

Minimization after Bracketing

1 2 4 3 5 6

slide-25
SLIDE 25

Part I: Part I: Finding a Bracketing Interval

Consider two points

  • a, b
  • f(a) > f(b)

Take successively larger steps beyond b

until function starts increasing

slide-26
SLIDE 26

Bracketing in C

#define SCALE 1.618 void bracket (double (*f)(double), double* a, double* b, double* c) { double fa = (*f)( *a); double fa = (*f)( *a); double fb = (*f)( *b); double fc = (*f)( *c = *b + SCALE * (*b - *a) ); while (fb > fc) e ( b c) { *a = *b; fa = fb; *b = *c; fb = fc; *c = *b + SCALE * (*b - *a); fc = (*f) (*c); } }

slide-27
SLIDE 27

Bracketing in C++

#define SCALE 1.618 void bracket (double (*f)(double), double & a, double & b, double & c) { double fa = (*f)(a); double fa = (*f)(a); double fb = (*f)(b); double fc = (*f)(c = b + SCALE * (b - a) ); while (fb > fc) e ( b c) { a = b; fa = fb; b = c; fb = fc; c = b + SCALE * (b - a); fc = (*f) (c); } }

slide-28
SLIDE 28

Part II: Part II: Finding Minimum after Bracketing

Given 3 points such that

  • a < b < c
  • f(b) < f(a) and f(b) < f(c)

How do we select new trial point?

slide-29
SLIDE 29

Consider …

B A B C What is the best location for a new point X?

slide-30
SLIDE 30

Consider …

B A B C X We want to minimize the size of the next h i t l hi h ill b ith search interval which will be either from A to X or from B to C

slide-31
SLIDE 31

Formulae …

a c a b w − − = a c b x z − − = length have will Segments so y possibilit case worst minimize want to We

  • r

1 z w w + − so... y possibilit case worst minimize want to We

slide-32
SLIDE 32

Effectively …

i i l Th 2 1 is case

  • ptimal

The − = w z 1 = − w w z gives This 38197 . 5

  • 3

w gives This = = 2

slide-33
SLIDE 33

Golden Search

slide-34
SLIDE 34

The Golden Ratio

A B C

Bracketing Triplet

A B C

slide-35
SLIDE 35

The Golden Ratio

A B C X

New Point

A B C X

0.38196 0.38196

The number 0.38196 is related to the golden mean studied by Pythagoras

slide-36
SLIDE 36

The Golden Ratio

B

New Bracketing Triplet

A B X

0.38196

Alternative New Bracketing Triplet

B X C

0.38196

slide-37
SLIDE 37

Golden Search

Reduces bracketing by ~40% after each

function evaluation

Performance is independent of the function Performance is independent of the function

that is being minimized

In many cases, better schemes are

available Any ideas? available… Any ideas?

slide-38
SLIDE 38

Golden Step

#define GOLD 0.38196 #define ZEPS 1e-10 double golden step (double a double b double c) double golden_step (double a, double b, double c) { double mid = (a + c) * 0.5; if (b > mid) return GOLD * (a - b); else return GOLD * (c - b); }

slide-39
SLIDE 39

Golden Search Golden Search

double golden_search(double (*func)(double), double a, double b, double c, double e) { d bl fb (*f )(b) double fb = (*func)(b); while ( fabs(c - a) > fabs(b * e) + ZEPS) { d bl b + ld t ( b ) double x = b + golden_step(a, b, c); double fx = (*func)(x); if (fx < fb) { if (x > b) { a = b; } else { c = b; } b = x; fb = fx; } l else if (x < b) { a = x; } else { c = x; } } return b; }

slide-40
SLIDE 40

Further Improvements

As with root finding, performance can

improve substantially when a local approximation is used …

However, a linear approximation won't

do in this case!

slide-41
SLIDE 41

Approximating The Function

slide-42
SLIDE 42

Recommended Reading

Numerical Recipes in C (or C++)

  • Press, Teukolsky, Vetterling, Flannery
  • Ch

10 0 10 2

  • Chapters 10.0 – 10.2

Excellent resource for scientific computing

Excellent resource for scientific computing

O li t

Online at

  • http://www.numerical-recipes.com/
  • http://www library cornell edu/nr/
  • http://www.library.cornell.edu/nr/