0% found this document useful (0 votes)
27 views78 pages

CVX Finance Slides

Uploaded by

rykandepistoler
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)
27 views78 pages

CVX Finance Slides

Uploaded by

rykandepistoler
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/ 78

Convex Optimization in Quantitative Finance

Stephen Boyd Kasper Johansson Philipp Schiele

Stanford University

June 11, 2024


Overview

convex optimization problems


▶ are a special type of mathematical optimization problem
▶ can be efficiently solved
▶ are easily specified using domain specific languages such as CVXPY
▶ can be used to solve a wide variety of problems arising in finance

these slides give many examples in finance


▶ our examples are simplified, but readily extended
▶ we give code snippets for all of them
▶ full code is available at https://github.com/cvxgrp/cvx-finance-examples

2
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

3
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

4
Optimization problem

minimize f0 (x)
subject to fi (x) ≤ 0, i = 1, . . . , m
gi (x) = 0, i = 1, . . . , p

▶ x ∈ Rn is (vector) variable to be chosen


▶ f0 is the objective function, to be minimized
▶ f1 , . . . , fm are the inequality constraint functions
▶ g1 , . . . , gp are the equality constraint functions

▶ variations: maximize objective, multiple objectives, . . .

5
Convex optimization problem

minimize f0 (x)
subject to fi (x) ≤ 0, i = 1, . . . , m
Ax = b

▶ variable x ∈ Rn
▶ equality constraints are linear
▶ f0 , . . . , fm are convex: for 𝜃 ∈ [0, 1] ,

fi (𝜃x + (1 − 𝜃)y) ≤ 𝜃fi (x) + (1 − 𝜃)fi (y)

i.e., fi have nonnegative (upward) curvature

▶ variations: maximize concave objective, multiple convex objectives, . . .

6
Why

for convex optimization problems there are


▶ effective algorithms
– get global solution (and optimality certificate)
– theory: polynomial complexity
– practice: fast and reliable (no need to tune parameters)
– many open source and commercial implementations
▶ many applications in machine learning, signal processing, statistics, control, engineering
design, and finance

7
Modeling languages

▶ high level language support for convex optimization


– describe problem in high level language
– simple syntax rules to certify problem convexity
– description automatically transformed to a standard form
– solved by standard solver, transformed back to original form
▶ implementations:
– CVXPY (Python)
– YALMIP, CVX (Matlab)
– Convex.jl (Julia)
– CVXR (R)
▶ can be coupled with open source or commercial solvers
▶ work well for problems up to around 100k variables

8
CVXPY

a modeling language in Python for convex optimization


▶ developed since 2014
▶ open source all the way to the solvers
▶ syntax very similar to NumPy
▶ used in many research projects, courses, companies
▶ tens of thousands of users, including many in finance
▶ over 27,000,000 downloads on PyPI
▶ many extensions available

9
Example

regularized least squares problem with bounds:

minimize ∥Ax − b∥ 22 + 𝛾∥x∥ 1


subject to ∥x∥ ∞ ≤ 1

CVXPY specification:

import cvxpy as cp
x = cp.Variable(n)
cost = cp.sum_squares(A@x-b) + gamma*cp.norm(x,1)
prob = cp.Problem(cp.Minimize(cost),[cp.norm(x,"inf")<=1])
opt_val = prob.solve()
solution = x.value
▶ A, b, gamma are constants, gamma nonnegative
▶ solve method converts problem to standard form, solves, assigns value attributes

10
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

11
Mean-variance (Markowitz) optimization

maximize 𝜇T w
subject to wT Σw ≤ (𝜎 tar ) 2 , 1T w = 1

▶ variable w ∈ Rn of portfolio weights


▶ 𝜇 ∈ Rn and Σ ∈ Sn++ are asset return mean and covariance
▶ 𝜎 tar is target (per period) volatility
▶ basic form goes back to [Markowitz, 1952]

w = cp.Variable(n)
objective = mu.T @ w
constraints = [cp.quad_form(w, Sigma) <= sigma**2, cp.sum(w) == 1]
prob = cp.Problem(cp.Maximize(objective), constraints)
prob.solve()

12
Adding practical constraints and objective terms

▶ include cash holdings c, previous holdings wpre , trades z = w − wpre


