统计中常用标准差衡量随机变量离散程度,在金融市场中标准差被称之为波动率。
所以首先需要先从excel中导入交易数据
引入Shibor​​1​​,Libor​2​​ , Hibor​3

  1. 使用lambda函数求平均值
  2. 使用def语法求波动率

Notice:

1

import

2

data= xlrd.open_workbook('SLH.xlsx')
table = data.sheet_by_name(u'Sheet1')

3

col_values(列数,起点,终点)左闭右开

Full code

import xlrd
data= xlrd.open_workbook('SLH.xlsx')
table = data.sheet_by_name(u'Sheet1')
#print(table.row_values(2))
Shibor=table.col_values(1,1,16)
Libor=table.col_values(2,1,16)
Hibor=table.col_values(3,1,16)
print(Shibor)
print(Libor)
print(Hibor)
f_mean=lambda x: sum(x)/len(x)
Shibor_mean=f_mean(x=Shibor)
Libor_mean=f_mean(x=Libor)
Hibor_mean=f_mean(x=Hibor)
# print(round(Shibor_mean,6))
# print(round(Libor_mean,6))
# print(round(Hibor_mean,6))
def f_sigma(x):
n=len(x)
u_mean=sum(x)/n;z=[]
for t in range(n):
z.append((x[t]-u_mean)**2)
return (sum(z)/(n-1))**0.5
Shibor_sigma=f_sigma(x=Shibor)
Libor_sigma=f_sigma(x=Libor)
Hibor_sigma=f_sigma(x=Hibor)
print(round(Shibor_sigma,6))
print(round(Libor_sigma,6))
print(round(Hibor_sigma,6))

  1. 上海银行间同业拆放利率(Shanghai Interbank Offered Rate,简称Shibor。引用自​​百度知道​​ ​​↩︎​
  2. LIBOR利率(London Interbank Offered Rate,简写LIBOR ),即伦敦同业拆借利率。引用自​​百度知道​​ ​​↩︎​
  3. HIBOR (Hongkong InterBank Offered Rate),是香港银行同行业拆借利率。引用自​​百度知道​​ ​​↩︎​