用插值法求国债收益率

用插值法求国债收益率

-- coding: utf-8 --

“”"
Created on Tue Mar 16 20:41:11 2021

@author: Administrator
“”"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pylab import mpl
mpl.rcParams[‘font.sans-serif’]=[‘SimHei’]
mpl.rcParams[‘axes.unicode_minus’]=False #rc_pmpl.rc_params[‘axes.unicode_minus’]=False
from scipy import interpolate
t=np.array([0.25,0.5,0.75,1.0,3.0,5.0])
t_new=np.array([0.25,0.5,0.75,1.0,1.5,2.5,3.0,4.0,5.0])#值域范围在中间,插值法
rates=np.array([0.27344,0.27898,0.28382,0.2882,0.30414,0.31746])
types=[“nearest”,“zero”,“slinear”,“quadratic”,“cubic”]
plt.figure(figsize=(8,6))
for i in types:
f=interpolate.interp1d(x=t,y=rates,kind=i)
#print(f)
rates_new=f(t_new)
print(i,rates_new)
plt.plot(t_new,rates_new,‘o’) #曲线的点o
plt.plot(t_new,rates_new,’-’,label=i) # 坐标画图用循环语句
plt.xticks(fontsize=14)
plt.xlabel(u’期限’,fontsize=14) #X轴
plt.yticks(fontsize=14)
plt.ylabel(u’收益率’,fontsize=14,rotation=0)
plt.legend(loc=0,fontsize=14)
plt.title(u’用插值法求2年期和4年期的远期国债到期收益率’)

上一篇:C/C++ 关于 const


下一篇:drf频率组件