楼主: georgefox
890 6

Fama-French Factor Model in Python [推广有奖]

  • 0关注
  • 0粉丝

已卖:435份资源

大专生

6%

还不是VIP/贵宾

-

威望
0
论坛币
402 个
通用积分
68.4743
学术水平
5 点
热心指数
5 点
信用等级
5 点
经验
1652 点
帖子
23
精华
0
在线时间
9 小时
注册时间
2024-4-10
最后登录
2024-7-10

楼主
georgefox 发表于 2024-4-18 21:01:23 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
In this tutorial, we'll guide you through applying the Fama-French Three-Factor Model to analyze the NASDAQ 100 index, specifically through the QQQ ETF, utilizing various Python's libraries. Our focus will be on QQQ's monthly returns from 2006 to 2023 to understand the influence of the three Fama-French factors on the ETF's performance.

[color=rgba(0, 0, 0, 0.87)]The Fama-French Three-Factor Model is a widely used tool in finance to analyze the performance of stocks and portfolios. It decomposes the returns of a stock or portfolio into three factors: market excess returns, company size, and value. By conducting an Ordinary Least Squares (OLS) regression analysis, we aim to reveal the extent to which these factors can explain the performance of QQQ, and demonstrate how to interpret the results.



[color=var(--jp-content-font-color1)]How Fama-French's Factor Model Works

The Fama-French Factor Model is a way to determine what factors influence returns. Returns could be the return of a single stock, a portfolio of multiple stocks or a specific index such as the SP500. It can be applied to individual stocks, portfolios of stocks, mutual funds, or any other type of investment that can be compared to the broader market.

Originally, the model started with three factors, also known as the Fama-French three-factor model. These factors are:

  • Market excess returns (Mkt-RF): The return of the SP500 index minus the risk-free rate. This factor captures the overall market movement.
  • Size factor (SMB, Small Minus Big): The return of small-cap stocks minus the return of large-cap stocks. This factor captures the outperformance of small-cap stocks over large-cap stocks.
  • Value factor (HML, High Minus Low): The return of value stocks minus the return of growth stocks. This factor captures the outperformance of value stocks over growth stocks.

In the later version of the model, the Fama-French five-factor model introduced in 2015, Eugene Fama and Kenneth French added two new factors to the original three-factor model. These additional factors are:

  • Profitability factor (RMW, Robust Minus Weak): The excess return of firms with high operating profitability over those with low operating profitability.
  • Investment factor (CMA, Conservative Minus Aggressive): The excess return of firms that invest conservatively relative to their assets over those that invest aggressively.

When applying the Fama-French model to a single stock, you're investigating how much of the stock's excess return (over the risk-free rate) can be explained by these three or five factors. You can understand the factors contributing to the stock's performance, which can be especially useful for stock selection and portfolio construction.

Assuming your portfolio made a 15% return last year, the Fama-French model doesn't directly attribute fixed percentages of this return to the factors (e.g., 6% from Mkt-Rf, 8% from SMB, 1% from HML). Instead, it provides a framework to analyze how much of your portfolio's excess return over the risk-free rate can be explained by these factors based on its sensitivity to them.

In order to determine the sentivity and explanatory power of each factor on the portfolio's return, a simple linear regression model is used. The model's output is interpreted as follows:

  • Model Coefficients: The regression analysis yields coefficients (betas) for each factor, indicating your portfolio's sensitivity to that factor. For example, if your portfolio is heavily invested in small-cap stocks, your coefficient for SMB would be high, indicating a higher sensitivity to the SMB factor.
  • Error Term: The model also includes an error term to account for the portion of the portfolio's return that cannot be explained by the three or five factors. This captures specific risks associated with the portfolio or other unexplained variations.

In essence, the model helps you understand the relationship between your portfolio's excess return and the factors, rather than attributing its return to the factors in a direct, additive way. The actual contribution of each factor to your portfolio's return depends on the portfolio's exposure to those factors, as quantified by the regression coefficients, not just the overall market or average returns of small-cap vs. large-cap or value vs. growth stocks.

