Quantitative Analysis, Risk Management, Modelling, Algo Trading, and Big Data Analysis

Visualisation of N-Asset Portfolio in Matlab

Many of you who wished to use the Matlab’s Financial Toolbox for portfolio visualization most probably realised that something was wrong. Not every time the annualised risk and return for different time-series matched with what was expected. Below you can find an improved piece of code which does the job correctly.

The input sampling of your financial time-series now can be specified directly as daily, weekly, monthly, or quarterly and the risk-return diagram will be displayed with annualised results. Otherwise, you have an option, instead of annualised value, specify another time-horizon. All smooth and easy.

% Specialised plotting function for visualisation of portfolios
% based on the Financial Toolbox's portfolio objects
% 
% Modified by Pawel Lachowicz, 2014/03/01
%
% Remarks: Essential corrections introduced in the calculation and plotting
% of the annualised risk and returns coded incorrectly by the team at 
% The MathWorks, Inc.
 
function plotportfolio(varargin)
 
    plottitle = varargin{1};  % plot title, e.g. 'Efficient Frontier'
    plotfreq =  varargin{2};  % frequency of raw data (dt; or 'daily', etc.)
    plotfreq2=  varargin{3};  % e.g. ’annualised’
    plotfreq3=  varargin{4};
    plotlegend = [];
 
    switch plotfreq2
        case 'annualised'
            switch plotfreq
                case 'daily'
                    periods=252;
                case 'weekly'
                    periods=52;
                case 'monthly'
                    periods=12;
                case 'quarterly'
                    periods=4;
            end
        otherwise
            periods=plotfreq3;
    end
 
    for i = 5:nargin
        plotinfo = varargin{i};
 
        plottype = plotinfo{1};
        plotrsk  = plotinfo{2};
        plotret  = plotinfo{3};
        if numel(plotinfo) > 3
            plotlabel = plotinfo{4};
        else
            plotlabel = [];
        end
        if numel(plotinfo) > 4
            plotstyle = plotinfo{5};
            if isempty(plotstyle)
                plotstyle = 'b';
            end
        else
            if strcmpi(plottype,'line')
                plotstyle = 'b';
            else
                plotstyle = 'g';
            end
        end
        if numel(plotinfo) > 5
            plotline = plotinfo{6};
            if isempty(plotline)
                plotline = 1;
            end
        else
            if strcmpi(plottype,'line')
                plotline = 2;
            else
                plotline = [];
            end
        end
 
        % line plot
        if strcmpi(plottype,'line')
            hold on;
            for k = 1:size(plotrsk,2)
                plot(plotrsk(:,k)*sqrt(periods),((plotret(:,k)+1).^periods-1), ...
                     plotstyle, 'LineWidth', plotline);
                if i == 2 && k == 1
                    hold on
                end
                if ~isempty(plotlabel) && ~isempty(plotlabel{k})
                    plotlegend = [ plotlegend, {plotlabel{k}} ];
                end
            end
 
        % scatter plot
        else
            if any(plotstyle == '.')
                scatter(plotrsk*sqrt(periods),((plotret(:,k)+1).^periods-1),'or','Filled');
            else
                scatter(plotrsk*sqrt(periods),((plotret(:,k)+1).^periods-1),'og','Filled');
            end			
            if i==2
                hold on
            end
            if ~isempty(plotlabel)
                for k = 1:numel(plotrsk)
                    if ~isempty(plotlabel{k})
                        text(plotrsk(k)*sqrt(periods)+0.005, ...
                            ((plotret(k)+1).^periods-1), plotlabel{k},'FontSize', 9);
                    end
                end
            end
        end
    end
 
    if ~isempty(plotlegend)
        legend(plotlegend,'Location','SouthEast');
    end
    if(plotfreq2=='annualised')
        xlabel('Annualised Volatility');
        ylabel('Annualised Expected Returns');
    else
        xlabel('Volatility');
        ylabel('Returns');
    end
    grid on
 
end

Now, you can call it with various parameters of your interest, matching the data directly, for instance:

plotportfolio('Portfolio',dt,'annualised',[], ... 
    {'line', prsk, pret}, ...
    {'scatter',CashRsk,CashRet, {'Cash'}}, ...
    {'scatter', sqrt(diag(p.AssetCovar)), p.AssetMean, p.AssetList,'.r'});

For some awesome practical application of this function in the process of construction and analysis of $N$-asset portfolio grab Applied Portfolio Optimization with Risk Management using Matlab ebook today.

Information Ratio and its Relative Strength for Portfolio Managers

The risk and return are the two most significant quantities investigated within the investment performance. We expect the return to be highest at the minimum risk. In practice, a set of various combinations is always achieved depending on a number of factors related to the investment strategies and market behaviours, respectively. The portfolio manager or investment analyst is interested in the application of most relevant tool in order to described the overall performance. The arsenal is rich but we need to know the character of outcomes we wish to underline prior to fetching the proper instruments from the workshop.


In general there are two schools of investment performance. The first one is strictly related to the analysis of time-series on the tick-by-tick basis. All I mean by tick is a time scale of your data, e.g. minutes or months. The analyst is concerned about changes in investments and risk tracked continuously. A good example of this sort of approach is measurement of Value-at-Risk and Expected Shortfall, Below Target Risk or higher-moments of Coskewness and Cokurtosis. It all supplements the n-Asset Portfolio Construction with Efficient Frontier diagnostic.

The second school in the investment analysis is a macro-perspective. It includes the computation on $n$-period rolling median returns (or corresponding quantities), preferably annualized for the sake of comparison. This approach carries more meaning behind the lines rather than a pure tick time-series analysis. For the former, we track a sliding changes in accumulation and performance while for the latter we focus more closely on tick-by-tick risks and returns. For the sake of global picture and performance (market-oriented vantage point), a rolling investigation of risk and return reveals mutual tracking factors between different considered scenarios.

