I am trying to implement a portfolio optimization that uses constraints to define e.g. max exposure to country/ sector/ industry etc. I have implemented the following code below, where I pass in a 'africa' vector to map stocks to country africa, in my constraints I then bound those to be not more then 40% in weights overall. The only way that I managed to implement it is to use sum_weights over the indexes where africa = 1. I also tried using the Parameter function but didn't succeed. There must be a more elegant way to apply these kind of constraints, hopefully. Any suggestion is appreciated. Also if anyone knows about an example that shows the use of tracking error constraints, turnover constraints or volatility constraints, these are the ones where I am also still struggling with.
import numpy as np
from cvxpy import *
np.random.seed(1)
n = 10 # number of assets
mu = np.abs(np.random.randn(n,1)) #mean
Sigma = np.random.randn(n,n)
Sigma = Sigma.T.dot(Sigma)
# Long only PFO Opt
w = Variable(n)
#africa = Parameter(10, sign='positive')
#africa.value = [1,1,1,0,0,0,0,0,0,0]
africa = [0,0,0,0,0,0,0,1,1,1]
gamma = Parameter(sign='positive')
ret = mu.T*w
risk = quad_form(w,Sigma)
filters = [i for i in range(len(africa)) if africa[i] == 1]
constraints = [sum_entries(w) == 1, w >=0, w[1] > 0.50, w[0] == 0, sum_entries(w[filters]) == 0.4]
#prob = Problem(Maximize(ret - gamma*risk), [sum_entries(w) == 1, w >=0])
prob = Problem(Minimize(risk), constraints)
SAMPLE = 1000
risk_data = np.zeros(SAMPLE)
ret_data = np.zeros(SAMPLE)
gamma_vals = np.logspace(-2,3,num=SAMPLE)
for i in range(SAMPLE):
gamma.value = gamma_vals[i]
prob.solve()
risk_data[i] = sqrt(risk).value
ret_data[i] = ret.value
print(prob.status)
print(prob.value)
print('OPT WEIGHTS : ')
for i in range(n):
print(round(w[i].value,3))