I would like to calculate the absolute value of the sum of two matrices in my objective function but for some reason I kept on getting the error message " bad operand type for unary -: 'GenExpr' ".
#Data
hyperparameter = 0.5
weightss = np.array([0.25, 0.25, 0.25, 0.25])
weightss
transactional_costs = np.array([0.01, 0.01, 0.01, 0.01])
transactional_costs
# Add matrix variable for the stocks
x = m.addMVar(len(stocks))
# Objective is to maximize the return rate and minimize the risk
portfolio_objective = delta @ x - hyperparameter * (x @ sigma @ x) - gp.abs_(transactional_costs @ realweights - transactional_costs @ weightss)
m.setObjective(portfolio_objective, GRB.MAXIMIZE)
I have tried calculating the part for the absolute value outside the line portfolio_objective but I still encounter the same problem. Could someone give me a direction?
Update: the data is from yahoo finance
closes = np.transpose(np.array(data.Close)) # matrix of daily closing prices
absdiff = np.diff(closes) # change in closing price each day
reldiff = np.divide(absdiff, closes[:,:-1]) # relative change in daily closing price
delta = np.mean(reldiff, axis=1) # mean price change
sigma = np.cov(reldiff) # covariance (standard deviations)
std = np.std(reldiff, axis=1) # standard deviation