given several vectors:
x1 = [3 4 6]
x2 = [2 8 1]
x3 = [5 5 4]
x4 = [6 2 1]
I wanna find weight w1, w2, w3 to each item, and get the weighted sum of each vector: yi = w1*i1 + w2*i2 + w3*i3
. for example, y1 = 3*w1 + 4*w2 + 6*w3
to make the variance of these values(y1, y2, y3, y4) to be minimized.
notice: w1, w2, w3 should > 0, and w1 + w2 + w3 = 1
I don't know what kind of problems it should be... and how to solve it in python or matlab?
My full solution can be viewed in PDF.
The trick is to put the vectors
x_i
as columns of a matrixX
.Then writing the problem becomes a Convex Problem with constrain of the solution to be on the Unit Simplex.
I solved it using Projected Sub Gradient Method.
I calculated the Gradient of the objective function and created a projection to the Unit Simplex.
Now all needed is to iterate them.
I validated my solution using CVX.
You can see the full code in StackOverflow Q44984132 (PDF is available as well).
You can start with building a loss function stating the variance and the constraints on
w
's. The mean ism = (1/4)*(y1 + y2 + y3 + y4)
. The variance is then(1/4)*((y1-m)^2 + (y2-m)^2 + (y3-m)^2 + (y4-m)^2)
and the constraint isa*(w1+w2+w3 - 1)
wherea
is the Lagrange multiplier. The problem looks like to me a convex optimisation with convex constraints since the loss function is quadratic with respect to target variables (w1,w2,w3) and the constraints are linear. You can look for projected gradient descent algorithms which respect to the constraints provided. Take a look to here http://www.ifp.illinois.edu/~angelia/L5_exist_optimality.pdf There are no straightforward analytic solutions to such kind of problems in general.I don't know much about optimization problem, but I get the idea of gradient descent so I tried to reduce the weight between the max score and min score, my script is below:
In my practice it really can reduce the variance. I am very glad but I don't know whether my solution is solid in math.
I think I maybe get the purpose of your problem.But if you want to find the smallest value, I hope this can help you. I just make the values fixed, you can make it to be the
def
when you see this is one way to solve your question.