The dependent variable (the excess return = outcome of the model) is what you are trying to explain or predict using the model. It is the outcome variable whose variations you aim to understand based on independent variables (factors).






[color=var(--jp-content-font-color1)]Example

Consider an illustrative scenario where we analyze the annual returns of a certain portfolio using the Fama-French Three-Factor Model. The analysis yields the following coefficients and associated p-values for each factor:

  • Market Excess Return (Mkt-Rf): Coefficient = 1.2, p-value = 0.001
  • Size Factor (SMB): Coefficient = 0.5, p-value = 0.030
  • Value Factor (HML): Coefficient = -0.3, p-value = 0.014

Interpretation

  • Market Excess Return (Mkt-Rf) stands out as the predominant factor influencing the portfolio's returns, evidenced by its substantial coefficient of 1.2. This indicates that for every percentage point increase in the market's excess return, the portfolio's return is expected to increase by 1.2 percentage points, holding other factors constant. The statistical significance of this relationship is underscored by its p-value (0.001), suggesting a highly reliable predictor of the portfolio's performance.
  • Size Factor (SMB) also plays a notable role, albeit to a lesser extent compared to the market excess return. With a coefficient of 0.5, it implies that the portfolio has a positive but moderate sensitivity to small-cap overperformance relative to large caps. The significance of this factor is confirmed by a p-value of 0.030, indicating a significant but less pronounced impact than the market excess return.
  • Value Factor (HML), with a coefficient of -0.3, reveals a negative relationship between the portfolio's returns and the performance of value stocks relative to growth stocks. This suggests that the portfolio may be positioned in such a way that it benefits less from the outperformance of value stocks, or possibly is more aligned with growth stocks. Despite the negative correlation, the factor's influence is statistically significant, as denoted by its p-value of 0.014, albeit it's the least impactful compared to the market excess return and size factor.

This analysis demonstrates that the portfolio's performance is most significantly driven by market movements (Mkt-Rf), with a substantial and statistically significant coefficient. It also exhibits sensitivity to the size of firms (SMB), albeit to a lesser degree, and shows a slight inverse correlation with the value factor (HML). The statistical significance of all factors, as indicated by p-values below the 0.05 threshold, validates their relevance in explaining the portfolio's returns, with the hierarchy of influence clearly starting with the market excess return, followed by the size factor, and finally, the value factor.






[color=var(--jp-content-font-color1)]Steps to Replicate Fama-French Three-Factor Model on QQQ in Python

The following example guides you through the steps to apply the Fama-French three-factor model to the monthly returns of the NASDAQ 100 index (ticker: QQQ), and evaluate the impact of the three factors (Mkt-RF, SMB, HML) on QQQ's monthly returns from 2006 to 2023. The steps include:

  • Collect Monthly Returns of QQQ
  • Collect Monthly Returns of Fama-French Factors
  • Merge QQQ Returns and Fama-French Factors
  • Fit the Fama-French Three-Factor Model
  • Interpret the Model Results




[color=var(--jp-content-font-color1)]

https://sec-api.io/resources/fama-french-factor-model





[color=var(--jp-content-font-color1)]






二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:FRENCH python factor Facto model

沙发
georgefox 发表于 2024-4-18 21:02:16
Step 1. Collect Monthly Returns of Portfolio or Asset
We use Yahoo Finance to collect the daily pricing data of QQQ from 2006 to 2023. The yfinance library is used to fetch the historical data of QQQ. As we are interested in monthly returns, we resample the daily adjusted closing prices to monthly adjusted closing prices, allowing us to calculate the monthly returns.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as style
import seaborn as sns
import yfinance as yf

style.use("default")

params = {
    "axes.labelsize": 8, "font.size": 8, "legend.fontsize": 8,
    "xtick.labelsize": 8, "ytick.labelsize": 8, "text.usetex": False,
    "font.family": "sans-serif", "axes.spines.top": False, "axes.spines.right": False,
    "grid.color": "grey", "axes.grid": True,  "grid.alpha": 0.5, "grid.linestyle": ":",
}