In this article I scratch the surface of the benchmark-oriented analysis with a special focus on the Information Ratio (IR) as an investment performance-related measure. Based on the market data, I test the IR in action and introduce a new supplementary measure of Information Ratio Relative Strength working as a second dimension for IR results.

1. Benchmark-oriented Analysis

Let’s assume you a portfolio manager and you manage over last 10 years 11 investment strategies. Each strategy is different, corresponds to different markets, style of investing, and asset allocation. You wish to get a macro-picture how well your investment performed and are they beating the benchmark. You collect a set of 120 monthly returns for each investment strategy plus their benchmarks. The easiest way to kick off the comparison process is the calculation of $n$-period median (or mean) returns.

Annualized Returns

The rolling $n$Y ($n$-Year) median return is defined as:
$$
\mbox{rR}_{j}(n\mbox{Y}) = \left[ \prod_{i=1}^{12n} (r_{i,j}+1)^{1/n} \right] -1
\ \ \ \ \ \mbox{for}\ \ \ j=12n,…,N\ \ (\mbox{at}\ \Delta j =1)
$$
where $r$ are the monthly median returns meeting the initial data selection criteria (if any) and $N$ denotes the total number of data available (in our case, $N=120$).

Any pre-selection (pre-filtering) of the input data becomes more insightful when contrasted with the benchmark performance. Thereinafter, by the benchmark we can assume a relative measure we will refer to (e.g. S\&P500 Index for US stocks, etc.) calculated as the rolling $n$Y median. Therefore, for instance, the 1Y (rolling) returns can be compared against the corresponding benchmark (index). The selection of a good/proper benchmark belongs to us.

Active Returns

We define active returns on the monthly basis as the difference between actual returns and benchmark returns,
$$
a_i = r_i – b_i ,
$$
as available at $i$ where $i$ denotes a continuous counting of months over past 120 months.

Annualized Tracking Risk (Tracking Error)

We assume the widely accepted definition of the tracking risk as the standard deviation of the active returns. For the sake of comparison, we transform it the form of the annualized tracking risk in the following way:
$$
\mbox{TR}_{j}(n\mbox{Y}) = \sqrt{12} \times \sqrt{ \frac{1}{12n-1} \sum_{i=1}^{12n} \left( a_{i,j}-\langle a_{i,j} \rangle \right)^2 }
$$
where the square root of 12 secures a proper normalization between calculations and $j=12n,…,N$.

Annualized Information Ratio (IR)

The information ratio is often referred to as a variation or generalised version of Sharpe ratio. It tells us how much excess return ($a_i$) is generated from the amount of excess risk taken relative to the benchmark. We calculate the annualized information ratio by dividing the annualised active returns by the annualised tracking risk as follows:
$$
\mbox{IR}_j(n\mbox{Y}) = \frac{ \left[ \prod_{i=1}^{12n} (a_{i,j}+1)^{1/n} \right] -1 } { TR_j(n\mbox{Y}) }
\ \ \ \ \ \mbox{for}\ \ \ j=12n,…,N
$$

We assume the following definition of the investment under-performance and out-performance, $P$, as defined based on IR:
$$
P_j =
\begin{cases}
P_j^{-} =\mbox{under-performance}, & \text{if } \mbox{IR}_j(n\mbox{Y}) \le 0 \\
P_j^{+} =\mbox{out-performance}, & \text{if } \mbox{IR}_j(n\mbox{Y}) > 0 .
\end{cases}
$$

Information Ratio Relative Strength of Performance

Since $P_j$ marks in time only those periods of time (months) where a given quantity under investigation achieved better results than the benchmark or not, it does not tell us anything about the importance (weight; strength) of this under/over-performance. The case could be, for instance, to observe the out-performance for 65\% of time over 10 years but barely beating the benchmark in the same period. Therefore, we introduce an additional measure, namely, the IR relative strength defined as:
$$
\mbox{IRS} = \frac{ \int P^{+} dt } { \left| \int P^{-} dt \right| } .
$$
The IRS measures the area under $\mbox{IR}_j(n\mbox{Y})$ function when $\mbox{IR}_j(n\mbox{Y})$ is positive (out-performance) relative to the area cofined between $\mbox{IR}_j(n\mbox{Y})$ function and $\mbox{IR}_j(n\mbox{Y})=0$. Thanks to that measure, we are able to estimate the revelance of
$$
\frac { n(P^{+}) } { n(P^{-}) + n(P^{+}) }
$$
ratio, i.e. the fraction of time when the examined quantity displayed out-performance over specified period of time (e.g. 10 years of data within our analysis). Here, $n(P^{+})$ counts number of $P_j^{+}$ instances.

2. Case Study

Let’s demonstrate the aforementioned theory in practice. Making use of real data, we perform a benchmark-oriented analysis for all 11 investment strategies. We derive 1Y rolling median returns supplemented by the correspoding 1Y tracking risk and information ratio measures. The following figure presents the results for 1Y returns of for the Balanced Options:

ir-fig01
Interestingly to notice, the Investment Strategy-2 (Inv-2) displays an increasing tracking risk between mid-2007 and mid-2009 corresponding to the Credit Global Financial Crisis (GFC). This consistent surge in TR is explained by worse than average performance of the individual options, most probably dictated by higher than expected volatility of assets within portfolio. The 1Y returns of Inv-2 were able to beat the benchmark over only 25% of time, i.e. about 2.25 years since Jun/30 2004. We derive that conclusion based on the quantitative inspection of the Information Ratio plot (bottom panel).

