用Python对CAPM和Fama French Three Factor model的初步学习

用Python对CAPM和Fama French Three Factor model的初步学习

概述

本文主要使用CAPM(资本资产定价模型)和Fama French Three Factor model来评估中国股票市场的股票价格。 使用CAPM来选择投资组合,然后建立Fama French Three Factor model来评估投资组合。
首先,使用SML绘制CAPM图表,然后随机选择50只股票将其在图上标记,保存高于该线的股票。 接下来显示这些股票之间的相关性。 之后根据收益率和相关性选择我们的投资组合并分配quantity。
其次,建立Fama French Three Factor model,包括计算三个因素,并使用投资组合的回报率进行回归。 在这一部分中,编写了两个函数来使代码更加清晰和高效。
总的来说,本文有三个部分,介绍,建模和分析,以及代码。

介绍

关于两个模型介绍部分不多赘述。简单看一下公式。
在这里插入图片描述在这里插入图片描述

建模与分析

CAPM

本文用的股票数据来源于Tushare,可以注册获取API。官方学生认证的话可以加2000积分。
首先,引入一些第三方库

import tushare
import requests
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import random
import time
pro = tushare.pro_api('官网注册获取')

无风险利率用的是中国国债利率,找了个均值,如果能有时间序列的无风险利率更好,大家如果知道怎么获取到的话,可以留言。
这里用国证指数来表示市场回报率。

# drew the CAPM picture, we need rf and rm
gzA = pro.index_daily(ts_code='399317.SZ', start_date='20000101', end_date='20190901')
gzA = gzA.sort_values('trade_date', ascending=True)
rf = 0.03064  # 3.064%
rm = gzA['pct_chg'].mean()

这里获取市场上股票列表,有3672支股。

stock_basic = pro.stock_basic()
stock_basic.head()

output:
在这里插入图片描述
随机选取50支股票,计算Beta和收益率,Mark在图上。
并且保存在线上的点。

x = np.linspace(0,2,50)
y = rf + x * (rm - rf)
plt.figure(figsize=(16, 12))
plt.title('CAPM', fontsize = 18)
plt.ylim(0 , 0.4)
plt.xlim(0 , 2)
plt.ylabel('E(ri)')
plt.xlabel('Beta')
plt.plot(x,y, label = 'Security Market Line')
plt.scatter(0 , rf , label = 'Rf' , marker='*', color = 'orange', s = 28 ** 2)
#plt.plot(0 , rf , "yo", label = 'rf')
i = 0
portfolio = pd.DataFrame()
while(i<50):
    rd = random.randint(0 ,
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值