Beruflich Dokumente
Kultur Dokumente
February 7, 2014
1/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Some notes
2/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Table of Contents
1 Introduction
3 R implementations
3/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
4/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Optimization in statistics
5/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
6/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
7/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Unimodality
8/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Table of Contents
1 Introduction
3 R implementations
9/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
10/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
11/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
12/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
13/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
14/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
15/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Parabolic interpolation
16/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
17/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
18/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Brent’s method
19/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Newton-Raphson
Newton’s method
f 0 (xn )
xn+1 = xn − .
f 00 (xn )
20/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Newton-Raphson
Newton-Raphson
21/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Newton-Raphson
Newton-Raphson
If we have an initial guess x0 , we can approximate f 0 (x) using a
Taylor series expansion:
f 0 (x0 ) + (x − x0 ) × f 00 (x0 ) = 0
⇐⇒ x × f 00 (x0 ) = x0 × f 00 (x0 ) − f 0 (x0 )
f 0 (x0 )
⇐⇒ x = x0 − .
f 00 (x0 )
22/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Newton-Raphson
f 0 (x0 )
x1 = x0 −
f 00 (x0 )
23/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Newton-Raphson
Newton-Raphson: remarks
24/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Table of Contents
1 Introduction
3 R implementations
25/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Univariate optimization in R
26/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
optimize()
Exercise 7.1.1 a:
R> func1 <- function(x) return(abs(x-3.5) + abs(x-2)
+ + abs(x-1))
R> optimize(f=func1, interval=c(0, 5))
$minimum
[1] 2.000005
$objective
[1] 2.500005
27/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
optimize()
We may pass arguments to the function to optimize: (Exercise
7.1.1 b)
R> func2 <- function(x, y) return(abs(x-y[1]) +
+ abs(x-y[2]) + abs(x-y[3])+ abs(x-y[4]))
R> optimize(f=func2, interval=c(0,5), y=c(3.2, 3.5, 2, 1))
$minimum
[1] 2.553556
$objective
[1] 3.7
The function is always optimized with respect to the first argument
of the function. Note that minimum is the argument value at the
minimum. 28/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
nlm()
The function can conduct multivariate optimization but only
univariate examples are presented here. If no derivatives are
supplied, numerical derivatives are calculated and used. Example
7.1:
R> func3 <- function(x) return(exp(-x)+x^4)
R> sol3 <- nlm(f=func3, p=2)
R> sol3$minimum
[1] 0.6675038
R> sol3$estimate
[1] 0.5282519
Note that minimum is the function value at the minimum and
estimate is the argument value at the minimum. 29/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
nlm()
We can add the first and second derivatives as attributes to the
output of the function.
R> func4 <- function(x){
+ res <- exp(-x)+x^4
+ attr(res, "gradient") <- -exp(-x)+4*x^3
+ attr(res, "hessian") <- exp(-x)+12*x^2
+ return(res)
+ }
R> sol4 <- nlm(func4, 2)
R> sol4$iterations
[1] 7
R> sol3$iterations
[1] 10 30/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
nlm() vs optimize()
Using derivatives is faster in theory, but how the methods are
implemented can make this not so in practice.
R> system.time({ for(i in 1:2000) optimize(func3,
+ interval=c(-1000, 1000), tol=1e-6)})
user system elapsed
0.33 0.00 0.33
R> system.time({ for(i in 1:2000) nlm(func4, 2)})
user system elapsed
0.41 0.00 0.41
R> system.time({ for(i in 1:2000) nlm(func4, 0.5)})
user system elapsed
0.28 0.00 0.28 31/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
A faster Newton-Raphson
$minimum
[1] 0.5282519
$iterations
[1] 7
R> system.time({for(i in 1:2000) NewtonOpt(2, func3,
+ dfunc3, d2func3) })
user system elapsed
0.15 0.00 0.15 33/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension
Introduction Optimization in one dimension R implementations
Plotting tools
R> curve(func3, from=0, to=1)
Equivalent to plot(func3, from=0, to=1).
1.3
1.2
1.1
func3(x)
1.0
0.9
0.8
0.7
Plotting tools
R> par(mfrow=c(1, 2))
R> curve(dfunc3, from=0, to=1)
R> curve(d2func3, from=0, to=1)
10 12
3
d2func3(x)
dfunc3(x)
8
6
1
4
0
2
−1
0.0 0.2 0.4 0.6 0.8 1.0 0.0 0.2 0.4 0.6 0.8 1.0
x x
35/35
Björn Andersson (w/ Jianxin Wei) Department of Statistics, Uppsala University
Lecture 5 - Optimization in one dimension