0% found this document useful (0 votes)
12 views

Optimization Portfolio

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Optimization Portfolio

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

An introduction to portfolio optimization

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

Purpose of this talk

• Show that solving portfolio optimization problems with


Mosek is very easy.
• Show how you can do it. (Try it at home!)
• Show some possible extensions.

Mosek is a software package for solving

• Linear and conic problems.


• Convex quadratically constrained problems.
• Mixed-integer versions of the above.

Mosek Fusion: High level interface, almost direct mapping of math


to code. (You will see later.) 3 / 50
What is a conic problem?

Convex optimization Conic optimization

minimize f (x) minimize cT x


subject to g(x) ≤ 0, subject to Ax + b ∈ K,
h(x) = 0,

• 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

• Qn = {x ∈ Rn |x1 ≥ kx2..n k2 }, also called second-order cone.


• Used for modeling problems with quadratic functions.
(Portfolio optimization!)
• 2D example: |x2 | ≤ x1 ⇐⇒ (x1 , x2 ) ∈ Q2 .
• 3D example: k(x2 , x3 )k2 ≤ x1 ⇐⇒ (x1 , x2 , x3 ) ∈ Q3 .

x1 Q3
2
Q

x2

5 / 50
Section 2

Mean-variance portfolio optimization


The mean-variance model

Goal
Maximize the return of the investment, while keeping the
investment risk (variance) acceptable.

• Ri is the rate of return of security i at the end of the


investment period. (random variable)
• µi = E(Ri ) is the expected return security i.
• Σi,j = cov(Ri , Rj ) is the covariance of security pair (i, j).

Modern Portfolio Theory

• Risk is reduced through diversification, forming a portfolio.


• Portfolio weights control the tradeoff between return and risk.
• Find the optimal weights, then invest!
7 / 50
The mean-variance model

• x is the portfolio weight vector, xi is the fraction of funds


invested into security i.
• Rx = i xi Ri is the portfolio return. (random)
P

• µx = E(Rx ) = xT µ is the expected portfolio return.


• σx2 = cov(Rx ) = xT Σx is the portfolio variance.

Mean–variance optimization (MVO)

• Find x, that gives the optimal tradeoff between µx and σx2 .


• Variance as risk measure: MVO is a quadratic optimization
(QO) problem.

8 / 50
Solution of MVO

Data
• µ: The estimate of the vector µ.
• Σ: The estimate of the matrix Σ.

Simplifications

• Only risky securities in the universe.


• No transaction costs.
• No initial holdings: x0 = 0.
• Fully invested portfolio: i xi = 1.
P

We can formulate four equivalent optimization problems.


9 / 50
Solution of MVO

Minimize the portfolio risk, bound the portfolio return

minimize xT Σx
subject to µT x ≥ rmin , (1)
1T x = 1.
QO problem, easy to solve.

Maximize the portfolio return, bound the portfolio risk

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 the portfolio return with variance penalty

δ
maximize µT x − xT Σx
2 (3)
subject to 1T x = 1.
• QO problem.
• δ: Tradeoff parameter, no intuitive meaning.

Maximize the portfolio return with std. dev. penalty



maximize µT x − δ̃ xT Σx
(4)
subject to 1T x = 1.
• Not QO, but easy after conic reformulation.
• Penalty term is of the same scale as portfolio return.
• δ̃: Distance from mean return in std. dev. units. (Intuitive.)
11 / 50
Conic formulation

1. Factorize the covariance matrix


• Σ = GGT , where G ∈ RN ×K .
• Cholesky factorization: A = LLT , for A  0, L lower triang.
• Factor model: Has direct financial interpretation. (see later)

2. Rewrite portfolio variance constraint

• Quadratic constraint: xT Σx ≤ γ 2
• xT Σx = xT GGT x = (GT x)T (GT x) = kGT xk22
• Equivalent, conic representable constraint: kGT xk2 ≤ γ

3. Model with quadratic cone


kGT xk2 ≤ γ ⇐⇒ (γ, GT x) ∈ QK+1
12 / 50
Conic formulation

Problem (2) (return maximization)

maximize µT x
subject to xT Σx ≤ γ 2 , (2)
1T x = 1.

The conic equivalent of problem (2)

maximize µT x
subject to (γ, GT x) ∈ QK+1 , (2’)
1T x = 1.

13 / 50
Conic formulation

Problem (4) (std. dev. penalty)



maximize µT x − δ̃ xT Σx
(4)
subject to 1T x = 1.

The conic equivalent of problem (4)



• New variable s: Upper bound of xT Σx = kGT xk2 .
• Add constraint kGT xk2 ≤ s, model using the quadratic cone.

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

