In this particular article, I’ve decided to try out a slightly different style of relaying information. The basic philosophy is as follows: I’ll provide low resolution information on a particular topic, then as we go through the article it will gradually be recalled again to where a higher resolution of it is provided. Fingers crossed it works as intended (Please let me know in the responses if it has).

Contents

Part 1: Investment Instruments

Part 2: Portfolio Theory

Part 3: Portfolio Optimization

Part 4: Application of Portfolio Optimization

Conclusion

1- Investment Instruments

It is not uncommon to be bombarded left, right, and center by ‘opportunities’ to ‘invest’. Whether by a barrage of Youtube ads from brokers or your barber convincing you “it’s a good time to invest in gold”. We are constantly being told to invest. The question we must ask, is why?

Let’s consider the case for an individual. We’ll analyze our spending and our earnings curve over time. The average life expectancy in Kuwait is about 75 years, so we’ll take examine typical spendings and earnings from birth till death. It’ll look something like this:

Now the ideal scenario would be to always possess more earnings than spending. This case and its ideal scenario can be expanded to entities too — such as businesses or any type of funds. To realize this ideal scenario, we ought to invest.

Moving on from the intuition behind why we should invest, we still lay bare to another obvious question,

1.1 What do we invest in?

Anything. So long as this anything generates wealth of course or has the potential to do so. For how long? That’s up to you. What if, contrary to initial belief, said investment ends up generating negative wealth (loss)? The mere posing of this question implies the uncertain nature of the investments potential, and that uncertainty is the risk factor.

We’ll back up before we go on too far, how about we narrow down our investment opportunities to a simple pool of common investments:

Furthermore, we shall proceed to make further distinctions within our simple pool of investments:

  • Financial Instruments: Stocks, bonds, indices, currencies, commodities, and derivatives
  • Other: the remainder

For a concise understanding of fincancial instruments, check out this article:

What we invest in depends on our risk and return, our risk appetite, our investment goal (how much we want to make), and the various ideas and strategies we could potentially want to carry out.

2- Portfolio Theory

2.1 Leading up to the idea of Portfolios

I would strongly suggest you read this article as a primer to lend you as scope before we delve deeper:

Anything within our simple pool of investments is an asset (something that is of value). A collection of assets is a portfolio. Since each asset is something of value, it follows that the portfolio is also of value. We can characterize this value as such:

Value of portfolio = the sum of the values of component assets

To make sense of value, we will require a natural base valuation unit for each asset (dollar valuation). Thus a portfolios value will be expressed by base unit value of each asset multiplied by their allocation in the portfolio which are then summed together. To formalize this:

  • Denote the value at time t of the base unit of the ith asset by Si(t)
  • Denote the allocation to the ith asset in our portfolio by αᵢ

Take this example:

Suppose we have two assets and their prices at t=0:

Asset 1: is a stock where S₁(0)=$100 (per share) and α₁=100,000

Asset 2: is another stock where S₂(0)=$150 (per share) and α₂=10,000

Using this formula:

We want to find the value of the portfolio at time t=0:

V(0)=α₁*S₁(0)+α₂*S₂(0)

V(0)=100,000*$100+10,000*$150=$11,500,000

This example looked at two assets at the time of acquiring them (t=0). At time t=1, 2, 3,… the S₁(t) and S₂(t) will either increase or decrease in value. Mathematically we regard them as random variables, keep that in mind for now — we’ll come back to it later. Any how, the fluctuation in their values in turn, cause a fluctuation in V(t).

Recall our previous example where we had 100,000 of asset 1 and 10,000 of asset 2 in our portfolio. So N (number of assets) in this case was N=2. Stating that this portfolio is composed of 2 assets amounting to a total of 110,000 units allocated between both. Let us calculate the weight w of each asset allocation. So w₁=100,000/110,000=0.909 and w₂=10,000/ 110,000=0.091. Notice that adding w₁ and w₂ gives us 1, which is the total allocation.

Having the value of a portfolio at any time t is useful. If we have the value of the portfolio at time t it also means we can calculate its value at time t+1 or t+1000, provided we know the value of each asset component at whatever time was specified.

What can we do to further expand this idea? Suppose you have a portfolio called ‘college funding’, where you’ve invested your hard earned cash into a bundle of assets. Let’s say at time t=0 at the initial purchase (and creation of portfolio), V(0)= $12,000. You check on your portfolio in a year and find that V(1)=$13,000. Essentially your profit/return is $1000. This return is over one year — thus its an annualized return. To find the rate of (annualized) return we perform this simple calculation:

