Generate one plot containing the monthly log return time series?

1 view (last 30 days)
Hi, i´m pretty much a newbie in MATLAB and are struggling with how I can generate one plot containing the monthly log return time series of two different assets.
Does anyone know how i can proceed, and which code I could use?
Thank yoou so much in advance!

Accepted Answer

Jon
Jon on 30 Sep 2019
Edited: Jon on 30 Sep 2019
I assume your data with the log return values, or the values you will calculate them from, is in some sort of file, for example a .xlsx spreadsheet, or a .csv text file.
So you must first get that data into the Matlab workspace. Matlab provides alot of data import functions. A good starting point would be to check the documentation for readtable, and readtimetable. Just type doc readtable, or doc readtimetable on the command line and it will bring up the documenations.
Once you get the data into the workspace, you will have to assign some local variables with the relevant columns from the table that you imported. This link from the MATLAB documentation should get you going with that. https://www.mathworks.com/help/matlab/matlab_prog/create-a-table.html
Depending upon whether you have exactly what you want already in the table you may have to do some mathematical manipulation of the variables that you've assigned to caclulate the quantitites you want. I assume you know a little bit about doing basic math with MATLAB, but if not I suggest first completing the MATLAB On Ramp training (takes a couple of hours). https://www.mathworks.com/learn/tutorials/matlab-onramp.html
Finally once you get to the point that you have assigned some data you can plot it using the plot command. So, if for example, the times and values for the first and second asset respectively were now given by t1 and logReturn1 , and t2, logReturn2, (names are arbitrary, whatever you've assigned in your code), you could plot them with
plot(t1,logReturn1, t2,logReturn2)
Again, you can type doc plot on the command line to get documentation on the plot command.
After you've gone through all of the above, and you have written some code, if you are still stuck (errors that you can't understand or whatever) please attach your code, using the code button on the MATLAB answers toolbar, and copy and paste the errors you are getting, and ask for further assistance.
  5 Comments
Jon
Jon on 1 Oct 2019
I'm sorry, I hoped maybe I could see something obvious but I don't know how you've defined/assigned the variables Y and YLag are so I'm not sure what is causing the problem. If it is not too long, maybe you could include your whole script, or better a small self contained part of it that demonstrates the problem.
Jon
Jon on 1 Oct 2019
Edited: Jon on 1 Oct 2019
Hi I saw your answer with some more of your code.
First, just so that it's easier for people to follow the thread, it is better to put that kind of thing as a comment to my answer. We should save answers, for people who are providing a new answer to the original question.
I don't have the function lagmatrix in my Matlab version, maybe it comes from some toolbox that I don't have. So I'm not sure what arguments it expects.
I would suspect however that the problem goes back further. Perhaps when you run
TBILLmonthly=readtable('TBILLmonthly.xlsx')
The table it retuns is somehow a little different than the one you read for the CRSP monthly. Maybe the formatting in the two original .xlsx files is a little different. I would check that first to see if you are really importing the data correctly in the second case. You can look at the imported table in the "workspace" tab in your matlab window, or even more directly, if it isn't too big you can just display it by typing TBillmonthly on the command line after you run your script. Compare it to CRSPmonthly. See if somehow one of the columns has a cell array rather than numerical values or something like that. Also look at the two original .xlsx files to see what differences there are in how the data is arranged of formatted.
One other thing I noticed in your second call to lagmatrix your second argument is a 2 and in the first call it was a 1. I'm not sure if that makes a difference.
When you paste your code you can use the code button in the Answers toolbar and it comes out nicely formatted.

Sign in to comment.

More Answers (1)

Therese Haugstulen
Therese Haugstulen on 1 Oct 2019
At first I computed the CRSPs log return
CRSPmonthly=readtable('CRSPmonthly.xlsx')
CRSPmonthly(1:97,:) = [];
CRSPmonthly.Properties.VariableNames {1} = 'Time';
CRSPmonthly.Properties.VariableNames {2} = 'Index';
CRSP_monthly=2*log(CRSPmonthly.Index./ lagmatrix(CRSPmonthly.Index,1));
And then I tried to compute the log return to the Treasure Bill as well, and made it this far
TBILLmonthly=readtable('TBILLmonthly.xlsx')
TBILLmonthly(1:6,:) = [];
TBILLmonthly(1021:end,:) = [];
TBILLmonthly.Properties.VariableNames {1} = 'Time';
TBILLmonthly.Properties.VariableNames {2} = 'Index';
But when I added the code to compute the log return (the same code I used for the CRSP):
TBILL_monthly=2*log(TBILLmonthly.Index./ lagmatrix(TBILLmonthly.Index,2));
I got the error message
Conversion to double from cell is not possible.
Error in lagmatrix (line 92)
YLag((L + 1):end,columns) = Y(1:(end - L), :);
So it seems the problem occurred when I tried to compute the same log return as for the previous stock. So I´m not sure if I need a different code to compute 2 seperate log returns rather than one (or compute them in one and same code), or if I´m simply doing it all wrong..

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!