We choose to solve problem (2) (return maximization).


• Additional constraint to prevent short selling.
• Risk limit of γ 2 = 0.05.
• Cholesky factorization: Σ = GGT , G ∈ R8×8 .

Conic model of problem (2)

maximize √ µT x
subject to ( 0.05, GT x) ∈ Q9 ,
(2”)
1T x = 1,
x ≥ 0.

16 / 50
Example in MOSEK Fusion: Simple MVO

The Mosek Python Fusion model of problem (2”):


with Model("markowitz") as M:
M.setLogHandler(sys.stdout) # Send log to stdout.

# Decision variable (imposes the no short-selling constraint)


x = M.variable('x', N, Domain.greaterThan(0.0))

# 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))

M.solve() # Solve optimization

preturn = M.primalObjValue()
portfolio = x.level()

Optimal x: [0.0, 0.091, 0.269, 0.0, 0.025, 0.322, 0.177, 0.116].


17 / 50
Example in MOSEK Fusion: Efficient frontier

Efficient frontier
• The collection of optimal (expected return, variance) points.
• Easiest to find using problem (4) by varying the δ̃ parameter.

Conic model of problem (4)

maximize µT x − δ̃s
subject to 1T x = 1,
(4”)
x ≥ 0,
(s, GT x) ∈ Q9 .

18 / 50
Example in MOSEK Fusion: Efficient frontier

The Mosek Python Fusion model of problem (4”):


with Model("efficient_frontier") as M:
M.setLogHandler(sys.stdout) # Send log to stdout.

# Decision variable (imposes the no short-selling constraint)


x = M.variable('x', N, Domain.greaterThan(0.0))
# Variable 's' models the portfolio std. dev. term in the objective.
s = M.variable('s', 1, Domain.unbounded())

# Budget constraint and portfolio risk constraint


M.constraint('budget', Expr.sum(x), Domain.equalsTo(1))
M.constraint('risk', Expr.vstack(s, Expr.mul(G.T, x)), Domain.inQCone()

# Objective (with model parameter)


delta = M.parameter()
M.objective('obj', ObjectiveSense.Maximize,
Expr.sub(Expr.dot(m, x), Expr.mul(delta, s)))

[continued below]

19 / 50
Example in MOSEK Fusion: Efficient frontier

[continued from above]


# Solve the optimization model for all parameter values
for d in deltas:
# Update parameter
delta.setValue(d)

# Solve optimization
M.solve()

# Save results
portfolio_return = m @ x.level()
portfolio_risk = s.level()[0]

20 / 50
Example in MOSEK Fusion: Efficient frontier

The resulting efficient frontier:

21 / 50
Example in MOSEK Fusion: Efficient frontier

The resulting porfolios:

22 / 50
Summary

Takeaway of this section

1 Conic optimization is a generalization of linear programming.


2 You can convert portfolio optimization problems into conic
equivalents.
3 You can easily map math into code using Mosek Fusion API.
4 You can solve conic problems very efficiently.

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 Σθ .

Factor model1 structure of the covariance matrix


• Σ = βΣF β T + Σθ , where Σθ is diagonal.
• Only N (K + 1) + K(K + 1)/2 parameters (linear in N ).
• No factor model: N (N + 1)/2 covariances (quadratic in N ).

1 25 / 50
Exact, static factor models with weakly stationary Ft .
Conic constrains with factor model

We have the decomposition Σ = βΣF β T + Σθ , where


• ΣF is the K × K sample factor covariance matrix (K factors).
• Typically K  N , thus ΣF is very low dimensional.
• Very cheap to compute ΣF = FFT , and get Σ = GGT with
h i
G = βF Σ1/2 θ
.

Sparsity

• This form of G is typically very sparse.


• Computational efficiency: sparsity is more important than the
number of variables and constraints!
• Dimension: N × (N + K) > N × N .
• Nonzeros: N (K + 1)  N (N + 1)/2. (storage size)
• Leads to significant reduction in the solution time! 26 / 50
Example in Python: 2-factor model

Assume we have two index ETFs as factors for the eight stocks:
 
RF1 ,t
Rt = α + β + εt .
RF2 ,t

Estimate β, ΣF , Σθ (in code B, S_F, and S_theta):


 
0.4256 0.1869

 0.2413 0.3877 


 0.2235 0.3697 
  
 0.1503 0.4612  0.0620 0.0577
β= , ΣF = ,

 1.5325 −0.2633 
 0.0577 0.0908

 1.2741 −0.2613 

 0.6939 0.2372 
0.5425 0.2116
Σθ = Diag([0.0720, 0.0508, 0.0377, 0.0394, 0.0663, 0.0224, 0.0417, 0.0459]),

27 / 50
Example in Python: 2-factor model

Compute the matrix G:


P = np.linalg.cholesky(S_F)
G = np.block([[B @ P, np.sqrt(S_theta)]])
Then G will be
array([
[0.149, 0.036, 0.268, 0., 0., 0., 0., 0., 0., 0. ],
[0.150, 0.075, 0., 0.225, 0., 0., 0., 0., 0., 0. ],
[0.141, 0.071, 0., 0., 0.194, 0., 0., 0., 0., 0. ],
[0.144, 0.089, 0., 0., 0., 0.199, 0., 0., 0., 0. ],
[0.321,-0.051, 0., 0., 0., 0., 0.258, 0., 0., 0. ],
[0.257,-0.050, 0., 0., 0., 0., 0., 0.150, 0., 0. ],
[0.228, 0.046, 0., 0., 0., 0., 0., 0., 0.204, 0. ],
[0.184, 0.041, 0., 0., 0., 0., 0., 0., 0., 0.214]
])
Compare to Cholesky factor:
• Size: 8 × 10 vs. 8 × 8.
• Nonzeros: 24 vs. 36.
• For N = 1000 and K = 50: 51000 vs. half million nonzeros.
(See next talk.) 28 / 50
Summary

Takeaway of this section

1 Factor models impose a special structure on the covariance


matrix: Σ = βΣF β T + Σθ , ΣF ∈ RK×K , K  N .
2 If you use a factor model, you can exploit this structure in
1/2
conic optimization: G = [βF Σθ ] very sparse.
3 This can make large scale portfolio optimization problems
run orders of magnitude faster!

29 / 50
Section 4

Transaction cost modeling


Transaction costs

Cost model
Pn
i=1 Ci (x̃i ), where x̃i = xi − x0,i .

MVO model with transaction cost

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

Not convex, but solvable as Mixed integer (MIO) problem.


32 / 50
Fixed + variable cost model

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.

• y+ , y− : Binary vectors indicating buys/sells. (On/off switch.)


• u+ + − − + −
i yi and ui yi : Forces x̃ /x̃ to 0 if not traded.
• y+ + y− ≤ 1: The transaction is either buy or sell.
• Budget constraint: x̃+ and x̃− will not be both positive in
any optimal solution. 33 / 50
Example: Fixed + variable cost

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))

