# CVX with 2 for loops

Hello,

Could you kindly help me with the following issue: Is there a more efficient way to formulate the problem below?
It involves two for loops and takes about 6h to run in matlab.

``````lambda = 0.001:0.001:0.02;     %pricing error

for j = 1:T-t    %rolling window over time
for k = 1:length(lambda)    %for each pricing error
cvx_begin quiet
variable weights(N)
minimize( sum_square_pos(data(j:t+j,: ) * weights) * 0.5 */T - sum(weights) + lambda(k)*norm(weights(3:end)) ;
cvx_end
weights_RW(j,k,: ) = weights;
dispersion_RW(j,k) = -cvx_optval;
end
end
``````

So I am basically trying to minimize the variance of a return portfolio (data*weights) over rolling windows of size t+j, for each value of the pricing error lambda, while penalizing some of the weights using an L2 norm.

Thank you!

How big is N? What is the total number of times CVX is called inside the double for loop? How much time is spect by the solver vs. time spent by CVX (how long until solver output starts if you remove the quiet option vs. time before solver output starts?)?

Hi Mark, thank you for your reply. N = 12. There are 170 rolling windows (for the first loop) and about 200 possible values for the pricing error (second loop). Without the loop, the CVX is solved in about a minute.

Running through the double for loop takes `~ (170*200 problems) *(1 minute per problem) =` more than 23 days vs. 6 hours to run per your first post. What am I misinterpreting?

Can you post a reproducible example, with input data, for a single CVX invocation?. I don’t see why it should take a minute to solve.