▶ account for (convex) holding costs 𝜙hold and trading costs 𝜙trade
▶ limit weights, cash, trades, turnover T = ∥z∥ 1 , and leverage L = ∥w∥ 1

maximize 𝜇T w − 𝛾 hold 𝜙hold (w, c) − 𝛾 trade 𝜙trade (z)


subject to 1T w + c = 1, z = w − wpre ,
wmin ≤ w ≤ wmax , cmin ≤ c ≤ cmax , L ≤ Ltar ,
zmin ≤ z ≤ zmax , T ≤ T tar ,
∥Σ1/2 w∥ 2 ≤ 𝜎 tar
▶ variation: soften constraints, i.e., penalize violations
▶ can be implemented in around ten lines in CVXPY
▶ see [Boyd et al., 2024] for details and reference implementation

13
Factor covariance model

Σ = FΣf F T + D

▶ F ∈ Rn×k is matrix of factor loadings


▶ k is number of factors, typically with k ≪ n
▶ Σf is k × k factor covariance matrix
▶ D is diagonal matrix of unexplained (idiosyncratic) variances
▶ a strong regularizer which can give better return covariance estimates

14
Exploiting a factor model

▶ with factor model, cost of portfolio optimization reduced from O(n3 ) to O(nk2 ) flops
[Boyd and Vandenberghe, 2004]
▶ easily exploited in CVXPY
▶ timings for Clarabel open source solver:

solve time (s)


assets n factors k factor model full covariance
100 10 0.002 0.040
300 20 0.010 0.700
1000 30 0.080 25.600
3000 50 0.600 460.000

15
Backtesting

▶ fast solve time enables backtesting of strategy variations


– what-if analysis
– sensitivity analysis
– hyperparameter tuning
▶ with 1000 assets and 30 factors, we can backtest 3 years of daily trading in a minute
▶ in one hour, we can carry out 2000 3 year backtests on a 32-core machine

16
Robustifying Markowitz

▶ basic mean-variance optimization can be sensitive to estimation errors in 𝜇, Σ


▶ replace mean return 𝜇T w with worst-case return

Rwc = min{(𝜇 + 𝛿) T w | |𝛿| ≤ 𝜌} = 𝜇T w − 𝜌 T |w|

where 𝜌 ≥ 0 is vector of mean return uncertainties


▶ replace risk wT Σw with worst-case risk
2
𝜎 wc = max{wT (Σ + Δ)w | |Δij | ≤ 𝜚(Σii Σjj ) 1/2 }
n
!2
∑︁
2 1/2
= 𝜎 +𝜚 Σii |wi |
i=1

where 𝜚 ≥ 0 gives covariance uncertainty


▶ easily handled by CVXPY

17
Example
▶ S&P 100, simulated but realistic 𝜇, target annualized risk 10%
▶ hyper-parameters tuned each year based on previous two years
▶ out-of-sample portfolio performance for basic Markowitz and robust Markowitz
▶ Sharpe ratios 0.2 and 4.6 (using the same mean and covariance)

18
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

19
Expected utility maximization

▶ asset returns r ∈ Rn ; portfolio weights w ∈ Rn


▶ portfolio return rT w; wealth grows by factor 1 + rT w
▶ expected utility is E U(1 + rT w) , where U is concave increasing utility function
▶ choose portfolio weights w ∈ W (a convex set) to maximize expected utility
▶ a convex optimization problem [Von Neumann and Morgenstern, 1947]
▶ reduces to mean-variance in some cases (e.g., exponential utility, Gaussian returns)
[Markowitz and Blay, 2014; Luxenberg and Boyd, 2024]
▶ allows handling of options, nonlinear payoffs, . . .
▶ with U(x) = log x we get Kelly gambling [Kelly, 1956]; maximizes wealth growth rate

20
Sample based approximation

▶ when E U(1 + rT w) can’t be expressed analytically, use sample based approximation


▶ generate N samples r1 , . . . , rN , with probabilities 𝜋1 , . . . , 𝜋N
▶ approximate expected utility as E U(1 + rT w) ≈ Ni=1 𝜋i U(1 + riT w)
Í

▶ sample based approximate expected utility maximization:


ÍN
maximize 𝜋i U(1 + riT w)
i=1
subject to w∈W

▶ easily handled by CVXPY

21
Sample based approximation in CVXPY

▶ returns ri are columns of N × n array returns


