2
votes

This is the formula for Sortino Ratio in Matlab:

Sortino = (mean(Data) - MAR) / sqrt(lpm(Data, MAR, 2))

Can someone please advise how to run this equation on every single variable in my Matlab dataset (one after the other)?

My Matlab dataset is as follows:

I have approx. 1400 variables in columns, the number of data points depends on a variable. Thus, not all of them are the same (i.e. returns for various stocks).

Also, is it possible to get the output (Sortino's for all variables) in a Table?

PS. I had a look at various loop suggestions but could not understand a single thing. They are nothing like VBA or STATA.

Data Format (with NaN as empty cells):

enter image description here

Thank you West

1
Could you please show a bit of your data (not all 1400, but just two or three). I do not really understand the structure of the data. - Patrick Happel
The data is in columns. Just like in the Yahoo Finance e.g. FTSE100 download. - Westra
I have never heard about this dataset and I'm not willing to google it. You need help on how to loop through your data. Thus, include an examplary snippet of your data. If you expect help, you should provide all necessary information. Reading this might help. - Patrick Happel
I'm not sure which part of a "column" with numbers you don't understand. It's beyond explanation, really. - Westra
It's beyond explanation, really. Are the missing data in your columns NaN, 0 or something else? What is the variable the number of data points depends on? Is this variable used to select a subset of data points from your columns or are the data in your columns already adapted? As you see, everything is self-explaining. - Patrick Happel

1 Answers

0
votes

Okay, I'll give it a try.

I'll use the data as shown in the docs

First, load it (you don't need to, you have your data already), but I haven't:

load FundMarketCash 
Returns = tick2ret(TestData);

Since you have some NaNs in you data, lets replace some values with NaNs:

Returns(end-8:end, 1) = NaN;
Returns(end-5:end, 2) = NaN;

As in the docs, let's use the mean of the third column (withou NaNs) as MAR:

MAR = mean(Returns(:,3));

No, let's try to compute the nominator of the Sortino ratio. Since MATLAB can operate on matrices, there is no need for a loop.

>> mean(Returns) - MAR

ans =

   NaN   NaN     0

Huh? The mean of the columns containing NaN is NaN. So, your true question might be "How to compute the mean of a column that contains NaNs?" The answer to this question would be "Use nanmean".

>> nanmean(Returns) - MAR

ans =

    0.0019   -0.0002         0

Let's further check if lpm can deal NaNs by checking the first two columns:

% Copy columns to col1 and col2 for logical indexing
col1 = Returns(:,1);
col2 = Returns(:,2);

Test col1 with NaNs

>> lpm(col1, MAR, 0:2)
 ans =

    0.4314
    0.0084
    0.0003

and without NaNs:

>> lpm(col1(~isnan(col1)), MAR, 0:2)

ans =

    0.4314
    0.0084
    0.0003

Seems to be the same. Lets repeat it for col2:

>> lpm(col2, MAR, 0:2)

ans =

    0.4444
    0.0155
    0.0009

>> lpm(col2(~isnan(col2)), MAR, 0:2)

ans =

    0.4444
    0.0155
    0.0009

Also the same. Thus, let's assume lpm can handle NaNs.

So, you can simply compute the Sortino ratio in a one-liner:

Sortino = (nanmean(Returns) - MAR) ./ sqrt(lpm(Returns, MAR, 2))

Sortino =

    0.1077   -0.0053         0