-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathportfolios.py
64 lines (50 loc) · 1.58 KB
/
portfolios.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
import warnings
import numpy as np
import cvxopt as opt
import cvxopt.solvers as optsolvers
def equal_portfolio(Sigma):
N = Sigma.shape[0]
return np.full(N, 1. / N)
def min_var_portfolio(Sigma, leverage=1., gamma=None):
"""
Computes the minimum variance portfolio.
Note: As the variance is not invariant with respect
to leverage, it is not possible to construct non-trivial
market neutral minimum variance portfolios. This is because
the variance approaches zero with decreasing leverage,
i.e. the market neutral portfolio with minimum variance
is not invested at all.
Parameters
----------
Sigma: pandas.DataFrame
Covariance matrix of asset returns.
allow_short: bool, optional
If 'False' construct a long-only portfolio.
If 'True' allow shorting, i.e. negative weights.
Returns
-------
weights: pandas.Series
Optimal asset weights.
"""
n = len(Sigma)
P = opt.matrix(Sigma)
q = opt.matrix(0.0, (n, 1))
# Constraints Gx <= h
if gamma is not None:
# x >= 0
G = opt.matrix(-np.identity(n))
h = opt.matrix(gamma, (n, 1))
else:
G = None
h = None
# Constraints Ax = b
# sum(x) = 1
A = opt.matrix(1.0, (1, n))
b = opt.matrix(leverage)
# Solve
optsolvers.options['show_progress'] = False
sol = optsolvers.qp(P, q, G, h, A, b)
if sol['status'] != 'optimal':
warnings.warn("Convergence problem")
return np.array(sol['x']).ravel()
portfolio_funs = {'eq': equal_portfolio, 'minvar': min_var_portfolio}