(V(1) — V(0) / V(1) ) * 100

So, ((13,000–12,000)/(13,000) )*100 = 7.69%.

This is quite a useful number as it tells you how much better the portfolio did in comparison with the year before.

Now we shall conclude this part with a more formal definition of a portfolio one we seek to use from now on:

A portfolio is a distribution across a given set of financial assets of some initial wealth to be invested.

2.2 Portfolio weights

In the last part, we briefly mentioned portfolio weights and we denoted them as w. Now we will formalize our understanding.

Portfolio weights are the proportions of the initial wealth assigned to each different assets.

  • w₁, w₂, …,wN are weights of each assets up to N.
  • w₁+w₂+…+wN =1

2.3 The return of a portfolio

Remember when we mentioned the rate of return? Well let us further solidify this idea.

Suppose we have portfolio consisting of: stock 1 (w₁=0.6) and stock 2 (w₂=0.4), with their respective prices at t=0, S₁(0)=$100 & S₂(0)=$150, and at t=1, S₁(1)=$110 & S₂(1)=$180.

Return of stock 1 (R₁)is: (110/100)-1=0.1 (10%)

Return of stock 2 (R₂) is: (180/150)-1=0.2 (20%)

So the return of our portfolio is: 0.6*0.1 + 0.4*0.2 = 0.14 (14%)

Formalized for N assets, Rp (return of portfolio),

Rp=w₁⋅R₁+w₂⋅R₂+…+wN⋅RN

2.4 Jumping into a little bit of probability

Remember where we said S(t) was regarded as a random variable? Now since S(t) and R are related, where R is dependent on S(t), by extension we can say R is also a random variable.

You may be asking, what is a random variable?

It is a real-valued function that assigns an outcome of our sample space and gives it a real value.

We shall equip ourselves with a few other definitions in the subject of probability to make the next part smooth sailing;

What is a sample space?

It is a collection of all possible outcomes of a random experiment. It is denoted as Ω. Another way of looking at it is that it is a set of all possible outcomes and each outcome is an element of that set.

Ω={ω₁, ω₂, ω₃, …} ; where ω is a generic element of our sample space.

Eg. the sample space for a coin toss is Ω={H,T}

An example of random variables

Recall the coin toss example. Let us toss the coin twice. Now our sample space is: Ω={HH,HT,TH,TT}.

Let us define two random variables (who’s value depends on the outcome of the toss):

  • X = number of heads; X(ω)→ real value
  • Y = number of tails; Y(ω)→ real value

So for example,

X(outcome HT)=X(HT)=1

Y(outcome TT)=Y(TT)=2

What is a probability measure?

It is a function that maps a generic element ω in our sample space Ω, to a number between 0 and 1.

P(ω)→[0,1]

Sum of all P(ω) in Ω is equal to 1.

Eg. the probability measure for a heads P(H)=0.5 and tails P(T)=0.5

What is a probability space?

To make a (finite) probability space we need two things:

  • A sample space, Ω
  • a probability measure, P

What is the distribution of a random variable?

It is the probability by which a particular random variable takes various values. It provides the possible outcomes and the probability of such outcomes.

The sources of information for the distribution of our random variable X can be from:

  1. Knowledge of the probability distribution for the random variable X
  2. Sample data for the random variable X drawn from X distribution.

2.5 The expected return of a portfolio

Since the return on an asset relies on the future cashflow and future price (you’ve seen this in the last example). Unfortunately we do not possess perfect foresight and thus we will never be certain of the future cashflow nor future price of an asset. This means that returns are random variables. So R is a random variable.

Knowing this also means that since we are always uncertain of the future price S(t) and hence future return R, using historical data (from which we ascertained the probability distribution) we can calculate the expected return of a portfolio as such:

E[Rp]=w₁⋅E[R₁]+w₂⋅E[R₂]+…+wN⋅E[RN]

It is important to note that throughout this article we’ve been interchangeably using rates of return and return. This was done for shortening purposes.

For an arbitrary stock i,

For a portfolio,

E[Rp]=Σ wᵢ ⋅ Rᵢ

For example, let our portfolio contain stock A with wA=0.5 and stock B with wB=0.5,

2.6 Variance and standard deviation for individual assets

Another calculation we perform when we know the probability distribution (other than calculating the expected value) is variance.

