Conteúdo do Curso
Introduction to Portfolio Management with Python
Introduction to Portfolio Management with Python
The Capital Asset Pricing Model
Now, when we've covered the basics of Factor Analysis, let’s dive into the first and likely the most famous factor model - The Capital Asset Pricing Model, or simply - CAPM.
Before we move forward, however, we need to define an important component that will be essential for this model.
Excess Return
An important component to discuss is Excess Return.
Practically, excess return can be computed using the following formula:
Defining Model
Now, when we've discovered what excess return is, let's define The Capital Asset Pricing Model itself.
To do this, we will use the following formula:
Here, we also consider market's excess return in addition to portfolio's one.
The expected return of the market is practically measured using a broad market index, that reflects the overall performance of a large segment or the entirety of a particular market, typically representing a country's or region's equity market.
It usually includes a diverse mix of stocks across various sectors and industries, giving a general overview of the market's performance and economic health.
As an example of such index, we can consider S&P 500, which tracks 500 large-cap U.S. companies, broadly representing the U.S. stock market.
Alternatively, we can rewrite the following formula in the next way, with the same notation:
Here, we can see, that this expression looks like a special case of linear regression, where the bias is constant and equal to risk-free rate, while market's excessive return is an independent variable, used for predicting portfolio's expected return.
We'll use this fact later.
So, practically, The Capital Asset Pricing Model is utilized to compute the expected return of an asset or portfolio, where the market's return is taken into account as a factor.
Another key concept to introduce is Beta, which we will explore in a more detailed way in the following video, along with additional explanations and code examples:
Here is a code example of computing beta:
# Importing pandas for data manipulations, numpy for working with vectors and statsmodels for linear regression import pandas as pd import numpy as np import statsmodels.api as sm # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon for the period from `2024-09-06` to `2024-09-16` stocks = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/d3c6f4c2-7887-4a07-8596-d956c61f2a2a/section2/stocks_capm_example.csv') print('Stocks:') print(stocks) # Creating DataFrame `sp500` with values of S&P 500 index sp500 = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/d3c6f4c2-7887-4a07-8596-d956c61f2a2a/section2/sp500_capm_.csv') print('S&P 500:') print(sp500) # Defining list of weights and risk-free rate weights = [0.5, 0.3, 0.2] risk_free_rate = 0.002 # Computing returns of stocks returns_stocks = stocks.pct_change() # Computing returns of S&P 500 index returns_sp = sp500.pct_change() # Computing returns of portfolio returns_portfolio = returns_stocks.dot(weights) # Computing excess returns of portfolio portfolio_excess_returns = returns_portfolio.values[1:] - risk_free_rate # Computing excess returns of S&P 500 index sp500_excess_returns = returns_sp.values[1:] - risk_free_rate # Creating and fitting linear regression model `lr` lr = sm.OLS(sp500_excess_returns, portfolio_excess_returns) results = lr.fit() # Computing portfolio's beta beta = results.params[0] print('Portfolio\'s Beta:') print(beta)
Here is a corresponding Google Colab.
In conclusion, it's important to emphasize that another use of beta is estimating risk.
If a portfolio has a high beta, whether positive or negative, it signifies that the portfolio is very risky, as it is highly sensitive to market price fluctuations. As a result, it is advisable to avoid using this portfolio.
Swipe to show code editor
- Compute excess return for an entire portfolio.
- Define linear regression model for computing beta.
- Retrieve value of beta from the model.
Obrigado pelo seu feedback!
The Capital Asset Pricing Model
Now, when we've covered the basics of Factor Analysis, let’s dive into the first and likely the most famous factor model - The Capital Asset Pricing Model, or simply - CAPM.
Before we move forward, however, we need to define an important component that will be essential for this model.
Excess Return
An important component to discuss is Excess Return.
Practically, excess return can be computed using the following formula:
Defining Model
Now, when we've discovered what excess return is, let's define The Capital Asset Pricing Model itself.
To do this, we will use the following formula:
Here, we also consider market's excess return in addition to portfolio's one.
The expected return of the market is practically measured using a broad market index, that reflects the overall performance of a large segment or the entirety of a particular market, typically representing a country's or region's equity market.
It usually includes a diverse mix of stocks across various sectors and industries, giving a general overview of the market's performance and economic health.
As an example of such index, we can consider S&P 500, which tracks 500 large-cap U.S. companies, broadly representing the U.S. stock market.
Alternatively, we can rewrite the following formula in the next way, with the same notation:
Here, we can see, that this expression looks like a special case of linear regression, where the bias is constant and equal to risk-free rate, while market's excessive return is an independent variable, used for predicting portfolio's expected return.
We'll use this fact later.
So, practically, The Capital Asset Pricing Model is utilized to compute the expected return of an asset or portfolio, where the market's return is taken into account as a factor.
Another key concept to introduce is Beta, which we will explore in a more detailed way in the following video, along with additional explanations and code examples:
Here is a code example of computing beta:
# Importing pandas for data manipulations, numpy for working with vectors and statsmodels for linear regression import pandas as pd import numpy as np import statsmodels.api as sm # Creating DataFrame `stocks` with values of stock prices of Apple, Meta and Amazon for the period from `2024-09-06` to `2024-09-16` stocks = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/d3c6f4c2-7887-4a07-8596-d956c61f2a2a/section2/stocks_capm_example.csv') print('Stocks:') print(stocks) # Creating DataFrame `sp500` with values of S&P 500 index sp500 = pd.read_csv('https://codefinity-content-media-v2.s3.eu-west-1.amazonaws.com/courses/d3c6f4c2-7887-4a07-8596-d956c61f2a2a/section2/sp500_capm_.csv') print('S&P 500:') print(sp500) # Defining list of weights and risk-free rate weights = [0.5, 0.3, 0.2] risk_free_rate = 0.002 # Computing returns of stocks returns_stocks = stocks.pct_change() # Computing returns of S&P 500 index returns_sp = sp500.pct_change() # Computing returns of portfolio returns_portfolio = returns_stocks.dot(weights) # Computing excess returns of portfolio portfolio_excess_returns = returns_portfolio.values[1:] - risk_free_rate # Computing excess returns of S&P 500 index sp500_excess_returns = returns_sp.values[1:] - risk_free_rate # Creating and fitting linear regression model `lr` lr = sm.OLS(sp500_excess_returns, portfolio_excess_returns) results = lr.fit() # Computing portfolio's beta beta = results.params[0] print('Portfolio\'s Beta:') print(beta)
Here is a corresponding Google Colab.
In conclusion, it's important to emphasize that another use of beta is estimating risk.
If a portfolio has a high beta, whether positive or negative, it signifies that the portfolio is very risky, as it is highly sensitive to market price fluctuations. As a result, it is advisable to avoid using this portfolio.
Swipe to show code editor
- Compute excess return for an entire portfolio.
- Define linear regression model for computing beta.
- Retrieve value of beta from the model.
Obrigado pelo seu feedback!