Codes (Latex Environment)
\chapter{附件}
\section{Python代码}
编程软件:Python3.8\ jupyter\ notebook
电脑系统:Window\ 10\ 家庭中文版
电脑型号:ASUS\ TUF\ Gaming\ FA506IU
处理器:AMD\ Ryzen\ 7\ 4800H\ with\ Radeon\ Graphics\ 2.90GHz
电脑内存(RAM):16.0GB
\subsection{收益率边缘分布}
\begin{lstlisting}[language=Python]
import numpy as np
import pandas as pd
import scipy.stats as st
from scipy.stats import t
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']
sh = pd.read_csv('上证指数历史数据(2).csv')
sz = pd.read_csv('深证成份指数历史数据(2).csv')
hs = pd.read_csv('香港恒生指数历史数据.csv')
def get_pct(data):
data.index = data['日期']
data_pct = data ['涨跌幅']
return data_pct
sh_pct = get_pct(sh)
sz_pct = get_pct(sz)
hs_pct = get_pct(hs)
#边缘密度
def show_edge(data,choose):
mu = data.mean()#.values
sigma = data.std()#.values
num_bins = 100
x = mu + sigma * np.random.randn(1000000)
# 正态分布的数据
data.hist(bins=300,figsize=(16,8),density = True,range=(-0.1,0.1)
,color='b',alpha=0.6,label='密度函数')
n, bins, patches = plt.hist(x, num_bins, density = True,alpha=0)
# 拟合曲线
y = st.norm.pdf(bins, mu, sigma)
plt.plot(bins, y,'r', alpha = 0.8,linewidth=2.5,label='正态分布')
#data.plot(kind = 'kde', color = 'g',linewidth=1.5,alpha=1,
label = '核密度图')
t_dimen = 10
z = np.linspace(t.ppf(0.01, t_dimen,loc=mu,scale=sigma),t.ppf(0.99
,t_dimen,loc=mu,scale=sigma), 1000)
plt.plot(z, t.pdf(z, t_dimen,loc=mu,scale=sigma), alpha=1.0
,linewidth=2.5, c='gray',label='t-分布')
scipy_kde=st.gaussian_kde(data)#高斯核密度估计
X_plot=np.linspace(-0.1,0.1,1000)
dens=scipy_kde.evaluate(X_plot)
plt.plot(X_plot,dens,c='g',linewidth=2.5,label='核密度图')
font = {'size': 15}#设置横纵坐标的名称以及对应字体格式、大小
plt.xlabel('Expectation',font)
plt.ylabel('Probability',font)
plt.title('histogram of normal distribution: $\mu =$' +
str(mu)[:6] + ', $\sigma =$'+ str(sigma)[:6],font)
axes = plt.gca()
axes.set_xlim([-0.075,0.075])
#显示题注
plt.legend(loc=0)
plt.legend(fontsize=15)#显示图例,设置图例字体大小
plt.grid(True)
plt.savefig(str(choose)+'.png')
plt.show()
show_edge(sh_pct,choose='sh')
show_edge(sz_pct,choose='sz')
show_edge(hs_pct,choose='hs')
time_preiod = ['2015年12月9日','2016年12月9日','2017年12月8日'
,'2018年12月7日','2019年12月9日','2020年12月9日']
def get_period_pct(time_preiod):
n = len(time_preiod)
S=[]
for i in range(n-1):
sh_mean,sh_std = sh_pct[time_preiod[i]:time_preiod[i+1]]
.mean(),sh_pct[time_preiod[i]:time_preiod[i+1]].std()
sz_mean,sz_std = sz_pct[time_preiod[i]:time_preiod[i+1]]
.mean(),sz_pct[time_preiod[i]:time_preiod[i+1]].std()
hs_mean,hs_std = hs_pct[time_preiod[i]:time_preiod[i+1]]
.mean(),hs_pct[time_preiod[i]:time_preiod[i+1]].std()
S.append([sh_mean,sh_std,sz_mean,sz_std,hs_mean,hs_std])
return pd.DataFrame(S)
get_period_pct(time_preiod)
\end{lstlisting}
\subsection{相关系数及等高线}
\begin{lstlisting}[language=Python]
def merge(datalist):
n=len(datalist)
result = pd.DataFrame(datalist[0]).copy()
result.columns=[0]
for i in range(1,n):