Python笔记-CAPM(资本资产定价模型)例子

x33g5p2x  于2022-02-28 转载在 Python  
字(3.5k)|赞(0)|评价(0)|浏览(168)

数据是这样对应的

| 沪深300 | 000300.SH.csv |
| 茅台 | 600519.SH.csv |
| 平安 | 601318.SH.csv |

如下代码:

# -*- coding: utf-8 -*-

import pandas as pd
import statsmodels.api as sm

if __name__ == '__main__':
    hs300 = pd.read_csv("000300.SH.csv", index_col="date")
    maoTai = pd.read_csv("600519.SH.csv", index_col="date")
    pingAn = pd.read_csv("601318.SH.csv", index_col="date")

    stock_list = [maoTai, pingAn, hs300]
    df = pd.concat([stock.pctChg / 100 for stock in stock_list], axis=1)
    df.columns = ["maoTai", "pingAn", "hs300"]
    df = df.sort_index(ascending=True)
    print(df.describe())

    # 填充数据
    returns = (df + 1).product() - 1
    print('累计收益率\n', returns)

    # 假设无风险固定收益为3.2%,那么平均每日的无风险收益率为
    rf = 1.032 ** (1 / 360) - 1
    print("平均每日的无风险收益率为: ", rf)

    # 茅台或平安 和 沪深300各自的风险溢价
    df_rp = df - rf

    stock_names = {
        'pingAn': '中国平安',
        'maoTai': '贵州茅台'
    }
    for stock in ["pingAn", "maoTai"]:
        model = sm.OLS(df_rp[stock], sm.add_constant(df_rp['hs300']))
        result = model.fit()
        print(stock_names[stock] + '\n')
        print(result.summary())
        print('\n\n')

    pass

运行如下:

D:\python\content\python.exe D:/PythonProject/demo/demo22.py
           maoTai      pingAn       hs300
count  243.000000  243.000000  243.000000
mean     0.000420   -0.001960   -0.000151
std      0.023567    0.016815    0.011708
min     -0.069911   -0.054476   -0.035325
25%     -0.012650   -0.011324   -0.006741
50%      0.000323   -0.003655    0.000398
75%      0.014569    0.004840    0.006918
max      0.095041    0.077337    0.031595
累计收益率
 maoTai    0.035688
pingAn   -0.399967
hs300    -0.051986
dtype: float64
平均每日的无风险收益率为:  8.750012529978868e-05
中国平安

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 pingAn   R-squared:                       0.249
Model:                            OLS   Adj. R-squared:                  0.245
Method:                 Least Squares   F-statistic:                     79.70
Date:                Tue, 18 Jan 2022   Prob (F-statistic):           1.14e-16
Time:                        15:49:00   Log-Likelihood:                 683.18
No. Observations:                 243   AIC:                            -1362.
Df Residuals:                     241   BIC:                            -1355.
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const         -0.0019      0.001     -2.002      0.046      -0.004   -3.04e-05
hs300          0.7159      0.080      8.927      0.000       0.558       0.874
==============================================================================
Omnibus:                       47.787   Durbin-Watson:                   2.111
Prob(Omnibus):                  0.000   Jarque-Bera (JB):              114.952
Skew:                           0.906   Prob(JB):                     1.09e-25
Kurtosis:                       5.841   Cond. No.                         85.6
==============================================================================

Notes:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.


贵州茅台

                            OLS Regression Results                            
==============================================================================
Dep. Variable:                 maoTai   R-squared:                       0.447
Model:                            OLS   Adj. R-squared:                  0.445
Method:                 Least Squares   F-statistic:                     195.1
Date:                Tue, 18 Jan 2022   Prob (F-statistic):           7.08e-33
Time:                        15:49:00   Log-Likelihood:                 638.49
No. Observations:                 243   AIC:                            -1273.
Df Residuals:                     241   BIC:                            -1266.
Df Model:                           1                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
const          0.0007      0.001      0.581      0.562      -0.002       0.003
hs300          1.3463      0.096     13.967      0.000       1.156       1.536
==============================================================================
Omnibus:                       34.503   Durbin-Watson:                   2.089
Prob(Omnibus):                  0.000   Jarque-Bera (JB):               73.171
Skew:                           0.699   Prob(JB):                     1.29e-16
Kurtosis:                       5.296   Cond. No.                         85.6
==============================================================================

这个数据的看法,关键是看这3个数据:

上面是平安的,截距项为-0.0019,意思就是除开大盘波动,自身还亏0.19%。β为0.7159,代表如大盘涨了10%,平安预期涨7.159%,R方为0.24代表拟合效果一般。

下面是茅台的

茅台的截距项为0.0007,代表除大盘波动带来的收益,其自身加载额外产生了0.07%的收益,β为1.3463,就是如果大盘涨了10%,那么茅台也涨13.463%,R方为0.44代表一般(0.5以上代码拟合可以)

相关文章