# AI for Trading Series №9: Portfolio Risks and Returns

## Learn about the basics of portfolio theory, which are a key for designing portfolios for mutual funds.

Till now in our ‘AI for Trading Series’, we have learned about the indices and ETFs, their applications in the real world and how they work on a transaction level. In this article, we will look at the risks and return properties of a collection of stocks.

For example, consider a scenario where you have done your research and have come up with a list of stocks you want to invest in. You have calculated the amount of money you have to spend and you are now ready to buy those stocks that are needed to construct your portfolio for an ETF. But the main question is

How much money should you invest in each stock?

On a high-level, you may feel that you should invest in those stocks that *you *expect to have highest returns. But what if the prices of these stocks end up fluctuating the most, i.e. they entail the greatest risk. For a while, you main gain returns but then, you could lose all of it. So the main question is:

How to distribute your money that not only maximizes returns, but also minimizes risks?

Let us look at some of the ways in which we can answer this question! 😄📈

# Reducing Portfolio Risks

## Diversification

Consider a scenario where you have done you research and have come to a conclusion that based on historical data, *Company A *belonging to a *IT Sector *is going to perform well in future. Based on this conclusion, you invest all of your money in Company A. Now, Company A does well for a while and then one fine day, it drops to half the price you purchased it for. Since you used all your money to buy the stock, you just lost half your money!!

What if you put half your money in *Company A *and the other half in *Company B* belonging to the *Pharma* sector. In this case, Company A could start doing well when it launches a new tech product, while Company B recovers from a failed drug trial. Also, Company B can start doing well after promising many research leads and Company A could struggle during management change.

In this scenario, we have reduced our portfolio risks by diversifying our portfolio. But one question still prevails: *can we reduce our risks indefinitely by spreading our money into more and more stocks?*

If all the sources of risks are independent, we could *in theory,* reduce risk to zero , by spreading our money between more and more stocks. This would be the case if each company is only subjected to its own independent sources of risk and there are no risks common to all companies.

Risks specific to individual companies are called *idiosyncratic risk or specific risk. *However, in real world, all companies are subjected to common sources of risk that affect the entire economy such as risks of inflation or recession. Risks that are attributable to market-wide sources are called *market-risk or systematic risk. *Next, we will see ways to *quantify *this risk and find some guidelines to allocate our money.

## Portfolio Mean

Consider the above scenario, where you have invested in 2 companies: A and B. We will call *Wa *and *Wb *are the weights on assets A and B and these weights sum up to 1. Now, to calculate the mean and variance of our portfolio, we will need to think of future log returns as random variables. You can think of returns at a future time as being indexed by the variable *i.*

The total return of the portfolio in each scenario is defined as the weighted sum of returns of each individual asset.

The expected value of portfolio return is the weighted sum the individual stock’s expected returns.

## Portfolio Variance

Now, we will measure the *total risk inherit to the portfolio. *We will measure this risk with *volatility *or more specifically with *portfolio variance (*square of the volatility)*. *The variance of portfolio can be represented as follows:

We can write the formula for variance of an individual asset in terms of *Covariance. A covariance is a measure of joint variability of two random variables. *When stock A is above its average and stock B is also above its average, we can say that the two stocks are varying together or *covarying and have a positive covariance.*

**Deriving the Portfolio Variance**

Over here, we will see how variance of log return distribution is related to the covariance of stocks in your portfolio.

## Portfolio Covariance

*Covariance* is the correlation between the two variables times each of their standard deviations. *Correlation coefficient* *takes values between -1 and +1*. Both correlation and covariance are measures of how much two variables vary together.

Hence, the portfolio variance can be re-written as:

Now, let’s see what happens when the correlation between stock A and stock B is +1 and -1.

**When Covariance Coefficient is +1**

In this scenario, the correlation between our two-asset portfolio (stock A and stock B) is +1. The graph of two positively correlated stocks should be as follows:

Now, we will place +1 in place of the correlation coefficient to get the portfolio variance when the stocks in our portfolio are positively correlated. The updated portfolio variance is as follows:

**2. When Covariance Coefficient is -1**

Now, lets consider a scenario when the correlation between the two stocks in our portfolio is -1. The graph of two negatively correlated stocks shoould be as follows:

Again, putting the correlation coefficient as -1 in the formula, we get:

In case when the correlation between the two stocks in -1, we can get a *perfectly hedged portfolio.*