The variance of an arbitrary stock with the return Rᵢ which is a random variable, is the probability sum of squared dispersion terms.

Formalized for an individual asset:

σ² = Σ(Rⱼ — E[Rⱼ])² ⋅ Pⱼ

Taking the square root of the variance gives the standard deviation:

σ = σ² = [Σ(Rⱼ — E[Rⱼ])² ⋅ Pⱼ]

Incredibly enough, the standard deviation is used as the measure of volatility and hence forth risk of an asset.

The best way to understand this is by visualizing what standard deviation is. Suppose we graph a stock A and B:

Let us build on the previous example and calculate the variances and standard deviation of the respective stocks,

2.7 The variance of the return of a portfolio of two assets

How about we further expand this idea. Let’s explore what happens when we have two assets in a portfolio instead of an individual asset.

Let:

  • Asset 1 and 2 have weight w₁ and w₂, respectively
  • Asset 1 and 2 have variances σ₁² and σ₂², respectively

Therefore the variance of return of this portfolio is:

σp² =w₁²⋅σ₁²+w₂²⋅σ₂²+2ρ⋅w₁⋅w₂⋅σ₁⋅σ₂

What is ρ? Hold that thought.

2.8 Covariance

Consider two assets, stock 1 and 2. Covariance fundamentally indicates that if one stock rises, what happens to the other? Does it rise or fall?

Recall our previous table, we added an extra column to the end to get:

Using the formula for covariance, we get:

In order to make sense of ‘what happens to the other’, we have the following rules:

  • cov(A,B) > 0 ⇔ If RA rises then RB rises, If RA falls then RB falls
  • cov(A,B) < 0 ⇔ If RA rises then RB falls, If RA falls then RB rises

What if we expand this to N assets rather than just two? Here it becomes imperative that we begin to use matrices. An example of such a matrix is the covariance matrix.

In order not to further complicate things, we will stave off the mathematics behind the covariance matrix (in this article).

The issue with covariance is that it does not have an upper bound. Which is why it is more or less only useful to look at the sign that it has and not its number.

2.9 Correlation

To appease the issue mentioned above, we rely on the correlation between assets A and B. Why is that? Well, upon finding the correlation coefficient ρAB, we will have a number with an upper and lower bound.

Recalling the example we’ve been consistently working on, we then apply the correlation formula to get this:

Like for covariance, correlation also has a set of rules:

  • ρAB>0 ⇔ cov(A,B) > 0 ⇔ If RA rises then RB rises, If RA falls then RB falls
  • ρAB<0 ⇔ cov(A,B) < 0 ⇔ If RA rises then RB falls, If RA falls then RB rises

Here’s another visual. This time we need one to easily visualize the difference between covariance and correlation:

There is an aspect of the section of Portfolio theory that has been omitted and instead has been placed in the next section .*

3- Portfolio Optimization

How do we make sense of all of this now? What you encountered above was Modern Portfolio Theory (MPT). It was developed by Dr. Harry Markowitz in 1952. MPT practically serves as a standardized framework for evaluating risk and return. Since MPT is a theory, it relies on assumptions that are not always realistic, hence it does not provide certain predictions. However, it does establish a range of reasonable expectations.

3.1 The function of a portfolio optimizer

Structurally speaking, a portfolio optimizer becomes of use when we are,

Given:

  • A set of assets
  • A target return

And asked to,

Find:

  • The allocation to each asset that minimizes risk.

That is not good enough. Having learnt the basics of MPT we now have a working knowledge of:

  • Risk: specifically volatility, σ
  • Return: specifically expected return, E[R]
  • Preferences: risk-return preference, target return, risk tolerance
  • Opportunities: a set of available assets to choose from (and relationships)
  • Relationships between assets: covariance (cov) and correlation (ρ)

This mean we can do better. In fact, with MPT we seek to find the best allocation to each asset that minimizes risk and increases return.

Now,

3.2 What is a portfolio optimizer?

It is the process by which we select the best portfolio out of a set of all portfolios being considered, according to some objective.

3.3 Types of portfolio optimization

We won’t go into much detail but there are three important ones:

  • Mean-variance
  • Mean-absolute deviation
  • Conditional value-at-risk

In this article, we’ll be focusing on the first one — Mean-variance optimization.

3.4 A portfolio and a better portfolio

Imagine we a have a huge universe of possible portfolio choices. Each of these choices are assets. Suppose we try to construct a portfolio (with these assets ) which are a weighted combination of these assets.

