## What will we cover?

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 CA**P**M 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.

```
cov = log_returns.cov()
var = log_returns['^GSPC'].var()
beta = cov.loc['AAPL', '^GSPC']/var
risk_free_return = 0.0138
market_return = .105
expected_return = risk_free_return + beta*(market_return - risk_free_return)
```

Notice, you can calculate it all simultaneously.

## Want to learn more?

This is part of a 2.5-hour full video course in 8 parts about Risk and Return.

If you are serious about learning **Python for Finance check out this course**.

- Learn
**Python for Finance**with pandas and NumPy. **21 hours**of video in over**180 lectures**.*“Excellent course for anyone trying to learn to code and invest.”*–**Lorenzo B**.

## Learn Python

**Learn Python A BEGINNERS GUIDE TO PYTHON**

- 70 pages to get you started on your journey to
**master Python**. - How to install your setup with
**Anaconda**. - Written
**description**and introduction to all concepts. **Jupyter Notebooks**prepared for 17 projects.

**Python 101: A CRASH COURSE**

- How to
**get started**with this 8 hours.**Python 101: A CRASH COURSE** **Best practices**for learning Python.- How to download the
**material**to follow along and create projects. - A chapter for each lesson with a
**description**,**code****snippets**for easy reference, and links to a**lesson video**.

## Expert Data Science Blueprint

**Expert Data Science Blueprint**

- Master the Data Science Workflow for actionable data insights.
- How to download the material to follow along and create projects.
- A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

## Machine Learning

**Machine Learning – The Simple Path to Mastery**

- How to get started with Machine Learning.
- How to download the material to follow along and make the projects.
- One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.