I'm tyring to solve an optimizatio problem using GEKKO. The original problem is one where you have a linear objective function with thousands of variables, constraints (some of which are non-linear) and boundaries. I was kindly suggested to use GEKKO, and since I don't fully understand the mechanics of it I'm having some problem in implementing it. I keep getting object of type 'int' has no len()
error. I've simplified the problem so now it's only twenty varialbes and there's no constraints but 10 bounds that have to be respected. This way I think I get to isolate and pindown the source of error. As follows is the code:
import pandas as pd
import numpy as np
import copy as cp
from gekko import GEKKO
from scipy.optimize import minimize
df_uni = pd.read_csv(r'D:\US\IIT\lectures_textbooks\QIS\week_4\universe_sets.csv')
df_uni["begin_wt"]=np.zeros(10)
#df_holding=df_uni
df_holding=cp.deepcopy(df_uni)
x_holding=np.array([0.1,0.25,0.2,0.2,0.05,0.05,0.1,0.15,-0.05,-0.05])
df_holding["begin_wt"]=x_holding
df_holding.loc[df_holding['begin_wt'] >0, "up_limit"] = df_holding['begin_wt']
df_holding.loc[df_holding['begin_wt'] <0, "up_limit"] = 0
Alpha_pickup=3
df_holding.loc[df_holding['begin_wt'] >0, "Alpha"] = df_holding['Alpha']+Alpha_pickup
df_holding.loc[df_holding['begin_wt'] <0, "Alpha"] = df_holding['Alpha']-Alpha_pickup
df_holding.loc[df_holding['begin_wt'] >0, "low_limit"] = 0
df_holding.loc[df_holding['begin_wt'] <0,"low_limit"]=df_holding['begin_wt']
df_holding=df_holding.drop("begin_w",axis=1)
df_uni=df_uni.drop("begin_w",axis=1)
sect_offset=0.1
lncap_offset=0.1
sect1=sum(df_uni.loc[df_holding['Sector_1'] ==1]['ben_wt'])
sect2=sum(df_uni.loc[df_holding['Sector_2'] ==1]['ben_wt'])
lncap1=sum(df_uni.loc[df_holding['Sector_1'] ==1]['lncap'])
lncap2=sum(df_uni.loc[df_holding['Sector_2'] ==1]['lncap'])
list_uni_alpha=list(df_uni['Alpha'])
list_holding_alpha=list(df_holding['Alpha'])
bind_list_alpha=list_uni_alpha+list_holding_alpha
#x=[1 for i in range(20)]
def objective(x):
l=0
sum_of_Alpha=0
for i in bind_list_alpha:
sum_of_Alpha=sum_of_Alpha+x[l]*i
print(sum_of_Alpha)
l=l+1
return sum_of_Alpha
# constraints always writing them in terms of f(x)>0
# consolidated weights are bound
# security offsets
uni_begin=list(df_uni['begin_wt'])
holding_begin=list(df_holding['begin_wt'])
#initial guess
ig=cp.deepcopy(uni_begin+holding_begin)
m=GEKKO()
x = m.Array(m.Var,(20))
x_holding=x[10:20]
i=0
#bounds
for xi in x_holding:
xi.value = x_holding[i]
xi.lower = df_holding['low_limit'][i]
xi.upper = df_holding['up_limit'][i]
i = i + 1
m.Obj(objective(x))
m.solve()
print(x)
Forgive me for including the block of code that doesn't seem to be relevant. But to give context to thoso who are familar with portfolio construction, I'm actually trying to construct a portfolio of stocks. The obejctve function is the linear combination of the stocks' alphas. "The holding" means the stock you're currently holding while "the universe" is the large pool of stocks you get to invest in. I'm doing active management so I tend to overweigh stocks whose prospect I think are good and underweigh those prosepct I don't think are good. But of course I don't want my portfolio to look vastly different than the benchmark because that would make the portfolio bear a lot of systematic risk. Hence those constraints you'll see towards the end of the code. I've been looking for an optimizer that can accommodate constraints written in the form of aX=b, where both a and b are array-like and X is a matrix. But for now, I think this particular optimizer will do me just as good!
Thank you!