I'm coding a markowitz efficient frontier with a group of 10 stocks. Until now, a get almost everything right, but, when i printed the weights gived for each stock, the sum was bigger than 1, and i cant resolve this.
#normalization of returns
log_ret = np.log(carteira/carteira.shift(1))
log_ret.head()
log_ret.cov()
np.random.seed(42)
num_ports = 6000
all_weights = np.zeros((num_ports, len(carteira.columns)))
ret_arr = np.zeros(num_ports)
vol_arr = np.zeros(num_ports)
sharpe_arr = np.zeros(num_ports)
for x in range(num_ports):
weights = np.array(np.random.random(10))
weighs = weights/np.sum(weights)
all_weights[x,:] = weights
#expected return
ret_arr [x] = np.sum( (log_ret.mean() *weights * 264))
#expected volatility
vol_arr[x] = np.sqrt(np.dot(weights.T, np.dot(log_ret.cov()*264, weights )))
#sharpe ration
sharpe_arr[x] = ret_arr[x]/vol_arr[x]
print('Max sharpe ration: {}'.format(sharpe_arr.max()))
print(' location array: {}'.format(sharpe_arr.argmax()))
print(all_weights[1506,:])
with this specific stocks, my weight is almost 4 but i need that return sum = 1.
Someone can help me ?