In order to understand the overall performance for all eleven investment strategies, we allow ourselves to compute the under-performance and out-performance ratios,
$$
\frac { n(P^{-}) } { n(P^{-}) + n(P^{+}) } \ \ \ \mbox{and} \ \ \ \frac { n(P^{+}) } { n(P^{-}) + n(P^{+}) } ,
$$
and plot these ratios in red and blue colour in the following figure, respectively:

ir-fig02
Only two investment strategies, Inv-7 and Inv-11, managed to keep the out-performance time longer than their under-performance. Inv-8 denoted the highest ratio of under-performance to out-performance but also Inv-2 (consider above)performed equally badly.

The 3rd dimension to this picture delivers the investigation of the IR Relative Strength measure for all investment strategies. We derive and display the results in next figure:
ir-fig03
The plot reveals close-to-linear correlation between two considered quantities. Inv-7 and Inv-11 strategies confirm their relative strength of out-performance while Inv-2 and Inv-8 their weakness in delivering strong returns over their short period of out-performance.

It is of great interest to compare Inv-3, Inv-4, and Inv-10 strategies. While relative strength of out-performance of Inv-4 remains lower as contrasted with the latter ones (and can be explained by low volatility within this sort of investment strategy), Inv-10 denotes much firmer gains over benchmark than Inv-3 despite the fact that their out-performance period was nearly the same ($\sim$40%). Only the additional cross-correlation of IR Relative Strength outcomes as a function of the average return year-over-year is able to address the drivers of this observed relation.

Discuss this topic on QaR Forum

You can discuss the current post with other quants within a new Quant At Risk Forum.

Create a Portfolio of Stocks based on Google Finance Data fed by Quandl

There is an old saying Be careful what you wish for. Many quants and traders wished for a long time for a better quality of publicly available data. It wasn’t an easy task to accomplish but with a minimum of effort, it has become possible to get all what you truly dreamt of. When I started my experience with daily backtesting of my algo strategies I initially relied on Yahoo! data. Yeah, easily accessible but of low quality. Since the inception of Google Finance the same dream had been reborn. More precise data, covering more aspects of trading, platforms, markets, instruments. But how to get them all?


Quandl.com provides us with an enormous access to an enormous database. Just visit the webpage to find out what I mean by barley putting my thoughts all together in one sentence. In fact, the portal’s accessibility via different programming languages (e.g. Matlab, C++, Python, etc.) makes that database more alive than ever. The space of parameters the quants get an access to becomes astronomical in number!

Alleluja! Let’s ride this horse. Enough of watching the online charts changing a direction of our investing moods with a one-mintute time resolution. We were born to be smarter than stochastic processes taking control over our daily strategies. In this short post, making use of the Matlab R2013a platform, I want to show you how quickly you can construct any portfolio of US stocks taken from the Dow Jones Index universe (as an example). We will use the Google Finance stock data fed by Quandl.com.

Google Finance Stock Data for Model Feeding

Similar to Yahoo! Finance’s resources, Quandl.com has an own system of coding the names of all US stocks. Therefore, we need to know it before doing any further step. The .csv file containing all of the codes for Quandl’s stock data can be obtained via API for Stock Data webpage. The file contains the header of the form:

Ticker      Stock Name      Price Code      Ratios Code      In Market?

linking Google Finance ticker with Quandl’s code (Price Code). Doing some extra job with data filtering, I created the pre-processed set of all tickers excluding those non-active in the markets and removing all invalid data. You can download it here QuandlStockCodeListUS.xlsx as an Excel workbook.

The second list we need to have is a list of potential stock we want to deal with in our portfolio. As for today, the current list of Dow Jones Index’s components you can download here: dowjones.lst. Let’s start with it:

1
2
3
4
5
6
7
8
9
10
11
12
13
% Create a Portfolio from Dow Jones Stocks based on Google Finance
%  Data fed by Quandl.com
%
% (c) 2013 QuantAtRisk.com, by Pawel Lachowicz
 
 
close all; clear all; clc;
 
% Read the list of Dow Jones components
fileID = fopen('dowjones.lst');
tmp = textscan(fileID, '%s');
fclose(fileID);
dowjc=tmp{1};  % a list as a cell array

The guys from Mathworks suggest to make a new and good practice of using textscan command (lines 10-12) instead of dataread for reading data from the text files. The latter will be removed from the next Matlab release.

Next, we need to import the current list of tickers for US stocks and their corresponding codes in the Quandl’s database. We will make the use of my pre-processed data set of QuandlStockCodeListUS.xlsx as mentioned earlier in the following way:

15
16
17
% Read in the list of tickers and internal code from Quandl.com
[ndata, text, alldata] = xlsread('QuandlStockCodeListUS.xlsx');
quandlc=text(:,1); % again, as a list in a cell array

For the simplicity of our example, we will be considering all 30 stocks as a complete sample set of stocks in our portfolio. A portfolio construction is not solely a selection of stocks. It usually links a time period of the past performance of each individual stock. In the following move, we will collect daily stock returns over last 365 calendar year (corresponding to about 252 trading days).

This framework is pretty handful. Why? If you build or run live a model, most probably your wish is to update your database when US markets are closed. You fetch Quandl.com for data. Now, if your model employs risk management or portfolio optimisation as an integrated building-block, it’s very likely, you are interested in risk(-return) estimation, e.g. by calculating Value-at-Risk (VaR) or similar risk measures. Of course, the choice of the time-frame is up to you based on your strategy.

Having that said,

19
20
21
% fetch stock data for last 365 days
date2=datestr(today,'yyyy-mm-dd')     % from
date1=datestr(today-365,'yyyy-mm-dd') % to

