I am looking for a method that chooses the weights that minimize the portfolio variance.
For example:
I have 3 assets; their returns are given in an array below:
import numpy as np
x = np.array([[0.2,-0.1,0.5,-0.2],[0, -0.9, 0.8, 0.2],[0.4,0.5,-0.3,-.01]])
I can weight them how I want to as long as sum of their weights adds to 1. I am looking for such weights that minimize variance of the portfolio.
Here are two examples of randomly chosen weights:
weight_1 = [0.3,0.3,0.4]
weighted_x_1 = [ele_x*ele_w for ele_x,ele_w in zip (x,weight_1)]
var_1 = np.var(sum(weighted_x_1))
weight_2 = [-0.2,0.4,0.8]
weighted_x_2 = [ele_x*ele_w for ele_x,ele_w in zip (x,weight_2)]
var_2 = np.var(sum(weighted_x_2))
Output:
>>> var_1
0.02351675000000001
>>> var_2
0.012071999999999999
The second way is better.
Is there a Python (or a Python library) method that could do this for me? If not any suggestions on what method I should use to do the above are welcome.
Thank You in Advance