%%
%% Compute Maximum Drawdown
% This example shows how to compute the maximum drawdown (MaxDD) using example data with a fund, a market, and a cash series.

load FundMarketCash
MaxDD = maxdrawdown(TestData)

% The maximum drop in the given time period was 16.58% for the fund series, and 33.81% for the market. There was no decline in the cash series, as expected, because the cash account never loses value.
% maxdrawdown also returns the indices (MaxDDIndex) of the maximum drawdown intervals for each series in an optional output argument.

[MaxDD, MaxDDIndex] = maxdrawdown(TestData)

% The first two series experience their maximum drawdowns from the second to the 18th month in the data. The indices for the third series are NaNs because it never has a drawdown.
% The 16.58% value loss from month 2 to month 18 for the fund series is verified using the reported indices.

Start = MaxDDIndex(1,:);
End = MaxDDIndex(2,:);
(TestData(Start(1),1) - TestData(End(1),1))/TestData(Start(1),1)

% Although the maximum drawdown is measured in terms of returns, maxdrawdown can measure the drawdown in terms of absolute drop in value, or in terms of log-returns. To contrast these alternatives more clearly, you can work with the fund series, assuming an initial investment of 50 dollars:

Fund50 = 50*TestData(:,1);
plot(Fund50);
title('\bfFive-Year Fund Performance, Initial Investment 50 usd');
xlabel('Months');
ylabel('Value of Investment');

% First, compute the standard maximum drawdown, which coincides with the results above because returns are independent of the initial amounts invested.

MaxDD50Ret = maxdrawdown(Fund50)

% Next, compute the maximum drop in value, using the arithmetic argument.

[MaxDD50Arith, Ind50Arith] = maxdrawdown(Fund50,'arithmetic')

% The value of this investment was $50.84 in month 2, but by month 18 the value was down to $42.41, a drop of $8.43. This is the largest loss in dollar value from a previous high in the given time period. In this case, the maximum drawdown period, from the 2nd to 18th month, is the same independently of whether drawdown is measured as return or as dollar value loss.

[MaxDD50LogRet, Ind50LogRet] = maxdrawdown(Fund50,'geometric')

% Note, the last measure is equivalent to finding the arithmetic maximum drawdown for the log of the series.

MaxDD50LogRet2 = maxdrawdown(log(Fund50),'arithmetic')

% Copyright 2020 The MathWorks, Inc.