欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

沪深300与中证500收益率相关性分析_python_数据分析_6

程序员文章站 2024-03-05 15:04:48
...
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
plt.rcParams['font.sans-serif'] = ['KaiTi'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题



def get_data():
    #获取沪深300收益率数列
    HS300 = pd.read_excel('stocks.xlsx',sheet_name='510300',index_col=0)
    # print(HS300)
    HS300RET = HS300.loc[:, 'close'].pct_change()
    HS300RET = HS300RET[~np.isnan(HS300RET)]
    HS300RET = HS300RET * 100
    ZZ500 = pd.read_excel('stocks.xlsx',sheet_name='399905',index_col=0)

    ZZ500RET = ZZ500.loc[:, 'close'].pct_change()
    ZZ500RET = ZZ500RET[~np.isnan(ZZ500RET)]
    ZZ500RET = ZZ500RET * 100
    return HS300RET,ZZ500RET

hs300,zz500= get_data()
stocks = pd.DataFrame(hs300)
stocks.columns = ['hs300',]
stocks.loc[:,'zz500'] = zz500
stocks_corr = stocks.corr()
stocks_cov = stocks.cov()
print('沪深300与中证500收益率相关系数')
print(stocks_corr)
print('沪深300与中证500收益率协方差')
print(stocks_cov)
corr = stocks.loc[:,'hs300'].corr(stocks.loc[:,'zz500'])
print('沪深300与中证500收益率相关系数%.2f'%corr)

plt.scatter(stocks.loc[:,'hs300'],stocks.loc[:,'zz500'])
plt.title('沪深300与中证500收益率散点图')
plt.xlabel('沪深300收益率')
plt.ylabel('中证500收益率')
plt.show()

沪深300与中证500收益率相关性分析_python_数据分析_6

沪深300与中证500收益率相关性分析_python_数据分析_6