▶ probabilities 𝜋 are in array probabilities
▶ CRRA utility with relative risk aversion 𝜌 ≥ 0, U(x) = (x1−𝜌 − 1)/(1 − 𝜌)

def U(x):
return (x**(1-rho) - 1)/(1-rho)

w = cp.Variable(n)

objective = probabilities @ U(1 + returns @ w)


constraints = [cp.sum(w) == 1]

prob = cp.Problem(cp.Maximize(objective), constraints)


prob.solve()

22
Example

▶ optimize portfolio of one underlying, one call, and one put, both at-the-money
▶ underlying with 1 + r log-normal
▶ CRRA utility with relative risk aversion 𝜌 , W = {w | 1T w = 1}
▶ sample approximation with N = 105 samples

Asset returns Portfolio return distributions

23
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

24
Sparse inverse covariance estimation

▶ model return in period t as rt ∼ N (0, Σ)


▶ log-likelihood
1 
lt (𝜃) = −n log(2𝜋) + log det 𝜃 − rtT 𝜃rt ,
2
where 𝜃 = Σ −1 is the precision matrix
▶ sparse inverse covariance estimation problem [Friedman et al., 2007]
ÍT Í
maximize t=1 lt (𝜃) −𝜆 i<j |𝜃 ij |
subject to 𝜃≥0

with variable 𝜃 ; 𝜆 > 0 is a (sparsity) regularization parameter


▶ a convex problem; yields matrix with sparse precision matrix 𝜃
▶ 𝜃 ij = 0 means returns (rt )i , (rt )j are conditionally independent given the others

25
Sparse inverse covariance estimation in CVXPY

▶ log_likelihood is sum of log-likelihoods up to positive scaling and additive constant

Theta = cp.Variable((n, n), PSD=True)

log_likelihood = cp.sum(cp.hstack(
[cp.log_det(Theta) - cp.quad_form(r, Theta) for r in returns]
))
mask = np.triu(np.ones((n, n)), k=1).astype(bool)
objective = log_likelihood - alpha * cp.norm1(Theta[mask])

prob = cp.Problem(cp.Maximize(objective))
prob.solve()

26
Example

▶ daily returns of US, Europe, Asia, and Africa stock indices from 2009 to 2024
▶ figure shows yearly sparsity pattern of inverse covariance; white boxes denote zero entries
2009 2010 2011 2012 2013 2014 2015 2016

2017 2018 2019 2020 2021 2022 2023 2024

27
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

28
Worst-case portfolio risk

▶ we hold n assets with weights w ∈ Rn , 1T w = 1


▶ variance of portfolio return is wT Σw, where Σ ∈ Rn×n is the return covariance
▶ now suppose Σ ∈ S but otherwise uncertain
▶ set of possible covariances S is a convex set, e.g.,

S = Σ ≥ 0 | Lij ≤ Σij ≤ Uij , i, j = 1, . . . , n

where L and U are lower and upper bounds on entries


▶ the worst-case variance consistent with our belief Σ ∈ S is
2
= sup wT Σw | Σ ≥ 0, Σ ∈ S

𝜎wc
2 is a convex optimization problem
▶ evaluating 𝜎wc

29
Worst-case portfolio risk in CVXPY

▶ weights denote portfolio weights


▶ L and U are matrices of lower and upper bounds on covariances

Sigma = cp.Variable((n, n), PSD=True)

objective = cp.Maximize(cp.quad_form(weights, Sigma))


constraints = []
for i in range(n):
for j in range(i):
constraints += [L[i, j] <= Sigma[i, j], Sigma[i, j] <= U[i, j]]

prob = cp.Problem(objective, constraints)


prob.solve()

30
Example
▶ portfolio weights and uncertain covariance

 0.5  
 0.2 + + ± 

 0.25   + 0.1 − − 
w =  , Σ =  ,
 −0.05  + − 0.3 +
 
 

 0.3 


 ± − + 0.1 

▶ + means nonnegative, − means nonpositive, and ± means unknown sign
▶ worst-case risk is 0.18 (volatility 42%)
▶ risk with diagonal covariance matrix is 0.07 (volatility 26%)
▶ worst-case covariance is
 0.20
 0.14 −0.24 0.14 

 0.14
 0.10 −0.17 0.10 

 −0.24 −0.17 0.30 −0.17 
 
 0.14
 0.10 −0.17 0.10 

