In this lesson we will learn about the CAPM and how to calculate it.
The objectives of this tutorial is:
Understand the CAPM (Capital Asset Pricing Model).
Beta and CAPM calculations.
Expected return of an investment.
Step 1: What is the CAPM?
The CAPM calculates the relationship between systematic risk and expected return. There are several assumptions behind the CAPM formula that have been shown not to hold in reality. But still, the CAPM formula is still widely used.
The formula is as follows.
Step 2: Get some data to make calculations on
Let’s get some data and calculate it.
import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
tickers = ['AAPL', 'MSFT', 'TWTR', 'IBM', '^GSPC']
start = dt.datetime(2015, 12, 1)
end = dt.datetime(2021, 1, 1)
data = pdr.get_data_yahoo(tickers, start, end, interval="m")
data = data['Adj Close']
log_returns = np.log(data/data.shift())
Feel free to change the tickers to your choice and remember to update the dates to fit your purpose.
Step 3: How to calculate CAPM with Python (NumPy and pandas)
The calculations are done quite easily.
Again, when we look at the formula, the risk free return is often set to 0. Otherwise, the 10 years treasury note is used. Here, we use 1.38%. You can update it for more up to date value with the link.
In this lesson we will learn about Linear Regression, difference from Correlation and how to visualize Linear Regression.
The objective of this tutorial is.
Understand the difference between Linear Regression and Correlation.
Understand the difference between true random and correlated variables
Visualize linear regression.
Step 1: Similarities and differences between linear regression and correlation
Let’s first see what the similarities and difference between Linear Regression and Correlation is.
Similarities.
Quantify the direction and strength of the relationship between two variables, here we look at stock prices.
Differences.
Correlation is a single statistic. It is just a number between -1 and 1 (both inclusive).
Linear regression produces an equation.
Step 2: Visualize data with no correlation
A great way to learn about relationships between variables is to compare it to random variables.
Let’s start by doing that.
import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
import pandas_datareader as pdr
import datetime as dt
import matplotlib.pyplot as plt
%matplotlib notebook
X = np.random.randn(5000)
Y = np.random.randn(5000)
fig, ax = plt.subplots()
ax.scatter(X, Y, alpha=.2)
Giving the following scatter chart.
Which shows the how two non-correlated variables look like.
Step 3: How to visualize correlated stock prices
To compare that to two correlated, we need some data.
The function takes the two tickers and get’s the log returns in NumPy arrays. They are reshaped to fit the required format.
The the Linear Regression model (LinearRegression) is used and applied to predict values. The alpha and beta are the liner variables. Finally, we scatter plot all the points and a prediction line.
Let’s try linear_regression(“AAPL”, “^GSPC”).
Where we see the red line as the prediction line.
Step 4: A few more examples
Other examples linear_regression(“AAPL”, “MSFT”)
And linear_regression(“AAPL”, “TWTR”).
Where it visually shows that AAPL and TWTR are not as closely correlated as the other examples.
Want more?
This is part of 8 lesson and 2.5h video course with prepared Jupyter Notebooks with the Python code.
In this lesson we will learn about correlation of assets, calculations of correlation, and risk and coherence.
The learning objectives of this tutorial.
What is correlation and how to use it
Calculate correlation
Find negatively correlated assets
Step 1: What is Correlation
Correlation is a statistic that measures the degree to which two variables move in relation to each other. Correlation measures association, but doesn’t show if x causes y or vice versa.
The correlation between two stocks is a number form -1 to 1 (both inclusive).
A positive correlation means, when stock x goes up, we expect stock y to go up, and opposite.
A negative correlation means, when stock x goes up, we expect stock y to go down, and opposite.
A zero correlation, we cannot say anything in relation to each other.
The formula for calculating the correlation is quite a mouthful.
Step 2: Calculate the Correlation with DataFrames (pandas)
Luckily, the DataFrames can calculate it for us. Hence, we do not need to master how to do it.
Let’s get started. First, we need to load some time series of historic stock prices.
We identify, that the correlation on the diagonal is 1.0. This is obvious, since the diagonal shows the correlation between itself (AAPL and AAPL, and so forth).
Other than that, we can conclude that AAPL and MSFT are correlated the most.
Step 3: Calculate the correlation to the general market
Where we see that AAPL and MSFT are mostly correlated to S&P 500 index. This is not surprising, as they are a big part of the weight of the market cap in the index.
Step 4: Find Negative Correlated assets when Investing using Python
We will add this helper function to help find correlations.
We are in particular interested in negative correlation here.
First an introduction to the concept and then how to use Sharpe Ratio to find the optimal portfolio with Monte Carlo Simulation.
The learning objective will be.
The principles behind Monte Carlo Simulation
Applying Monte Carlo Simulation using Sharpe Ratioto get the optimal portfolio
Create a visual Efficient Frontier based on Sharpe Ratio
Step 1: What is Monte Carlo Simulation
Monte Carlo Simulation is a great tool to master. It can be used to simulate risk and uncertainty that can affect the outcome of different decision options.
Simply said, if there are too many variables affecting the outcome, then it can simulate them and find the optimal based on the values.
Monte Carlo simulations are used to model the probability of different outcomes in a process that cannot easily be predicted due to the intervention of random variables. It is a technique used to understand the impact of risk and uncertainty in prediction and forecasting models.
Step 2: A simple example to demonstrate Monte Carlo Simulation
Here we will first use it for simple example, which we can precisely calculate. This is only to get an idea of what Monte Carlo Simulations can do for us.
The game we play.
You roll two dice.
When you roll 7, then you gain 5 dollars.
If you roll anything else than 7, you lose 1 dollar.
How can we simulate this game?
Well, the roll of two dice can be simulated with NumPy as follows.
import numpy as np
def roll_dice():
return np.sum(np.random.randint(1, 7, 2))
Where are roll is simulated with a call to the roll_dice(). It simply uses the np.random.randint(1, 7, 2), which returns an array of length 2 with 2 integers in the range 1 to 7 (where 7 is not included, but 1 is). The np.sum(…) sums the two integers into the sum of the two simulated dice.
Now to the Monte Carlo Simulation.
This is simply to make a trial run and then see if it is a good game or not.
def monte_carlo_simulation(runs=1000):
results = np.zeros(2)
for _ in range(runs):
if roll_dice() == 7:
results[0] += 1
else:
results[1] += 1
return results
This is done by keeping track of the how many games I win and lose.
A run could look like this.
monte_carlo_simulation()
It could return array([176., 824.]), which would result in my win of 176*5 = 880 USD and lose of 824 USD. A total gain of 56 USD.
Each run will most likely give different conclusions.
Step 3: Visualize the result of Monte Carlo Simulation Example
A way to get a more precise picture is to make more runs. Here, we will try to record a series of runs and visualize them.
results = np.zeros(1000)
for i in range(1000):
results[i] = monte_carlo_simulation()[0]
import matplotlib.pyplot as plt
%matplotlib notebook
fig, ax = plt.subplots()
ax.hist(results, bins=15)
Resulting in this figure.
This gives an idea of how a game of 1000 rolls returns and how volatile it is. See, if the game was less volatile, it would center around one place.
For these particular runs we have that results.mean()*5 gives the average return of 833.34 USD(notice, you will not get the exact same number due to the randomness involved).
The average loss will be 1000 – results.mean()= 833.332 USD.
This looks like a pretty even game.
Step 4: Making the precise calculation of the example
Can we calculate this exactly?
Yes. The reason is, that this is a simple situation are simulating. When we have more variable (as we will have in a moment with portfolio simulation) it will not be the case.
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
tickers = ['AAPL', 'MSFT', 'TWTR', 'IBM']
start = dt.datetime(2020, 1, 1)
data = pdr.get_data_yahoo(tickers, start)
data = data['Adj Close']
To use it with Sharpe Ratio, we will calculate the log returns.
log_returns = np.log(data/data.shift())
The Monte Carlo Simulations can be done as follows.
# Monte Carlo Simulation
n = 5000
weights = np.zeros((n, 4))
exp_rtns = np.zeros(n)
exp_vols = np.zeros(n)
sharpe_ratios = np.zeros(n)
for i in range(n):
weight = np.random.random(4)
weight /= weight.sum()
weights[i] = weight
exp_rtns[i] = np.sum(log_returns.mean()*weight)*252
exp_vols[i] = np.sqrt(np.dot(weight.T, np.dot(log_returns.cov()*252, weight)))
sharpe_ratios[i] = exp_rtns[i] / exp_vols[i]
The code will run 5000 experiments. We will keep all the data from each run. The weights of the portfolios (weights), the expected return (exp_rtns), the expected volatility (exp_vols) and the Sharpe Ratio (sharpe_ratios).
Then we iterate over the range.
First we create a random portfolio in weight (notice it will have the sum 1). Then we calculate the expected annual return. The expected volatility is calculated a bit different than we learned in the lesson about Sharpe Ratio. This is only to make it perform faster.
Finally, the Sharpe Ratio is calculated.
In this specific run (you might get different values) we get that the maximum Sharpe Ratio is, given by sharpe_ratios.max(), 1.1398396630767385.
To get the optimal weight (portfolio), call weights[sharpe_ratios.argmax()]. In this specific run, array([4.57478167e-01, 6.75247425e-02, 4.74612301e-01, 3.84789577e-04]). This concludes to hold 45.7% to AAPL, 6.7% to MSFT, 47.5% to TWTR, and 0,03% to IBM is optimal.
Step 6: Visualizing the Monte Carlo Simulation of the Efficient Frontier
This can be visualized as follows in an Efficient Frontier.
In this tutorial we will see how to calculate the Sharpe Ratio using pandas DataFrames and NumPy with Python.
The Sharpe Ratio combines Risk and Return in one number.
The Sharpe Ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk. Volatility is a measure of the price fluctuations of an asset or portfolio (source).
Step 1: The formula for Sharpe Ratio and how to interpret the result
The Sharpe Ratio is the average return earned in excess of the risk-free rate per unit of volatility or total risk.
The idea with Sharpe Ratio, is to have one number to represent both return and risk. This makes it easy to compare different weights of portfolios. We will use that in the next lesson about Monte Carlo Simulations for Portfolio Optimization.
Now that is a lot of words. How does the Sharpe Ratio look like.
We need the return of the portfolio and the risk free return, as well as the standard deviation of the portfolio.
The return of the portfolio we covered in lesson 1, but we will calculate it with log returns here.
It is custom for the risk free return to use the10 Year Treasury Note, but as it has been low for long time, often 0 is used.
The standard deviation is a measure of the volatility, and is used here to represent the risk. This is similar to Average True Range.
Step 2: Get a portfolio of stock prices with Pandas Datareader
To get started, we need to read time series data of historic stock prices for a portfolio. This can be done as follows.
import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
tickers = ['AAPL', 'MSFT', 'TWTR', 'IBM']
start = dt.datetime(2020, 1, 1)
data = pdr.get_data_yahoo(tickers, start)
data = data['Adj Close']
Where our portfolio will consist of the tickers for Apple, Microsoft, Twitter and IBM (AAPL, MSFT, TWTR, IBM). We read the data from start 2020 from the Yahoo! Finance API using Pandas Datareader.
Finally, we only keep the Adjusted Close price.
Step 3: Calculate the log-return of the portfolio
Let’s assume our portfolio is balanced as follows, 25%, 15%, 40%, and 20% to AAPL, MSFT, TWTR, IBM, respectively.
Then we can calculate the daily log return of the portfolio as follows.
This gives an impression of how volatile the portfolio is. The more data is centered around 0.0, the less volatile and risky.
Step 5: Calculate the Sharpe Ratio of the Portfolio
The Sharpe Ratio can be calculate directly as follows.
sharpe_ratio = log_return.mean()/log_return.std()
This gives a daily Sharpe Ratio, where we have the return to be the mean value. That is, the average return of the investment. And divided by the standard deviation.
The greater is the standard deviation the greater the magnitude of the deviation from the meanvalue can be expected.
To get an annualized Sharpe Ratio.
asr = sharpe_ratio*252**.5
This is the measure we will use in the next lesson, where we will optimize the portfolio using Monte Carlo Simulation.
What next?
If you want to learn more? Then this is one part of a 8 lesson video course.
In this tutorial we will learn about the volatility of a stock price measured with Average True Range. The Volatility of a stock is one measure of investment risk.
The Average True Range was originally designed to calculate the volatility of commodities, but the technical indicator can also be used for stock prices, as we will do in this tutorial.
We will get some stock price time series data and make the calculation using pandas DataFrames and NumPy.
Step 1: Get historic stock price data
To get started, we need some historic stock prices. This can be done as follows and is covered in Lesson 1.
import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
start = dt.datetime(2020, 1, 1)
data = pdr.get_data_yahoo("NFLX", start)
This reads the time series data of the historic stock prices of Netflix (ticker NFLX).
Step 2: The formula of Average True Range (ATR)
To calculate the Average True Range (ATR) we need a formula, which is given on investoperia.org.
The Average True Range (ATR) is a moving average of the True Range (TR). And the TR is given by the maximum of the current high (H) minus current low (L), the absolute value of current high (H) minus previous close (Cp), and the absolute value of current low (L) and previous close (Cp).
Sted 3: The calculations of Average True Range with DataFrames and NumPy
The above formula can look intimidating at first, but don’t worry, this is where the power of PandasDataFrames and Series come into the picture.
It is always a good idea to make your calculations simple.
In this part we will get familiar with NumPy. We will assume familiarity with the Pandas library. If you are new to Pandas we will suggest you start with this FREE 2h course. This part will look at how Pandas and NumPy is connected.
In this tutorial we will cover the following.
Refresher of working with Pandas and Pandas Datareader to use them to read historic stock prices.
How PandasDataFrame and NumPy arrays are related and different.
Calculations of return of a portfolio, which is a primary evaluation factor of an investment.
Step 1: Get some data with Pandas Datareader
First, we need some historic time series stock prices. This can be easily done with Pandas Datareader.
import numpy as np
import pandas_datareader as pdr
import datetime as dt
import pandas as pd
start = dt.datetime(2020, 1, 1)
data = pdr.get_data_yahoo("AAPL", start)
This will read historic stock prices from Apple (ticker AAPL) starting from 2020 and up until today. The data is in a DataFrame (Pandas main data structure).
It is a good habit to verify that the data is as expected to avoid surprises later in the process. That can be done by calling head() on the DataFramedata, which will show the first 5 lines.
data.head()
Resulting in.
High Low Open Close Volume Adj Close
Date
2020-01-02 75.150002 73.797501 74.059998 75.087502 135480400.0 74.333511
2020-01-03 75.144997 74.125000 74.287498 74.357498 146322800.0 73.610840
2020-01-06 74.989998 73.187500 73.447502 74.949997 118387200.0 74.197395
2020-01-07 75.224998 74.370003 74.959999 74.597504 108872000.0 73.848442
2020-01-08 76.110001 74.290001 74.290001 75.797501 132079200.0 75.036385
Recall that the index should be a DatetimeIndex. This makes it possible to take advantage of being a time series.
To remind ourselves further, we recall that each column in a DataFrame has a datatype.
data.dtypes
Shown below here.
High float64
Low float64
Open float64
Close float64
Volume float64
Adj Close float64
dtype: object
Step 2: Investigate how NumPy is different from DataFrames (pandas)
The next step in our journey is to see how NumPy is different from PandasDataFrames.
We can get the DataFrame as a NumPy array as follows.
arr = data.to_numpy()
The shape of a NumPy array gives the dimensions.
(303, 6)
Please notice, that you might get more rows than 303, as you run this later than we do here in the tutorial. There will be a row for each day open on the stock exchange market since beginning of 2020.
But you should get 6 columns, as there are 6 columns in our DataFrame, where the NumPy array comes from.
The first row of data can be accessed as follows.
arr[0]
Which gives the the data of the first row, as we know it from the DataFrame.
Notice the scientific notation. Other than that, you can see the figures are the same.
Now to an interesting difference from DataFrames. The NumPy array only has one datatype. That means, that all columns have the same datatype. The full array has the same datatype.
arr.dtype
Resulting in the following output.
dtype('float64')
To access the top 10 entries of the first column in our NumPy array (the one representing the High column), we can use the following notation.
small = arr[:10, 0].copy()
small
Which will output a one-dimensional array of size 10, containing the 10 first values of column 0.
Where the first two return the maximum value of the array, small. The argmax() returns the index of the maximum value.
The NumPy functionality works well on DataFrames, which comes in handy when working with financial data.
We can get the logarithm of values in a NumPy array as follows.
np.log(small)
Similarly, we can apply the logarithm on all entries in a DataFrame as follows.
np.log(data)
This is magic.
High Low Open Close Volume Adj Close
Date
2020-01-02 4.319486 4.301325 4.304876 4.318654 18.724338 4.308562
2020-01-03 4.319420 4.305753 4.307943 4.308885 18.801326 4.298792
2020-01-06 4.317355 4.293025 4.296571 4.316821 18.589471 4.306729
2020-01-07 4.320484 4.309053 4.316955 4.312107 18.505683 4.302015
2020-01-08 4.332180 4.307976 4.307976 4.328065 18.698912 4.317973
While the logarithm of all the columns here does not make sense. Later we will use this and it will all make sense.
Step 4: Calculate the daily return
We can calculate the daily return as follows.
data/data.shift()
Resulting in the following output (or first few lines).
High Low Open Close Volume Adj Close
Date
2020-01-02 NaN NaN NaN NaN NaN NaN
2020-01-03 0.999933 1.004438 1.003072 0.990278 1.080029 0.990278
2020-01-06 0.997937 0.987352 0.988693 1.007968 0.809082 1.007968
2020-01-07 1.003134 1.016157 1.020593 0.995297 0.919626 0.995297
2020-01-08 1.011765 0.998924 0.991062 1.016086 1.213160 1.016086
Let’s investigate that a bit. Recall the data (you can get the first 5 lines: data.head())
High Low Open Close Volume Adj Close
Date
2020-01-02 75.150002 73.797501 74.059998 75.087502 135480400.0 74.333511
2020-01-03 75.144997 74.125000 74.287498 74.357498 146322800.0 73.610840
2020-01-06 74.989998 73.187500 73.447502 74.949997 118387200.0 74.197395
2020-01-07 75.224998 74.370003 74.959999 74.597504 108872000.0 73.848442
2020-01-08 76.110001 74.290001 74.290001 75.797501 132079200.0 75.036385
Notice the the calculation.
75.144997/75.150002
Gives.
0.9999333998687053
Wait. Hence the second row of High divided by the first gives the same value of the second row of data/data.shift().
This is no coincidence. The line takes each entry in data and divides it with the corresponding entry in data.shift(), and it happens that data.shift() is shifted one forward by date. Hence, it will divide by the previous row.
Now we understand that, let’s get back to the logarithm. Because, we love log returns. Why? Let’s see this example.
np.sum(np.log(data/data.shift()))
Giving.
High 0.502488
Low 0.507521
Open 0.515809
Close 0.492561
Volume -1.278826
Adj Close 0.502653
dtype: float64
And the following.
np.log(data/data.iloc[0]).tail(1)
Giving the following.
High Low Open Close Volume Adj Close
Date
2021-03-17 0.502488 0.507521 0.515809 0.492561 -1.278826 0.502653
Now why are we so excited about that?
Well, because we can sum the log daily returns and get the full return. This is really handy when we want to calculate returns of changing portfolios or similar.
We do not care where the log returns comes from. If our money was invested one day in one portfolio, we get the log return from that. The next day our money is invested in another portfolio. Then we get the log return from that. The sum of those two log returns give the full return.
That’s the magic.
Step 5: Reading data from multiple tickers
We also cover how to reshape data in the video lecture.
Then we consider how to calculate with portfolio and get the return.
This requires us to read data from multiple tickers to create a portfolio.
As you see, we start with 100000 USD and end with 169031 USD in this case. You might get a bit different result, as you run yours on a later day.
This is handy to explore a portfolio composition.
Actually, when we get to Monte Carlo Simulation, this will be handy. There, we will generate multiple random portfolios and calculate the return and risk for each of them, to optimize the portfolio composition.
A random portfolio can be generated as follows with NumPy.
Notice, that we generate 4 random numbers (one for each ticker) and then we divide by the sum of them. This ensures the sum of the weights will be 1, hence, representing a portfolio.
This was the first lesson.
Want more?
Check out the full 2.5 course with prepared JuPyter Notebooks to follow along.