Issues using an efficient positivity constraint

3 vues (au cours des 30 derniers jours)
Martin Pott
Martin Pott le 24 Nov 2014
I want to use the following command to obtain only values of 0 or higher for the values in the 600x495 weight matrix p_w:
for j = 1:size(p_w,1)
for i = 1:size(p_w,2)
p_w(j,i) = max(0,p_w(j,i))/nansum(max(0,p_w(j,:)));
end
end
However, when running the optimization it takes a very long time (probably because of the loops), so I have tried to get rid of the loops and came up with the following:
temp = sum(max(0,p_w),2);
p_w_plus = max(0,p_w)./repmat(temp, 1, 495);
However, the optimized values in this case are very different from the function with the loops ,and they are also so extreme that they seem erroneous, so I am probably making a mistake somewhere in transforming the code.
Hopefully someone can tell me what the mistake is or suggest another way to make the first code more efficient.
Thanks in advance,
Martin

Réponse acceptée

Thorsten
Thorsten le 24 Nov 2014
Modifié(e) : Thorsten le 24 Nov 2014
I see. Than you can use your second code or (only one max, probably faster):
temp = max(0,p_w);
p_w_plus = temp./repmat(sum(temp, 2), 1, size(p_w, 2));
Your first solution with the loops is wrong, as I have explained in my first answer. You can check this using
sum(p_w, 2)

Plus de réponses (3)

Martin Pott
Martin Pott le 24 Nov 2014
Dear Thorsten,
I did not make this very clear in my question, sorry. The matrix consist of weights within a portfolio construction setting, both with a time-series dimension (j) and a cross-sectional dimension (i). So each cell is the weight at time j for stock i.
If the values of p_w are negative, it means that it is actually optimal to have a negative weight for that stock at that time. However, it is not (legally) allowed for all investors to hold negative weights in stocks. Therefore, I would like to replace negative weights with zero (the closest non-negative value). So far, no problem.
However, Because the weights should sum to one, the sum of each row (each time period) should also be one. Therefore, I also need to take the sum of the positive weights at time j, in order to make sure that the division is done right.

Thorsten
Thorsten le 24 Nov 2014
Modifié(e) : Thorsten le 24 Nov 2014
To obtain values of 0 or higher, of course you can simply use
p_w = abs(p_w);
or
p_w = max(0, p_w);
Obviously to try to meet another constrain, maybe that the rows contain only positive values that sum to 1? You can achieve this with your second code. The reason why this "optimized" code gives different results than your loop is that you change p_w within the loop, so nansum(max(0, p_w(j,:)) will give different results depending on i.
So far I am not sure what additional constrains your p_w has to meet. Please expand on that.

Martin Pott
Martin Pott le 26 Nov 2014
Indeed, doing the calculations I see the first one is not correct and the second one is. Thank you.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by