필터 지우기
필터 지우기

Issues using an efficient positivity constraint

조회 수: 1 (최근 30일)
Martin Pott
Martin Pott 2014년 11월 24일
답변: Martin Pott 2014년 11월 26일
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