关键词搜索

源码搜索 ×
×

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

发布2022-02-28浏览1726次

详情内容

数据是这样对应的

沪深300000300.SH.csv
茅台600519.SH.csv
平安601318.SH.csv

如下代码:

  1. # -*- coding: utf-8 -*-
  2. import pandas as pd
  3. import statsmodels.api as sm
  4. if __name__ == '__main__':
  5. hs300 = pd.read_csv("000300.SH.csv", index_col="date")
  6. maoTai = pd.read_csv("600519.SH.csv", index_col="date")
  7. pingAn = pd.read_csv("601318.SH.csv", index_col="date")
  8. stock_list = [maoTai, pingAn, hs300]
  9. df = pd.concat([stock.pctChg / 100 for stock in stock_list], axis=1)
  10. df.columns = ["maoTai", "pingAn", "hs300"]
  11. df = df.sort_index(ascending=True)
  12. print(df.describe())
  13. # 填充数据
  14. returns = (df + 1).product() - 1
  15. print('累计收益率\n', returns)
  16. # 假设无风险固定收益为3.2%,那么平均每日的无风险收益率为
  17. rf = 1.032 ** (1 / 360) - 1
  18. print("平均每日的无风险收益率为: ", rf)
  19. # 茅台或平安 和 沪深300各自的风险溢价
  20. df_rp = df - rf
  21. stock_names = {
  22. 'pingAn': '中国平安',
  23. 'maoTai': '贵州茅台'
  24. }
  25. for stock in ["pingAn", "maoTai"]:
  26. model = sm.OLS(df_rp[stock], sm.add_constant(df_rp['hs300']))
  27. result = model.fit()
  28. print(stock_names[stock] + '\n')
  29. print(result.summary())
  30. print('\n\n')
  31. pass

运行如下:

  1. D:\python\content\python.exe D:/PythonProject/demo/demo22.py
  2. maoTai pingAn hs300
  3. count 243.000000 243.000000 243.000000
  4. mean 0.000420 -0.001960 -0.000151
  5. std 0.023567 0.016815 0.011708
  6. min -0.069911 -0.054476 -0.035325
  7. 25% -0.012650 -0.011324 -0.006741
  8. 50% 0.000323 -0.003655 0.000398
  9. 75% 0.014569 0.004840 0.006918
  10. max 0.095041 0.077337 0.031595
  11. 累计收益率
  12. maoTai 0.035688
  13. pingAn -0.399967
  14. hs300 -0.051986
  15. dtype: float64
  16. 平均每日的无风险收益率为: 8.750012529978868e-05
  17. 中国平安
  18. OLS Regression Results
  19. ==============================================================================
  20. Dep. Variable: pingAn R-squared: 0.249
  21. Model: OLS Adj. R-squared: 0.245
  22. Method: Least Squares F-statistic: 79.70
  23. Date: Tue, 18 Jan 2022 Prob (F-statistic): 1.14e-16
  24. Time: 15:49:00 Log-Likelihood: 683.18
  25. No. Observations: 243 AIC: -1362.
  26. Df Residuals: 241 BIC: -1355.
  27. Df Model: 1
  28. Covariance Type: nonrobust
  29. ==============================================================================
  30. coef std err t P>|t| [0.025 0.975]
  31. ------------------------------------------------------------------------------
  32. const -0.0019 0.001 -2.002 0.046 -0.004 -3.04e-05
  33. hs300 0.7159 0.080 8.927 0.000 0.558 0.874
  34. ==============================================================================
  35. Omnibus: 47.787 Durbin-Watson: 2.111
  36. Prob(Omnibus): 0.000 Jarque-Bera (JB): 114.952
  37. Skew: 0.906 Prob(JB): 1.09e-25
  38. Kurtosis: 5.841 Cond. No. 85.6
  39. ==============================================================================
  40. Notes:
  41. [1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
  42. 贵州茅台
  43. OLS Regression Results
  44. ==============================================================================
  45. Dep. Variable: maoTai R-squared: 0.447
  46. Model: OLS Adj. R-squared: 0.445
  47. Method: Least Squares F-statistic: 195.1
  48. Date: Tue, 18 Jan 2022 Prob (F-statistic): 7.08e-33
  49. Time: 15:49:00 Log-Likelihood: 638.49
  50. No. Observations: 243 AIC: -1273.
  51. Df Residuals: 241 BIC: -1266.
  52. Df Model: 1
  53. Covariance Type: nonrobust
  54. ==============================================================================
  55. coef std err t P>|t| [0.025 0.975]
  56. ------------------------------------------------------------------------------
  57. const 0.0007 0.001 0.581 0.562 -0.002 0.003
  58. hs300 1.3463 0.096 13.967 0.000 1.156 1.536
  59. ==============================================================================
  60. Omnibus: 34.503 Durbin-Watson: 2.089
  61. Prob(Omnibus): 0.000 Jarque-Bera (JB): 73.171
  62. Skew: 0.699 Prob(JB): 1.29e-16
  63. Kurtosis: 5.296 Cond. No. 85.6
  64. ==============================================================================

这个数据的看法,关键是看这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以上代码拟合可以)

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载