plt.rcParams.update(params)
# use Yahoo Finance to download historical data for QQQ
# over the last 15 years, from 2006-01-01 to 2021-01-01
qqq_daily = yf.download("QQQ", start="2006-01-01", end="2023-12-31")
qqq_daily["Adj Close"].plot(title="QQQ Daily Adjusted Close", figsize=(5, 3))
plt.show()
[*********************100%%**********************]  1 of 1 completed

# calculate monthly returns of QQQ
qqq_monthly = qqq_daily["Adj Close"].resample("M").ffill().to_frame()
qqq_monthly.index = qqq_monthly.index.to_period("M")
qqq_monthly["Return"] = qqq_monthly["Adj Close"].pct_change() * 100
qqq_monthly.dropna(inplace=True)
qqq_monthly
Adj Close        Return
Date               
2006-02        35.565815        -2.142885
2006-03        36.309525        2.091080
2006-04        36.240250        -0.190790
2006-05        33.616402        -7.240149
2006-06        33.595703        -0.061573
...        ...        ...
2023-08        376.493195        -1.483010
2023-09        357.367859        -5.079862
2023-10        349.986481        -2.065485
2023-11        387.850891        10.818821
2023-12        409.519989        5.586966
215 rows × 2 columns

藤椅
georgefox 发表于 2024-4-18 21:02:20
Step 1. Collect Monthly Returns of Portfolio or Asset
We use Yahoo Finance to collect the daily pricing data of QQQ from 2006 to 2023. The yfinance library is used to fetch the historical data of QQQ. As we are interested in monthly returns, we resample the daily adjusted closing prices to monthly adjusted closing prices, allowing us to calculate the monthly returns.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as style
import seaborn as sns
import yfinance as yf

style.use("default")

params = {
    "axes.labelsize": 8, "font.size": 8, "legend.fontsize": 8,
    "xtick.labelsize": 8, "ytick.labelsize": 8, "text.usetex": False,
    "font.family": "sans-serif", "axes.spines.top": False, "axes.spines.right": False,
    "grid.color": "grey", "axes.grid": True,  "grid.alpha": 0.5, "grid.linestyle": ":",
}

plt.rcParams.update(params)
# use Yahoo Finance to download historical data for QQQ
# over the last 15 years, from 2006-01-01 to 2021-01-01
qqq_daily = yf.download("QQQ", start="2006-01-01", end="2023-12-31")
qqq_daily["Adj Close"].plot(title="QQQ Daily Adjusted Close", figsize=(5, 3))
plt.show()
[*********************100%%**********************]  1 of 1 completed

# calculate monthly returns of QQQ
qqq_monthly = qqq_daily["Adj Close"].resample("M").ffill().to_frame()
qqq_monthly.index = qqq_monthly.index.to_period("M")
qqq_monthly["Return"] = qqq_monthly["Adj Close"].pct_change() * 100
qqq_monthly.dropna(inplace=True)
qqq_monthly
Adj Close        Return
Date               
2006-02        35.565815        -2.142885
2006-03        36.309525        2.091080
2006-04        36.240250        -0.190790
2006-05        33.616402        -7.240149
2006-06        33.595703        -0.061573
...        ...        ...
2023-08        376.493195        -1.483010
2023-09        357.367859        -5.079862
2023-10        349.986481        -2.065485
2023-11        387.850891        10.818821
2023-12        409.519989        5.586966
215 rows × 2 columns

板凳
georgefox 发表于 2024-4-18 21:02:27
Step 1. Collect Monthly Returns of Portfolio or Asset
We use Yahoo Finance to collect the daily pricing data of QQQ from 2006 to 2023. The yfinance library is used to fetch the historical data of QQQ. As we are interested in monthly returns, we resample the daily adjusted closing prices to monthly adjusted closing prices, allowing us to calculate the monthly returns.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as style
import seaborn as sns
import yfinance as yf

style.use("default")