31
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

32
Investment arbitrage

▶ invest xj in asset j, with prices p1 , . . . , pn ; initial cost is pT x


▶ at the end of the investment period there are only m possible outcomes
▶ Vij is the payoff of asset j in outcome i
▶ first investment is risk-free (cash): p1 = 1 and Vi1 = 1 for all i
▶ arbitrage: there is an x with pT x < 0, Vx ≥ 0
▶ i.e., we receive money up front, and cannot lose

▶ standard assumption: the prices are such that there is no arbitrage

33
Fundamental theorem of asset pricing

▶ by Farkas’ lemma, there is no arbitrage ⇐⇒ there exists 𝜋 ∈ Rm T


+ with V 𝜋 = p
T
▶ first column of V is 1, so we have 1 𝜋 = 1
▶ 𝜋 is interpreted as a risk-neutral probability on the outcomes 1, . . . , m
▶ V T 𝜋 are the expected values of the payoffs under the risk-neutral probability
▶ V T 𝜋 = p means asset prices equal their expected payoff under the risk-neutral probability

▶ fundamental theorem of asset pricing:


there is no arbitrage ⇐⇒ there exists a risk-neutral probability distribution under which
each asset price is its expected payoff

34
Check for arbitrage in CVXPY

pi = cp.Variable(m, nonneg=True)
prob = cp.Problem(cp.Minimize(0), [V.T @ pi == p])
prob.solve()

if prob.status == 'optimal':
print('No arbitrage exists')
elif prob.status == 'infeasible':
print('Arbitrage exists')

35
Option price bounds

▶ suppose p1 , . . . , pn−1 are known, but pn is unknown


▶ arbitrage-free range for pn is found by solving

minimize/maximize pn
subject to V T 𝜋 = p, 𝜋 ≥ 0, 1T 𝜋 = 1

with variables pn ∈ R and 𝜋 ∈ Rm


▶ can be solved in CVXPY
▶ if the minimum and maximum are equal, the market is complete

36
Option price bounds in CVXPY

▶ p_known is vector of known prices, of length n − 1

pi = cp.Variable(m, nonneg=True)
p_n = cp.Variable()
p = cp.hstack([p_known, p_n])

prob = cp.Problem(cp.Minimize(p_n), [V.T @ pi == p])


prob.solve()
print(f'Minimum arbitrage-free price: {p_n.value}')

prob = cp.Problem(cp.Maximize(p_n), [V.T @ pi == p])


prob.solve()
print(f'Maximum arbitrage-free price: {p_n.value}')

37
Example
▶ n = 7 assets:
– a risk-free asset with price 1 and payoff 1
– an underlying asset with price 1 and uncertain payoff
– four vanilla options on the underlying with known (market) prices

Type Strike Price


Call 1.1 0.06
Call 1.2 0.03
Put 0.8 0.02
Put 0.7 0.01

option types and prices option payoff diagram

▶ m = 200 possible outcomes for the underlying asset, uniformly between 0.5 and 2
▶ we seek price bounds on a collar option with floor 0.9 and cap 1.15
▶ solving optimization problem gives the arbitrage-free collar price range [−0.015, 0.033]
38
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

39
Currency exchange problem

▶ we hold cinit = (cinit init


1 , . . . , cn ) of n currencies, in USD under nominal exchange rates
▶ want to exchange them to obtain (at least) creq = (creq req
1 , . . . , cn ) , valued in USD
▶ X ∈ Rn×n is currency exchange matrix; Xij ≥ 0 the amount of j we exchange for i, in USD
▶ Δij ≥ 0 is cost of exchanging one USD of currency j for currency i, expressed as a fraction
▶ exchange Xij costs us Xij Δij USD

▶ optimal currency exchange: find X that minimizes cost


Ín
minimize i,j=1 Xij Δij
subject to Xij ≥ 0, diag(X) = 0,
req
cinit
Í Í
i + j Xij − j Xji ≥ ci , i = 1, . . . , n

40
Currency exchange in CVXPY

X = cp.Variable((n, n), nonneg=True)

objective = cp.sum(cp.multiply(X, Delta))


constraints = [
cp.diag(X) == 0,
c_init + cp.sum(X, axis=1) - cp.sum(X, axis=0) >= c_req
]

