Issues using an efficient positivity constraint

1 次查看(过去 30 天)
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

采纳的回答

Thorsten
Thorsten 2014-11-24
编辑:Thorsten 2014-11-24
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)

更多回答(3 个)

Martin Pott
Martin Pott 2014-11-24
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 2014-11-24
编辑:Thorsten 2014-11-24
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 2014-11-26
Indeed, doing the calculations I see the first one is not correct and the second one is. Thank you.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by