Biostatistics 615/815 Lecture 13: Numerical Optimization

Similar documents
Biostatistics 615/815 Lecture 16: Importance sampling Single dimensional optimization

Biostatistics 615/815 Lecture 19: Multidimensional Optimizations

Today. Golden section, discussion of error Newton s method. Newton s method, steepest descent, conjugate gradient

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Biostatistics 615/815 Lecture 5: Divide and Conquer Algorithms Sorting Algorithms

Exercises C-Programming

Biostatistics 615/815 Lecture 20: Expectation-Maximization (EM) Algorithm

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Constrained and Unconstrained Optimization

Biostatistics 615/815 Lecture 6: Linear Sorting Algorithms and Elementary Data Structures

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

Announcements. Biostatistics 615/815 Lecture 7: Data Structures. Recap : Radix sort. Recap : Elementary data structures. Another good and bad news

Biostatistics 615/815 - Lecture 3 C++ Basics & Implementing Fisher s Exact Test

Contents. Hilary Term. Summary of Numerical Analysis for this term. Sources of error in numerical calculation. Solving Problems

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Biostatistics 615/815 Implementing Fisher s Exact Test

Biostatistics 615/815 Lecture 10: Hidden Markov Models

Biostatistics 615/815 Lecture 13: Programming with Matrix

Remember Java Data Types

Floating-point numbers. Phys 420/580 Lecture 6

An interesting related problem is Buffon s Needle which was first proposed in the mid-1700 s.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

1.00 Lecture 19. Numerical Methods: Root Finding

Biostatistics 615/815 Statistical Computing

10.4 Linear interpolation method Newton s method

Numerical Methods Lecture 7 - Optimization

Handout 2 - Root Finding using MATLAB

Functions and Recursion

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

LECTURE 11 TEST DESIGN TECHNIQUES IV

Modern Methods of Data Analysis - WS 07/08

Biostatistics 615/815 Lecture 5: More on STLs, and Divide and Conquer Algorithms

3.3 Function minimization

9.1 Bracketing and Bisection

Recap: Hash Tables. Recap : Open hash. Recap: Illustration of ChainedHash. Key features. Key components. Probing strategies.

1.00 Lecture 19. Packaging Functions in Objects

Biostatistics 615 / 815

Formal Methods: Model Checking and Other Applications. Orna Grumberg Technion, Israel. Marktoberdorf 2017

W4260: Modeling the Universe. Lecture 4

Today s class. Roots of equation Finish up incremental search Open methods. Numerical Methods, Fall 2011 Lecture 5. Prof. Jinbo Bi CSE, UConn

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Problem Solving (Computing) Array and Pointer

Biostatistics 615/815 Lecture 4: User-defined Data Types, Standard Template Library, and Divide and Conquer Algorithms

Computers in Engineering Root Finding Michael A. Hawker

CSCI-1200 Data Structures Fall 2017 Lecture 12 Advanced Recursion

CSCI-1200 Data Structures Fall 2017 Lecture 2 STL Strings & Vectors

Recap: The E-M algorithm. Biostatistics 615/815 Lecture 22: Gibbs Sampling. Recap - Local minimization methods

Summary of basic C++-commands

Biostatistics 615/815 Lecture 9: Dynamic Programming

The following program computes a Calculus value, the "trapezoidal approximation of

PROGRAMMING IN C++ CVIČENÍ

Machine Computation of the Sine Function

Data Structures Lecture 3 Order Notation and Recursion

CSCI-1200 Data Structures Spring 2015 Lecture 2 STL Strings & Vectors

Exercise 9 Pointers, Iterators and Recursion

Introduction to Languages for Scientific Computing, winter semester 14/15: Final Exam

1.00 Lecture 25. Root Finding

CE221 Programming in C++ Part 1 Introduction

The call stack and recursion and parameters revisited

C++ Constructor Insanity

C++ Programming Lecture 11 Functions Part I

Math 3 Coordinate Geometry Part 2 Graphing Solutions

The New Bisection Plus Algorithm by Namir Shammas

Introduction to C++ Introduction to C++ Week 6 Dr Alex Martin 2013 Slide 1

Bisecting Floating Point Numbers

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

Assignment #2: False Position Method

Common Misunderstandings from Exam 1 Material

Introduction to optimization methods and line search

Computer Science II Lecture 2 Strings, Vectors and Recursion

The Extreme Value Theorem (IVT)

Biostatistics 615/815 Lecture 23: The Baum-Welch Algorithm Advanced Hidden Markov Models