prob = cp.Problem(cp.Minimize(objective), constraints)


prob.solve()

41
Example
▶ USD, EUR, CAD, SEK, with initial and required holdings (in $106 )

cinit = (1, 1, 1, 1), creq = (2.1, 1.5, 0.3, 0.1)


▶ exchange rates in basis points (bps) (10−4 )
USD EUR CAD SEK
USD 0.0 0.1 4.4 4.8
Δ = EUR 0.1 0.0 5.0 5.7
CAD 2.8 6.9 0.0 8.5
SEK 1.1 7.9 7.6 0.0
▶ cheap to trade USD and EUR, expensive to trade CAD and SEK
▶ optimal exchanges (in $106 )
0.9
0.7

0.5
USD EUR CAD SEK

42
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

43
Purchase execution and risk

▶ want to purchase Q shares over T periods t = 1, . . . , T


▶ q ∈ RT is purchase schedule; q ≥ 0, 1T q = Q
▶ (bid-ask midpoint) price dynamics:

pt = pt−1 + 𝜉t , t = 2, . . . , T,

with p1 known, 𝜉t IID N (0, 𝜎 2 )


▶ nominal cost is random variable pT q, with

E(pT q) = p1 Q, var(pT q) = qT Σq

where Σkl = 𝜎 2 min(k − 1, l − 1)


▶ qT Σq is the risk

44
Market impact

▶ transaction (market impact) cost, in USD (‘squareroot model’):

T
∑︁ T
∑︁
𝜎𝜋t1/2 qt = 𝜎 q3/2 1/2
t /vt
t=1 t=1

▶ vt is market volume, 𝜋t = qt /vt is participation rate in period t


▶ actual cost of execution is nominal mean cost p1 Q plus transaction cost

45
Optimal execution

▶ trade off risk and transaction cost, with participation rate limit
Í  3/2 1/2 
minimize 𝜎 Tt=1 qt /vt + 𝛾qT Σq
subject to q ≥ 0, 1 q = Q, qt /vt ≤ 𝜋 max ,
T t = 1, . . . , T,

▶ 𝛾 > 0 is a risk aversion parameter


▶ 𝜋 max participation rate limit
▶ a convex problem [Almgren and Chriss, 2001]
▶ an alternate formulation reduces computational complexity from O(T 3 ) to O(T)
▶ without risk term and participation contraint, constant participation is optimal

46
Optimal execution in CVXPY

q = cp.Variable(T, nonneg=True)
pi = q / v

risk = cp.quad_form(q, Sigma)


transaction_cost = sigma * cp.power(q, 3 / 2) @ cp.power(v, -1 / 2)

objective = cp.Minimize(transaction_cost + gamma * risk)


constraints = [cp.sum(q) == Q, pi <= pi_max]

prob = cp.Problem(objective, constraints)


prob.solve()

47
Example

▶ purchase 10 million Apple shares over 10 trading days (Feb 8–22, 2024)
▶ participation rate limit 𝜋 max = 5%

volume optimal purchase schedule participation rate

48
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

49
Merton consumption-investment dynamics

▶ plan consumption and investment at times t = 0, . . . , T


▶ in period t, wealth is kt , consumption is ct , labor income is yt (all in inflation adjusted USD)
▶ remaining wealth invested in n assets with return mean 𝜇 and covariance Σ
▶ rt ∈ Rn is asset return in period t
▶ ht ∈ Rn denotes amounts invested in period t in USD
▶ rtT ht is the portfolio return in USD
▶ wealth dynamics given by
kt+1 = kt − ct + yt + rtT ht

50
Merton consumption-investment problem

▶ maximize expected utility of consumption and bequest

T −t
!
𝛽 𝜌 1 ∑︁ 𝜌
E kT + c
𝜌 𝜌 t=0 t

𝛽 > 0 sets relative importance of bequest; 𝜌 < 1 sets risk aversion


▶ a stochastic control problem, solved in [Merton, 1975]

51
Deterministic wealth dynamics

▶ replace stochastic wealth dynamics kt+1 = kt − ct + yt + rtT ht with deterministic dynamics

(1 − 𝜌) hTt Σht
kt+1 = kt − ct + yt + 𝜇T ht −
2 kt + vt
▶ vt is the present value of future labor income discounted at the risk-free rate 𝜇rf

