# Calculate asset allocation given “long and short” optimized portfolio weights

If the amount of capital that has to be allocated for each asset given the "long only" optimized portfolio weights is:

$allocation_i&space;=captial&space;\cdot&space;weight_i$

weights = [ 0., 0., 0., 0., 0., 0., 0., 0., 0., 3.205
0., 0., 0., 0., 1.84, 11.168, 0., 0., 0., 0.
0., 12.297, 11.339, 0., 0., 0., 0., 0., 0., 0.
0., 0., 11.489, 0., 6.807, 18.372, 0., 0., 0., 0.
0., 4.54, 0., 0., 0., 0., 0., 0., 0., 0.
0., 0., 14.06, 0., 0., 0., 0., 4.882]

#Sum of all weights is equal to 99.99900000000001% (all weights are in %)
#11 of 58 assets processed has been added to the portfolio


which is the "correct" approach to calculate how much capital has to be allocated for each asset according to "long and short" optimized portfolio weights?.

weights = [-16.236, 42.662, 9.071, -3.043, -30.727, 11.649, 9.688
21.987, 6.123, 37.917, -12.818, -17.302, 3.501, 56.237, 8.001, 18.2,
-9.894, -4.824, -7.25, -1.315, 0.673, 37.075, 35.864, -9.306, -21.19
-53.798, -22.175, -41.449, -15.007, -12.847, -56.741, 19.637, 21.805
-4.066, 25.44, 27.779, 10.321, 4.372, 7.127, 10.733, 13.87, 16.277
-9.371, -4.053, -22.877, 1.631, 8.721, -24.908, -6.497, -16.44, -11.304
-2.084, 24.29, 23.836, 5.427, -11.143, 4.654, 24.099]

#Sum of all weights is equal to 100.0020000000000% (all weights are in %)
#58 of 58 assets processed has been added to the portfolio


Following the code for the optimization.

def negative_sharpe(weights, average_annual_return, covariance_matrix,
risk_free = 0.02):
mu = weights.dot(average_annual_return)
sigma = np.sqrt(np.dot(weights, np.dot(cov_matrice, weights.T)))
L2_reg = (weights ** 2).sum()
return -(mu - risk_free) / sigma + L2_reg

def optimized_tangency_portfolio(n_assets, risk_free = 0.02,
average_annual_return, covariance_matrix, short = False):

if short:
b = (-1, 1)
else:
b = (0, 1)

init = np.array([1 / n_assets] * n_assets)
constraints = [{"type": "eq", "fun": lambda x: np.sum(x) - 1}]
bounds = tuple(b for x in range(n_assets))
args = (average_annual_return, covariance_matrix, risk_free)

result = sco.minimize(negative_sharpe,
x0 = init,
args = args,
method = "SLSQP",
bounds = bounds,
constraints = constraints,
)
#Output is an array of weights
return result["x"]

• I don’t understand the weights your showing, what’s their relation? How did you obtain them? Why not run the optimization twice, once with shorting, once without? – Bob Jansen Jan 8 at 18:29
• Both the arrays of weights are the result of the same portfolio optimization function I written. The first array has been obtained by performing an portfolio optimization using time series data that consists in minimize the negative Sharpe ratio (same as maximize) considering the risk free rate (the result are weights for an optimal tangency portfolio). – Nipper Jan 8 at 18:56
• The second array has been obtain performing the same optimization function with the same time series data but with different bounds (not 0 to 1 but -1 to 1 thus allowing short selling) given as input to the optimization module (spicy.optimization.minimize). Hoping that this is a good way to calculate weights considering both long and short selling of assets I’d like to know how calculate the allocation given i.e 100000 capital. – Nipper Jan 8 at 18:56
• Your second array appears longer to me. However, given the information, your approach seems to work for both? – Bob Jansen Jan 8 at 19:27
• Sorry I do not understand exactly what you mean with "longer". I performed the two optimizations using the same time series data of circa 50 assets (stock only). Only 7 has been considered to obtain an optimized long only portfolio (43.1% annual return, 19.6% annual volatility, Sharpe-ratio 2.10 as I recall) and 18 has been considered to obtain an optimized long/short portfolio. Changing bounds from (0, 1) to (-1, 1) allows the optimization module to consider negatives weights in order to perform the minimizing of negative Sharp Ratio. – Nipper Jan 8 at 19:59