Now picture a similar portfolio but this time with a target return.

Ideally we want a portfolio with a high return and low risk.

Remember our ‘simple pool of common investments’ in the first section? How about we graph it too, making our ‘simple pool of common investments’ our universe of possible portfolio choices.

The red line is called the efficient frontier. It is a set of efficient portfolios, or the portfolios with the highest levels of return for any given levels of risk (Risk/Return). The better portfolio is selected from any point on that line.

3.5 Building up to the efficient frontier

In order to construct the efficient frontier, we will rely on our knowledge of MPT from the previous section. This is the part that was omitted from that section*. The rationale is that it makes much more sense to introduce this concept once a bigger picture was created.

We will work our way to building an understanding of the efficient frontier by considering four cases. The first three show a sequential approach to that understanding, whilst the fourth case establishes a mechanism for it.

Case 1

Suppose there are three assets: A, B and C.

As evident from the graph, investors prefer:

  • Asset A over asset B. Why? Higher return for the same risk.
  • Asset C over asset A. Why? Lower risk for the same return.

Case 2

One again, consider two assets, A and B, where: E[RA]>E[RB] and σA>σB.

Recall the variance of a portfolio with two assets,

σp² =wA²⋅σA²+wB²⋅σB²+2ρ⋅wA⋅wB⋅σA⋅σB

then the standard deviation would be,

σp =(wA²⋅σA²+wB²⋅σB²+2ρ⋅wA⋅wB⋅σA⋅σB)

recall that -1≤ρ≤1, now let’s consider the extreme cases:

  • if ρ=+1, σp=wA⋅σA+wB⋅σB
  • if ρ=-1, σp=±[wA⋅σA-wB⋅σB]≥0, let σp=0, (wA/wB)=(σB/σA)
  • if ρ=0, σp=(wA²⋅σA²+wB²⋅σB²)<wB⋅σA+wB⋅σB

Case 3

Suppose we have a portfolio of N assets, where N≥3,

The efficient frontier there (red line), represents and infinite number of different portfolios, each one possesses a different tangent return. The mean variance frontier is ignored as it is effectively the opposite of the efficient frontier. Direct your attention at the two green points which both have the same σp value. Point 1 is a portfolio that is in a better situation than point 2, which is henceforth inferior to point 1. This is evident by the higher E[R] for point 1. As well as point 1 being on the efficient frontier.

Case 4

Suppose we have a portfolio of N assets with identical variance: σ², cov (between these assets):

What we can deduce from the graph and our calculation of the limit as N tends to infinity is that as N increases, the unique risk tends to diversify away, leaving behind only the market/systematic risk. Markowitz was famed for saying “Diversification is the only free lunch” — meaning: diversify to reduce risk.

It is important to note that if all asset returns are independently distributed then cov=0 and σ=0.

3.6 The capital market line

With now a refreshed view of what the efficient frontier is and the mechanism behind it, we can delve back into a real world scenario. As you can probably remember we categorized our ‘simple pool of common investments’ into financial instruments and other. We then further categorized financial instruments into six subcategories. The asset class we will focus on from financial instruments will be stocks. In the other category we will focus on cash. However with a slight difference — this cash will be invested at the risk-free rate RF.

Now consider the following:

  1. Risky portfolio, R₁ (which is the return on a risky portfolio), with weight 1-wF
  2. Risk-free asset, RF (which is the risk free rate), with weight wF

We treat the risky portfolio as an asset, which is part of a portfolio with two assets, call its return Rp.

Now we naturally perform the following calculations as we’ve done in the previous section:

Aha! We’ve managed to construct a linear equation with a slope that is called the Sharpe ratio. This ratio measures the excess return on a portfolio. It is essentially the risk-adjusted return and in many cases can be used as a measure of performance of a portfolio.

The diagram above showcases what was discussed in this topic and also shows that there are three important portfolios on the efficient frontier:

  • Maximum return portfolio: also maximum volatility
  • Minimum return portfolio: also minimum volatility
  • Balanced portfolio: trade-off between risk and return, essentially maximum sharpe ratio (we will select this).

3.7 Summary of portfolio optimization

The knowledge recently acquired boils down to three core concepts:

  • the efficient frontier: we want a portfolio on it
  • the capital market line: we want a portfolio with a steep slope for its tangent at K its market portfolio
  • sharpe ratio: the steep slope described above means we want to maximize the sharpe ratio