T
∑︁−1
vt = y 𝜏 exp(−𝜇rf (𝜏 − t))
𝜏=t

▶ we require kt + vt > 0, i.e., wealth plus future labor income is positive


▶ last term is a pessimistic adjustment for risk derived in [Moehle and Boyd, 2021]

52
Certainty equivalent convex optimization formulation

▶ yields deterministic convex optimization problem


𝛽 𝜌 1 ÍT −1 𝜌
maximize 𝜌 kT + 𝜌 t=0 ct
(1−𝜌) hTt Σht
subject to kt+1 ≤ kt − ct + yt + 𝜇T ht − 2 kt +vt
kt = 1T ht , ct ≥ 0

(dynamic equality is replaced by inequality constraint, which is tight at solution)


▶ this certainty equivalent problem also solves stochastic problem
▶ can be extended to include mortality, liabilities, taxes, portfolio constraints, . . .

53
Certainty equivalent Merton problem in CVXPY

k = cp.Variable(T + 1)
h = cp.Variable((n, T))
c = cp.Variable(T, nonneg=True)

Sigma_half = np.linalg.cholesky(Sigma)

objective = beta / rho * k[T] ** rho + 1 / rho * cp.sum(c**rho)


constraints = [k[0] == k0, k[:-1] == cp.sum(h, axis=0)]
constraints += [
k[t + 1] <= k[t] - c[t] + y[t] + mu.T @ h[:, t]
- (1 - rho) / 2 * cp.quad_over_lin(Sigma_half.T @ h[:, t], k[t] + v[t])
for t in range(T)
]

prob = cp.Problem(cp.Maximize(objective), constraints)


prob.solve()

54
Example
▶ plan over 80 years (age 25–105), with initial wealth k0 = 10, 000 USD
▶ n = 5 assets, utility parameter 𝜌 = −4, bequest parameter 𝛽 = 10
▶ five asset classes, with long only portfolio constraint ht ≥ 0
▶ salary grows until age 50, then is constant, then drops to 50% at age 65

optimal consumption optimal investments

55
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

56
Alternative investments

▶ investor makes commitments to an alternative investment in each quarter t = 1, . . . , T


▶ over time she puts committed money into the investment in response to capital calls
▶ she receives money through distributions
▶ examples: private equity, venture capital, infrastructure projects, . . .

57
Alternative investment dynamics

▶ ct , pt , dt ≥ 0 are commitments, capital calls, and distributions


▶ nt ≥ 0 is net asset value (NAV), rt is investment return
▶ ut ≥ 0 is total uncalled previous commitments
▶ dynamics:
nt+1 = nt (1 + rt ) + pt − dt , ut+1 = ut − pt + ct
with n0 = 0, u0 = 0
▶ simple model of calls and distributions:

pt = 𝛾 call ut , dt = 𝛾 dist nt

▶ 𝛾 call , 𝛾 dist ∈ (0, 1) are call and distribution intensities or rates

58
Alternative investment planning

▶ choose commitments c1 , . . . , cT to minimize


T+1 T −1
1 ∑︁ 1 ∑︁
(nt − ndes ) 2 + 𝜆 (ct+1 − ct ) 2 ,
T + 1 t=1 T − 1 t=1

where ndes is the desired NAV and 𝜆 > 0 is a smoothing parameter


▶ penalizes deviation from desired NAV, and encourages smooth commitment schedule
▶ can add constraints such as
ct ≤ cmax , ut ≤ umax
▶ yields convex problem
▶ can be extended to uncertain parameters, multiple illiquid investments, and mixed with
liquid investments [Luxenberg et al., 2022]

59
Alternative investment planning in CVXPY

n = cp.Variable(T+1, nonneg=True); u = cp.Variable(T+1, nonneg=True)


p = cp.Variable(T, nonneg=True); d = cp.Variable(T, nonneg=True)
c = cp.Variable(T, nonneg=True)

tracking = cp.mean((n-n_des)**2)
smoothing = lmbda * cp.mean(cp.diff(c)**2)

constraints = [c <= c_max, u <= u_max, n[0] == 0, u[0] == 0]


for t in range(T):
constraints += [n[t+1] == (1+r)*n[t]+p[t]-d[t]]
constraints += [u[t+1] == u[t]-p[t]+c[t]]
constraints += [p[t] == gamma_call*u[t], d[t] == gamma_dist*n[t]]

