classdef Statistics < handle
% This source file is subject to version 3 of the GPL license,
% that is bundled with this package in the file LICENSE, and is
% available online at http://www.gnu.org/licenses/gpl.txt
%
% This source file can be linked to GPL-incompatible facilities,
% produced or made available by MathWorks, Inc.
properties
end
methods (Static)
function ret = Beta(Assets, Benchmark)
% Equity Beta Statistics
% beta = cov(Asset,Benchmark)/var(Benchmark)
ret = NaN;
% Check if Assets and Benchmark series cover the same period
aCoverage = ftsbound(Assets);
bCoverage = ftsbound(Benchmark);
if ((aCoverage(1) ~= bCoverage(1)) || (aCoverage(2) ~= bCoverage(2)))
return;
end
% Check if benchmark has only one series
if (size(Benchmark,2) ~= 1)
return;
end
% Combine Assets and Benchmark, convert to return matrix and
% calculate covariance matrix
retSeries = cov(fts2mat(tick2ret([ Assets Benchmark ])));
% Calculate beta from cov matrix
ret = retSeries(size(retSeries,1),1:end-1)'/retSeries(size(retSeries,1),end);
end
function [ ret, period, MaxDD ] = DownsideCorr(Assets, Benchmark, beta)
% The function determines maximum drawdown for the period,
% than calculates correlation coefficients/betas between assets and
% benchmark. The output is either a vector of correlation
% coefficients, or a vector of betas, along with a vector of start/end dates
% of the max drawdown period, and percentage of the drawdown.
ret = NaN;
period = NaN;
MaxDD = NaN;
% If 'beta' is not given, assume beta = false
if (nargin <=2)
beta = false;
end
% Check if Assets and Benchmark series cover the same period
aCoverage = ftsbound(Assets);
bCoverage = ftsbound(Benchmark);
if ((aCoverage(1) ~= bCoverage(1)) || (aCoverage(2) ~= bCoverage(2)))
return;
end
% Check if benchmark has only one series
if (size(Benchmark,2) ~= 1)
return;
end
% Determine maxdrawdown for benchamrk, as well as start/end
% dates.
[MaxDD, MaxDDIndex] = maxdrawdown(fts2mat(Benchmark));
if (MaxDD == 0)
return;
end
% Establish start/end dates of the max drawdown period.
startDate = Benchmark.dates(MaxDDIndex(1));
endDate = Benchmark.dates(MaxDDIndex(2));
maxDDPeriod = sprintf('%s::%s', datestr(startDate,2), datestr(endDate,2));
% If beta is set to true, calculate and return a vector of
% betas. Calculate and return a vector of correlation
% coefficients otherwise.
if (beta == true)
ret = FM.Statistics.Beta(Assets(maxDDPeriod), Benchmark(maxDDPeriod));
else
retSeries = corrcoef(fts2mat([ Assets(maxDDPeriod) Benchmark(maxDDPeriod) ]));
ret = retSeries(size(retSeries,1),1:end-1)';
end
period = [ startDate, endDate]';
end
end
end