Optimization Portfolio
Optimization Portfolio
using MOSEK
Gábor Balló
Optimization Specialist, MOSEK ApS
Email: gabor.ballo@mosek.com
www.mosek.com
Outline
Introduction
Mean-variance portfolio optimization
Problem statement
Conic formulation
Examples
Factor models
Definition
Conic constrains with factor model
Example: 2-factor model
Transaction cost modeling
Definition
Fixed and variable cost model
Market impact cost
Examples
Benchmark relative portfolio optimization
Definition
Optimization problem
Example 1 / 50
Section 1
Introduction
Introduction
• New way.
• Old way. • K is a convex cone.
• f , components of g • Similar to linear
convex. programming.
• Components of h affine. • Interior-point solvers.
• Needs gradients, • Easy for Mosek financial
Hessians. customers.
4 / 50
Example: Quadratic cone
x1 Q3
2
Q
x2
5 / 50
Section 2
Goal
Maximize the return of the investment, while keeping the
investment risk (variance) acceptable.
8 / 50
Solution of MVO
Data
• µ: The estimate of the vector µ.
• Σ: The estimate of the matrix Σ.
Simplifications
minimize xT Σx
subject to µT x ≥ rmin , (1)
1T x = 1.
QO problem, easy to solve.
maximize µT x
subject to x Σx ≤ γ 2 ,
T
(2)
1T x = 1.
QCQO problem, easy after conic reformulation.
10 / 50
Solution of MVO
δ
maximize µT x − xT Σx
2 (3)
subject to 1T x = 1.
• QO problem.
• δ: Tradeoff parameter, no intuitive meaning.
• Quadratic constraint: xT Σx ≤ γ 2
• xT Σx = xT GGT x = (GT x)T (GT x) = kGT xk22
• Equivalent, conic representable constraint: kGT xk2 ≤ γ
maximize µT x
subject to xT Σx ≤ γ 2 , (2)
1T x = 1.
maximize µT x
subject to (γ, GT x) ∈ QK+1 , (2’)
1T x = 1.
13 / 50
Conic formulation
maximize µT x − δ̃s
subject to (s, GT x) ∈ QK+1 , (4’)
1T x = 1.
If Markowitz had known this, would he have worked with std. dev.?
14 / 50
Example in MOSEK Fusion: Simple MVO
Assumptions:
• µ is given, Σ is given and is positive definite.
• We create a long only portfolio of eight stocks. (N = 8)
Inputs:
# Expected returns and covariance matrix
m = np.array(
[0.0720, 0.1552, 0.1754, 0.0898, 0.4290, 0.3929, 0.3217, 0.1838]
)
S = np.array([
[0.0946, 0.0374, 0.0349, 0.0348, 0.0542, 0.0368, 0.0321, 0.0327],
[0.0374, 0.0775, 0.0387, 0.0367, 0.0382, 0.0363, 0.0356, 0.0342],
[0.0349, 0.0387, 0.0624, 0.0336, 0.0395, 0.0369, 0.0338, 0.0243],
[0.0348, 0.0367, 0.0336, 0.0682, 0.0402, 0.0335, 0.0436, 0.0371],
[0.0542, 0.0382, 0.0395, 0.0402, 0.1724, 0.0789, 0.0700, 0.0501],
[0.0368, 0.0363, 0.0369, 0.0335, 0.0789, 0.0909, 0.0536, 0.0449],
[0.0321, 0.0356, 0.0338, 0.0436, 0.0700, 0.0536, 0.0965, 0.0442],
[0.0327, 0.0342, 0.0243, 0.0371, 0.0501, 0.0449, 0.0442, 0.0816]
])
15 / 50
Example in MOSEK Fusion: Simple MVO
maximize √ µT x
subject to ( 0.05, GT x) ∈ Q9 ,
(2”)
1T x = 1,
x ≥ 0.
16 / 50
Example in MOSEK Fusion: Simple MVO
# Budget constraint
M.constraint('budget', Expr.sum(x), Domain.equalsTo(1))
# Portfolio risk constraint
M.constraint('risk', Expr.vstack(np.sqrt(0.05), Expr.mul(G.T, x)),
Domain.inQCone())
# Objective
M.objective('obj', ObjectiveSense.Maximize, Expr.dot(m, x))
preturn = M.primalObjValue()
portfolio = x.level()
Efficient frontier
• The collection of optimal (expected return, variance) points.
• Easiest to find using problem (4) by varying the δ̃ parameter.
maximize µT x − δ̃s
subject to 1T x = 1,
(4”)
x ≥ 0,
(s, GT x) ∈ Q9 .
18 / 50
Example in MOSEK Fusion: Efficient frontier
[continued below]
19 / 50
Example in MOSEK Fusion: Efficient frontier
# Solve optimization
M.solve()
# Save results
portfolio_return = m @ x.level()
portfolio_risk = s.level()[0]
20 / 50
Example in MOSEK Fusion: Efficient frontier
21 / 50
Example in MOSEK Fusion: Efficient frontier
22 / 50
Summary
23 / 50
Section 3
Factor models
Factor models
Definition:
• Explains random variable Rt (N dim.) through a small
number of common factors Ft (K dim.).
• Rt = βFt + θt .
• β: Factor exposure matrix. (N × K dim.)
• θt = α + εt : Specific component at time t. (N dim.)
• α = E(θt ), εt is white noise with covariance Σθ .
1 25 / 50
Exact, static factor models with weakly stationary Ft .
Conic constrains with factor model
Sparsity
Assume we have two index ETFs as factors for the eight stocks:
RF1 ,t
Rt = α + β + εt .
RF2 ,t
27 / 50
Example in Python: 2-factor model
29 / 50
Section 4
Cost model
Pn
i=1 Ci (x̃i ), where x̃i = xi − x0,i .
maximize µT x
n
X
subject to 1T x + Ci (x̃i ) = 1,
i=1
xT Σx ≤ γ 2 .
Self financing: No external cash is added to the portfolio.
31 / 50
Fixed + variable cost model
Cost function
Ci(x̃i )
0, x̃i = 0,
Ci (x̃i ) = fi+ + vi+ x̃i , x̃i > 0,
−
fi − vi− x̃i , x̃i < 0.
fi+
• x̃+ −
i , x̃i : Pos./neg. part of x̃i . fi−
• fi+ , fi− : Fixed costs of
buying/selling security i.
• vi+ , vi− : Cost rates of
buying/selling security i. x̃i
maximize µT x
+ T + T
v+ x̃+
f y
subject to 1T x + + − = 1,
f− y− v x̃−
x̃ = x̃+ − x̃− ,
x̃+ , x̃− ≥ 0,
x̃+ ≤ u+ ◦ y+ ,
x̃− ≤ u− ◦ y− ,
y + y−
+
≤ 1,
y+ , y− ∈ {0, 1}N ,
xT Σx ≤ γ2.
Assumptions:
• Short-selling is allowed up to the limit of 30% portfolio size.
• x̃ = x, because x0 = 0.
Variables:
# Real variables
xp = M.variable("xp", N, Domain.greaterThan(0.0))
xm = M.variable("xm", N, Domain.greaterThan(0.0))
# Buy/sell indicators
yp = M.variable("yp", N, Domain.binary())
ym = M.variable("ym", N, Domain.binary())
Constraints:
# Constraint assigning xp and xm to the positive and negative part of x.
M.constraint('pos-neg-part', Expr.sub(x, Expr.sub(xp, xm)),
Domain.equalsTo(0.0))
[continued below] 34 / 50
Example: Fixed + variable cost
36 / 50
Market impact cost
Cost function
Ci (x̃i ) = ai |x̃i |β ,
where ai is calibrated and β is typically 3/2.
maximize µT x + r f xf
subject to 1 x + aT t + xf = 1,
T
xT Σx ≤ γ 2 ,
1/β,(β−1)/β
(ti , 1, x̃i ) ∈ P3 , i = 1, . . . , N.
38 / 50
Example: Market impact cost
[continued below]
39 / 50
Example: Market impact cost
# Objective
delta = M.parameter()
return_terms = Expr.add(Expr.dot(m, x), Expr.mul(rf, xf))
M.objective('obj', ObjectiveSense.Maximize,
Expr.sub(return_terms, Expr.mul(delta, s)))
40 / 50
Example: Market impact cost
41 / 50
Summary
42 / 50
Section 5
44 / 50
Optimization problem
maximize αT x
T 2
subject to (x − xbm ) Σ(x − xbm ) ≤ γTE ,
x − xbm ≥ lh ,
x − xbm ≤ uh ,
βx − 1 ≥ lβ ,
βx − 1 ≤ uβ .
45 / 50
Example: Benchmark relative optimization
47 / 50
Summary
48 / 50
What have we learned
49 / 50
Further information
• Mosek https://mosek.com
• Trial and free academic license.
• Solves linear and conic mixed problems.
• Interfaces C, Python, Java, Julia, Matlab, R ...
• Documentation at
https://www.mosek.com/documentation/
• Modelling cookbook.
• Portfolio optimization cookbook.
• Modelling cheat sheet.
• The MOSEK notebook collection.
• Examples
• Tutorials at Github:
https://github.com/MOSEK/Tutorials
• Distributionally robust optimization notebook :
https://github.com/MOSEK/Tutorials/tree/master/
dist-robust-portfolio
50 / 50