Code covered by the BSD License  

Highlights from
FinMetrics

  • fm(varargin) This source file is subject to version 3 of the GPL license,
  • AssetThis source file is subject to version 3 of the GPL license,
  • AssetUniverseThis source file is subject to version 3 of the GPL license,
  • AxlThis source file is subject to version 3 of the GPL license,
  • CashPositionThis source file is subject to version 3 of the GPL license,
  • CashTransactionThis source file is subject to version 3 of the GPL license,
  • Config This source file is subject to version 3 of the GPL license,
  • ConsoleMenuThis source file is subject to version 3 of the GPL license,
  • ConsoleMenuItemThis source file is subject to version 3 of the GPL license,
  • CurrencyAssetThis source file is subject to version 3 of the GPL license,
  • ExchangeTradedAssetThis source file is subject to version 3 of the GPL license,
  • FinMetricsThis source file is subject to version 3 of the GPL license,
  • PortfolioThis source file is subject to version 3 of the GPL license,
  • PositionThis source file is subject to version 3 of the GPL license,
  • StatisticsThis source file is subject to version 3 of the GPL license,
  • StockPositionThis source file is subject to version 3 of the GPL license,
  • StockTransactionThis source file is subject to version 3 of the GPL license,
  • TextUIThis source file is subject to version 3 of the GPL license,
  • TextUILocaleThis source file is subject to version 3 of the GPL license,
  • TextUIQuestionThis source file is subject to version 3 of the GPL license,
  • TextUIQuestionnaireThis source file is subject to version 3 of the GPL license,
  • TransactionThis source file is subject to version 3 of the GPL license,
  • View all files

FinMetrics

by

 

Open source/open architecture quantitative portfolio management environment.

Statistics
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

Contact us