I am doing a portfolio optimization using Gekko. I am using the returns per asset and its covariance matrix as input. The code I am trying is
# Initialize Model
m = GEKKO(remote=True)
#initialize variables
w=m.Array(m.Var,23)
for i in range(23):
w[i].value = 0.04347826
w[i].lower = 0.0
w[i].upper = 1.0
#Equations
m.Equation(0.18 >= (np.dot(w.T,np.dot(covariance_ar, w)))**0.5)
m.Equation(np.sum(w[0:23]) == 1.0)
m.Equation(w[7]+w[9]+w[11] >= (np.sum(w[7:13])*0.3))
m.Equation(w[7]+w[9]+w[11] <= (np.sum(w[7:13])*0.7))
m.Equation(w[11]+w[12] >= 0.0)
m.Equation(w[11]+w[12] <= (np.sum(w[7:13])*0.15))
#Objective
m.Obj(-(np.sum(return_ar.T @ w)))
#Solve simulation
m.solve() # solve on public server
#Results
for i in range(23):
print('w['+str(i)+']='+str(w[i].value))
return_ar is an array of returns of individual assets of shape (23,1) and covariance_ar is the covariance array of returns of shape (23,23)
Although the code ran succesfully with small number of weights, with 23 weight parameters it is throwing error Exception: @error: Max Equation Length....... APM model error: string > 15000 characters Consider breaking up the line into multiple equations
Can you help me to formulate the code constraint so that this works.