What Secret the Bisection Method Hides? by Namir Clement Shammas

CS321 Introduction To Numerical Methods

Recap - helloworld - Have all tried?

Homework 1 Introduction to Computational Finance Spring 2019

Physics 234: Computational Physics

2.9 Linear Approximations and Differentials

Biostatistics 615/815 - Statistical Computing Lecture 1 : Introduction to Statistical Computing

The Newton Method as a Short WEB Example

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Quadratic Equations. Learning Objectives. Quadratic Function 2. where a, b, and c are real numbers and a 0

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

Numerical computing. How computers store real numbers and the problems that result

AC : MATHEMATICAL MODELING AND SIMULATION US- ING LABVIEW AND LABVIEW MATHSCRIPT

General Computer Science II Course: B International University Bremen Date: Dr. Jürgen Schönwälder Deadline:

C++ Constructor Insanity

ELEC 875 Design Recovery and Automated Evolution. Week 1 Class 2 Modeling. ELEC 875 Design Recovery and Automated Evolution

Programmazione. Prof. Marco Bertini

From: Robert Sharpley Subject: Homeworks #6 Date: February 22, :09:53 AM EST Cc: Robert Sharpley

CSE 333. Lecture 10 - references, const, classes. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

Lecture 03 Bits, Bytes and Data Types

The New Trisection and Trisection Plus Root- Seeking Algorithms by Namir Shammas

Exercises for a Numerical Methods Course

Informatik I (D-ITET) Übungsstunde 6, Hossein Shafagh

Biostatistics 615/815 Lecture 22: Matrix with C++

LECTURE 17. Array Searching and Sorting

Transcription:

Biostatistics 615/815 Lecture 13: Numerical Optimization Hyun Min Kang October 27th, 2011 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 1 / 35

The Problem Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 2 / 35

Specific Objectives Finding global minimum The lowest possible value of the function Very hard problem to solve generally Finding local minimum Smallest value within finite neighborhood Relatively easier problem Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 3 / 35

A quick detour - The root finding problem Consider the problem of finding zeros for f(x) Assume that you know Point a where f(a) is positive 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? Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 4 / 35

A C++ Example : definining a function object #include <iostream> class myfunc { // a typical way to define a function object public: double operator() (double x) const { return (x*x-1); ; int main(int argc, char** argv) { myfunc foo; std::cout << "foo(0) = " << foo(0) << std::endl; std::cout << "foo(2) = " << foo(2) << std::endl; Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 5 / 35

Root Finding with C++ // binary-search-like root finding algorithm double binaryzero(myfunc foo, double lo, double hi, double e) { for (int i=0;; ++i) { double d = hi - lo; double point = lo + d * 05; // find midpoint between lo and hi double fpoint = foo(point); // evaluate the value of the function if (fpoint < 00) { d = lo - point; lo = point; else { d = point - hi; hi = point; // e is tolerance level (higher e makes it faster but less accruate) if (fabs(d) < e fpoint == 00) { std::cout << "Iteration " << i << ", point = " << point << ", d = " << d << std::endl; return point; Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 6 / 35

Improvements to Root Finding Approximation using linear interpolation f f(b) f(a) (x) = f(a) + (x a) b a Root Finding Strategy Select a new trial point such that f (x) = 0 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 7 / 35

