To calculate portfolio returns

2 次查看(过去 30 天)
Hi, I have a question regarding the matrix multiplication with NaN cells in the matrix.
The two matrix are
W1= [0.1 0 0.9; 0 1/3 2/3; 1/3 1/3 1/3;0.2 0.4 0.4];
r=[0.2 0.1 NaN; -1 0 NaN; NaN 0.2 0.2; NaN 0.1 0];
The rows represent the time series. What I need is to get the vector R(t) which is the multiplication of W1(t-1)*r(t). That is R(2)=W1(1)*r(2)=[0.1 0 0.9]*[-1 0 NaN]=-0.1, R(3)=W1(2)*r(3)=[0 1/3 2/3]*[NaN 0.2 0.2]=0.2 and vice verse .I did it in the following way but the results do not show up as expected....Note: treat the NaN values as zeros:
for t=1:size(W1,1)
R(t,1)=W1(t-1,:)*returnm(t,:)';
end
Thank you very much for your help. I am waiting for that.

采纳的回答

Guillaume
Guillaume 2015-11-16
If you want to treat the NaNs as zero, make them zero:
r(isnan(r)) = 0;
Your calculation is then simply
R = [NaN; sum(W1(1:end-1, :) .* r(2:end, :), 2)] %no loop needed
I've put NaN for R(1) since you haven't specified in your description what it should be. Personally, I would simply change the offsets of R (i.e. have R(1) = sum(W1(1) .* r(2)))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Portfolio Optimization and Asset Allocation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by