# Is my python solution good? : Global Minimum Variance portfolio with 'no-short sale' constraint

### Question

• Is my python code an answer (at least a close answer) to get the weight vector of the Global Minimum Variance portfolio problem? My codes are shown below after some explanations.

• The GMV with no-short sale constraint portfolio problem can be described as below :
• The answer to the problem if the short sales are allowed, can be calculated as below :

• According to the question 'Tangent portfolio weights without short sales?' from mathematics stack exchange, we do not have an analytical solution to the GMV problem with no short-sales constraints.

• My python code answer to this is simple ; Set the negative weights in $$W_{gmv}$$ coming out of the calculation above to 0, and with the rest positive weights, make them sum up to 1. The code is shown as below.

cov_df = stock_data_df.cov()
inverse_cov_df = np.linalg.pinv(cov_df)

numerator = np.matmul(np.ones(20).T, inverse_cov_df)
denominator = np.matmul(np.ones(20), (np.matmul(inverse_cov_df, np.ones(20))))
GMV_weight_vector = numerator / denominator

GMV_weight_vector[GMV_weight_vector < 0] = 0
GMV_weight_vector = GMV_weight_vector/(GMV_weight_vector.sum())

• The stock_data_df has 20 stocks' 252 day-long daily return.
• The last 2 line at the bottom is the line that suffice the 'no-short selling constraint'.
• I am curious to know if these 2 lines are good enough to be consider an answer to the GMV portfolio problem without short-selling constraint.

### Disclaimer

• Many python libraries such as Pyportfolioopt uses the scipy.minimize function to solve this problem of 'no short-selling constraint', but I am not allowed to use any solver in my assignment.