These are some codes related to CAPM and portfolio management with Python. In the next lines I expect to explain the basics about CAPM, Beta, and some of the tools used to calculate it in Python, but I strongly recommend a review of the references at the end of this doc.
According to Wikipedia, CAPM is a model used to determine the appropriate rate of return of an asset, and it can be helpful when building a diversified portfolio. It can be expressed by:
Where the Beta represents the sensitivity of an asset in comparison with a benchmark:
The notebooks in this repository will rely on yfinance to import data from yahoo finance directly into the code, pandas will be used for data wrangling, matplotlib will be preferred for visualization, and stats module from SciPy will be the source of the statistical functions.
import yfinance as yf
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
As a first effort, two Brazilian companies were selected and compared to the iBovespa benchmark to find the beta: Petrobras and Itaú. Some graphs were built as well to assist the visualization.
1. Calculating the Beta - Petrobras & Itaú
and a simpler example was made for American companies.
2. Calculating the Beta - Netflix & Amazon
(...)
Please drop me a note with feedback, contribute to this repository and/or send me a message on LinkedIn.
Arthur Bretas