金融科技之數據分析:地產股票因子分析

地產股票因子分析

1.課題簡介

實習公司和幾家地產龍頭客戶都有合作,希望能通過分析他們的財務數據,來對股價有個分析或者說預測。

圖片

2.代碼實現

import相關的包

import pandas as pd
from sklearn.linear_model import LinearRegression
from xgboost import XGBRegressor
from xgboost import plot_importance
import matplotlib.pyplot as plt
import lightgbm as lgb
from scipy.stats import pearsonr
import numpy as np
from sklearn import preprocessing

讀取數據與預處理

#min-max規範化方法
min_max_scaler = preprocessing.MinMaxScaler()

#讀取第一個公司的數據
df=pd.read_excel('地產股.xlsx',sheet_name=0)
y=df['股價'].values
x=df.drop(['日期','股價'], axis=1)
headers=x.columns#這裏存儲因子的名字,方便結果展示
x=x.values
#規範化處理,映射到【0,1】
x=min_max_scaler.fit_transform(x)
y=min_max_scaler.fit_transform(np.array([y]).T)

#讀取其他四個公司的數據,進行標準化處理,並將數據合併在一起
for i in range(1,5):
    df=pd.read_excel('地產股.xlsx',sheet_name=i)
    tempy=df['股價'].values
    tempx=df.drop(['日期','股價'], axis=1)
    tempx=tempx.values
    tempx = min_max_scaler.fit_transform(tempx)
    tempy=min_max_scaler.fit_transform(np.array([tempy]).T)
    x=np.vstack((x,tempx))#將數據合併
    y=np.vstack((y,tempy))

注:這裏是將各個公司的數據分別進行標準化處理之後再合併,因爲考慮到不同公司的股價的區間不一致,這可能是由上市公司發放股票的策略決定的。而不同公司的股票的價差不是我們關注的重點,我們關注的是股票縱向變化的規律。將各個公司分別標準化可以消除公司間的一些差異。

線性迴歸版本

線性迴歸模型各因子的係數可以反映各因子的重要性

regr_ = LinearRegression().fit(x, y)#訓練模型
coef_=regr_.coef_[0]#係數
lr_res=[(headers[i],coef_[i]) for i in range(len(coef_))]
lr_res=sorted(lr_res,key=lambda x:x[1],reverse=True)#排序

header=[header for header,coef in lr_res]
coef=[coef for header,coef in lr_res]
lr_res=pd.DataFrame({'factor':header,'coef':coef})

結果:

factor coef
總資產報酬率 0.842764767
總市值(萬元) 0.448989966
每股營業收入 0.354270929
PE 0.266525706
市淨率PB 0.192295946
每股收益增速% 0.104891241
長期資產負債率 0.092473499
近12個月股息率 0.048185849
市現率 0.041146731
每股淨經營現金流淨額 0.039554912
資產負債率 0.014880207
銷售淨利率 -0.023532551
權益乘數 -0.046475972
每股息稅前利潤 -0.074944096
每股收益EPS -0.08202864
投入資本回報率ROIC -0.110803053
歸屬於母公司利潤增速(扣除非經常性損益) -0.176884016
淨資產收益率ROE -0.211790641
市銷率PS -0.248624017
存貨週轉率 -0.555995589

pearsonr相關性係數版本

corr_res=[]
for i in range(len(headers)):
    corr,p=pearsonr(x[:,i],y.ravel())#獲取相關性和p-value
    corr_res.append((headers[i],corr,p))
corr_res=sorted(corr_res,key=lambda x:x[1],reverse=True)#排序

header=[header for header,corr,p in corr_res]
corr=[corr for header,corr,p in corr_res]
p=[p for header,corr,p in corr_res]
corr_res=pd.DataFrame({'factor':header,'corr':corr,'p_value':p})

結果:

