Learn how you can become a Python programmer in just 12 weeks.

    We respect your privacy. Unsubscribe at anytime.

    Calculate the CAPM with Python in 3 Easy Steps

    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.
    Watch lesson

    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.

    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.

    This was the last lesson. In the first lesson you Get started with Pandas and NumPy for Finance for Risk and Return.

    12% Investment Solution

    Would you like to get 12% in return of your investments?

    D. A. Carter promises and shows how his simple investment strategy will deliver that in the book The 12% Solution. The book shows how to test this statement by using backtesting.

    Did Carter find a strategy that will consistently beat the market?

    Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.

    Python for Finance: Unlock Financial Freedom and Build Your Dream Life

    Discover the key to financial freedom and secure your dream life with Python for Finance!

    Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.

    Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.

    Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.

    Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!

    Python for Finance a 21 hours course that teaches investing with Python.

    Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.

    “Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.

    Leave a Comment