首页 > 编程知识 正文

capm模型,capm模型的意义

时间:2023-05-04 04:11:14 阅读:266195 作者:2904

**

利用tushare分析CAPM模型

完成学校投资学的股票估值模型,从csdn上向大家学习了如何使用tushare库,第一次实操,效果感觉还不错。步骤如下:
**
tushare的注册邀请链接:
https://tushare.pro/register?reg=371656

全部代码如下:

import pandas as pdimport tushare as tsts.set_token('')pro = ts.pro_api()shunfeng = pro.daily(ts_code='002352.SZ', start_date='20170101')yindu = pro.daily(ts_code='603277.SH', start_date='20170101')jiaojian = pro.daily(ts_code='601800.SH', start_date='20170101')fenghuo = pro.daily(ts_code='600498.SH', start_date='20170101')guangxian = pro.daily(ts_code='300251.SZ', start_date='20170101')zz500 = pro.index_daily(ts_code='000905.SH', start_date='20170101')stock_list = [shunfeng, yindu, jiaojian, fenghuo, guangxian, zz500]for stock in stock_list: stock.index = pd.to_datetime(stock.trade_date)df = pd.concat([stock.pct_chg / 100 for stock in stock_list], axis=1)df.columns = ['shunfeng', 'yindu', 'jiaojian', 'fenghuo', 'guangxian', 'zz500']df = df.sort_index(ascending=True)df.describe()df = df.fillna(0)returns = (df + 1).product() - 1print('累计收益率:n', returns)print('n标准差:n', df.std())import seaborn as snsimport matplotlib.pyplot as pltimport matplotlib as mplsns.set()mpl.rcParams['font.family'] = 'sans-serif'mpl.rcParams['font.sans-serif'] = 'SimHei'plt.figure(figsize=(10, 5))for col in df.columns: plt.plot(df[col], label=col)plt.title('日收益率时序图(2017至今)', fontsize=20)plt.legend();plt.figure(figsize=(10, 5))for col in df.columns: plt.plot((df[col]+1).cumprod()-1, label=col)plt.title('累计收益率时序图(2017至今)', fontsize=20)plt.legend();rf = 1.023 ** (1/360) - 1print(rf)df_rp = df - rfdf_rp.head()sns.pairplot(df_rp);import statsmodels.api as smstock_names = { 'shunfeng': '顺丰控股', 'yindu': '银都股份', 'jiaojian': '中国交建', 'fenghuo': '烽火通信', 'guangxian': '光线传媒' } for stock in ['shunfeng', 'yindu', 'jiaojian', 'fenghuo', 'guangxian']: model = sm.OLS(df_rp[stock], sm.add_constant(df_rp['zz500'])) result = model.fit() print(stock_names[stock] + 'n') print(result.summary()) print('nn')

截取部分效果图:


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。