Root Finding Using Linear Interpolation double linearzero (myfunc foo, double lo, double hi, double e) { double flo = foo(lo); // evaluate the function at the end pointss double fhi = foo(hi); for(int i=0;;++i) { double d = hi - lo; double point = lo + d * flo / (flo - fhi); // double fpoint = foo(point); if (fpoint < 00) { d = lo - point; lo = point; flo = fpoint; else { d = point - hi; hi = point; fhi = fpoint; if (fabs(d) < e fpoint == 00) { std::cout << "Iteration " << i << ", point = " << point << ", d = " << d << std::endl; return point; Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 8 / 35

Performance Comparison Finding sin(x) = 0 between π/4 and π/2 #include <cmath> class myfunc { public: ; double operator() (double x) const { return sin(x); int main(int argc, char** argv) { myfunc foo; binaryzero(foo,0-m_pi/4,m_pi/2,1e-5); linearzero(foo,0-m_pi/4,m_pi/2,1e-5); return 0; Experimental results binaryzero() : Iteration 17, point = -299606e-06, d = -898817e-06 linearzero() : Iteration 5, point = 0, d = -447489e-18 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 9 / 35

R example of root finding > uniroot( sin, c(0-pi/4,pi/2) ) $root [1] -3531885e-09 $froot [1] -3531885e-09 $iter [1] 4 $estimprec [1] 8719466e-05 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 10 / 35

on root finding Implemented two methods for root finding Bisection Method : binaryzero() False Position Method : linearzero() In the bisection method, the bracketing interval is halved at each step For well-behaved function, the False Position Method will converage faster, but there is no performance guarantee Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 11 / 35

Back to the Problem Consider a complex function f(x) (eg likelihood) Find x which f(x) is maximum or minimum value Maximization and minimization are equivalent Replace f(x) with f(x) Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 12 / 35

Notes from Root Finding Two approaches possibly applicable to minimization problems Bracketing Keep track of intervals containing solution Accuracy Recognize that solution has limited precision Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 13 / 35

Notes on Accuracy - Consider the Machine Precision 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 ϵ ϵ comes from the second-order Taylor approximation f(x) f(b) + 1 2 f (b)(x b) 2 For functions where higher order terms are important, accuracy could be even lower For example, the minimum for f(x) = 1 + x 4 is only estimated to about ϵ 1/4 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 14 / 35

Outline of Strategy 1 Bracket minimum 2 Successively tighten bracket interval Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 15 / 35

Detailed Strategy 1 Find 3 points such that a < b < c f(b) < f(a) and f(b) < f(c) 2 Then search for minimum by Selecting trial point in the interval Keep minimum and flanking points Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 16 / 35

after Bracketing Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 17 / 35

Part I : Finding a Bracketing Interval Consider two points x-values a, b y-values f(a) > f(b) Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 18 / 35

Bracketing in C++ #define SCALE 1618 void bracket( myfunc foo, double& a, double& b, double& c) { double fa = foo(a); double fb = foo(b); double fc = foo(c = b + SCALE*(b-a) ); while( fb > fc ) { a = b; fa = fb; b = c; fb = fc; c = b + SCALE * (b-a); fc = foo(c); Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 19 / 35

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? Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 20 / 35

What is the best location for a new point X? Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 21 / 35

What we want We want to minimize the size of next search interval, which will be either from A to X or from B to C Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 22 / 35

Minimizing worst case possibility Formulae w = b a c a z = x b c a Segments will have length either 1 w or w + z Optimal case 1 w = w + z z 1 w = w w = 3 5 2 = 038197 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 23 / 35

The Golden Search Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 24 / 35

The Golden Ratio Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 25 / 35

The Golden Ratio The number 038196 is related to the golden mean studied by Pythagoras Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 26 / 35

The Golden Ratio Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 27 / 35

Golden Search Reduces bracketing by 40% after function evaluation Performance is independent of the function that is being minimized In many cases, better schemes are available Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 28 / 35

Golden Step #define GOLD 038196 #define ZEPS 1e-10 // precision tolerance double goldenstep (double a, double b, double c) { double mid = ( a + c ) * 5; if ( b > mid ) return GOLD * (a-b); else return GOLD * (c-b); Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 29 / 35

Golden Search double goldensearch(myfunc foo, double a, double b, double c, double e) { int i = 0; double fb = foo(b); while ( fabs(c-a) > fabs(b*e) ) { double x = b + goldenstep(a, b, c); double fx = foo(x); if ( fx < fb ) { (x > b)? ( a = b ) : ( c = b); b = x; fb = fx; else { (x < b)? ( a = x ) : ( c = x ); ++i; std::cout << "i = " << i << ", b = " << b << ", f(b) = " << foo(b) << std::endl; return b; Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 30 / 35

A running example Finding minimum of f(x) = cos(x) class myfunc { public: ; double operator() (double x) const { return 0-cos(x); int main(int argc, char** argv) { myfunc foo; goldensearch(foo,0-m_pi/4,m_pi/4,m_pi/2,1e-5); return 0; Results ị = 66, b = -442163e-09, f(b) = -1 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 31 / 35

R example of minimization > optimize(cos,interval=c(0-pi/4,pi/2),maximum=true) $maximum [1] -8648147e-07 $objective [1] 1 Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 32 / 35

Further improvements As with root finding, performance can improve substantially when local approximation is used However, a linear approximation won t do in this case Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 33 / 35

Approximation Using Parabola Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 34 / 35

Today Root Finding Algorithms Bisection Method : Simple but likely less efficient False Position Method : More efficient for most well-behaved function Single-dimensional minimization Golden Search Next Lecture More Single-dimensional minimization Brent s method Multidimensional optimization Simplex method Hyun Min Kang Biostatistics 615/815 - Lecture 13 October 27th, 2011 35 / 35