'Minimizing SSD in Python with observation level constraints

I'm trying to minimize the sum of squared difference between a vector of weights (which sum to 1), w and a new vector, z = a*w, given constraints on each value such that y1 <= z <= y2.

I'm used to minimization problems with a constraint for each variable, not varying constraints for each variable.

Is there an obvious solution I am missing? Below is a toy example, the real problem has about 215 observations.

w = np.array[0.01795054, 0.05355763, 0.16370357, 0.01856683, 0.05610746,
       0.05578166, 0.17662216, 0.32852952, 0.08550193, 0.04367869]

y2 = np.array[0.0856799 , 0.04886273, 0.16629066, 0.0598285 , 0.12070527,
       0.14514881, 0.17162558, 0.17162558, 0.15048137, 0.13325034]

y1 = np.array[0.01243839, 0.00860495, 0.01947604, 0.01355602, 0.0039714 ,
       0.00853402, 0.00686692, 0.01595278, 0.01759997, 0.01807684]


Solution 1:[1]

Not sure which part you are struggling with, but you basically only need to write down the optimization problem in the comments and solve it with scipy.optimize.minimize. Note that the constraint y1 <= a*w <= y2 is equivalent to the two constraints y2 - a*w >= 0 and -y1 + a*w >= 0:

from scipy.optimize import minimize

def objective(a):
    return np.sum((a*w - w)**2)

# constraints
constrs = [
    {'type': 'ineq', 'fun': lambda a:  y2 - a*w},
    {'type': 'ineq', 'fun': lambda a: -y1 + a*w}
]

# initial guess
x0 = np.ones(w.size)

# solve the problem: res.x contains your solution
res = minimize(objective, constraints=constrs, x0=x0)

Solution 2:[2]

I won't tell you where the bottleneck is, but I can tell you how to find bottlenecks in complex programs. The keyword is profiling. A profiler is an application that will run alongside your code and measure the execution times of each statement. Search online for python profiler.

The poor person's version would be debugging and guesstimating the execution times of statements or using print statements or a library for measuring execution times. Using a profiler is an important skill that's not that difficult to learn, though.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 joni
Solution 2 Andreas Hartmann