Professional Documents
Culture Documents
R Tools For Portfolio Optimization: Guy Yollin
R Tools For Portfolio Optimization: Guy Yollin
Guy Yollin
Quantitative Research Analyst
Rotella Capital Management
Bellevue, Washington
Backgrounder
Insightful Corporation
Adjunct Instructor
Statistical Computing & Financial Time Series Analysis
Financial Engineer
Futures Trading, Risk Management, Business Development
Introduction
100
80
GM
C
60
DD
MMM
CAT
KFT
XOM
MRK
PFE
AXP
CVX
VZ
JNJ
BA
HPQ
GE
BAC
20
R-SIG-FINANCE QUESTION:
JPM
INTC
HD
PG
40
IBM
MCD
KO
UTX
T
WMT
MSFT
DIS
AA
10
100
15
90
stock price
95
80
85
Maximum
Drawdown
Jan
ANSWER:
-5
0.12
-15
P/L Distribution
-10
0.14
drawdown (%)
Mar
0.10
Jan
0.06
CVaR
0.02
0.04
Density
0.08
VaR
0.00
Yes!
Mar
-15
-10
-5
10
daily return
20
Outline
Mean-Variance Portfolio Optimization
quadratic programming
tseries, quadprog
Omega Optimization
Adding Constraints
Maximum Drawdown Optimization
R-Ratio Optimization
Wrap-Up
Portfolio Weights
0.10
200
BAC
0.08
WMT
DD
0.06
IBM
CAT
T
KO
VZ
AA
MSFT
MMM
CVX
GE
MCD
DIS
UTX
PG
JNJ
AXP
0.04
HPQ
PFE
XOM
BA
MRK
-100
0.02
KFT
GM
XOM
VZ
WMT
UTX
PG
PFE
MRK
MSFT
MMM
KO
MCD
KFT
JNJ
JPM
IBM
INTC
HD
HPQ
GE
GM
DD
DIS
CVX
200
CAT
150
BA
100
BAC
50
AA
0.00
AXP
100
JPM
INTC
HD
Description
computer mean-variance efficient portfolio
Usage
portfolio.optim(x, pm = mean(x), riskless = FALSE, shorts = FALSE,
rf = 0.0, reslow = NULL, reshigh = NULL, covmat = cov(x), ...)
Example
1 - returns vector
2 - covariance matrix
3 - minimum return
>
>
>
>
averet = matrix(colMeans(r),nrow=1)
rcov = cov(r)
target.return = 15/250
port.sol = portfolio.optim(x = averet, pm = target.return,
covmat = rcov, shorts = F, reslow = rep(0,30), reshigh = rep(0.1,30))
> w = cleanWeights(port.sol$pw,syms)
> w[w!=0]
HD IBM INTC JNJ
KO MCD MSFT PFE
PG
T
VZ WMT
0.05 0.10 0.04 0.10 0.10 0.10 0.07 0.04 0.10 0.10 0.10 0.10
Efficient Frontier
DJIA Returns: 02/04/2009 - 04/03/2009
efficient
frontier
with shorts
200
BAC
100
JPM
INTC
HD
DD
IBM
CAT
T
KO
VZ
AA
MSFT
MMM
CVX
GE
MCD
DIS
UTX
PG
JNJ
AXP
HPQ
PFE
XOM
BA
MRK
-100
WMT
C
KFT
efficient
frontier
long only
0
50
100
GM
150
200
200
BAC
100
JPM
INTC
HD
DD
IBM
CAT
T
KO
VZ
AA
MSFT
MMM
CVX
GE
MCD
DIS
UTX
PG
JNJ
AXP
HPQ
PFE
XOM
BA
MRK
-100
WMT
C
KFT
GM
50
100
150
200
callback
function calls
portfolio.optim()
use optimize() to
find return level
with maximum
Sharpe ratio
max.sh = which.max(ef$ret/ef$vol)
w = rep(0,ncol(averet))
xmin = optimize(f=optim.callback, interval=c(ef$ret[max.sh-1], upper=ef$ret[max.sh+1]),
averet=averet,rcov=rcov,reshigh=reshigh,reslow=reslow,shorts=shorts)
return(w)
}
10
Description
solve quadratic program
general quadratic program
Usage
solve.QP(Dmat, dvec, Amat, bvec, meq=0, factorized=FALSE)
11
Extending portfolio.optim
Modify portfolio.optim
Market neutral (weights sum to zero)
if (!is.null(reslow) & !is.null(reshigh)) {
a3 <- matrix(0, k, k)
diag(a3) <- 1
Amat <- t(rbind(a1, a2, a3, -a3))
b0 <- c(1, pm, reslow, -reshigh)
} else {
Amat <- t(rbind(a1, a2))
b0 <- c(1, pm)
}
12
Outline
Mean-Variance Portfolio Optimization
quadratic programming
tseries, quadprog
Omega Optimization
Adding Constraints
Maximum Drawdown Optimization
R-Ratio Optimization
Wrap-Up
13
Conditional Value-at-Risk
0.12
0.14
P/L Distribution
0.10
is our
confidence level
(e.g. 95% or 90%)
0.06
CVaR
0.00
0.02
0.04
Density
0.08
VaR
-15
mean of worst
1- losses
-10
-5
10
daily return
14
0.10
0.12
0.14
P/L Distribution
0.06
CVaR
ds
0.02
0.04
CVaR Optimization
0.00
Density
0.08
VaR
-15
-10
-5
10
daily return
15
Description
solves linear and MILP programs (via GNU Linear Programming Kit)
general linear program
Usage
Rglpk_solve_LP(obj, mat, dir, rhs, types = NULL, max = FALSE,
bounds = NULL, verbose = FALSE)
16
CVaR Optimization
cvarOpt = function(rmat, alpha=0.05, rmin=0, wmin=0, wmax=1, weight.sum=1)
{
require(Rglpk)
n = ncol(rmat) # number of assets
s = nrow(rmat) # number of scenarios i.e. periods
averet = colMeans(rmat)
# creat objective vector, constraint matrix, constraint rhs
Amat = rbind(cbind(rbind(1,averet),matrix(data=0,nrow=2,ncol=s+1)),
cbind(rmat,diag(s),1))
objL = c(rep(0,n), rep(-1/(alpha*s), s), -1)
bvec = c(weight.sum,rmin,rep(0,s))
# direction vector
dir.vec = c("==",">=",rep(">=",s))
# bounds on weights
bounds = list(lower = list(ind = 1:n, val = rep(wmin,n)),
upper = list(ind = 1:n, val = rep(wmax,n)))
supports general
equality/inequality
constraints
17
80
maximum
CVaR
ratio
GM
C
IBM
JPM
60
INTC
DD
GE
HD
PG
40
BAC
MMM
20
CAT
MRK
PFE
AXP
CVX
VZ
JNJ
HPQ
MCD UTX
KO
WMT
BA
T
MSFT
DIS
AA
minimum
CVaR
KFT
XOM
10
15
20
18
Outline
Mean-Variance Portfolio Optimization
quadratic programming
tseries, quadprog
Omega Optimization
Adding Constraints
Maximum Drawdown Optimization
R-Ratio Optimization
Wrap-Up
19
Differential Evolution
DE is a very simple and yet very powerful population
based stochastic function minimizer
Ideal for global optimization of multidimensional
multimodal functions (i.e. really hard problems)
Developed in mid-1990 by Berkeley researchers Ken
Price and Rainer Storm
Implemented in R in the package DEoptim
20
CR
see http://www.icsi.berkeley.edu/~storn/code.html
21
DE Example
global
minimum
at (1,1)
22
Description
performs evolutionary optimization via differential evolution
algorithm
Usage
DEoptim(FUN, lower, upper, control = list(), ...)
Example
> lower = c(-2,-2)
> upper = c(2,2)
> res = DEoptim(banana, lower, upper)
> res$optim
$bestmem
par1
par2
0.9987438 0.9976079
$bestval
[1] 2.986743e-06
$nfeval
[1] 5050
$iter
[1] 100
23
0.6
1.0
0.4
0.5
0.2
0.00.0
Proportion
Density<= x
0.8
1.5
1.0
a
-1.0
-0.5
-0.5
L
0.0
b
0.0
0.5
0.5
1.0
1.5
1.0
daily return
24
0.4
0.6
0.2
0.0
Proportion <= x
0.8
1.0
a
-5
L
0
b
5
daily return
10
simple calculation:
See
Keating & Shadwick 2002
Kazemi et. al., 2003
omega = mean(pmax(r-L,0))/mean(pmax(L-r,0))
25
Omega Optimization
objective
function
optOmega = function(x,ret,L)
{
retu = ret %*% x
obj = -Omega(retu,L=L,method="simple")
weight.penalty = 100*(1-sum(x))^2
return( obj + weight.penalty )
}
26
2.6
2.7
2.5
2.5
2.6
2.6
2.6
2.5
2.6
0.8
2.7
2.7
2.7
2.7
2.6
2.8
2.6
2.7
2.7
0.7
2.7
2.7
2.7
2.8
2.8
2.8
2.7
2.9
2.8
0.6
2.8
2.7
2.7
2.8
2.9
2.9
2.9
2.9
0.5
2.7
2.8
2.9
2.8
2.9
2.9
3.1
3.1
0.4
2.7
2.8
2.9
3.1
3.2
3.3
0.3
2.7
2.9
3.1
3.2
3.3
3.4
3.4
0.2
2.9
3.1
3.1
3.2
3.3
3.3
3.4
3.5
0.1
2.8
3.1
3.1
3.2
3.3
3.4
3.5
3.4
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
CR
NP=600, itermax=200
27
Max Omega
Omega = 3.50
Sharpe = 5.92
IBM
60
INTC
DD
HD
40
PG
MMM
Max Sharpe
Sharpe = 6.76
Omega = 3.07
KFT
XOM
20
MRK
PFE
CVX
VZ
JNJ
BA
HPQ
MCD
KO
WMT
UTX
T
DIS
MSFT
10
20
30
40
50
60
28
Weight Comparison
Weights of Max Sharpe Portfolio
Omega = 3.07
0.10
0.08
0.06
0.04
0.02
MRK
MSFT
PFE
PG
UTX
VZ
WMT
XOM
MRK
MSFT
PFE
PG
UTX
VZ
WMT
XOM
MMM
MCD
KO
KFT
JPM
JNJ
INTC
IBM
HPQ
HD
GM
GE
DIS
DD
CVX
CAT
BAC
BA
AXP
AA
0.00
MMM
MCD
KO
KFT
JPM
JNJ
INTC
IBM
HPQ
HD
GM
GE
DIS
DD
CVX
CAT
BAC
BA
AXP
AA
0.00
29
all non-zero
weights now
greater than 3%
0.10
0.08
0.06
0.04
0.02
XOM
WMT
VZ
UTX
PG
PFE
MSFT
MRK
MMM
MCD
KO
KFT
JPM
JNJ
INTC
IBM
HPQ
HD
GM
GE
DIS
DD
CVX
CAT
BAC
BA
AXP
AA
0.00
30
Maximum Drawdown
90
85
Maximum
Drawdown
80
stock price
95
100
Jan
Mar
-5
-10
-15
drawdown (%)
Jan
Mar
31
could readily
implement
optimization
on Calmar or
Sterling
ratios
res = DEoptim(optMDD.gt3,lower,upper,
control=list(NP=2000,itermax=1000,F=0.2,CR=0.8),
ret=coredata(r))
XOM
WMT
VZ
UTX
PG
PFE
MSFT
MRK
MMM
MCD
KO
KFT
JPM
JNJ
INTC
IBM
HPQ
HD
GM
GE
DIS
DD
CVX
CAT
BAC
BA
AXP
AA
0.00
32
-0.5
-1.0
15
MDD
Omega
Sharpe
MaxDD = 0.47%
-1.5
drawdown (%)
0.0
Jan
Mar
0.0
-0.5
-1.0
-1.5
MaxDD = 1.85%
cs
drawdown (%)
10
Jan
Mar
-0.5
-1.0
MaxDD = 0.98%
-1.5
drawdown (%)
0.0
Jan
Jan
Mar
Mar
33
0.5
0.6
P/L Distribution
0.1
0.2
0.3
CVaR+
0.0
Density
0.4
CVaR-
-2
-1
daily return
34
R-Ratio Optimization
optRR.gt3 = function(x,ret)
{
retu = ret %*% x
obj = -CVaR(-retu)/CVaR(retu)
weight.penalty = 100*(1-sum(x))^2
small.weight.penalty = 100*sum(x[x<0.03])
return( obj + weight.penalty + small.weight.penalty )
}
res = DEoptim(optRR.gt3,lower,upper,
control=list(NP=2000,itermax=1000,F=0.2,CR=0.8),
ret=coredata(r))
XOM
WMT
VZ
UTX
PG
PFE
MSFT
MRK
MMM
MCD
KO
KFT
JPM
JNJ
INTC
IBM
HPQ
HD
GM
GE
DIS
DD
CVX
CAT
BAC
BA
AXP
AA
0.00
35
R-Ratio Comparison
R-Ratio = 1.7
1.5
R-Ratio = 2.6
0.6
CVaR+
0.0
10
0.0
0.2
0.5
CVaR+
1.0
CVaR-
RR
MDD
Omega
Sharpe
Density
CVaR-
0.4
Density
15
0.8
1.0
Optimal R-Ratio
Optimal Max DD
Cummulative Returns for 4 Optimized Portfolios
-0.5
0.0
0.5
daily return
daily return
Optimal Omega
Optimal Sharpe
2.0
cs
-1
R-Ratio = 2.1
1.5
2.0
R-Ratio = 1.9
1.0
1.5
CVaRCVaR+
0.0
0.5
1.0
Density
1.0
0.5
CVaR+
0.0
Density
CVaR-
-1.0
-0.5
0.0
0.5
Jan
1.0
1.5
2.0
2.5
-0.5
Mar
0.0
daily return
0.5
1.0
daily return
36
Summary
Mean-Variance Portfolio Optimization
high-level function: portfolio.optim
low-level function: solve.QP
37
Thank You
Questions & Answers
Contact the Author
Guy Yollin
guy.yollin@rotellacapital.com
38