MATLAB Answers

DSK
0

Multiple Column Sorting and Calculation of Returns

Asked by DSK
on 12 Aug 2018
Latest activity Commented on by DSK
on 13 Aug 2018
Hi,
I am fairly new in Matlab and I have a question on how to sort a table.
I have a table of stocks for many years with returns, skewness and volatility, so it looks like this:
Stock 1 1989 Return Skewness Volatility
Stock 1 1990 Return Skewness Volatility
Stock 2 1989 Return Skewness Volatility
Stock 2 1990 Return Skewness Volatility
Stock 2 1991 Return Skewness Volatility
etc. (some stocks have more yearly observations than others).
I want to double sort all stocks on a yearly basis based on both skewness and volatility on 5 quantiles each. That means I want to end up with 25 portfolios, and then use Column 3 to calculate the return of each portfolio.
I don't really know what to do. Your help is great appreciated. Thanks.

  0 Comments

Sign in to comment.

1 Answer

Answer by KSSV
on 13 Aug 2018

fid = fopen('data.txt','r') ;
S = textscan(fid,'%s %f %f %f %f %f') ;
fclose(fid) ;
stock = S{2} ;
year = S{3} ;
return = S{4} ;
skewness = S{5} ;
volatility = S{6} ;
You can sort them the way you want now.

  3 Comments

Thanks for your answer! Could you please elaborate on that as I’m new in Matlab. I have already used sortrows(myTable, [2 4 5]) where 2 is the column of years, 3 is for skewness and 5 for volatility but it sorts only for years and skewness and omits volatility.
How can I get from the code you wrote to a code that will give me a 5x5 table of portfolio sorts each year?
Thanks for your help!
YOu need to work on specific column, which you want to sort.
Thank you for your help. I am still unsure how to proceed but I will see what I can do with your tips.
I appreciate your time and help. If you could provide any other tips or hints, I would be grateful.

Sign in to comment.