To calculate portfolio returns
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Qian cao
le 16 Nov 2015
Réponse apportée : Guillaume
le 16 Nov 2015
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.
0 commentaires
Réponse acceptée
Guillaume
le 16 Nov 2015
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Portfolio Optimization and Asset Allocation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!