what employs an improved Matlab’s command of datestr with formatting as a parameter. It’s really useful when it comes to data fetching from Quandl.com as the format of the date they like to see in your code is yyyy-mm-dd. The above two commands return in Matlab the following values:

date2 =
  2013-11-03
date1 =
  2012-11-03

as for the time of writing this article (string type).

Get an Access to Quandl’s Resources


Excellent! Let’s catch some fishes in the pond! Before doing it we need to make sure that Quandl’s software is installed and linked to our Matlab console. In order to download Quandl Matlab package just visit this website and follow the installation procedure at the top of the page. Add proper path in Matlab environment too. The best practice is to register yourself on Quandl. That allows you to rise the daily number of data call from 50 to 500. If you think you will need more data requests per day, from the same webpage you can send your request.

When registered, in your Profile section on Quandi, you will gain an access to your individual Authorization Token, for example:
qtokenYou will need to replace ‘yourauthenticationtoken’ in the Matlab code (see line 35) with your token in order to get the data.

Fetching Stock Data

From this point the road stops to wind. For all 30 stocks belonging to the current composition of Dow Jones Index, we get their Open, High, Low, Close prices (as retrieved from Google Finance) in the following way:

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
% create an empty array for storing stock data
stockd={};
% scan the Dow Jones tickers and fetch the data from Quandl.com
for i=1:length(dowjc)
    for j=1:length(quandlc)
        if(strcmp(dowjc{i},quandlc{j}))
            fprintf('%4.0f %s\n',i,quandlc{j});
            % fetch the data of the stock from Quandl
            % using recommanded Quandl's command and
            % saving them directly into Matlab's FTS object (fts)
            fts=0;
            [fts,headers]=Quandl.get(quandlcode{j},'type','fints', ...
                          'authcode','yourauthenticationtoken',...
                          'start_date',date1,'end_date',date2);
            stockd{i}=fts; % entire FTS object in an array's cell
        end
    end
end
% use 'save' command to save all actual variables from memory on the disc,
%  if you need it for multiple code running
% save data

A code in line 35 to 37 is an example how you call Quandi for data and fetch them into FINTS (financial time series object from the Financial Toolbox in Matlab). So easy. If you wish to change the time frequency, apply transformation or get the data not in FINTS but .csv format, all is available for you as a parameter at the level of calling Quandl.get() command (explore all possibilities here).

Having that, you can easily plot the Google Finance data, for instance, of IBM stock straight away:

% diplay High-Low Chart for IBM
highlow(stockd{11})
title('IBM');
ylabel('Stock Price ($)');

to get:
quandl-fig01

or prepare a return-risk plot for your further portfolio optimisation

ret=[];
for i=1:length(stockd)
    % extract the Close Price
    tmp=fts2mat(stockd{i}.Close,0);
    % calculate a vector of daily returns
    r=tmp(2:end)./tmp(1:end-1)-1;
    ret=[ret r];
end
% calculate the annualized mean returns for 30 stocks
mret=mean(ret)*sqrt(250);
% and corresponding standard deviations
msig=std(ret)*sqrt(250);
 
% plot return-risk diagram
p=100*[msig; mret]'
plot(p(:,1),p(:,2),'b.');
xlim([0 30]);
xlabel('Annualized Risk of Stock (%)')
ylabel('Annualized Mean Return (%)');

providing us with
quandl-fig02

Forum: Further Discussions

You can discuss the current post with other quants within a new Quant At Risk Forum (Data Handling and Pre-Processing section).

Coskewness and Cokurtosis Computation for Portfolio Managers


Suppose you are responsible for the management of a current company’s portfolio $P$ consisting of $N$ securities (e.g. equities) traded actively at NASDAQ. As a portfolio manager you probably are interested in portfolio performance, new asset allocation, and risk control. Therefore, your basic set of statistics should include portfolio expected return, $\mu_P$, and portfolio standard deviation, $\sigma_P$, as two key numbers you want to calculate every time you evaluate your portfolio performance.

Coskewness, $s_P$, and cokurtosis, $k_P$, are two additional multivariate higher moments (co-moments) important in asset allocation process and portfolio management. Similarly to the fundamental meaning of a sample’s skewness and kurtosis, the co-skewness and co-kurtosis provides a portfolio manager with an ability to test the same portfolio under different composition in order to facilitate changes required to be introduced (e.g. an elimination of some allocations in badly performing securities causing more pronounced negative coskewness).

In this post I will provide with the basic matrix algebra covering the computational solution for four portfolio statistics with a Matlab code example as a practical implementation of mathematics in action.

Expected Return on Portfolio

Let $P$ is our portfolio containing $N$ securities $K_i$ such that $P=\{K_1,K_2,…,K_N\}$ and $i=1,…,N$. Each security has a trading history for last T days (or months, or years) and we are looking for its $T-1$ rates of return $r_t$ for $t=1,…,T-1$. Therefore, let us denote by $K_i$ a column vector of dimensions $(T-1,1)$ such that:
$$
K_i = [r_1,r_2,…,r_{T-1}]^T\ ,
$$
and portfolio matrix, $P$, containing all securities as:
$$
P =
[K_1\ |\ K_2\ |\ …\ |\ K_N ]
=
\left[
\begin{array}{cccc}
r_{1,1} & r_{1,2} & … & r_{1,N} \\
r_{2,1} & r_{2,2} & … & r_{2,N} \\
… & … & … & … \\
r_{T-1,1} & r_{T-1,2} & … & r_{T-1,N}
\end{array}
\right] \ .
$$ Our portfolio can be described in term of their weights:
$$
w_i = \frac{y_iS_i(0)}{V(0)}
$$ where numerator describes the product of the number of shares and stock purchase price in ratio to the amount initially invested in the portfolio. Rewriting it into one-row matrix we have:
$$
w = [\ w_1\ \ w_2\ \ … \ \ w_N\ ]
$$ where from obvious reason the sum of individual weights is one or $1=uw^T$. For each security we can calculate its expected return $\mu_i=E(K_i)$ and form a vector:
$$
m = [\ \mu_1\ \ \mu_2\ \ … \ \ \mu_N\ ] \ .
$$
Finally, the expected return on portfolio $P$ is given as:
$$
\mu_P = mw^T \
$$ what follows:
$$
\mu_P = E(K_i) = E\left(\sum_{i=1}^{N} w_iK_i \right) = \sum_{i=1}^{N} w_i\mu_i = mw^T .
$$