prob = cp.Problem(cp.Minimize(tracking+smoothing), constraints)


prob.solve()

60
Example

▶ T = 32 (eight years), rt = 0.04 (4% quarterly return), 𝛾 call = .23, 𝛾 dist = .15
▶ planning parameters: cmax = 4, umax = 10, ndes = 15, and 𝜆 = 5

61
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

62
Blending forecasts

▶ we observe x1 , . . . , xt ∈ Rd and seek a forecast x̂t+1 of xt+1


▶ we have K forecasts x̂t+1 1 , . . . , x̂K
t+1
▶ called K experts [Hastie et al., 2009]
▶ we blend them using weights 𝜋t1 , . . . , 𝜋tK with 𝜋t ≥ 0, 1T 𝜋t = 1

K
∑︁
x̂t+1 = 𝜋tk x̂t+1
k

k=1

▶ the weights can vary over time


▶ we may want them smoothly varying, i.e., 𝜋t+1 ≈ 𝜋t
▶ we may want them close to some prior or baseline weights 𝜋 pri
▶ examples: return mean, return covariance, . . .

63
Blending forecasts using convex optimization

▶ find weights 𝜋t as solution of convex optimization problem


1 Ít
minimize M 𝜏=t−M+1 ℓ(x̂ 𝜏 , x 𝜏 ) + rsm (𝜋, 𝜋t−1 ) + rpri (𝜋, 𝜋 pri )
ÍK
subject to x̂ 𝜏 = k=1 𝜋k x̂k𝜏 , 𝜋 ≥ 0, 1T 𝜋 = 1

with variable 𝜋 ∈ RK
▶ ℓ is prediction loss, rsm penalizes weight change, rpri penalizes deviation from prior
▶ we assume ℓ, rsm , rpri are convex in 𝜋
▶ idea: use blending weights that would have worked well over the last M periods

64
Blending forecasts in CVXPY

▶ X_hat is an M × K matrix of expert forecasts over the last M periods 𝜏 = t − M + 1, . . . , t


▶ x is an M -vector of observed quantities over the same period

pi = cp.Variable(K, nonneg=True)
x_hat = X_hat @ pi

objective = cp.Minimize(cp.mean(loss(x_hat, x)))


constraints = [cp.sum(pi) == 1]

prob = cp.Problem(objective, constraints)


prob.solve()

65
Example
▶ predict log of daily trading volume of Apple, 1982–2024
▶ K = 3 predictors: 5-day (fast), 21-day (medium), and 63-day (slow) moving medians
▶ absolute loss ℓ(x̂ 𝜏 , x 𝜏 ) = |x̂ 𝜏 − x 𝜏 | ; M = 250 trading days

250-day rolling median absolute error weights (𝜋)

error fast median slow blend


median 0.25 0.27 0.29 0.24
90th percentile 0.72 0.74 0.82 0.68
10th percentile 0.05 0.05 0.05 0.04

66
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

67
Bond price model

▶ bond pays holder ct in periods t = 1, . . . , T (coupons and principal)


▶ price of bond is discounted present value of payments

T
∑︁
p= ct exp(−t(yt + s)),
t=1

where y = (y1 , . . . , yT ) ∈ RT is the yield curve, and s ≥ 0 the spread


▶ we assume y = Ya where Y are basis functions and a are coefficients
▶ can use principal component analysis to fit Y to historical data
▶ spread depends on bond rating (readily extended to depend on other attributes)

68
Matrix pricing problem

▶ we are given market prices pi and ratings ri ∈ {1, . . . , K} of n bonds, i = 1, . . . , n


▶ we want to fit a yield curve y ∈ RT and spreads s ∈ RK to this data
▶ we add constraint 0 ≤ s1 ≤ · · · ≤ sK (higher ratings have lower spreads)
▶ using square error we fit y and s by solving problem

Ín  2
pi − Tt=1 ci,t exp(−t(yt + sri ))
Í
minimize i=1
subject to 0 ≤ s1 ≤ · · · ≤ sK

with variables y and s


▶ not convex, but can be solved (approximately) as a sequence of convex problems
▶ linearize exponential term and iteratively fit yields and spreads

69
Matrix pricing in CVXPY
▶ use CVXPY to automatically linearize the exponential term as p current + Delta hat
▶ would add trust penalty to the iterates in practice
▶ code below computes first iteration

