Optimization With R
Optimization With R
Optimization With R
40
60
80
100
120
Optimization with R
40
Guy Yollin
Seattle R Users
60
80
2010)
Optimization with R
Seattle R Users
1 / 45
Personal Introduction
Guy Yollin - http://www.linkedin.com/in/guyyollin
mm
40
60
80
100
120
Professional Experience
Rotella Capital Management, Quantitative Research Analyst
Insightful Corporation, Director of Financial Engineering
40
J.E. Moody, LLC, Financial Engineer
Oregon Graduate Institute (OHSU), Adjunct Instructor
Electro Scientific Industries, Director of Engineering, Vision Products
Division
60
Education
Oregon Graduate Institute, Masters in Computational Finance
Drexel University, Bachelors in Electrical Engineering
80
2010)
Optimization with R
Seattle R Users
2 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
3 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
4 / 45
Overview of Optimization
mm
40
60
80
100
120
Optimization Problem
6
4
2
f (x)
10
80
2010)
Optimization with R
Seattle R Users
5 / 45
Overview of Optimization
mm
40
60
80
100
120
80
2010)
Optimization with R
Seattle R Users
6 / 45
Optimization Functions in R
R directly
supports a 40
number of powerful
optimization
capabilities
mm
60
80
100
120
2010)
Optimization with R
Seattle R Users
7 / 45
optimize()
Single variable optimization: f (x) = |x 3.5| + (x 2)2
mm
40
60
80
100
120
R Code:
> args(optimize)
function (f,
40 interval, ..., lower = min(interval), upper = max(interval),
maximum = FALSE, tol = .Machine$double.eps^0.25)
NULL
> f <- function(x) {
abs(x - 3.5) + (x - 2)^2
}
60
> op <- optimize(f
= f, interval = c(1, 5))
> op
$minimum
[1] 2.5
f (x)
10
Optimization Problem
$objective
80
[1] 1.25
2010)
Optimization with R
Seattle R Users
8 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
9 / 45
40
60
80
100
120
2010)
Optimization with R
Seattle R Users
10 / 45
100
120
0.04
60
0.03
0.02
0.01
40
0.00
mm
40
60
80
100
minutes
L(,80
1 , 1 , 2 , 2 ) =
n
X
i=1
2010)
log [
yi 1
1 yi 2
(
)+
(
)]
1
1
2
2
Optimization with R
Seattle R Users
11 / 45
Optimization Example
R Code: optimizing with optim
mm
40
60
80
100
120
60
s1
u2
5.000000 80.000000
s2
5.000000
> # optimize
> mix.nl0 <- optim(p0, mix.obj, x = waiting)
> mix.nl0$par
p
u1
0.3073520 54.1964325
s1
u2
4.9483442 80.3601824
s2
7.5110485
80
> mix.nl0$convergence
[1] 0
Guy Yollin (Copyright
2010)
Optimization with R
Seattle R Users
12 / 45
Time between
Eruptions
60
80
0.04
40
100
120
0.03
Normal mixture
Nonparametric
0.02
40
0.00
0.01
60
80
40
60
80
100
minutes
2010)
Optimization with R
Seattle R Users
13 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
14 / 45
Linear Programming
Linear programming
(LP)
function and120
mm
40 problems
60have a linear
80 objective100
linear constraints
cT x
minx
40
Ax b
x 0
where
60
2010)
Optimization with R
Seattle R Users
15 / 45
Menu Planner
mm the cost
Minimize
worth of meals
Ds
40of a days 60
80 at Mickey100
Subject to
120
nutritional constraints
variety constraints (max 4 of any item)
integer constraints (no fractional Big Macs)
40
Food
Quarter Pounder
McLean Delux
Big Mac
60
FiletOFish
McGrilled Chicken
Small Fries
Sausage McMuffin
Lowfat Milk
Orange Juice
80
Minimum
Maximum
Cost
1.84
2.19
1.84
1.44
2.29
0.77
1.29
0.60
0.72
2010)
Cals
510
370
500
370
400
220
345
110
80
2000
3500
Carbs
34
35
42
38
42
26
27
12
20
350
375
Protein
28
24
25
14
31
3
15
9
1
55
Optimization with R
VitaA
15
15
6
2
8
0
4
10
2
100
VitaB
6
10
2
0
15
15
0
4
120
100
Calc
30
20
25
15
15
0
20
30
2
100
Iron
20
20
20
10
8
2
15
0
2
100
Seattle R Users
16 / 45
mm
40
60
80
100
120
> library(Rglpk)
Using the GLPK callable library version 4.42
> args(Rglpk_solve_LP)
function (obj, mat, dir, rhs, types = NULL, max = FALSE, bounds = NULL,
verbose
40= FALSE)
NULL
obj
60
mat
dir
rhs
type
80
bounds
max
2010)
Optimization with R
Seattle R Users
17 / 45
mm
40
60
80
100
120
> fmat
Quarter Pounder
McLean Delux
Big Mac
FiletOFish40
McGrilled Chicken
Small Fries
Sausage McMuffin
Lowfat Milk
Orange Juice
> Cost
60
Quarter Pounder
McLean Delux
1.84
2.19
FiletOFish McGrilled Chicken
1.44
2.29
80
Sausage McMuffin
Lowfat Milk
1.29
0.60
2010)
Big Mac
1.84
Small Fries
0.77
Orange Juice
0.72
Optimization with R
Seattle R Users
18 / 45
mm
40
60
80
100
120
Carbs Protein
350
55
VitaA
100
VitaB
100
Calc
100
Iron
100
VitaA
Inf
VitaB
Inf
Calc
Inf
Iron
Inf
40
Carbs Protein
375
Inf
2010)
Optimization with R
Seattle R Users
19 / 45
mm
40
60
80
100
120
2010)
Optimization with R
Seattle R Users
20 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
21 / 45
Quadratic Programming
Quadratic programming (QP) problems have a quadratic objective
40
60
80
100
functionmm
and linear constraints
minx
s.t.
40
where
60
120
1
c T x + x T Qx
2
Ax b
x 0
2010)
Optimization with R
Seattle R Users
22 / 45
Style Analysis
mm
40
60
40
s.t.
60
wi = 1
0 wi 1
var (R
80MF w1 RB1 . . . wn RBn ) = 2(
2010)
w T Vw
c T w ) + var (RMF )
2
Optimization with R
Seattle R Users
23 / 45
40
60
80
100
120
> library(quadprog)
> args(solve.QP)
function (Dmat, dvec, Amat, bvec, meq = 0, factorized = FALSE)
40
NULL
2010)
Optimization with R
Seattle R Users
24 / 45
Quadratic Programming
R Code: setup to call solve.QP
mm
40
60
80
100
120
> head(z)
WINDSOR
2005-10-03 -0.1593626
2005-10-04 -0.7202912
2005-10-05 -1.3748700
40
2005-10-06 -0.2445986
2005-10-07 0.4073325
2005-10-10 -0.8163311
LARGEVALUE
-0.09535756
-1.27326280
-1.66080920
-0.51217665
0.41993619
-0.88589613
LARGEGROWTH
-0.005995983
-0.762412906
-1.475015837
-0.485592013
0.330131430
-0.564549912
SMALLVALUE SMALLGROWTH
0.3403397
0.4733873
-1.0561886 -0.9461192
-2.8568633 -2.9082448
-0.5641660 -1.1559082
0.7721558
0.7480504
-0.9743418 -1.0118028
LARGEVALUE
LARGEGROWTH SMALLVALUE SMALLGROWTH
80
2.878281
2.437179
3.214248
2.948217
2010)
Optimization with R
Seattle R Users
25 / 45
Quadratic Programming
R Code: mm
setup to call solve.QP
40
60
80
100
120
large
small
value growth
69
27
0
4
80
2010)
Optimization with R
Seattle R Users
26 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
27 / 45
40
60
80
100
120
2010)
Optimization with R
Seattle R Users
28 / 45
mm
120
40
60
80
2010)
Optimization with R
Seattle R Users
29 / 45
mm
x1
0.32
0.29
0.68
-0.51
1.29
-0.94
x2
0.15
0.52
-1.75
0.64
-0.84
1.78
x1
1.29
-0.94
0.32
0.29
0.68
-0.51
x2
-0.84
1.78
0.15
0.52
-1.75
0.64
40
60
80
100
120
Permutation 2
F
Mutant Population
40
x1
-0.48
1.66
-0.23
0.65
-0.45
-0.02
x2
1.31
-2.76
-0.88
-0.74
2.51
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
Trial Population
x1
x2
60
x1
-0.94
-0.51
-0.23
0.64
0.32
1.29
x2
1.31
0.64
-0.88
-0.74
2.00
1.06
Current Population
x1
-0.94
-0.51
0.29
0.68
0.32
1.29
x2
1.78
0.64
0.52
-1.75
0.15
-0.84
trial member
>
current member
80
2010)
Optimization with R
x1
-0.94
-0.51
0.29
0.65
0.32
1.29
x2
1.31
0.64
0.52
-0.74
0.15
1.06
Seattle R Users
30 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
31 / 45
G2 Function
4 +cos(x )2 2 cos(x )2
2
1
x12 +2x22
f (x1 , x2 ) = | cos(x1 )
mm
40
60
80
cos(x2 )2
|
100
120
where: 0 x1 10 , 0 x2 10 , x1 x2 0.75 , x1 + x2 15
40
60
80
2010)
Optimization with R
Seattle R Users
32 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
33 / 45
40
60
80
100
120
2010)
Optimization with R
Seattle R Users
34 / 45
mm
40
60
80
100
120
> args(DEoptim.control)
function (VTR = -Inf, strategy = 2, bs = FALSE, NP = 50, itermax = 200,
CR = 0.5, F = 0.8, trace = TRUE, initialpop = NULL, storepopfrom = itermax +
1, storepopfreq = 1, checkWinner = FALSE, avWinner = TRUE,
40
p = 0.2)
NULL
2010)
Optimization with R
Seattle R Users
35 / 45
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
2010)
Optimization with R
Seattle R Users
36 / 45
40
60
80
100
120
60
$iter
[1] 100
80
2010)
Optimization with R
Seattle R Users
37 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
38 / 45
40
60
80
100
120
40
60
80
2010)
Optimization with R
Seattle R Users
39 / 45
40
Project
60
80
100
120
Description
Location
Contributors
optimizer
R-Forge
Rmetrics2AMPL
rmetrics.org
Wuertz, Chalabi
COIN-OR
coin-or.org
COIN-OR Foundation
msi.co.jp
Yamashita, Tanabe
40
60
RnuOPT
80
2010)
Optimization with R
Seattle R Users
40 / 45
Outline
mm
40
Overview of Optimization
Linear40
Programming
Quadratic Programming
Differential
Evolution Algorithm
60
Optimization Activities in R
Optimization
References
80
2010)
60
Optimization with R
80
100
Seattle R Users
120
41 / 45
40
60
80
100
120
2010)
Optimization with R
Seattle R Users
42 / 45
40
60
80
100
120
80
2010)
Optimization with R
Seattle R Users
43 / 45
mm
40
60
80
100
120
4.0
HAM1 Performance
HAM1
3.5
EDHEC LS EQ
Factor models
2.5
2.0
1.5
1.0
0.05
0.00
Monthly Return
0.05
0.0 0.10
0.1
0.2
0.4
Drawdown
0.3
40 computational finance
Survey
methods and techniques through
the development of R software
Cumulative Return
3.0
SP500 TR
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
optimization
80
2010)
Optimization with R
Seattle R Users
44 / 45
Conclusion
mm
40
60
80
100
120
40
80
2010)
Optimization with R
Seattle R Users
45 / 45