Portfolio Variance


By evaluation of variance, we are able to show that the expected portfolio variance can be expressed as:
$$
\sigma_P = w M_2 w^T
$$
where $M_2$ is the covariance matrix $(N,N)$ with the individual covariances denoted by $c_{ij}=Cov(K_i,K_j)$. At this point, each element of $M_2$ could be calculated as $c_{ij}=\rho_{ij}\sigma_i\sigma_j$, i.e. with a knowledge of coefficient of correlation, $\rho_{ij}$, between the securities $i$ and $j$, and corresponding standard deviations calculated from the history provided by $P$ matrix.

Just as an educational remark, let me remind you that elements $c_{ij}$ can be obtained in the following way:
$$
c_{ij} = \frac{1}{[(T-1)-1)]} \sum_{i,j=1}^{N} \sum_{t=1}^{T-1} (K_{t,i}-\mu_i)(K_{t,j}-\mu_j) \ ,
$$ therefore
$$
M_2 =
\left[
\begin{array}{cccc}
c_{11} & c_{12} & … & c_{1N} \\
c_{21} & c_{22} & … & c_{2N} \\
… & … & … & … \\
c_{N1} & c_{N2} & … & c_{NN}
\end{array}
\right] .
$$

Portfolio Coskewness

It can calculated as a product of:
$$
s_P = w M_3 (w^T\otimes w^T)
$$
where the symbol of $\otimes$ denotes the Kronecker product between $w$ and $w$, and
$$
M_3 = E[(r-\mu)(r-\mu)^T\otimes(r-\mu)^T] = \{a_{ijk}\} \ , \\
a_{ijk} = E[(r_i-\mu_i)(r_j-\mu_j)(r_k-\mu_k)] \ \ \mbox{for}\ \ i,j,k=1,…,N.
$$
The co-skewness matrix of $M_3$ of dimensions $(N,N^2)$ can be represented by $N$ $A_{ijk}$ matrixes $(N,N)$ such that:
$$
M_3 = [\ A_{1jk}\ A_{1jk}\ …\ A_{Njk}\ ]
$$
where $j,k=1,…,N$ as well as for an index $i$. The individual elements of the matrix can be obtained as:
$$
a_{ijk} = \frac{1}{T-1} \sum_{i,j,k=1}^{N} \sum_{t=1}^{T-1} (K_{t,i}-\mu_i)(K_{t,j}-\mu_j)(K_{t,k}-\mu_k) \ .
$$

Portfolio Cokurtosis

Cokurtosis can be obtained by calculation of the following products:
$$
k_P = w M_4 (w^T\otimes w^T\otimes w^T)
$$ where
$$
M_4 = E[(r-\mu)(r-\mu)^T\otimes(r-\mu)^T\otimes(r-\mu)^T] = \{b_{ijkl}\} \ , \\
b_{ijkl} = E[(r_i-\mu_i)(r_j-\mu_j)(r_k-\mu_k)(r_l-\mu_l)] \ \ \mbox{for}\ \ i,j,k,l=1,…,N.
$$
The co-kurtosis matrix $M_4$ of dimensions $(N,N^3)$ can be represented by $N$ $B_{ijkl}$ matrixes $(N,N)$ such that:
$$
M_4 = [\ B_{11kl}\ B_{12kl}\ …\ B_{1Nkl}\ |\ B_{21kl}\ B_{22kl}\ …\ B_{2Nkl}\ |\ …\ |\ B_{N1kl}\ B_{N2kl}\ …\ B_{NNkl}\ ]
$$
where $k,l=1,…,N$. The individual elements of the matrix can be obtained as:
$$
b_{ijkl} = \frac{1}{T-1} \sum_{i,j,k,l=1}^{N} \sum_{t=1}^{T-1} (K_{t,i}-\mu_i)(K_{t,j}-\mu_j)(K_{t,k}-\mu_k)(K_{t,l}-\mu_l) \ .
$$

Example

Below I present a simple Matlab code which computes discussed above portfolio key four portfolio statistics based on $N=3$ securities traded within given $P$. The choice of portfolio content is not solely limited to equities, and $P$ can contain any asset class under management, or even their mixture. Since the expected portfolio return and risk, $\mu_P$ and $\sigma_P$, respectively, are assumed to be expressed as percent, their transformation to a physical unit of money can be straightforwardly obtained.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
% Program demonstrates the calculattion of portfolio four key statistics:
% the expected portfolio return (muP), portfolio standard deviation (sigP),
% portfolio co-skewness (sP), and portfolio co-kurtosis (kP).
%
% (c) 2013, QuantAtRisk.com, by Pawel Lachowicz
%
% Assumptions: * portfolio comprises 252 trading days, i.e. T=252-1 values
%                of daily returns
%              * daily returns are simulated from the normal distribution
%                with zero mean and unit standard deviation (in practice
%                one can use real market data)
%              * N=3, number of securities in portfolio P
%
% Remark: the code can be easily modified for the need of any N securities
%         considered in your portfolio P; it is not difficult to note that
%         the calcution of all four statistics can be performed by risk
%         managers who manage the porfolio P of portfolios P_i such that
%         P=[P_1 P_2 ... P_N]
 