# Exclusive buy-sell constraint


M.constraint('exclusion', Expr.add(yp, ym), Domain.lessThan(1.0))

[continued below] 34 / 50
Example: Fixed + variable cost

[continued from above]


# Budget constraint with transaction cost terms
fixcost_terms = Expr.add([Expr.dot(fp, yp), Expr.dot(fm, ym)])
varcost_terms = Expr.add([Expr.dot(vp, xp), Expr.dot(vm, xm)])
budget_terms = Expr.add([Expr.sum(x), varcost_terms, fixcost_terms])
M.constraint('budget', budget_terms, Domain.equalsTo(1.0))

# Auxiliary variable for 130/30 leverage constraint


z = M.variable("z", N, Domain.unbounded())

# 130/30 leverage constraint


M.constraint('leverage-gt', Expr.sub(z, x), Domain.greaterThan(0.0))
M.constraint('leverage-ls', Expr.add(z, x), Domain.greaterThan(0.0))
terms = Expr.add([Expr.sum(z), varcost_terms, fixcost_terms])
M.constraint('leverage-sum', terms, Domain.equalsTo(1.6))

# Bound constraints for fixed cost


M.constraint('ub-p', Expr.sub(Expr.mul(up, yp), xp), Domain.greaterThan(0.0
M.constraint('ub-m', Expr.sub(Expr.mul(um, ym), xm), Domain.greaterThan(0.0
M.constraint('lb-p', Expr.sub(xp, Expr.mul(lp, yp)), Domain.greaterThan(0.0
M.constraint('lb-m', Expr.sub(xm, Expr.mul(lm, ym)), Domain.greaterThan(0.0
35 / 50
Example: Fixed + variable cost

The resulting efficient frontiers:

36 / 50
Market impact cost

Cost function

Ci (x̃i ) = ai |x̃i |β ,
where ai is calibrated and β is typically 3/2.

Modeling with power cone

• Pnα,1−α = x ∈ Rn xα1 x1−α



2 ≥ kx3..n k2 , x1 , x2 ≥ 0 .
• |x̃i |β ≤ ti ⇐⇒ (ti , 1, x̃i ) ∈ P31/β,(β−1)/β .

Total market impact cost model


N N
1/β,(β−1)/β
X X
β
ai |x̃i | ⇐⇒ ai ti , (ti , 1, x̃i ) ∈ P3 for i = 1 . . . N
i=1 i=1
37 / 50
Market impact cost

Ensuring |x̃i |β = ti at the optimal solution:


• Add a risk-free security to the model.
• xf : The weight of the risk-free security.
• rf : The return of the risk-free security.

Optimization problem with market impact cost

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

We update the Fusion model of (4) with new variables and


constraints:
with Model("Market impact") as M:
M.setLogHandler(sys.stdout) # Send log to stdout.

# Decision variable (imposes the no short-selling constraint)


x = M.variable('x', N, Domain.greaterThan(0.0))
# Variable 's' models the portfolio variance term in the objective.
s = M.variable('s', 1, Domain.unbounded())
# Variable for risk-free security (no borrowing)
xf = M.variable("xf", 1, Domain.greaterThan(0.0))
# Auxiliary variable to model market impact
t = M.variable("t", N, Domain.unbounded())

# Budget constraint, portfolio risk


budget_terms = Expr.hstack(Expr.sum(x), xf, Expr.dot(a, t))
M.constraint('budget', Expr.sum(budget_terms), Domain.equalsTo(1))
M.constraint('risk', Expr.vstack(s, Expr.mul(G.T, x)),
Domain.inQCone())

[continued below]
39 / 50
Example: Market impact cost

[continued from above]


# Market impact constraint (x0 assumed to be 100% cash)
M.constraint('market_impact', Expr.hstack(t, Expr.constTerm(N, 1.0), x)
Domain.inPPowerCone(1.0 / beta))

# 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

The resulting efficient frontiers:

41 / 50
Summary

Takeaway of this section

1 We have discussed some examples of transaction cost


models in portfolio optimization.
2 You can also model these as conic and mixed integer conic
problems, and solve them using Mosek.

42 / 50
Section 5

Benchmark relative portfolio optimization


Quantities of interest

In benchmark relative setting we use the following quantities:


• Active holdings: xa = x − xbm .
• Active return (return above benchmark return): Rxa = xT
a R.
• Tracking error (std. dev. of active return):
q
σxa (Rx , Rxbm ) = xT a Σxa , where Σ = Cov(R).

Systematic component of active return

• Rxa = (βx − 1)Rxbm + θx .


• Active beta: βx − 1.
• Alpha: αx = αT x = E(θx ).

44 / 50
Optimization problem

• Optimizing tradeoff between portfolio alpha and the squared


tracking error.
• Relevant constrains: bounds on portfolio active beta or on
active holdings.

Benchmark relative portfolio optimization

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

The benchmark is the equally weighted portfolio, xbm = 1/N .


Modifications in the Fusion model of problem (4):
# Active holdings
xa = Expr.sub(x, xbm)

# Constraint for the portfolio squared tracking error


M.constraint('risk', Expr.vstack(s, 1, Expr.mul(G.T, xa)),
Domain.inRotatedQCone())

# Constraint on active holdings and active beta


M.constraint('ub-h', Expr.sub(uh, xa), Domain.greaterThan(0.0))
M.constraint('lb-h', Expr.sub(xa, lh), Domain.greaterThan(0.0))
port_act_beta = Expr.sub(Expr.dot(B, x), 1)
M.constraint('ub-b', Expr.sub(ub, port_act_beta), Domain.greaterThan(0.0))
M.constraint('lb-b', Expr.sub(port_act_beta, lb), Domain.greaterThan(0.0))

# Objective: max portfolio alpha


delta = M.parameter()
M.objective('obj', ObjectiveSense.Maximize,
Expr.sub(Expr.dot(a, x), Expr.mul(delta, s)))
46 / 50
Example: Benchmark relative optimization

The resulting porfolios:

47 / 50
Summary

Takeaway of this section

1 Conic formulation is general so you can model portfolio risk or


tracking error the same way.
2 You can convert the Mosek Fusion code of an MVO problem
into benchmark relative without much effort.

48 / 50
What have we learned

Takeaway of the talk

1 Conic optimization is a modern framework allowing the


efficient solution of a very diverse set of problems.
2 Mosek is a general purpose tool that helps you easily
implement and solve conic optimization models.
3 Conic modeling can also be applied to portfolio
optimization.
4 Mosek Fusion code is a direct mapping of math formulas.
5 Factor models can be used to achieve orders of magnitude
speedup.
6 Adding various practical constraints usually requires just a
few lines of new code.

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy