Resetting original order after sorting out NaN values in the matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hey everybody!
I have some problems regarding portfolio optimization and the extraction of weights. My problem can be summarized as fighting with NaNs constantly:
I have a matrix full of returns according to the following:
Asset1 Asset2 Asset3 .... Asset1000
1960_01 0.01 NaN NaN
1960_02 0.015 NaN 0.02
1960_03 -0.02 NaN 0.1
...
2014_12
Now, some assets have many NaNs so I have written a code to keep only those Assets where I have all returns during the time period:
testifnan = all(~isnan(returns)); %this command returns a 1 if there are only NaNs in the column
new_ret = returns(:,testifnan); %returns a matrix with returns of stocks that do not have any NaN
Now, what I receive is a new matrix that is not a 660x1000 matrix as before, but only a 660x200 (800 stocks have only NaNs)
I do all this on a rolling basis, so after running the optimization code, I receive the set of optimal weights. I want these weights (of one optimization) in a 1x1000 matrix with a weight of zero if the asset had only NaNs in that time period.
Now, I created a matrix of zeros: weights = zeros(1,1000) and I want to update the weights in the original order..
I am really struggling with this and hope that somebody can help me out! Also, let me know whether you need more info!
Thank you so much!
Clemens
1 comentario
the cyclist
el 10 de Jul. de 2015
Just an FYI, you could also have done the NaN test in a slightly easier way:
testifnan = any(isnan(returns))
Respuestas (1)
the cyclist
el 10 de Jul. de 2015
Use the index you created on the left-hand side:
weights(not(testifnan),:) = <...>
0 comentarios
Ver también
Categorías
Más información sobre Quadratic Programming and Cone Programming en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!