-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPortfolioOptimization.py
More file actions
executable file
·386 lines (294 loc) · 13.4 KB
/
Copy pathPortfolioOptimization.py
File metadata and controls
executable file
·386 lines (294 loc) · 13.4 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from scipy.optimize import minimize, Bounds, LinearConstraint, NonlinearConstraint
import numpy as np
from cvxopt import matrix, solvers
solvers.options['show_progress'] = False
import warnings
def utility_transcosts(d,mu,k,V,g,x0,n):
"""Negative mean-variance utility function with transaction costs"""
return -d[:n]@mu + d[n:]@mu - x0@mu + d[:n]@k + d[n:]@k + \
(d[:n]@V@d[:n] - 2*d[:n]@V@d[n:] + 2*d[:n]@V@x0 + d[n:]@V@d[n:] - 2*d[n:]@V@x0 + x0@V@x0)*g/2
def constr_d(n,d):
return d[:n]@d[n:]
def jac_c(n,d):
return np.append(d[n:],d[:n])
def hessian_c(n,d,v):
return np.vstack([np.hstack([np.zeros((n,n)),np.eye(n)]),np.hstack([np.eye(n),np.zeros((n,n))])])
def constr_transcosts(n,x0,nonlin_constr=True,supply_H_and_jac=True):
bounds = Bounds(np.array([0]*(2*n)), np.append(1-x0,x0) if nonlin_constr else np.inf)
A = np.append(np.ones((1,n)),np.ones((1,n))*-1)
lb,ub = 0,0
if not nonlin_constr:
lincomb_bound = np.zeros((n,2*n))
for i in range(n):
lincomb_bound[i,i] = 1
lincomb_bound[i,i+n] = -1
A = np.vstack([A[None,:],lincomb_bound])
lb = np.append(lb,-x0)
ub = np.append(ub,1-x0)
constr = [LinearConstraint(A,lb,ub)]
if nonlin_constr:
constr.append( NonlinearConstraint(lambda x: constr_d(n,x),0,0,
**({} if not supply_H_and_jac \
else {'jac': lambda x: jac_c(n,x),
'hess': lambda x,v: hessian_c(n,x,v)}))
)
return {'bounds': bounds, 'constraints': constr}
def jac_trcost(d,mu,k,V,g,x0,n):
return np.append(-mu.T + k + g*(d[:n]-d[n:]+x0)@V, mu.T + k - g*(d[:n]-d[n:]+x0)@V)
def H_trcost(d,mu,k,V,g,x0,n):
return g*np.vstack([np.hstack([V,-V]),np.hstack([-V,V])])
def random_change(x0,alpha):
allo = np.random.dirichlet(alpha)
dx = allo - x0
dplus = np.zeros(len(x0))
dplus[dx>0] = dx[dx>0]
dminus = np.zeros(len(x0))
dminus[dx<0] = dx[dx<0]*-1
return np.append(dplus,dminus)
class OptP:
def __init__(self,exp_return,tr_cost,cov,risk_aversion,start_allocation):
"""
Class for mean-variance portfolio optimization under proportional transaction
costs.
..math:: \min_{x} \frac{\gamma}{2}x'\Sigma x + k'|x^+ + x^-| - \mu'x
subject to `x = x^+ - x^- + x_0`, `1'x = 1`, `0<=x<=1` and `x^+, x^- >=0`
(and optionally x^+'x^- = 0). `x_0` is the start allocation.
Parameters:
-----------
exp_return : numpy.array
One dimensional array of n assets' expected returns.
tr_cost : numpy.array
One dimensional array of n assets' transaction costs.
cov : numpy.array
(n x n) return covariance matrix.
risk_aversion : float
Investor risk aversion.
start_allocation : numpy.array
Current assets' portfolio weights.
"""
self.mu = exp_return
self.k = tr_cost
self.V = cov
self.g = risk_aversion
self.x0 = start_allocation
def generate_qp_matrices(self):
"""Generate matrices used by the cvxopt solver."""
m = self.x0.shape[0]
n = m*2
Zero = np.zeros(self.V.shape)
self.P = matrix(np.c_[np.r_[self.V,self.V], np.r_[self.V,self.V]]*self.g)
Q = np.c_[np.r_[self.V,Zero], np.r_[Zero,self.V]]
self.q = matrix(self.g*np.append(self.x0,self.x0)@Q+np.append(self.k,-self.k)-np.append(self.mu,self.mu))
upper_bound = np.zeros((m,n))
for i in range(m):
upper_bound[i,i] = 1
upper_bound[i,i+m] = 1
lower_bound = upper_bound*-1
self.G = matrix(np.r_[upper_bound,
lower_bound,
np.diag(np.append(np.ones(m)*-1,np.ones(m)))]
)
self.h = matrix(np.r_[1-self.x0,self.x0,np.zeros(n)])
self.A = matrix(1.0,(1,n))
self.b = matrix(0.0)
return
def optimize_p(self,solver='scipy',**kwargs):
"""
Optimize portfolio.
Parameters
----------
solver = str (default 'scipy')
If 'scipy', uses scipy's minimize function. Allows handling
of non-linear constraints. If 'qp', uses cvxopt's solver
function. A lot faster than scipy, but cannot handle non-linear
contraints.
krawgs : dict
Optional keyword arguments passed to the solver function.
Returns
-------
None. Result is provided in the `solution` attribute of the class.
"""
if solver=='scipy':
self.opt_scipy(**kwargs)
elif solver=='qp':
if kwargs.get('nonlin_constr')==True:
raise ValueError("Non-linear constraints not compatible with quadratic program.")
self.generate_qp_matrices()
kwargs.update({'nonlin_constr': False})
self.opt_cvx(**kwargs)
else:
raise ValueError(f"{solver} not a valid method; use 'scipy' or 'qp'")
return
def opt_cvx(self,**kwargs):
"""
Solve optimization problem using CVXOPT solver for
quadratic programs.
If called standalone (not via `optimize_p`), the P, q,
G, h, A and b matrices (see CVXOPT documentation) must
be provided as class attributes first.
Parameters
----------
default_to_scipy : bool (optional)
If True, use scipy solver if cvxopt solver throws an error.
kwargs : dict
Rest of the optional keyword arguments are passed to scipy
solver, if used.
Returns
-------
None. Result is provided in the `solution` attribute of the class.
"""
try:
sol = solvers.qp(self.P,self.q,self.G,self.h,self.A,self.b)
if 'optimal' not in sol['status']:
raise ValueError("Method did not converge")
except Exception as e:
warnings.warn(f"CVXOPT solver unsuccessful; default to scipy `minimize`; Error raised: {e}")
if kwargs.get('default_to_scipy',False):
self.opt_scipy(**kwargs)
else:
self.solution = {'dx': np.nan, 'x': np.nan}
else:
dx_star = np.array(sol['x']).reshape(2,-1)
x_star = self.x0 + dx_star.sum(axis=0)
self.solution = {'dx': dx_star, 'x': x_star}
return
def opt_scipy(self,init='random',n_repeat=10,alpha=1,nonlin_constr=True,supply_H_and_jac=True):
"""
Solve optimization problem using scipy minimize.
Parameters
----------
init : str or numpy.ndarray (default 'random')
Starting value for the optimization. If 'at_zero',
the starting value for the change in allocation is set
to zero for all assets. If 'random', the optimization
is initialized at a random starting point in addition
to an initialization at a zero change. A custom starting
point can be provided by passing a numpy array of shape
(2*n,) where n is the number of assets. The first n
elements are the portfolio increases (x^+), the latter
n elements are portfolio decreases (x^-).
n_repeat : int
Only relevent if `init='random'`. Number of times the
optimization is performed at different random starting
points.
alpha : float (>0)
Only relevent if `init='random'`. Starting point for
the optimization is given by the difference between a
vector of random portfolio allocations and the initial
portfolio weights (x_0). The random allocation is drawn
from a Dirichlet distribution with parameters (alpha, ...
alpha).
nonlin_constr : bool (default True)
If True, the optimization takes the constraint `x^+'x^-=0`
into account. That is, simulatenous allocation increases and
decreases are not allowed.
supply_H_and_jac : bool (default True)
If True, the analytical Hessian and jacobian for the optimization
problem and the nonlinear contraint (if chosen) are passed. If False,
scipy computes them numerically.
Returns
-------
None. Result is provided in the `solution` attribute of the class.
"""
n = len(self.mu)
if init == 'random' or init=='at_zero':
# initalize with zero change
res = minimize(utility_transcosts,[0]*2*n,\
args=(self.mu,self.k,self.V,self.g,self.x0,n),\
method='trust-constr',\
**constr_transcosts(n,self.x0,nonlin_constr=nonlin_constr,supply_H_and_jac=supply_H_and_jac),
**({} if not supply_H_and_jac else \
{'jac': jac_trcost,
'hess': H_trcost})
)
u_init = utility_transcosts(res.x,self.mu,self.k,self.V,self.g,self.x0,n)
dx_init = res.x
if init == 'random':
for i in range(n_repeat):
d_init = random_change(self.x0, [alpha]*n)
res = minimize(utility_transcosts,d_init,\
args=(self.mu,self.k,self.V,self.g,self.x0,n),\
method='trust-constr',\
**constr_transcosts(n,self.x0,nonlin_constr=nonlin_constr,supply_H_and_jac=supply_H_and_jac),
**({} if not supply_H_and_jac else \
{'jac': jac_trcost,
'hess': H_trcost})
)
u_new = utility_transcosts(res.x,self.mu,self.k,self.V,self.g,self.x0,n)
if u_new < u_init:
u_init = u_new
dx_init = res.x
dx_star = dx_init.reshape(2,-1)
dx_star[1,:] *= -1
x_star = self.x0 + dx_star.sum(axis=0)
else:
res = minimize(utility_transcosts,init,\
args=(self.mu,self.k,self.V,self.g,self.x0,n),\
method='trust-constr',\
**constr_transcosts(n,self.x0,nonlin_constr=nonlin_constr,supply_H_and_jac=supply_H_and_jac),
**({} if not supply_H_and_jac else \
{'jac': jac_trcost,
'hess': H_trcost})
)
dx_star = res.x.reshape(2,-1)
dx_star[1,:] *= -1
x_star = self.x0 + dx_star.sum(axis=0)
self.solution = {'dx': dx_star, 'x': x_star}
return
def utility(self,dx):
"""
Utility in terms of allocation changes.
Parameters:
-----------
dx : numpy.array
(2,n) array of allocation changes of n assets. First row refers
to allocation increases, second row to decreases. Accordingly,
the first row should contain positive values only, the second row
only negative values.
Returns:
--------
float
"""
x = self.x0 + dx.sum(axis=0)
return self.portfolio_return(dx,gross=False) - self.portfolio_var(x)*self.g/2
def portfolio_var(self,x):
"""Portfolio variance given allocations `x`."""
return x@(self.V)@x
def portfolio_return(self,dx,gross=False):
"""
Portfolio return.
Parameters:
-----------
dx : numpy.array
(2,n) array of allocation changes of n assets. First row refers
to allocation increases, second row to decreases. Accordingly,
the first row should contain positive values only, the second row
only negative values.
gross : bool (default : False)
If False, returns portfolio return minus transaction costs. Returns
portfolio return not accounted for transaction costs otherwise
Returns:
--------
float
"""
x = self.x0 + dx.sum(axis=0)
if gross:
return x@self.mu
else:
return x@self.mu - self.portfolio_implcost(dx)
def portfolio_implcost(self,dx):
"""
Transaction costs for implementing portfolio allocation
changes.
Parameters:
-----------
dx : numpy.array
(2,n) array of allocation changes of n assets. First row refers
to increases in allocation, second row to decreases. Accordingly,
the first row should contain positive values only, the second row
only negative values.
Returns:
--------
float
"""
return dx@self.k@[1,-1]