Professional Documents
Culture Documents
Optimization With R PDF
Optimization With R PDF
Optimization with R
40
Guy Yollin
60 Seattle R Users
80
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
mm 40 60 80 100 120
Optimization Problem
10
problem:
40
8
Given a function f (x)
Find the value x ∗
f (x)
6
Such that f (x ∗ ) obtains a
4
maximum
60 (or minimum) value
Subject to other constraints
2
on x
1 2 3 4 5
80
mm 40 60 80 100 120
80
R directly
mm supports a 40
number of powerful
60 optimization
80 capabilities
100 120
R Code:
> args(optimize)
40 interval, ..., lower = min(interval), upper = max(interval),
function (f,
maximum = FALSE, tol = .Machine$double.eps^0.25)
NULL
> f <- function(x) {
abs(x - 3.5) + (x - 2)^2 Optimization Problem
10
60
> op <- optimize(f = f, interval = c(1, 5))
8
> op
f (x)
6
$minimum
4
[1] 2.5
2
$objective
[1] 1.25
80 1 2 3
x
4 5
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
> args(optim)
function (par, fn, gr = NULL, ..., method = c("Nelder-Mead",
"BFGS",
40"CG", "L-BFGS-B", "SANN"), lower = -Inf, upper = Inf,
control = list(), hessian = FALSE)
NULL
mm 40 60 80 100
Time between Eruptions 120
0.04
From section 16.3 of Venables
& Ripley 4th Edition
0.03
Time between Old Faithful
40
0.02
geyser eruptions
Objective function is the
0.01
log-likelihood of a mixture of 2
0.00
normal60
distributions
40 60 80 100
minutes
n
X π yi − µ1 1 − π yi − µ2
L(π,80
µ1 , σ1 , µ2 , σ2 ) = log [ φ( )+ φ( )]
σ1 σ1 σ2 σ2
i=1
mm 40 60
Time between 80
Eruptions 100 120
0.04
Normal mixture
0.03 Nonparametric
40
0.02
60
0.01
0.00
80
40 60 80 100
minutes
Guy Yollin (Copyright © 2010) Optimization with R Seattle R Users 13 / 45
Outline
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
Linear programming
mm (LP)
40 problems
60have a linear
80 objective100
function and120
linear constraints
minx cT x
Ax ≥ b
40
x ≥0
where 60
x is a vector of variables to be opitmized (length n)
c is a vector of objective coefficients (length n)
A is a constraints matrix (dimension m x n)
80 b is a vector of constraints bounds (length m)
Minimize
mm the cost
40of a day’s 60
worth of meals
80 at Mickey100
D’s 120
Subject to
nutritional constraints
variety constraints (max 4 of any item)
integer constraints (no fractional Big Macs)
40
$val
[1] 4 4 4 80
4 4 4 4 4 4
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
1
minx c T x + x T Qx
2
s.t. Ax ≥ b
40
x ≥0
where
60
x is a vector of variables to be optimized (length n)
Q is a symmetric matrix (dimension m x n)
c is a vector of objective coefficients (length n)
80A is a constraints matrix (dimension m x n)
b is a vector of constraints bounds (length m)
Guy Yollin (Copyright © 2010) Optimization with R Seattle R Users 22 / 45
Style Analysis
Style Analysis is a technique that
mm 40 60 attempts80to determine
100the 120
fundamental drivers of a mutual
fund’s returns
Sharpe’s return-based style analysis:
40
minw var (RMF −w1 RB1 −. . . wn RBn )
X
s.t. wi = 1
60 0 ≤ wi ≤ 1
w T Vw
80MF − w1 RB1 − . . . wn RBn ) = 2(
var (R − c T w ) + var (RMF )
2
mm 40 60 80 100 120
R Code: the solve.QP function
> library(quadprog)
> args(solve.QP)
function (Dmat, dvec, Amat, bvec, meq = 0, factorized = FALSE)
NULL 40
R Code: mm
setup to call solve.QP
40 60 80 100 120
> (Amat <- t(rbind(rep(1,N), diag(N), -diag(N))))
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 1 0 0 0 -1 0 0 0
[2,] 1 0 1 0 0 0 -1 0 0
[3,] 1 40 0 0 1 0 0 0 -1 0
[4,] 1 0 0 0 1 0 0 0 -1
> # weights sum to 1, weights >=0, weights <= 0
> b0 <- c(1, rep(0,N),rep(-1,N))
> #
> optimal <- solve.QP(Dmat, dvec, Amat, bvec = b0, meq = 1)
60
> W <- matrix(round(100*optimal$solution),nrow=2,byrow=T)
> dimnames(W) <- list(c("large","small"),c("value","growth"))
> W
value growth
large 69 27
small 80
0 4
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
mm 40 60 80 100 120
Differential Evolution (DE) is a very simple and yet very powerful
population based stochastic function minimizer
2 2 2
mm 1 , x2 ) = (1 −
f (x40 802 − x1 ) 100
60x1 ) + 100(x 120
40
60
80
Permutation 1
x1 0.32 0.29 mm
0.68 -0.51 1.29 -0.94
40 60 80 100 120
x2 0.15 0.52 -1.75 0.64 -0.84 1.78
E C D B F A +
Permutation 2 -
x1 1.29 -0.94 0.32 0.29 0.68 -0.51
F
x2 -0.84 1.78 0.15 0.52 -1.75 0.64
F A E C D B + Mutant Population
40 + x1
x2
-0.48
1.31
1.66
-2.76
-0.23
-0.88
0.65
-0.74
-0.45
2.51
-0.02
1.06
Permutation 3
x1 0.29 0.68 -0.51 1.29 -0.94 0.32
x2 0.52 -1.75 0.64 -0.84 1.78 0.15 Random Crossover Template Trial Population
A E F B C D x1 F F T T F F x1 -0.94 -0.51 -0.23 0.64 0.32 1.29
x2 T F T T T T X x2 1.31 0.64 -0.88 -0.74 2.00 1.06
A’ B’ C’ D’ E’ F’
60 Current Population
x1 -0.94 -0.51 0.29 0.68 0.32 1.29 trial member
>
x2 1.78 0.64 0.52 -1.75 0.15 -0.84 current member
A B C D E F
80 A’ B C D’ E F’
mm 40 60 80 100 120
40
60
80
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
R Code: DEoptim arguments
> args(DEoptim)
function (fn, lower, upper, control = DEoptim.control(), ...)
40
NULL
80
R Code: mm
call to DEoptim
40 60 80 100 120
> G2 = function( x ) {
if( x[1] >=0 & x[1] <=10 & x[2] >=0 & x[2] <= 10 &
x[1] * x[2] >= 0.75 & x[1] + x[2] <= 15 ) {
s <- cos(x[1])^4+cos(x[2])^4
p <- 40
2*cos(x[1])^2*cos(x[2])^2
r <- sqrt(x[1]^2+2*x[2]^2)
f <- -abs((s-p)/r)
} else {
f <- 0
}
60
return(f)
}
> lower = c(0,0)
> upper = c(10,10)
> res = DEoptim(G2, lower, upper,
control = list(NP = 20, itermax = 100, strategy=1, trace=F))
80
mm 40 60 80 100 120
R Code: results of DEoptim
> res$optim
$bestmem
par1 40 par2
1.6013695 0.4683657
$bestval
[1] -0.3649653
$nfeval 60
[1] 2020
$iter
[1] 100
80
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
mm 40 60 80 100 120
40
60
80
mm 40 60 80 100 120
80
mm 40 60 80 100 120
1 Overview of Optimization
3 Linear40
Programming
4 Quadratic Programming
5 Differential
60 Evolution Algorithm
6 Optimization Activities in R
7 Optimization
80
References
mm 40 60 80 100 120
80
mm 40 60 80 100 120
80
mm 40 60 80 100 120
University of Washington Online HAM1 Performance
4.0
Certificate in Computational Finance HAM1
EDHEC LS EQ
3.5
SP500 TR
3.0
Cumulative Return
2.5
2.0
40 computational finance
Survey
1.5
methods and techniques through
1.0
the development of R software
0.05
Monthly Return
0.00
Statistical analysis of asset
−0.05
60
returns
0.0 −0.10
−0.1
Financial time series modeling Drawdown
−0.2
−0.3
Factor models
−0.4
Jan 96 Jan 97 Jan 98 Jan 99 Jan 00 Jan 01 Jan 02 Jan 03 Jan 04 Jan 05 Jan 06 Dec 06
Date
Portfolio
80 optimization
Plot from the PerformanceAnalytics package
mm 40 60 80 100 120
40
80