params = {
    "axes.labelsize": 8, "font.size": 8, "legend.fontsize": 8,
    "xtick.labelsize": 8, "ytick.labelsize": 8, "text.usetex": False,
    "font.family": "sans-serif", "axes.spines.top": False, "axes.spines.right": False,
    "grid.color": "grey", "axes.grid": True,  "grid.alpha": 0.5, "grid.linestyle": ":",
}

plt.rcParams.update(params)
# use Yahoo Finance to download historical data for QQQ
# over the last 15 years, from 2006-01-01 to 2021-01-01
qqq_daily = yf.download("QQQ", start="2006-01-01", end="2023-12-31")
qqq_daily["Adj Close"].plot(title="QQQ Daily Adjusted Close", figsize=(5, 3))
plt.show()
[*********************100%%**********************]  1 of 1 completed

# calculate monthly returns of QQQ
qqq_monthly = qqq_daily["Adj Close"].resample("M").ffill().to_frame()
qqq_monthly.index = qqq_monthly.index.to_period("M")
qqq_monthly["Return"] = qqq_monthly["Adj Close"].pct_change() * 100
qqq_monthly.dropna(inplace=True)
qqq_monthly
Adj Close        Return
Date               
2006-02        35.565815        -2.142885
2006-03        36.309525        2.091080
2006-04        36.240250        -0.190790
2006-05        33.616402        -7.240149
2006-06        33.595703        -0.061573
...        ...        ...
2023-08        376.493195        -1.483010
2023-09        357.367859        -5.079862
2023-10        349.986481        -2.065485
2023-11        387.850891        10.818821
2023-12        409.519989        5.586966
215 rows × 2 columns

报纸
georgefox 发表于 2024-4-18 21:02:34
Step 1. Collect Monthly Returns of Portfolio or Asset
We use Yahoo Finance to collect the daily pricing data of QQQ from 2006 to 2023. The yfinance library is used to fetch the historical data of QQQ. As we are interested in monthly returns, we resample the daily adjusted closing prices to monthly adjusted closing prices, allowing us to calculate the monthly returns.

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.style as style
import seaborn as sns
import yfinance as yf

style.use("default")

params = {
    "axes.labelsize": 8, "font.size": 8, "legend.fontsize": 8,
    "xtick.labelsize": 8, "ytick.labelsize": 8, "text.usetex": False,
    "font.family": "sans-serif", "axes.spines.top": False, "axes.spines.right": False,
    "grid.color": "grey", "axes.grid": True,  "grid.alpha": 0.5, "grid.linestyle": ":",
}

plt.rcParams.update(params)
# use Yahoo Finance to download historical data for QQQ
# over the last 15 years, from 2006-01-01 to 2021-01-01
qqq_daily = yf.download("QQQ", start="2006-01-01", end="2023-12-31")
qqq_daily["Adj Close"].plot(title="QQQ Daily Adjusted Close", figsize=(5, 3))
plt.show()
[*********************100%%**********************]  1 of 1 completed

# calculate monthly returns of QQQ
qqq_monthly = qqq_daily["Adj Close"].resample("M").ffill().to_frame()
qqq_monthly.index = qqq_monthly.index.to_period("M")
qqq_monthly["Return"] = qqq_monthly["Adj Close"].pct_change() * 100
qqq_monthly.dropna(inplace=True)
qqq_monthly
Adj Close        Return
Date               
2006-02        35.565815        -2.142885
2006-03        36.309525        2.091080
2006-04        36.240250        -0.190790
2006-05        33.616402        -7.240149
2006-06        33.595703        -0.061573
...        ...        ...
2023-08        376.493195        -1.483010
2023-09        357.367859        -5.079862
2023-10        349.986481        -2.065485
2023-11        387.850891        10.818821
2023-12        409.519989        5.586966
215 rows × 2 columns

地板
tianwk 发表于 2024-4-19 00:24:33
thanks for sharing

7
yiyijiayuan 在职认证  发表于 2024-4-20 03:15:17
纯粹路过。

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2026-2-6 05:43