Three-minute introduction to quantification (8): Capital Asset Pricing Model

hello, i'm edamame. This series uses the most streamlined codes and cases to take you quickly to get started with quantification, and only talks about the most dry goods. Friends who want to learn quantification but don’t know how to get started, hurry up and read it!

Previous review:

Three-minute introduction to quantification (1): Acquisition of market data & drawing candlestick charts

Three-minute introduction to quantification (2): Introduction to Tushare Pro data interface

Three-minute introduction to quantification (3): Calculate the rate of return

Three-minute introduction to quantification (4): Statistical analysis of market data

Three-minute introduction to quantification (5): Inferential statistics of yield

Three-minute introduction to quantification (6): correlation analysis

Three-minute introduction to quantification (7): regression analysis

This issue will introduce the capital asset pricing model.

The Capital Asset Pricing Model (CAPM) is developed on the basis of the portfolio theory and is the pillar of the modern financial market price theory. It mainly studies the relationship between the expected return rate of assets in the securities market and risky assets, and how the equilibrium price is formed. CAPM is a single index model, which believes that there is a positive correlation between the expected return rate of an asset and a scale β value that measures the risk of the asset.

When the capital market reaches equilibrium, the marginal price of risk remains unchanged, and the marginal effect of any investment that changes the market portfolio is the same, that is, the compensation for increasing a unit of risk is the same. According to the definition of β, under the condition of equilibrium capital market, the capital asset pricing model is obtained:

E(ri)=rf+βim(E(rm)-rf)

E(ri): Expected rate of return on asset i

rf: risk-free rate

βim: β coefficient of asset i, that is, systemic risk of asset i

E(rm): Expected rate of return in market m

E(rm)-rf: The risk premium of the market

The description of the capital asset pricing model is as follows: 1. The expected rate of return of a single security consists of two parts, the risk-free rate and the compensation for the risk taken - the risk premium. 2. The size of the risk premium depends on the value of β. The higher the beta value, the riskier the individual security, and the higher the compensation. 3. β measures the systematic risk of a single security, and there is no risk compensation for unsystematic risk.

The CAPM draws a very simple conclusion: there is only one reason for investors to get higher returns, and that is to invest in high-risk stocks. CAPM is not a perfect model, but it provides a model that can measure the size of risk to help investors decide whether the extra return obtained matches the risk.

Let's take a look at how to build a CAPM model with python. Maodou will update this series every weekend. It is recommended that you collect it for easy learning.

Build the CAPM model

First import the relevant packages and authenticate the Tushare Pro account.

import tushare as ts
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.api as sm
import scipy.stats as stats
​
pro = ts.pro_api('your token')

Get the data of CSI 300 Index since this year and calculate the rate of return:

df1=ts.get_k_data('hs300',start='2023-01-01',end='2023-06-02')#沪深300
df1['lagclose']=df1.close.shift(1)
df1['mktRet']=(df1['close']-df1['lagclose'])/df1['lagclose']
df1.tail()

returns as follows:

Take Oriental Fortune (300059) as an example, get the market data since this year, and calculate the rate of return:

df2=ts.get_k_data('300059',start='2023-01-01',end='2023-06-02')#东方财富
df2['lagclose']=df2['close'].shift(1)
df2['dcRet']=(df2['close']-df2['lagclose'])/df2['lagclose']
df2.tail()

returns as follows:

Combine the two sets of yields:

#合并两组收益率
df_mktRet=df1[['date','mktRet']].dropna()
df_dcRet=df2[['date','dcRet']].dropna()
Ret=pd.merge(df_mktRet,df_dcRet,on='date',how='inner')
Ret.tail()

Draw a scatterplot of returns:

#解决中文乱码
plt.rcParams['font.sans-serif']=['Arial Unicode MS']#用于mac
#plt.rcParams['font.sans-serif']=['SimHei']#用于windows
plt.rcParams['axes.unicode_minus'] = False
​
plt.scatter(Ret['dcRet'],Ret['mktRet'])
plt.title('东方财富收益率与沪深300收益率散点图')
plt.xlabel('东方财富收益率')
plt.ylabel('沪深300收益率')
plt.savefig('fig1.png',dpi=400,bbox_inches='tight')

returns as follows:

Calculate the risk-free interest rate. The risk-free rate of return refers to the interest rate that can be obtained by investing funds in a certain investment object without any risk. You can choose the short-term treasury bond rate as the risk-free rate.

# 以国债年化利率3.6%计算无风险收益率
rf=1.036**(1/360)-1
rf

Use the linear regression learned in the previous section to fit the CAPM model:

# 用简单线性回归ols拟合CAPM模型
model=sm.OLS((Ret['dcRet']-rf),sm.add_constant(Ret['mktRet']-rf))
result=model.fit()
result.summary()

returns as follows:

The results show that the β coefficient of Orient Wealth stock is 2.07, that is, for every 1% fluctuation in the market return rate, the stock return rate will fluctuate by 2.07%. For investors, when the market price falls, they can invest in stocks with a lower beta value, and when the market rises, they can invest in stocks with a higher beta value.

The above is all the content of today’s dry goods. Maodou will update this series every weekend, and will continue to share with you the real situation of the Whirlwind Charge quantitative strategy every trading day. Welcome everyone to like and follow.

Guess you like

Origin blog.csdn.net/weixin_37475278/article/details/131271603