clear all; clc; close all;
 
T=252;
 
% Simulated returns for 3 securities X,Y,Z expressed in [%]
X=random('normal',0,1,T-1,1);
Y=random('normal',0,1,T-1,1);
Z=random('normal',0,1,T-1,1);
 
% Portfolio matrix
P=[X Y Z];
 
% Portfolio weights for all securities (sum=1)
w=[0.2 0.5 0.3];
 
% m-vector containing the expected returns of securities
m=mean([P(:,1) P(:,2) P(:,3)]);
 
% Computation of M2, the covariance matrix
S=[];
for i=1:size(P,2)
    for j=1:size(P,2)
        u=0;
        for t=1:T-1
            u=u+((P(t,i)-mean(P(:,i)))*(P(t,j)-mean(P(:,j))));
        end
        S(i,j)=u/((T-1)-1);
    end
end
M2=S; %
 
% Computation of M3, the co-skewness matrix
M3=[];
for i=1:size(P,2)
    S=[];
    for j=1:size(P,2)
        for k=1:size(P,2)
            u=0;
            for t=1:T-1 
                u=u+((P(t,i)-mean(P(:,i)))*(P(t,j)-mean(P(:,j))) ...
                    *(P(t,k)-mean(P(:,k))));
            end
            S(j,k)=u/(T-1); 
        end
    end
    M3=[M3 S];
end
 
% Computation of M4, the co-kurtosis matrix
M4=[];
for i=1:size(P,2)
    for j=1:size(P,2)
        S=[];
        for k=1:size(P,2)
            for l=1:size(P,2)
                u=0;
                for t=1:T-1 
                    u=u+((P(t,i)-mean(P(:,i)))*(P(t,j)-mean(P(:,j)))* ...
                        (P(t,k)-mean(P(:,k)))*(P(t,l)-mean(P(:,l))));
                end
                S(k,l)=u/(T-1);
            end
        end
        M4=[M4 S];
    end
end
 
