第八章 上市公司综合评价
import os
os.chdir("C:\\Users\\Administrator\\Desktop")
import pandas as pd
import numpy as np
8.3 基于总体规模与投资效率的综合评价
8.3.1 数据读取与处理
data=pd.read_excel('data.xlsx')
data2=data.iloc[data['Accper'].values=='2016-12-31',[ 0,2,3,4,5,6,7,8,9,10,11]]
data2=data2[data2>0]
data2=data2.dropna()
from sklearn.preprocessing import StandardScaler
X=data2.iloc[:,1:]
scaler = StandardScaler()
scaler.fit(X)
X=scaler.transform(X)
print(X)
[[-0.12077184 -0.20946319 -0.21733916 ... -0.6705318 -0.43377652
-0.5735834 ]
[ 0.69083519 0.04012502 0.31852587 ... -0.4671922 2.19794302
-0.36514147]
[-0.00884509 -0.01754464 -0.02982446 ... 0.1218636 0.52963048
0.81434203]
...
[-0.05291601 -0.09141147 -0.10170258 ... 0.03683563 0.57123415
1.19270256]
[-0.1299293 -0.13124677 -0.14376188 ... -0.46828542 -0.37996209
0.53013973]
[-0.03878784 0.02360231 0.15033841 ... 0.54963119 1.37186741
-0.28226897]]
8.3.2 主成分分析
from sklearn.decomposition import PCA
pca=PCA(n_components=0.95)
Y=pca.fit_transform(X)
gxl=pca.explained_variance_ratio_
import numpy as np
F=np.zeros((len(Y)))
for i in range(len(gxl)):
f=Y[:,i]*gxl[i]
F=F+f
fs1=pd.Series(F,index=data2['Stkcd'].values)
Fscore1=fs1.sort_values(ascending=False)
co=pd.read_excel('TRD_Co.xlsx')
Co=pd.Series(co['Stknme'].values,index=co['Stkcd'].values)
Co1=Co[data2['Stkcd'].values]
fs2=pd.Series(F,index=Co1.values)
Fscore2=fs2.sort_values(ascending=False)
print(Fscore2)
中国石化 30.563527
中国石油 24.194503
中国建筑 17.446082
上汽集团 16.703156
中国神华 13.124306
...
华控赛格 -0.828231
凌云B股 -0.838433
亚太实业 -0.843052
津滨发展 -0.843923
湖南天雁 -0.844017
Length: 2632, dtype: float64
8.3.3 收益率计算
trd=pd.read_excel('trd_'+str(2017)+'.xlsx')
r_list=[]
for i in range(30):
code=Fscore1.index[i]
dt=trd.iloc[trd.iloc[:,0].values==code,:]
I1=dt['Trddt'].values>='2017-05-01'
I2=dt['Trddt'].values<='2017-12-31'
dtt=dt.iloc[I1&I2,:].sort_values('Trddt')
if len(dtt)>1:
p1=dtt.iloc[0,3]
p2=dtt.iloc[len(dtt)-1,3]
r_list.append((p2-p1)/p1)
r_total=sum(r_list)
print(r_list)
print(r_total)
[0.11848232513294617, 0.06639396173239795, -0.00957204605344639, 0.24324498700188213, 0.33375393445521223, 0.718365987273417, 0.6724152918201658, -0.25877663920161725, 0.167044365645404, -0.13941897927926225, 0.5521399919651451, -0.041227204250203285, 0.6325010784315266, 0.3862167370074773, -0.158298128127196, -0.13344885503590906, 0.3732512878750164, 0.018515265504756885, -0.33504476427681906, 0.2319943293144153, 0.06135919351588847, -0.06931526128058003, 0.42151090413976866, -0.13356645323993702, 0.6683480412877023, 0.4315378745751686, -0.15229884531475582, -0.12314705275614667]
4.542961327862418
8.4 基于成长与价值的综合评价
8.4.1 读取数据与处理
data=pd.read_excel('data1.xlsx')
data2=data.iloc[data['Accper'].values=='2014-12-31',[0,2,3,4,5,6,7,8,9]]
da=data2.values
da=da[(da[:,8]<4)&(da[:,8]>-4),:]
da[:,8]=1-(da[:,8]-min(da[:,8]))/(max(da[:,8])-min(da[:,8]))
for i in np.arange(1,8):
da=da[da[:,i]>0,:]
da=da[da[:,i]<8*np.mean(da[:,i]),:]
da=da[da[:,6]>=0.06,:]
from sklearn.preprocessing import MinMaxScaler
X=da[:,1:]
scaler = MinMaxScaler()
scaler.fit(X)
X=scaler.transform(X)
X[:,2]=1-X[:,2]
X[:,3]=1-X[:,3]
X[:,4]=1-X[:,4]
8.4.2 主成分分析
from sklearn.decomposition import PCA
pca=PCA(n_components=0.95)
Y=pca.fit_transform(X)
gxl=pca.explained_variance_ratio_
F=np.zeros((len(Y)))
for i in range(len(gxl)):
f=Y[:,i]*gxl[i]
F=F+f
Fs1=pd.Series(F,index=da[:,0])
Fscore1=Fs1.sort_values(ascending=False)
co=pd.read_excel('TRD_Co.xlsx')
Co=pd.Series(co['Stknme'].values,index=co['Stkcd'].values)
Co1=Co[da[:,0]]
Fs2=pd.Series(F,index=Co1.values)
Fscore2=Fs2.sort_values(ascending=False)
print(Fscore2)
卫士通 0.368559
碧水源 0.362696
天华院 0.327474
东方财富 0.318198
卫宁健康 0.263902
...
中国建筑 -0.118624
友好集团 -0.124643
中国电建 -0.138318
中国交建 -0.142683
中国铁建 -0.143686
Length: 393, dtype: float64
8.4.3 收益率计算
trd=pd.read_excel('trd_2015.xlsx')
r_list=[]
for i in range(20):
code=Fscore1.index[i]
dt=trd.iloc[trd.iloc[:,0].values==code,:]
I1=dt['Trddt'].values>='2015-05-01'
I2=dt['Trddt'].values<='2015-12-31'
dtt=dt.iloc[I1&I2,:].sort_values('Trddt')
if len(dtt)>1:
p1=dtt.iloc[0,3]
p2=dtt.iloc[len(dtt)-1,3]
r_list.append((p2-p1)/p1)
r_total=sum(r_list)
print(r_list)
print(r_total)
[-0.29099395425856417, 0.0016465509116702908, 0.13170400063037052, -0.3234949951148852, -0.3635258329288782, 0.03491991497445836, -0.3254119292919446, 0.08069107621569863, -0.03410173778894735, -0.08281314491633895, -0.0008824897477356481, -0.22985003208399216, -0.48254381330704277, 0.13847581256734715, -0.3634755889760709, 0.30135162937699844, -0.08644292394733565, 0.49002629865897385, 0.5785515572286188, 0.8824904840499304]
0.05632088225233067