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
NaN,0or something else? What is thevariablethe 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