4- Application of Portfolio Optimization

In the previous three sections we developed an intuitive understanding of why and in what should we invest?, Modern Portfolio Theory, how portfolio optimization is linked to it, and the underlying framework of what we are looking for in our portfolio. What remains is to redefine some familiar concepts, gloss over the process of portfolio optimization, and then finally to jump in to applying it using Matlab.

4.1 Redefining our understanding

Now with the necessary mathematical framework covered, we can use a novel definition for portfolio optimization:

Portfolio optimization is the process that finds the asset allocation that maximizes the return or minimizes the risk, subject to a set of investment constraints.

Specifically, Mean variance optimization (MVO):

Mean variance optimization is a quantitative tool that uses MPT to find the optimal asset allocation considering the trade-off between risk and return, whilst maximizing expected return subject to a selected level of risk (and/or other constraints).

Both definitions hold an important factor that is consistently considered: constraints. Fundamentally, MVO is a constrained optimization problem. In section 3 we discussed the function of a portfolio optimizer, explicitly mentioning the inputs which are the constraints.

Below is a list of constraints (of which not all will be considered in our application):

  • Equality
  • Inequality
  • Group ratio
  • Budget
  • Transaction cost
  • Upper/lower bounds
  • One way turnover or turnover

4.2 The process of portfolio optimization

As with most models, we need data. This is required so that we may construct a portfolio, determine each assets distribution, estimate the mean and covariance, apply our input constraints, and then to optimize our portfolio.

4.3 Applying MVO using Matlab

Firstly we collected data of 39 stocks with their prices from 8/9/2019 to 8/12/2020. These 39 stocks would become our universe of possible portfolio choices. Once we were done with that step, we moved on to Matlab and then commenced with the process of portfolio optimization — from step 1 to step 5.

Step 1: Import Market Data

Step 2: Create a portfolio from Ω

We used the portfolio function from the Financial Toolbox in Matlab to create a portfolio from these stocks, after having first converted each of their prices to daily returns.

Step 3: Estimate mean and covariance

Firstly we set the risk free rate as 1% per annum then we estimated the assets moments i.e. the mean and covariance.

Step 4: Set investment constraints

Now that we have the mean and covariance, we set the default constraints — which essenntially entails the fact that we cannot:

  • short the assets
  • must invest 100% of capital
The default constraints are the LowerBudge, UpperBudget, and LowerBound

Step 5: Optimize Portfolio

Next we will maximize the sharpe ratio to find the optimal weights — which is our goal.

Then we calculate the risk and return of our portfolio.

This looks like this on a risk vs return graph where we can see that our portfolio successfully lies on the efficient frontier:

Conclusion

In this article we traversed a long but fruitful path of asking questions and providing intuitive answers. Once we saw the why and in what to invest, we moved onto the nuclear idea of a financial asset then worked up to the compound idea of a portfolio.

After that we understood how to derive the value/wealth of a portfolio, we bridged the link to the return of a portfolio. That was not enough. We concluded that the nature of the future price of all assets in a portfolio as being a random variable. This allowed us to hit the ground running and breezing through a select few concepts of probability that would then catapult us to understanding the intrinsic reason why we need to calculate the expected return of a portfolio.

Subsequently we delved deeper and deeper into Modern Portfolio Theory, fundamentally deducing that the standard deviation is a measure of variability and hence is a suitable measure of risk. In addition, we unearthed the rules of correlation and covariance that ultimately govern the relationship between assets.

Upon discussing portfolio optimization we declared that we were going to focus on the mean variance optimization. We eluded to the concept of an efficient frontier as a set of portfolios with the highest levels of return for any given levels of risk (Risk/Return). To showcase the intuition behind it, we relied on four cases where we then ultimately discovered the capital market line and its slope being the sharpe ratio. This made us also focus on creating a balanced portfolio where there is a trade-off between risk and return, essentially maximizing the sharpe ratio.

Finally, with our newfound knowledge we redefined some concepts to introduce more clarity so that we may begin to apply what we’ve learnt — noting that portfolio optimization is a constrained optimization problem. We begun by explicitly formulating the mean variance optimization process into 5 distinct steps (which were carried out): importing market data, creating a portfolio from the universe of possible portfolio choices, estimating the mean and covariance, setting investment constraints, and finally optimizing the portfolio for a maximum sharpe ratio and optimal weights of assets.