y = cp.Variable(T, value=y_init)
s = cp.Variable(K, value=S_init, nonneg=True)
a = cp.Variable(a.size, values=a_init)

discount = cp.exp(cp.multiply(-t, y.reshape((1, -1)) + s[ratings].reshape((-1, 1))))


p_current = cp.sum(cp.multiply(C, discount), axis=1)

Delta_hat = p_current.grad[y].T @ (y-y.value) + p_current.grad[s].T @ (s - s.value)


objective = cp.norm2(p - (p_current.value + Delta_hat))
constraints = [cp.diff(s) >= 0, y == Y @ a]

problem = cp.Problem(cp.Minimize(objective), constraints)


problem.solve()

70
Example
▶ consider n = 1000 bonds, with a maturity of up to 30 years
▶ bonds are rated AAA, AA, A, BBB, BB
▶ used data from 1990 to 2024 to fit basis functions, latest yields, and spread to price bonds
▶ fit yields and spreads to bond prices gives $0.03 RMSE (2.9 bps)

iteratively fitted yields for rating AAA rating spreads (vs. AAA)

71
Outline
Convex optimization
Markowitz portfolio construction
Maximum expected utility portfolio construction
Sparse inverse covariance estimation
Worst-case risk analysis
Option pricing
Currency exchange
Optimal execution
Optimal consumption
Alternative investment planning
Blending forecasts
Bond pricing
Model predictive control

72
Stochastic control

▶ dynamics xt+1 = f (xt , ut , wt ) , t = 0, 1, . . . , T − 1


▶ xt ∈ X is the state, ut ∈ U is the input or action, wt ∈ W is the disturbance
▶ x0 , w0 , . . . , wT −1 are independent random variables
▶ state feedback policy ut = 𝜋t (xt ) , t = 0, 1, . . . , T − 1
▶ stochastic control problem: choose policy to minimize

T −1
!
∑︁
J=E gt (xt , ut ) + gT (xT )
t=0

▶ stage cost gt (xt , ut ) ; terminal cost gT (xT )


▶ examples: investing, execution, consumption, . . .

73
Solution via dynamic programming

▶ exact solution from Bellman [1954]


▶ only practical in special cases
– X , U finite
– linear dynamics and quadratic cost
– xt ∈ Rn with n very small, like 2 or 3
– a few other special cases (e.g., Merton problem)

▶ but several heuristics and approximations work very well

74
Model predictive control

mpc
to evaluate 𝜋t (xt ) :
▶ forecast: predict stochastic future values wt , . . . , wT −1 as ŵt|t , . . . , ŵT −1|t
▶ plan: solve certainty equivalent problem assuming forecasts are correct
ÍT −1
minimize 𝜏=t gt (x̂ 𝜏 |t , ut ) + gT (x̂T |t )
subject to x̂ 𝜏+1|t = f (x 𝜏 |t , u 𝜏 |t , ŵ 𝜏 |t ), 𝜏 = t, . . . , T − 1, x̂t|t = xt

with variables u 𝜏 |t , . . . , uT −1|t , x̂t|t , . . . , x̂T |t


▶ execute: 𝜋tmpc (xt ) = ut|t (i.e., take first action in plan)

75
Model predictive control

▶ when f is linear in x, u and gt are convex, planning problem is convex, hence tractable
▶ MPC is optimal in a few special cases, but often performs extremely well
▶ used in many industries, e.g., guiding SpaceX’s Falcon first stages to their landings
[Blackmore, 2016]

▶ receding horizon MPC


– a variation for when there is no terminal time T
– solve planning problem over H -period horizon 𝜏 = t to 𝜏 = t + H
– can include terminal cost or constraint

76
Example: Order execution via MPC
▶ purchase 10 million Apple shares over 10 trading days (Feb 8–22, 2024)
▶ participation rate limit 𝜋 max = 5%
▶ forecasts:
– v̂ 𝜏 |t is 5-day trailing median of volumes, 𝜏 = t, . . . , T
– 𝜎𝜏 |t is 21-day trailing standard deviation, 𝜏 = t, . . . , T
▶ transaction cost is 1.881B USD for fixed schedule and 1.877B USD for MPC
▶ MPC saves us 4M USD, about 20 bps

77
Example: Order execution via MPC

78

You might also like