% Results
muP=m*w'                           % expected return on a portfolio [%]
stdP=sqrt(w*M2*w')                 % portfolio standard deviation [%]
sK=w*M3*kron(w',w')                % portfolio coskewness
sP=w*M4*kron(kron(w',w'),w')       % portfolio cokurtosis

Measure of Risk of any Security in Equilibrium


The standard Capital Asset Pricing Model (CAPM) is an equilibrium model which construction allows us to determine the relevant measure of risk for any asset and the relationship between expected return and risk for asset when market are in equilibrium. Within CAPM the expected return on security $i$ is given by:
$$
\bar{R}_i = R_F + \beta_i(\bar{R}_M-R_F)
$$ where $\beta_i$ is argued to be the correct measure of a security’s risk in a very well-diversified portfolios. For such portfolios the non-systematic risk tends to go to zero and the reminder – the systematic risk – is measured by $\beta_i$. For the Market Portfolio its Beta is equal one. Therefore, the aforementioned equation defines the security market line.

It is possible to go one step further and write the same CAPM formula as follows:
$$
\bar{R}_i = R_F + \left( \frac{\bar{R}_M-R_F}{\sigma_M} \right) \frac{\sigma_{iM}}{\sigma_M}
$$ what keeps its linear relationship between the expected return but in $\sigma_{iM}/\sigma_M$ space. Recall that the term in braces is the market price of the risk at riskless rate of interest given by $R_F$. $\sigma_{iM}$ denotes the measure of the risk of security $i$ but if left defined as $\sigma_{iM}/\sigma_M$ is provide us with more intuitive understanding itself as the measure of how the risk on a security affects the risk of the market portfolio.

In other words, $\sigma_{iM}/\sigma_M$ is the measure of risk of any security in equilibrium and, as we will show further below, it is equal:
$$
\frac{\sigma_{iM}}{\sigma_M} = \frac{X_i^2\sigma_i^2 + \sum_{j=1,\\ j\ne 1}^{N} X_j \sigma_{ij}} {\sigma_M}
$$ We may get that performing a calculation of the first derivative of the standard deviation of the market portfolio $\sigma_M$, i.e. for
$$
\sigma_M^2 = \sum_{i=1}^{N} X_i^2\sigma_i^2 + \sum_{i=1}^N \sum_{j=1, j\ne i}^{N} X_iX_j \sigma_{ij}
$$ the risk of a security is the change in the risk of the market portfolio. as the holdings of that security are varied:
$$
\frac{d\sigma_M}{dX_i} = \frac { \left[ \sum_{i=1}^{N} X_i^2\sigma_i^2 + \sum_{i=1}^N \sum_{j=1, j\ne i}^{N} X_iX_j\sigma_{ij} \right]^{1/2} }
{ dX_i }
$$
$$
= \frac{1}{2} \left[ 2X_i\sigma_i^2 + 2 \sum_{j=1, j\ne 1}^{N} X_j\sigma_{ij} \right] \times \\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \left[ \sum_{i=1}^{N} X_i^2\sigma_i^2 + \sum_{i=1}^N \sum_{j=1, j\ne i}^{N} X_iX_j\sigma_{ij} \right]^{-1/2}
$$
$$
= \frac{X_i^2\sigma_i^2 + \sum_{j=1, j\ne 1}^{N} X_j \sigma_{ij} } {\sigma_M}
= \frac{\sigma_{iM}}{\sigma_M}
$$ Above, $\sigma_{ij}$ denotes a covariance between any $i$ and $j$ security in the market portfolio and $X_i$ is the share of capital invested in $i$’s security.

Interestingly, the only and remaining question is: how do we know what is best choice of the market portfolio? I will try to address a separate post around the research one may conduct covering that topic. As for now, one may assume that S&P500 or S&P900 universe of stocks would serve here as a quite good example. If so, that would also mean that in order to find $\sigma_{iM}/\sigma_M$ for any security we invested our money in, we need to find $\sigma_M$. Is it hard? Well, if we assume the market to be S&P500 then for any $i$ its $X_i=1/500$. Now, all we need to care is to have an idea about the security’s variance $\sigma_i^2$ and the measure of covariance, i.e. how two securities like each other in out market portfolio. A clever reader would ask immediately: But what about a time-window? And that’s where the devil conceals itself.

Final Frontier of Efficient Frontier


Have you ever considered why in the risk-return space the efficient frontier takes its shape which resembles some of the Bézier curves? And also, why everybody around suggests you that if you approach the efficient frontier from the right-hand side, your selection of a new investment could be the best choice?

Well, as for the latter question the answer seems to be easy. First, you plot all your stocks or the combination of stocks into a number of various portfolios (i.e. you derive the expected return and volatility of a given stock or portfolio, respectively; check out my article on 2-Asset Portfolio Construction for some fundamentals in that field) in the expected risk-return space. Your result can be similar to that one that follows:

where red dots correspond to specific positions of securities which you actually consider for a future investment. The data in the plot are annualized, what simply means we are looking for a potential return and risk (volatility) in a 1-year time horizon only. Secondly, from the plot you can read out that UTX, based on historical data, offers the highest potential return but at quite high risk. If you are not a risk-taker, the plot suggest immediately to choose MMM or JNJ but at lower expected return. So far so good.

The blue line represent so called efficient frontier in the risk-return space. The investor wants to choose the portfolio on the efficient frontier becasue – for each volatility – there is portfolio on the efficient frontier that has a higher rate of return than any other portfolio with the same volatility. In addition, by holding a portfolio on the efficient frontier the investor benefits from the diversification (see also Riskless Diversification). That brings us to an attempt of answering the very first question we posed at the beginning: about the shape, therefore about:

THE MATHEMATICS OF EFFICIENT FRONTIER


As we will show below, the efficient frontier is a solution to the problem of efficient selection between two securities. It is sufficient to conduct the basic calculation based on two assets considered together. For the higher number of potential securities or portfolios of securities (as presented in the plot above), the finding of the efficient frontier line is the subject to more advanced optimization.

Let’s start from the simple case of two securities, A and B, respectively. In our consideration we will omit the scenario where short sales are allowed. The expected portfolio return would be:
$$
\bar{R_P} = x_A\bar{R_A} + x_B\bar{R_B}
$$
where $x_A+x_B=1$ represents the sum of shares (weights), therefore $x_B=1-x_A$ simply means that the our whole initial capital has been fully invested into both securities, and we firstly decided to put a part of $x_A$ of our money into security A (say, $x_A=0.75$ or 75%). By rewriting the above we get:
$$
\bar{R_P} = x_A\bar{R_A} + (1-x_A)\bar{R_B} .
$$
The volatility (standard deviation) of that portfolio is therefore equal:
$$
\sigma_P = [x_A^2\sigma_A^2 + (1-x_A)^2\sigma_B^2 + 2\sigma_A\sigma_B\rho_{AB}x_A(1-x_A)]^{1/2}
$$
where by $\rho_{AB}$ we denoted a linear correlation between the expected rates of return for both securities, also possible to denote as $corr(\bar{R_A},\bar{R_B})$. Please also note the the third term can be jotted down as:
$$
2\sigma_A\sigma_B\rho_{AB}x_A(1-x_A) = 2x_A(1-x_A)cov(\bar{R_A},\bar{R_B})
$$
where the relation between covariance and correlation holds as follows:
$$
cov(\bar{R_A},\bar{R_B}) = \sigma_A\sigma_B corr(\bar{R_A},\bar{R_B}) .
$$
The correlation can be a free parameter if we are given from the historical data of securities A and B the following parameters: $\sigma_A, \sigma_B$ and $\bar{R_A}, \bar{R_B}$. This is so important to keep it in mind!

Below, we will consider four cases leading us to the final mathematical curvature of the efficient frontier.

CASE 1: Perfect Positive Correlation ($\rho_{AB}=+1$)

Considering two assets only, A and B, respectively, the perfect positive correlation of $\rho_{AB}=+1$ leads us to the portfolio volatility:
$$
\sigma_P = [x_A^2\sigma_A^2 + (1-x_A)^2\sigma_B^2 + 2\sigma_A\sigma_B\times 1 \times x_A(1-x_A)]^{1/2}
$$
where we recognize $a^2+b^2+2ab=(a+b)^2$ what allows us to rewrite it as follows:
$$
\sigma_P = [x_A\sigma_A + (1-x_A)\sigma_B] \ \ \mbox{for}\ \ \rho_{AB}=+1 .
$$
Solving for $x_A$ we get:
$$
x_A = \frac{\sigma_P-\sigma_B}{\sigma_A-\sigma_B} \ \ \mbox{i.e.}\ \ x_A \propto \sigma_P .
$$
Substituting that into the formula for the expected portfolio return of 2-assets, we arrive at:
$$
\bar{R_P} = x_A\bar{R_A} + (1-x_A)\bar{R_B} \\
= \frac{\sigma_P-\sigma_B}{\sigma_A-\sigma_B}\bar{R_A} + \left(1-\frac{\sigma_P-\sigma_B}{\sigma_A-\sigma_B}\right)\bar{R_B} \\
… \\
= \sigma_P\left(\frac{\bar{R_A}-\bar{R_B}}{\sigma_A-\sigma_B}\right) – \sigma_B\left(\frac{\bar{R_A}-\bar{R_B}}{\sigma_A-\sigma_B}\right)+\bar{R_B} .
$$
Therefore, the solution for that case can be summarized by the linear relation holding between the expected portfolio return and the portfolio volatility if the portfolio is constructed based on two assets perfectly positively correlated:
$$
\bar{R_P}(\sigma_P) \propto \sigma_P .
$$

CASE 2: Perfect Negative Correlation ($\rho_{AB}=-1$)

In this case, we can denote the portfolio volatility as:
$$
\sigma_P = [x_A^2\sigma_A^2 + (1-x_A)^2\sigma_B^2 + 2\sigma_A\sigma_B\times (-1) \times x_A(1-x_A)]^{1/2}
$$
where similarly as in Case 1, we recognize $a^2+b^2-2ab=(a-b)^2$ what lead us to double possible solutions, namely:
$$
\sigma_P = x_A\sigma_A – (1-x_A)\sigma_B \ \ \ \ \ \ \ \mbox{or} \\
\sigma_P = -x_A\sigma_A + (1-x_A)\sigma_B \ .
$$
Since $\sigma_P$ must always be greater than zero, thus one of the solutions holds anytime.

Please also note that $\sigma_P$(Case 2) is always less than $\sigma_P$(Case 1) for all $x_A\in\langle 0,1\rangle$. That allows us to formulate a very important note: The risk on the portfolio with $\rho_{AB}=-1$ is smaller than for the portfolio with $\rho_{AB}=+1$. Interesting, isn’t it? Hold on and check this out!

It is possible to obtain a portfolio with zero risk by setting $\sigma_P$(Case 2) to zero and solving for $x_A$ (i.e. what fraction of our money we need to invest in asset A to minimize the risk) as follows:
$$
x_A\sigma_A – (1-x_A)\sigma_B = 0 \\
x_A^2\sigma_A – \sigma_B – x_A\sigma_B =0 \\
x_A(\sigma_A+\sigma_B) = \sigma_B
$$
what provide us with a solution of:
$$
x_A = \frac{\sigma_B}{\sigma_A+\sigma_B} .
$$
It simply means that for such derived value of $x_A$ the minimal risk of the portfolio occurs, i.e. $\sigma_P=0$.

CASE 3: No Correlation between returns on the assets ($\rho_{AB}=0$)

In this case the cross-term vanishes leaving us with the simplified form of the expected portfolio volatility:
$$
\sigma_P = [x_A^2\sigma_A^2 + (1-x_A)^2\sigma_B^2 ]^{1/2} .
$$
The graphical presentation is as follows:

The point marked by a dot is peculiar. This portfolio has a minimum risk. It is possible to derive for what sort of value of $x_A$ this is achievable. In order to do it, for a general form of $\sigma_P$,
$$
\sigma_P = [x_A^2\sigma_A^2 + (1-x_A)^2\sigma_B^2 + 2x_A(1-x_A)\sigma_A\sigma_B\rho_{AB}]^{1/2} ,
$$
we find $x_A$ by calculating the first derivative,
$$
\frac{d{\sigma_P}}{d{x_A}} = \left(\frac{1}{2}\right) \frac{2x_A\sigma_A^2-2\sigma_B^2+2x_A\sigma_B^2+2\sigma_A\sigma_B\rho_{AB}-4x_A\sigma_A\sigma_B\rho_{AB}}
{[x_A^2\sigma_A^2+(1-x_A)^2\sigma_B^2+2x_A(1-x_A)\sigma_A\sigma_B\rho_{AB}]^{1/2}} \\
= \left(\frac{1}{2}\right) \frac{2x_A\sigma_A^2-2\sigma_B^2+2x_A\sigma_B^2+2\sigma_A\sigma_B\rho_{AB}-4x_A\sigma_A\sigma_B\rho_{AB}}{\sigma_P} ,
$$
and by setting it to zero:
$$
\frac{d{\sigma_P}}{d{x_A}} = 0
$$
we get:
$$
x_A = \frac{\sigma_B^2-\sigma_A\sigma_B\rho_{AB}}{\sigma_A^2+\sigma_B^2-2\sigma_A\sigma_B\rho_{AB}}
$$
what in case of $\rho_{AB}=0$ reduces to:
$$
x_A = \frac{\sigma_B^2}{\sigma_A^2+\sigma_B^2} .
$$
This solution is also know as the minimum variance portfolio when two assets are combined in a portfolio.

CASE 4: All other correlations between two assets (e.g. $\rho_{AB}=0.4$)

Given $\sigma_A$ and $\sigma_B$ at $\rho_{AB}$ not equal to zero, $-1$ or $1$, one can find that the formula for the expected variance of 2-asset portfolio will take, in general, the following form:
$$
\sigma_P = \sqrt{c_1x_A^2 + c_2}
$$
where $c_1$ and $c_2$ denote some coefficients and it can be presented in the graphical form as follows:

There is particular point of interest here and the shape of the curve is simply concaved. Since the investor is always to choose the asset which offers a bigger rate of return (say, $\bar{R}_A\gt\bar{R}_B$) at lower risk ($\sigma_B\lt\sigma_A$), therefore the best combination of portfolios we get every time following these rules are called as efficient frontier, in general represented by the Case 4’s line.

Contact Form Powered By : XYZScripts.com