Programming in R: Modelling Investment Portfolios with Matrix Algebra


Investment portfolios are a collection of investments.  These investments can be anything including real estate, merchandise inventory, or a collection of businesses in a multinational corporation. However, the term is most commonly used to describe an investment in stocks and bonds in financial markets. Whatever the context may be, a portfolio is a collection of assets purchased at a certain price, held for a certain time, may provide income/cost during the holding period, and then are sold for profit/loss.

Matrix algebra is a branch of mathematics that is often used to model investment portfolios.  The goal of this post is to introduce the used of matrix algebra via the programming language R to solve commonly asked questions about investment portfolios in stocks.   The expected return and the riskiness of the portfolio will be analyzed both analytically and computationally.

1. Vectors and Matrix Definition

The following example is for 3 assets but could easily be extended to a many asset model representation of the portfolio problem. The following notation is used to represent the asset returns, their joint normal distributions, expected returns, variance of returns, and the covariance of returns.

ScreenHunter_04 Dec. 28 11.34R represents the asset return for investments A, B, C. The returns are distributed as a multivariate normal, mu subscript i is the expected return for asset i, sigma squared subscript i is the variance or returns for asset i, and sigma subscript ij is the covariance of returns between asset i and j. The share of wealth invested in assets A, B, and C (not pictured) is represented by x subscript i. The following notation uses the definitions above to construct vectors and the variance-covariance matrix used to further define the model for portfolio returns.

ScreenHunter_04 Dec. 28 11.45

2. Portfolio Returns and Expected Returns

A portfolio’s returns is the weighted average of the individual returns.  The weights are the share of wealth invested each asset which is then multiplied by the return on that asset. One can represent the returns and the expected returns with vectors in the following way:

ScreenHunter_04 Dec. 28 11.55R subscript p,x represents the returns to portfolio p given a certain allocation of wealth x among the assets in portfolio p. Mu represents the expected portfolio returns given the same assets and allocation of wealth.

3. Portfolio risk or variance

The variance of a portfolio can also be written in vector/matrix notation. Recall that when multiplying a vector with itself one must transpose the second multiple. The matrix calculation is shown below with vectors and matrices (bold) and then the familiar variance formula is written out in non-matrix form.

ScreenHunter_04 Dec. 28 12.11

2. Modelling in R

The next step is to programmatically represent the model in R using vectors and matrices.  The following code creates a vector that contains 3 sets of assets with returns of 1%, 4%, and 2%.

ScreenHunter_04 Dec. 28 13.06

Next the variance covariance matrix sigma and the share of wealth invested in assets needs to be programmed into the system.

ScreenHunter_05 Dec. 28 13.12

The final step is to calculate the expected portfolio returns and the variance of the portfolio us cross products, matrix transposes, and matrix multiplication.

ScreenHunter_05 Dec. 28 13.18

These calculations suggested that the expected return on this portfolio is 2.3% with a variance of .0048.  Using the code above one can experiment on how different assets and asset allocation effect the risk and rewards of the portfolio.  In later post we will see how to obtain minimum variance portfolios by allocating shares in a way that reduces risk while maximizing profit, as well as other optimization techniques.