A perfect hedge is a position undertaken by an investor that would eliminate the risk of an existing position, or a position that eliminates all market risk from a portfolio. In order to be a perfect hedge, a position would need to have a 100% inverse correlation to the initial position. As such, the perfect hedge is rarely found.

The standard deviationofthe hedge portfolio is zero.

We can solve the below equation, to get a perfectly hedged portfolio:

However, in reality, since every asset is affected by *systematic risk, the correlation between two assets will never reach -1.*

## Covariance Matrix

Now, lets look back at the *portfolio variance formula *between two stocks.

Now that we know how to calculate portfolio variance in terms of portfolio weights and covariance matrix, let us use the *Numpy library *to calculate the covariance matrix, given the return series of a set of stocks. For this, we will leverage the *numpy.cov** *method. The Jupyter notebook for the same can be found below:

# The Efficient Frontier

Till now, we have learned about diversification and how to calculate portfolio mean and variance. Now, we will learn, what are the best ways to assign each stock in our portfolio. Before we dive into portfolio optimisation, let’s have a look at all the sets of portfolios.

Let us understand this question with an example. Suppose you want to invest $10,000 in a portfolio of 7 stocks. Obviously, you would want to do this in such a way where, you have the highest returns and the least risk (volatility). For this, you will assign weights to the stocks in your portfolio and experiment with multiple combinations of weights that will give you the maximum weights with the least risk.

The figure shows just one of the many simulations we would do in order to get the perfect combination of return v/s risk. For example in the below table, we can see that both *Scenario 1 & 2* have the same risk, but *Scenario 1 *has a higher expected return. So, we will prefer *Scenario 1*. Also, *Scenario 4 *has the highest expected return but it also has the highest risk. *Scenario 4* is a bit unattractive portfolio as it is the one with the highest risk.

Now, if you plot all these simulations on a graph, i.e. *plotting expected portfolio return v/s portfolio volatility, *we will get a graph as below. Each dot represents a possible risk-return combination that can be generated by a portfolio of stocks. The x-axis is the *volatility (risk) *and the y-axis is the *return.*

Also, if you carefully notice the graph, all the dots on the upper boundary perform significantly better than the dots below the boundary line. This is the *Efficient Frontier. *The efficient frontier is the upper boundary of the set of possible portfolios. Portfolios on this boundary have the maximum achievable return for a given level of risk. *Any portfolios above the frontier are* *unachievable*.

# The Capital Market Line

A *risk-free asset *is an investment instrument that entails *absolutely no risk or uncertainity. *In theory, if you invest in such an instrument, you receive a guaranteed rate of return called the *risk-free rate. *In reality, entirely risk-free rate doesn’t exist since all the investments carry* a *certain level of risk. However, in practice, people normally refer to the rate of return on a *three-month treasury bill* as the risk-free rate.

Let us consider a risk-free asset (x-axis=0) and a market portfolio. Now this new portfolio would be a weighted sum of risk-free asset and the market portfolio. In the figure below, if you chose the green dot as your market portfolio and the red dot as your risk-free asset, then* the line between them represents the potential portfolios that you can construct with the two assets.*

Now, in order to achieve the best return for a given level of risk, we would chose, *a market portfolio that allows us to draw a straight line starting from the risk-free asset that just touches the top of the efficient frontier.*

Now, in order to calculate the expected return of the portfolio consisting of the risky portfolio and the risk-free asset, we calculate the formula for the capital market line, because *each point on this line gives the return as a function of a possible combination of the risky portfolio and the risk-free asset.*

# The Sharpe Ratio

The Sharpe Ratio is the **ratio of reward to volatility**. It’s a popular way to look at the performance of an asset relative to its risk.

The numerator of the Sharpe ratio is called the *excess return*, *differential return* as well as the *risk premium*. It’s called “excess return” because this is the return in excess of the risk-free rate. It’s also called the “risk premium”, because this represents the premium that investors should be rewarded with for taking on risk. The denominator is the volatility of the excess return.

The Sharpe Ratio allows us to compare stocks of different returns, because the Sharpe ratio adjusts the returns by their level of risk.

## Annualized Sharpe Ratio

The Sharpe Ratio depends on the time period over which it is measured, and it’s normally annualized. The formula for calculating annualized Sharpe Ratio is given as follows:

Here, 252 represents the number of trading days in a year.

This article is a part of my ‘AI for Trading’ Series. You can find the link to previous articles from the series: https://thestockgram.com/blog or follow my medium blog here: https://purvasingh.medium.com! 😄

**References**