Python笔记-CAPM(资本资产定价模型)例子
生活随笔
收集整理的這篇文章主要介紹了
Python笔记-CAPM(资本资产定价模型)例子
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
數(shù)據(jù)是這樣對應(yīng)的
| 滬深300 | 000300.SH.csv |
| 茅臺 | 600519.SH.csv |
| 平安 | 601318.SH.csv |
如下代碼:
# -*- coding: utf-8 -*-import pandas as pd import statsmodels.api as smif __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())# 填充數(shù)據(jù)returns = (df + 1).product() - 1print('累計(jì)收益率\n', returns)# 假設(shè)無風(fēng)險固定收益為3.2%,那么平均每日的無風(fēng)險收益率為rf = 1.032 ** (1 / 360) - 1print("平均每日的無風(fēng)險收益率為: ", rf)# 茅臺或平安 和 滬深300各自的風(fēng)險溢價df_rp = df - rfstock_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運(yùn)行如下:
D:\python\content\python.exe D:/PythonProject/demo/demo22.pymaoTai 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 累計(jì)收益率maoTai 0.035688 pingAn -0.399967 hs300 -0.051986 dtype: float64 平均每日的無風(fēng)險收益率為: 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 ==============================================================================這個數(shù)據(jù)的看法,關(guān)鍵是看這3個數(shù)據(jù):
?上面是平安的,截距項(xiàng)為-0.0019,意思就是除開大盤波動,自身還虧0.19%。β為0.7159,代表如大盤漲了10%,平安預(yù)期漲7.159%,R方為0.24代表擬合效果一般。
下面是茅臺的
?茅臺的截距項(xiàng)為0.0007,代表除大盤波動帶來的收益,其自身加載額外產(chǎn)生了0.07%的收益,β為1.3463,就是如果大盤漲了10%,那么茅臺也漲13.463%,R方為0.44代表一般(0.5以上代碼擬合可以)
總結(jié)
以上是生活随笔為你收集整理的Python笔记-CAPM(资本资产定价模型)例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ios label文字行间距_iOS-
- 下一篇: MySQL笔记-Windows安装MyS