In [1]:
from cvxpy import *
from simple_portfolio_data import *
one = np.matrix(np.ones((n, 1)))
In [2]:
r_min = pbar.T * x_unif
risk_unif = quad_form(x_unif, S).value
print('Minimum return: {}'.format(r_min))
In [3]:
x = Variable(n)
objective_a = Minimize( quad_form(x, S) )
In [4]:
constraints_i = [pbar.T * x == r_min, sum(x) == 1]
problem_ai = Problem(objective_a, constraints_i)
risk_ai = problem_ai.solve()
In [5]:
constraints_ii = [pbar.T * x == r_min, sum(x) == 1, x >= 0]
problem_aii = Problem(objective_a, constraints_ii)
risk_aii = problem_aii.solve()
In [6]:
constraints_iii = [pbar.T * x == r_min, sum(x) == 1,
one.T * max(-x, 0) <= 0.5]
problem_aiii = Problem(objective_a, constraints_iii)
risk_aiii = problem_aiii.solve()
In [7]:
print('Uniform portfolio risk: {}'.format(risk_unif))
print('No additional constraints risk: {}'.format(risk_ai))
print('Long-only risk: {}'.format(risk_aii))
print('Total short: {}'.format(risk_aiii))
In [8]:
N = 20
mus = np.logspace(0, 5, N)
mean_longonly = np.zeros(N)
std_longonly = np.zeros(N)
mean_totalshort = np.zeros(N)
std_totalshort = np.zeros(N)
constraints_longonly = [sum(x) == 1, x >= 0]
constraints_totalshort = [sum(x) == 1, one.T * max(-x, 0) <= 0.5]
for i, mu in enumerate(mus):
print('mu = {}'.format(mu))
ret = pbar.T * x
risk = quad_form(x, S)
objective = Minimize( -ret + mu * risk )
# Long-only.
Problem(objective, constraints_longonly).solve()
mean_longonly[i] = ret.value
std_longonly[i] = np.sqrt(risk.value)
# Total short.
Problem(objective, constraints_totalshort).solve()
mean_totalshort[i] = ret.value
std_totalshort[i] = np.sqrt(risk.value)
In [9]:
pl.plot(std_longonly, mean_longonly, label='long-only')
pl.plot(std_totalshort, mean_totalshort, label='total short')
pl.xlabel('standard deviation of return')
pl.ylabel('mean return')
pl.legend(loc='lower right', frameon=False);