-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathportfolio_optimization.py
144 lines (112 loc) · 4.17 KB
/
portfolio_optimization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import numpy as np
import pandas as pd
import pandas_datareader.data as web
from mosek.fusion import *
from scipy.stats import norm
def min_risk(mu, H, target_return=0.0, shortselling=True, verbose=False):
"""
Minimum Variance Portfolio
min w'Hw
s.t.
w'mu = target_return
sum(w) = 1
w >= 0 (if no shortselling)
In conic form
min s2
s.t.
w'mu = target_return
sum(w) = 1
w >= 0 (if no shortselling)
s2 >= w'Hw <=> (1/2, s2, GW) \in Q_r^{n+2}
"""
names = mu.index # The asset names
n = mu.size # Number of assets
G = np.linalg.cholesky(H).T # A matrix such that H = G'G
if isinstance(mu, pd.Series): # Store as plain np.array
mu = mu.values
M = Model('Min Risk')
# Define portfolio weights
if shortselling:
w = M.variable('w', n, Domain.unbounded())
else:
w = M.variable('w', n, Domain.greaterThan(0.0))
# Variance
s2 = M.variable('s2', 1, Domain.greaterThan(0.0))
# The objective
M.objective('minvar', ObjectiveSense.Minimize, s2)
# Full investment
M.constraint('budget', Expr.sum(w), Domain.equalsTo(1.0))
# Get at least the target return
if target_return > 0.0:
M.constraint('target', Expr.dot(mu, w), Domain.greaterThan(target_return))
# Imposes a bound on the risk
M.constraint('s2 > ||Gw||_2^2',
Expr.vstack(Expr.constTerm(1, 0.5),
s2.asExpr(),
Expr.mul(G, w)), Domain.inRotatedQCone())
if verbose:
M.setLogHandler(sys.stdout)
M.solve()
return pd.Series(w.level(), index=names)
def min_VaR(mu, H, target_return=0.0, shortselling=True, alpha=0.01, verbose=False):
"""
Minimum Value-at-Risk Portfolio
min -(w'mu + w'Hw * q)^1/2
s.t.
w'mu = target_return
sum(w) = 1
w >= 0 (if no shortselling)
In conic form
min t
s.t.
w'mu = target_return
sum(w) = 1
w >= 0 (if no shortselling)
t >= -(w'mu + w'Hw * q)^1/2 <=> (-1/q * (t + w'mu), GW) \in Q^{n+1}
"""
names = mu.index # The asset names
q = norm.ppf(alpha) # The quantile
n = mu.size # Number of assets
G = np.linalg.cholesky(H).T # A matrix such that H = G'G
if isinstance(mu, pd.Series): # Store as plain np.array
mu = mu.values
M = Model('Min VaR')
# Define portfolio weights
if shortselling:
w = M.variable('w', n, Domain.unbounded())
else:
w = M.variable('w', n, Domain.greaterThan(0.0))
# The objective
t = M.variable('t', 1, Domain.greaterThan(0.0))
M.objective('min risk', ObjectiveSense.Minimize, t)
# Full investment
M.constraint('budget', Expr.sum(w), Domain.equalsTo(1.))
# Get at least the target return
if target_return > 0.0:
M.constraint('target', Expr.dot(mu, w), Domain.greaterThan(target_return))
# Imposes a bound on the risk
M.constraint('-1/q * (t + w`mu) > ||Gw||_2',
Expr.vstack(Expr.mul(-1/q, Expr.add(t, Expr.dot(w, mu))),
Expr.mul(G, w)), Domain.inQCone())
if verbose:
M.setLogHandler(sys.stdout)
M.solve()
return pd.Series(w.level(), index=names)
if __name__ == "__main__":
# Load French's portfolio data from pandas-datareader
data = web.DataReader("5_Industry_Portfolios", "famafrench", start=2000)
r = data[1] / 100
# Estimate the sample mean and covariance from the data
mu = r.mean()
H = r.cov()
# Configuration
target_return = 4e-04
shortselling = False
verbose = False
# Estimate minimum variance portfolio
w = min_risk(mu=mu, H=H, target_return=target_return, shortselling=shortselling, verbose=verbose)
# Estimate minimum Value-at-Risk portfolio
w = min_VaR(mu=mu, H=H, target_return=target_return, shortselling=shortselling, verbose=verbose)