factor corr p_value
總市值(萬元) 0.744397542 1.0249E-245
每股營業收入 0.600173815 8.7595E-137
每股收益EPS 0.513859816 1.62518E-94
權益乘數 0.504026043 1.95423E-90
市淨率PB 0.487645622 6.28211E-84
資產負債率 0.478420179 2.04251E-80
長期資產負債率 0.428756079 2.96606E-63
每股息稅前利潤 0.353733849 3.08641E-42
市銷率PS 0.319607724 2.2237E-34
近12個月股息率 0.25933871 8.46324E-23
淨資產收益率ROE 0.249068466 4.23874E-21
投入資本回報率ROIC 0.220798206 8.2545E-17
銷售淨利率 0.203669717 1.76032E-14
總資產報酬率 0.199825776 5.50691E-14
PE 0.182179083 7.73554E-12
每股淨經營現金流淨額 0.139141612 1.90617E-07
每股收益增速% 0.110584575 3.59838E-05
存貨週轉率 0.077161548 0.003995772
市現率 0.006789981 0.800326919
歸屬於母公司利潤增速(扣除非經常性損益) -0.012468487 0.642319104

XGBRegressor模型

model = XGBRegressor(importance_type='weight')
model.fit(x, y.ravel())#訓練模型
importances_=model.feature_importances_#獲取因子重要性

xgb_res=[(i,headers[i],importances_[i]) for i in range(len(model.feature_importances_))]
xgb_res=sorted(xgb_res,key=lambda x:x[2],reverse=True)#排序

index=[index for index,header,importance in xgb_res]
header=[header for index,header,importance in xgb_res]
importances=[importance for index,header,importance in xgb_res]
xgb_res=pd.DataFrame({'index':index,'factor':header,'importance':importances})#轉換成dataframe

plt.figure(figsize=(12,6))#畫圖
plot_importance(model, max_num_features=8)
plt.title("XGB")
plt.show()
print(xgb_res)

結果:

圖片

index即爲上方圖片中的序號

index factor importance
0 總市值(萬元) 0.295882344
1 PE 0.146764711
2 市淨率PB 0.113529414
5 近12個月股息率 0.107058823
4 市現率 0.104117647
3 市銷率PS 0.084411763
7 每股淨經營現金流淨額 0.01764706
8 每股營業收入 0.014705882
6 每股收益EPS 0.013529412
9 每股息稅前利潤 0.013529412
12 銷售淨利率 0.012058823
14 長期資產負債率 0.012058823
18 每股收益增速% 0.011764706
13 資產負債率 0.010882352
17 投入資本回報率ROIC 0.009705883
10 淨資產收益率ROE 0.008529412
16 存貨週轉率 0.006764706
19 歸屬於母公司利潤增速(扣除非經常性損益) 0.006176471
11 總資產報酬率 0.005588235
15 權益乘數 0.005294118

LGBMRegressor模型

gbm = lgb.LGBMRegressor(objective='regression')
gbm.fit(x, y.ravel())
importances_=gbm.feature_importances_
gbm_res=[(i,headers[i],importances_[i]) for i in range(len(importances_))]
gbm_res=sorted(gbm_res,key=lambda x:x[2],reverse=True)#排序

index=[index for index,header,importance in gbm_res]
header=[header for index,header,importance in gbm_res]
importances=[importance for index,header,importance in gbm_res]
gbm_res=pd.DataFrame({'index':index,'factor':header,'importance':importances})

lgb.plot_importance(gbm, max_num_features=20)
plt.title("GBM")
plt.show()

結果:

圖片

index factor importance
0 總市值(萬元) 561
5 近12個月股息率 428
2 市淨率PB 318
1 PE 310
4 市現率 291
3 市銷率PS 288
8 每股營業收入 101
6 每股收益EPS 84
14 長期資產負債率 84
7 每股淨經營現金流淨額 68
17 投入資本回報率ROIC 68
13 資產負債率 59
12 銷售淨利率 58
9 每股息稅前利潤 57
18 每股收益增速% 50
11 總資產報酬率 39
19 歸屬於母公司利潤增速(扣除非經常性損益) 39
10 淨資產收益率ROE 38
15 權益乘數 32
16 存貨週轉率 27

保存結果到excel中

with pd.ExcelWriter('res.xlsx') as writer:
    corr_res.to_excel(writer, 'corr_res', index=False)
    lr_res.to_excel(writer, 'lr_res', index=False)
    xgb_res.to_excel(writer,'xgb_res',index=False)
    gbm_res.to_excel(writer,'gbm_res',index=False)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章