Given a value $y$ and some values $x_1, ..., x_n$. How do I find weights $w_1, ... w_n$ so that the $error = y - w_1*x_1 + ... w_n*x_n$ is minimal, where the weights have to sum up to 1 or a different value?


2 Answers 2


The name of the model you are looking for is: Constrained Estimation of Ordinary Least Squares, this model allows you to introduce linear constrains to your estimation by just modifying the matrix expression of the OLS estimation.

The resulting model should satisfy the expression

$$Q^T\beta = c$$

It is solved with the equation: $$\hat{\beta^c} = \hat{\beta}-(X^TX)^{-1}Q(Q^T(X^TX)^{-1}Q)^{-1}(Q^T\hat{\beta}-c)$$

For your problem, $Q=[1,1,1]$, $c=1$, $X$ and $Y$ are the matrixes for your data.


This problem is called constrained optimization, where your constraint is that sum of the weights is x (with for example x=1).

It would probably be insightful, and not too complex, to write your own implementation of gradient descent, that respects this constraint. To do this, you would scale the weight vector (w) after each update so that it sums to x.

Another possibility is to use an existing implementation of constrained optimization, such as in scipy. I think that the constraint you are asking for is as follows:

from scipy.optimize import LinearConstraint
linear_constraint = LinearConstraint([[1] * n], [x], [x])

Where n is the number of weights, and x is the length that you want to constrain w to.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.