You are on page 1of 4

3/4/2019 Risk management explained - Reduce Risk by Combining Risky Stocks into a Portfolio

Risk management explained - Reduce


Risk by Combining Risky Stocks into a
Portfolio
Kyle Li Follow
May 2, 2018 · 4 min read

Photo by Nathan McBride on Unsplash

Risk-Return Tradeo
Risk and Return tradeo is well known in capitalism. You may have
heard that, f you want higher return, you need take higher risk. That
seems a golden rule. But there is a possibility to combine two or more
risky stocks to build a portfolio that leads to lower risk but not
signi cantly reduce the return.

In nance, risk involves the chance an investment’s actual return will


di er from the expected return. Therefore risk is usually measured by
standard deviation of historical price. It is sometimes called volatility.
The higher standard deviation means higher risk.

For demo purpose, I am coding up two set of arti cial stock prices. The
prices have the same trend regarding overall moving. As well, a
portfolio is built with 50% of stock 1 and 50% of stock 2 (If you haven’t
had an environment setup, check step 1 in this post).

https://towardsdatascience.com/reducing-risk-by-building-portfolio-8772d1ce0f21 1/6
3/4/2019 Risk management explained - Reduce Risk by Combining Risky Stocks into a Portfolio

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

periods = 252
noise = np.random.rand(252)
rng = pd.date_range('1/1/2011', periods=periods, freq='D')
# Artificially build two stock stock 1 and stock 2
stk1 = (np.arange(1, 1+(.001)*(periods), .001)) * 30 + noise
stk2 = (np.arange(1, 1+(.001)*(periods), .001)) * 30 - noise

# Build a porfolio with stock 1 and stock 2 weighted 50%


respectively
portfolio = .5 * stk1 + .5 * stk2
df = pd.DataFrame(index=rng, data={'STK1': stk1, 'STK2':
stk2, 'PORTFOLIO': portfolio})
print(df.head())

Output of the dataframe

PORTFOLIO STK1 STK2


2011-01-01 30.00 30.661445 29.338555
2011-01-02 30.03 30.355423 29.704577
2011-01-03 30.06 30.098417 30.021583
2011-01-04 30.09 30.749702 29.430298
2011-01-05 30.12 30.156122 30.083878
...

Plotting the individual stock price and the portfolio value

ax = df.plot(title='Stock Price')
ax.set_xlabel('date')
ax.set_ylabel('close price')
ax.grid()
plt.show()

As we see in the below graph, both individual stock 1 and stock 2 go up,
from ~30 to ~37, through the year. Price deviates from the expected
value in a similar range (because of added noise). But the overall trend
is up. It also indicates that stock 1 and stock 2 have similar level of risk.
However, the portfolio value constantly goes up without volatility.
From return perspective, the portfolio yields similar return by giving
nal value ~37. That means combining two risky stock could get us a
less risky portfolio which provide the same level of return.

https://towardsdatascience.com/reducing-risk-by-building-portfolio-8772d1ce0f21 2/6
3/4/2019 Risk management explained - Reduce Risk by Combining Risky Stocks into a Portfolio

Why?
If we take a closer look at volatility movement of the two stocks, from
code or graph. When stock 1 deviates upward, the stock 2 deviates
downward by the same amount of value. The makes the risk of two
stocks cancel each other. Hence, it yields a smooth (less risky) line.

The Magic — Math


How do we measure such relation of volatility movement in math? We
need it to write code to nd out existing stocks in real world. It can be
measured by correlation coe cient (NOT covariance) of the stock
daily return.

From Investopedia,

The correlation coe cient is a measure that determines the degree to


which two variables’ movements are associated. The range of values for the
correlation coe cient is -1.0 to 1.0. If a calculated correlation is greater
than 1.0 or less than -1.0, a mistake has been made. A correlation of -1.0
indicates a perfect negative correlation, while a correlation of 1.0 indicates
a perfect positive correlation.

https://towardsdatascience.com/reducing-risk-by-building-portfolio-8772d1ce0f21 3/6
3/4/2019 Risk management explained - Reduce Risk by Combining Risky Stocks into a Portfolio

To convert it to the code, we rst need to calculate the daily return of


two stocks. Daily return calculation is as simple as

daily return = price_t1 / price_t0 - 1

Once we have daily return, correlation coe cient can be calculated in


one line of code with 🐼

daily_return = (df / df.shift(1) - 1).dropna()


print("daily_return\n", daily_return.head())
corr = daily_return.corr()
print("corr: ", corr)

To verify, we print the correlation coe cient. The output prove that,
the stock 1 and stock 2 have -0.999787 which indicates the daily return
move inversely.

daily_return
PORTFOLIO STK1 STK2
2011-01-02 0.001000 -0.009390 0.011869
2011-01-03 0.000999 -0.008664 0.010894
2011-01-04 0.000998 0.019264 -0.017346
2011-01-05 0.000997 0.011912 -0.010373
2011-01-06 0.000996 -0.025108 0.028800

corr: PORTFOLIO STK1 STK2


PORTFOLIO 1.000000 0.012499 -0.000667
STK1 0.012499 1.000000 -0.999787
STK2 -0.000667 -0.999787 1.000000

That’s it. We can take advantage of correlation coe cient to discover


real-world stocks for portfolio optimization.

. . .

Recommended reading:

What Hedge Funds Really Do (Canada link)

https://towardsdatascience.com/reducing-risk-by-building-portfolio-8772d1ce0f21 4/6

You might also like