请 [注册] 或 [登录]  | 返回主站

量化交易吧 /  量化平台 帖子:3364707 新帖:28

HeathJarrowMorton模型对利率曲线的蒙特卡洛模拟

K线达人发表于:5 月 9 日 23:47回复(1)

好久不写东西了,近期先不谈股票相关的东西,谈谈利率衍生品的内容吧。文章可能一时用不到,但是HJM模型作为利率衍生品的定价神器,相信之后会有勇武之地。

针对股票标的奇异期权定价,在解析解未知的情况下,最容易想到的就是简单粗暴的蒙特卡罗法,其逻辑就是模拟出N条股价的路径,然后对每一条股价路径计算其期权的收益,最后在对这些期权收益求平均,折现。

本人一直好奇为什么蒙特卡洛法是正确的,经过学习得知以下内容:
在新的测度Q下,股票价格的现值为鞅(martingale)。如果期权可以被表示为股票和债券的自融资组合,在无套利的前提下,那么期权价值的现值也为一个鞅。 (反正你相信这个方法正确就可以了。鞅是什么并不重要)。

今天要讨论的并不是股票衍生品,而是利率衍生品。股票衍生品的标的股票在任何时刻是一个点价格,随着时间演变为一条线。而利率衍生品的标的是利率曲线,随着时间过程演变为一个面。标的由点变为面,导致利率衍生品的定价更加复杂。

利率有很多种,即期利率,远期利率等。
即期利率S(t;T)表示站在当前t时刻,t到T区间的利率。
远期利率F(t;T1,T2)表示站在当前t时刻,T1到T2区间的利率。远期利率可以由两个分别在T1,T2到期的零息债券根据无套利条件计算得到。
1.png

如果当T1和T2无穷接近的时候,这时候算出来的远期利率被叫做瞬时远期利率(instaneous forward rate)。
2.png

在HJM模型中,利率曲线的利率是瞬时远期利率。
对于股票衍生品,我们会对股价的运动定义一个SDE(Stochastic Differential Equation):
dS/S=rdtsigmadX
同样,我们也需要对债券衍生品定义一个SDE:
3.png

我们已知瞬时远期利率:
4.png
所以可以得到:
5.png
当前的漂移项是mu,那么在风险中性的世界里,在测度Q下,漂移项变r。
6.png
整理后,上面的式子变为:
7.png
做一个替换:
8.png
上面的式子更新为:
9.png
方便起见,我们把式子写成这个样子:
10.png
在上个式子中,随着时间的变动,t增加,而T不变,这样债券的期限tau就变短了。我们我们希望把式子中的T换为tau。 “rolling on the curve”. 目标结果:
11.png
推导开始:
12.png
13.png
在利率曲线上很多关键年,所以我们的模型为一个多因子模型:
14.png
15.png
利用PCA分解,每一个主成分为如下:
16.png
所以最终的结果如下:
17.png
模拟未来的瞬时远期利率,就等于:
18.png
这时候,我们就完成了一步模拟,得到下一时刻一条瞬时远期利率曲线,在Nstep个dt之后,我们就完成了一次模拟。得到一个“利率曲面”,第三条轴是时间的推移。根据这个“利率曲面”,就可以完成一次衍生品的定价。这件事做N次后,得到N个衍生品的价格,再求平均,折现,就得到了利率衍生品的价格。

下面的完整的程序中,在这一切工作之前,需要在时间序列上,计算不同期限的瞬时远期利率的方差-协方差矩阵,计算其特征值和特征向量,进行主成分分析。得到:
19.png

数据方面,我上传了瞬时远期利率曲线的数据当前瞬时远期利率曲线的数据,百度云盘:
链接:https://pan.baidu.com/s/1naCIad5sFiZ2FOy8KDSYiQ
提取码:sycm

后记:
如果系统的学习衍生品,那么在学什么。个人对衍生品有三点体会:
1.给标的定义一个随机过程。
2.根据无套利条件推导出一个偏微分等式。
3.把这个偏微分等式解出来。

当你有衍生品解析解的时候,剩下的基本上就为所欲为了。

# 从EXCEL中获取数据
import pandas as pd
data = pd.read_excel('InterestData.xlsx', sheetname='Data')
yc0 = pd.read_excel('InterestData.xlsx', sheetname='YC')
data.index=data['Tenor'].values
del data['Tenor']
# 计算远期利率的协方差矩阵,及其特征值和特征向量。
from numpy import linalg as LA
import random

data_value=data.diff().shift(-1).iloc[:-2,:].T
tenor=list(data_value.index)
tenor[0]=0    #由于这个后期要被当做short rate
covmatrix=cov(data_value)*252/10000
covmatrix_df=pd.DataFrame(covmatrix,index=tenor,columns=tenor)
# PCA
w,v=LA.eig(covmatrix)
#主成分我们选取前三大的为主成分
w=w[0:3]
v=v[:,0:3]
# 校准波动率
vol_matrix=[list(ones(shape(v[:,0]))*mean(sqrt(w[0])*v[:,0]))]
poly_matrix=[mean(sqrt(w[0])*v[:,0])]
for i in range(1,3):
    vol=sqrt(w)[i]*v[:,i]
    x_data=np.array(tenor)
    y_data=vol
    poly = np.polyfit(x_data,y_data,deg=3)
    y_fit_data=np.polyval(poly,x_data)
    vol_matrix.append(list(y_fit_data))
    poly_matrix.append(poly)
vol_matrix[2]=list(np.array(vol_matrix[2])*(-1))
vol_df=pd.DataFrame(vol_matrix,index=['Vol1','Vol2','Vol3'],columns=tenor)
# 校准drift项
def M_Calculator(Tau):
    if Tau==0:
        return 0

    dTau=0.01
    N=Tau/dTau

    Vol1=ones(N+1)*poly_matrix[0]
    M1=(0.5*Vol1[0]+sum(Vol1[1:-1])+0.5*Vol1[-1])*dTau
    M1=M1*Vol1[-1]

    x_data=arange(0,Tau+dTau,dTau)
    poly=poly_matrix[1]
    y_fit_data=np.polyval(poly,x_data)
    Vol2=y_fit_data
    M2=(0.5*Vol2[0]+sum(Vol2[1:-1])+0.5*Vol2[-1])*dTau
    M2=M2*Vol2[-1]

    poly=poly_matrix[2]
    y_fit_data=np.polyval(poly,x_data)
    Vol3=y_fit_data
    M3=(0.5*Vol3[0]+sum(Vol3[1:-1])+0.5*Vol3[-1])*dTau
    M3=M3*Vol3[-1]

    M=M1+M2+M3

    return M

# 计算不同期限的m项
m_list=[]
for t in tenor:
    m=M_Calculator(t)
    m_list.append(m)

# 与之前的vol合并为一个dataframe
m_df=pd.DataFrame(m_list,index=tenor,columns=['drift']).T
df=pd.concat([m_df,vol_df],axis=0)
#生产随机数,以及布局初始远期利率曲线。
dt=0.01
Nstep=1000
yc=pd.DataFrame(index=arange(Nstep+1),columns=tenor)
phi = np.random.normal(0,1,3*Nstep).reshape((Nstep,3))
yc.iloc[0,:]=yc0.values

for t in list(range(len(yc.index)))[:-1]:
    for tau in list(range(len(yc.columns))):
        m=df.iloc[0,tau]
        Vol1=df.iloc[1,tau]
        Vol2=df.iloc[2,tau]
        Vol3=df.iloc[3,tau]
        A=yc.iloc[t,tau]
        B=m*dt+(Vol1*phi[t,0]+Vol2*phi[t,1]+Vol3*phi[t,2])*sqrt(dt)        
        if tau==list(range(len(yc.columns)))[-1]:
            C=(yc.iloc[t,tau]-yc.iloc[t,tau-1])/(tenor[tau]-tenor[tau-1])*dt
        else:
            C=(yc.iloc[t,tau+1]-yc.iloc[t,tau])/(tenor[tau+1]-tenor[tau])*dt
        yc.iloc[t+1,tau]=A+B+C
yc
0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 ... 20.5 21.0 21.5 22.0 22.5 23.0 23.5 24.0 24.5 25.0
0 0.04613836 0.04525117 0.04291581 0.04283311 0.04349772 0.04405379 0.04443952 0.0447085 0.04490347 0.04505662 ... 0.04066505 0.04050125 0.04035301 0.04021908 0.04009825 0.03998929 0.03989096 0.03980205 0.03972144 0.03964844
1 0.0459943 0.04526421 0.04314509 0.04323417 0.0440396 0.04472192 0.04522213 0.04559412 0.04588093 0.04611491 ... 0.04135645 0.04118335 0.04103158 0.04090039 0.04078906 0.04069688 0.04062311 0.04056702 0.04052803 0.04050582
2 0.0457451 0.04487099 0.0426953 0.04271284 0.04344412 0.04406464 0.04451702 0.04485426 0.04511852 0.04534118 ... 0.04202739 0.04183243 0.04164717 0.0414702 0.04130013 0.04113558 0.04097515 0.04081746 0.04066126 0.04050525
3 0.04546677 0.04458554 0.04247211 0.04252181 0.04326825 0.04390229 0.04436872 0.04472035 0.0449991 0.04523606 ... 0.04207907 0.04187896 0.0416878 0.04150419 0.04132674 0.04115403 0.04098468 0.0408173 0.04065063 0.0404834
4 0.04494665 0.04393018 0.04176479 0.04174792 0.0424257 0.04300337 0.04342725 0.04374923 0.04401039 0.04424083 ... 0.04258147 0.04235743 0.04213044 0.04189845 0.04165937 0.04141114 0.04115171 0.04087903 0.04059116 0.04028621
5 0.04478853 0.0437614 0.04165358 0.04166735 0.04236184 0.04295701 0.04340112 0.04374572 0.04403149 0.04428809 ... 0.04323958 0.04300003 0.04275369 0.04249833 0.04223175 0.0419517 0.041656 0.04134243 0.04100894 0.04065384
6 0.04422231 0.04325939 0.04127138 0.04136803 0.04212202 0.04276819 0.04325719 0.04364067 0.04395943 0.04424313 ... 0.04271266 0.042476 0.04223719 0.04199433 0.04174551 0.04148883 0.0412224 0.04094435 0.04065293 0.04034726
7 0.04372539 0.04265662 0.04063504 0.04067924 0.04137476 0.04197034 0.04241824 0.04276948 0.04306421 0.04333139 ... 0.04260516 0.04235816 0.04210211 0.04183468 0.04155354 0.04125638 0.04094089 0.0406048 0.04024594 0.03986335
8 0.04248229 0.04150736 0.03963263 0.03978825 0.04057324 0.04125062 0.04177486 0.04219697 0.04255708 0.04288408 ... 0.04225569 0.04199513 0.04172684 0.04144871 0.04115864 0.04085452 0.04053425 0.04019576 0.03983714 0.03945813
9 0.04274173 0.04177068 0.03995491 0.04013812 0.04093277 0.04161587 0.04214397 0.04256803 0.04292812 0.04325297 ... 0.04222968 0.04197627 0.04171803 0.041453 0.04117923 0.04089476 0.04059766 0.04028601 0.0399581 0.03961423
10 0.04245147 0.04142337 0.03961258 0.03977795 0.04054503 0.04120421 0.04171371 0.04212406 0.04247487 0.04279439 ... 0.04222577 0.04196569 0.04169698 0.04141746 0.04112496 0.04081729 0.04049231 0.04014787 0.03978211 0.03939541
11 0.0427224 0.04169316 0.03993566 0.04012613 0.04090344 0.04157133 0.04208992 0.0425095 0.0428695 0.04319787 ... 0.04267975 0.04241702 0.0421455 0.04186303 0.04156743 0.04125655 0.04092823 0.04058038 0.04021117 0.03982142
12 0.042787 0.04179354 0.04011913 0.04035951 0.04116712 0.0418591 0.04239761 0.042833 0.04320472 0.04354069 ... 0.04248931 0.04223414 0.0419745 0.04170848 0.04143416 0.04114965 0.04085304 0.04054249 0.0402165 0.03987655
13 0.04425504 0.04309667 0.04131713 0.04143314 0.04211023 0.04267831 0.0431011 0.04342857 0.04369977 0.04394215 ... 0.04235917 0.04213173 0.04190046 0.04166325 0.041418 0.04116265 0.0408951 0.04061337 0.04031581 0.04000399
14 0.04298983 0.04186634 0.0401741 0.04035261 0.04108052 0.04170018 0.04217687 0.04256014 0.04288859 0.04318921 ... 0.04265748 0.04240062 0.04213382 0.04185478 0.0415612 0.04125081 0.04092133 0.04057057 0.04019675 0.0398015
15 0.04242433 0.04126275 0.03958253 0.03974794 0.04045087 0.04104611 0.04150062 0.04186367 0.04217363 0.04245716 ... 0.0418152 0.04156358 0.04130217 0.04102863 0.04074062 0.04043585 0.04011201 0.03976687 0.03939873 0.03900956
16 0.0437294 0.04262014 0.04102974 0.04124946 0.0419849 0.04260354 0.0430744 0.04344699 0.04375987 0.04403986 ... 0.04233672 0.04210391 0.04186931 0.04163102 0.04138714 0.04113579 0.04087511 0.04060332 0.04031921 0.04002559
17 0.04536045 0.04412967 0.04246903 0.04259799 0.04323527 0.04376028 0.04414354 0.04443424 0.04467058 0.04487901 ... 0.04271304 0.0425025 0.04229108 0.04207676 0.04185752 0.04163138 0.04139636 0.04115058 0.04089281 0.04062588
18 0.04623423 0.04504621 0.04346012 0.04362601 0.04427645 0.04480328 0.0451792 0.0454538 0.04566566 0.04584155 ... 0.04191013 0.04173641 0.04157424 0.04142221 0.04127892 0.04114298 0.04101304 0.04088784 0.04076683 0.04065358
19 0.04698613 0.04577324 0.04420821 0.04437262 0.04501208 0.04552993 0.04590027 0.04617235 0.04638437 0.04656269 ... 0.04297323 0.04279363 0.04262297 0.04245972 0.04230234 0.04214931 0.04199912 0.04185041 0.04170258 0.04155885
20 0.04654914 0.0453657 0.04387472 0.04409084 0.04477261 0.04533456 0.04575206 0.04607391 0.04633784 0.04656973 ... 0.04405982 0.0438515 0.04364568 0.04344058 0.04323443 0.04302548 0.04281199 0.04259238 0.0423659 0.04213541
21 0.04649895 0.04532919 0.04389155 0.04413557 0.04483291 0.04540933 0.04584168 0.04617852 0.04645736 0.0467038 ... 0.04434047 0.04412699 0.04391535 0.04370375 0.04349044 0.04327364 0.04305164 0.04282286 0.04258662 0.04234581
22 0.0466344 0.04534517 0.04383795 0.04399832 0.04461063 0.04511123 0.04547803 0.04575894 0.04599082 0.04619861 ... 0.04440674 0.04419141 0.04397201 0.04374635 0.04351223 0.0432675 0.04301002 0.04273782 0.0424499 0.04214875
23 0.04467181 0.04351281 0.04215995 0.04243731 0.04314282 0.0437242 0.04416125 0.04450221 0.04478422 0.04503246 ... 0.04242143 0.04221217 0.04200661 0.04180305 0.04159983 0.0413953 0.04118786 0.04097607 0.04075952 0.04054145
24 0.04493333 0.04379654 0.04250027 0.04280815 0.04353118 0.0441278 0.04457925 0.04493362 0.04522789 0.04548704 ... 0.04281754 0.04260735 0.04240165 0.04219882 0.04199727 0.0417954 0.04159168 0.04138479 0.04117444 0.04096392
25 0.04499879 0.04383514 0.04254642 0.04283733 0.04353189 0.0440991 0.04452162 0.04484742 0.0451133 0.0453441 ... 0.0421828 0.04198692 0.04179823 0.04161522 0.04143638 0.04126019 0.04108523 0.04091027 0.04073518 0.04056328
26 0.04509762 0.04397271 0.04275151 0.04308134 0.04379936 0.04438506 0.04482263 0.04516005 0.0454341 0.04566958 ... 0.04203153 0.04184317 0.04166584 0.04149822 0.04133902 0.04118696 0.04104083 0.04089965 0.04076358 0.04063604
27 0.0461185 0.0449109 0.04364354 0.04390746 0.04455263 0.04506856 0.0454407 0.0457167 0.04593307 0.04611428 ... 0.04207048 0.04190024 0.04174214 0.04159483 0.04145693 0.04132712 0.04120411 0.04108689 0.04097565 0.04087349
28 0.046945 0.04567733 0.04438736 0.04461111 0.04521112 0.04568664 0.04602412 0.0462708 0.04646274 0.04662392 ... 0.0428799 0.04270897 0.0425471 0.0423927 0.0422442 0.04210005 0.04195877 0.04181915 0.04168121 0.04154762
29 0.04541221 0.044244 0.04307951 0.04340139 0.04408503 0.0446395 0.04505255 0.04537129 0.04563163 0.04585738 ... 0.04256086 0.04236984 0.04218673 0.04201003 0.04183824 0.0416699 0.04150362 0.04133829 0.0411741 0.04101365
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
971 0.04623362 0.04782378 0.049279 0.05059936 0.05178479 0.05283542 0.05375174 0.05453476 0.05518604 0.05570771 ... 0.02110536 0.01961271 0.01822215 0.01695046 0.0158109 0.01480903 0.0139405 0.01319127 0.01253914 0.01195328
972 0.04650899 0.04815122 0.04965395 0.05101742 0.05224175 0.05332721 0.05427447 0.05508472 0.05575969 0.05630167 ... 0.02152425 0.02003185 0.01864381 0.01737697 0.01624461 0.01525224 0.01439548 0.01366028 0.01302442 0.01245653
973 0.04531445 0.04701025 0.04856677 0.04998415 0.05126238 0.05240167 0.05340258 0.05426619 0.05499414 0.05558861 ... 0.02159071 0.02007587 0.01866143 0.01736404 0.01619672 0.01516466 0.01426318 0.01347795 0.01278645 0.01215649
974 0.04480936 0.04653423 0.04811627 0.04955574 0.05085282 0.05200784 0.05302152 0.05389512 0.05463041 0.05522973 ... 0.02093701 0.01942746 0.01802092 0.01673411 0.01558003 0.01456381 0.01368072 0.0129164 0.0122483 0.01164377
975 0.04519136 0.04711475 0.04887227 0.05046511 0.05189441 0.05316144 0.05426786 0.05521587 0.0560082 0.05664811 ... 0.02056519 0.0190794 0.01771213 0.01648097 0.01539965 0.01447408 0.01370023 0.01306454 0.01254516 0.01210978
976 0.0455761 0.04752406 0.04930236 0.05091239 0.05235542 0.05363291 0.05474673 0.0556992 0.05649325 0.05713232 ... 0.02065299 0.01917565 0.01782006 0.01660386 0.01554084 0.01463682 0.01388777 0.0132801 0.01279195 0.01239011
977 0.04513805 0.04710574 0.0489041 0.05053444 0.05199801 0.05329623 0.05443089 0.0554043 0.05621933 0.05687936 ... 0.02074013 0.01925438 0.01788868 0.01666057 0.01558359 0.01466331 0.01389544 0.01326619 0.0127534 0.01232274
978 0.04627209 0.04826169 0.05007568 0.0517157 0.0531833 0.05448022 0.05560857 0.05657099 0.05737065 0.05801125 ... 0.02095206 0.01948741 0.01814943 0.01695585 0.01592036 0.01504865 0.01433654 0.01377038 0.01332805 0.01297461
979 0.04457861 0.04653386 0.04832107 0.04994151 0.05139639 0.05268708 0.05381535 0.05478348 0.05559427 0.05625109 ... 0.02021231 0.01872867 0.01736456 0.01613728 0.01505999 0.0141378 0.01336599 0.01273035 0.01220816 0.01176302
980 0.04375397 0.04570779 0.04749697 0.04912261 0.05058573 0.05188753 0.05302958 0.054014 0.0548434 0.05552096 ... 0.02013744 0.01864 0.01725794 0.01600829 0.01490384 0.01394932 0.01313962 0.01246016 0.01188777 0.01138504
981 0.04436766 0.04632788 0.04812399 0.04975705 0.05122804 0.05253814 0.05368888 0.05468234 0.05552111 0.05620831 ... 0.02102276 0.01952145 0.01813451 0.01687882 0.01576696 0.01480342 0.01398285 0.01329046 0.01270274 0.0121816
982 0.0443077 0.0462352 0.04800134 0.04960711 0.05105338 0.05234123 0.05347213 0.05444805 0.05527149 0.05594548 ... 0.02085966 0.01936068 0.01797498 0.01671924 0.01560577 0.01463876 0.01381256 0.01311213 0.01251353 0.01197803
983 0.04216953 0.04407177 0.04582038 0.04741594 0.04885896 0.05015014 0.05129056 0.05228181 0.05312603 0.05382586 ... 0.01991142 0.0183887 0.01697127 0.01567532 0.01451264 0.01348679 0.01259157 0.01181134 0.0111215 0.01048243
984 0.0436971 0.04564258 0.04742318 0.04904003 0.05049419 0.05178693 0.05291988 0.0538952 0.05471557 0.05538417 ... 0.01986409 0.01837624 0.01700512 0.01576739 0.01467518 0.01373244 0.01293329 0.01226247 0.01169563 0.01119347
985 0.04353988 0.04551613 0.04732221 0.04895951 0.05042935 0.05173321 0.052873 0.05385111 0.05467046 0.0553345 ... 0.01921002 0.01773488 0.01638113 0.01516559 0.01410046 0.01318971 0.01242752 0.01179868 0.01127878 0.01082824
986 0.04220398 0.04427352 0.04616274 0.04787341 0.04940726 0.05076619 0.05195252 0.05296903 0.05381909 0.05450652 ... 0.01767475 0.01620858 0.01487031 0.01367706 0.01264125 0.01176706 0.01104888 0.01047171 0.01001123 0.009627674
987 0.04289778 0.04496073 0.04684102 0.04854057 0.05006125 0.05140513 0.05257465 0.05357276 0.05440296 0.05506923 ... 0.01770007 0.01624894 0.01492929 0.01375826 0.01274825 0.01190337 0.01121797 0.01067698 0.01025592 0.009914425
988 0.0424899 0.04463735 0.04659835 0.0483749 0.04996894 0.05138259 0.05261838 0.05367934 0.05456901 0.05529145 ... 0.01834614 0.01687833 0.01554094 0.01435108 0.01332104 0.01245479 0.01174653 0.01118109 0.01073373 0.01036334
989 0.04337341 0.04539553 0.04724682 0.04892864 0.05044227 0.05178921 0.05297134 0.05399102 0.05485118 0.0555552 ... 0.01996476 0.01847915 0.01711297 0.01588255 0.01479936 0.01386653 0.01307741 0.01241597 0.01185648 0.01135648
990 0.04325092 0.04519878 0.04698676 0.04861574 0.05008654 0.05140017 0.05255804 0.05356204 0.05441462 0.05511869 ... 0.0206638 0.01916028 0.01776753 0.01650131 0.01537244 0.01438334 0.01352669 0.01278579 0.01213409 0.01152831
991 0.04455659 0.04644393 0.04817669 0.04975557 0.05118119 0.05245439 0.05357638 0.0545489 0.05537418 0.05605496 ... 0.02181643 0.02031519 0.01892233 0.0176533 0.01651855 0.01552012 0.0146503 0.013892 0.01321813 0.01258517
992 0.04583187 0.04769661 0.0494043 0.05095579 0.05235192 0.05359368 0.0546825 0.05562027 0.05640941 0.05705285 ... 0.02202612 0.02054709 0.0191813 0.01794429 0.0168465 0.01588994 0.01506689 0.01436022 0.01374271 0.01317103
993 0.04610817 0.04808691 0.04989583 0.05153631 0.05300969 0.05431749 0.05546165 0.05644459 0.05726925 0.05793907 ... 0.02197887 0.02051196 0.01916664 0.01795882 0.01689929 0.01599038 0.01522467 0.01458534 0.01404535 0.01356186
994 0.04556625 0.04755628 0.04937514 0.05102427 0.05250507 0.05381913 0.05496845 0.05595551 0.0567833 0.05745533 ... 0.02139409 0.01993002 0.01858856 0.01738555 0.01633167 0.01542909 0.01467026 0.01403821 0.01350562 0.01302944
995 0.04499043 0.04695287 0.04874775 0.05037634 0.0518399 0.05313988 0.05427812 0.05525697 0.05607927 0.05674837 ... 0.02102197 0.01955435 0.01820677 0.01699483 0.01592887 0.0150107 0.01423244 0.01357673 0.0130158 0.01250616
996 0.04415276 0.04606558 0.04782091 0.04941957 0.05086235 0.05215021 0.05328455 0.05426722 0.05510062 0.05578761 ... 0.02136362 0.01987173 0.01849057 0.01723517 0.0161152 0.0151318 0.01427641 0.01353099 0.01286698 0.01224025
997 0.04306947 0.04503992 0.04684786 0.0484943 0.0499802 0.05130673 0.05247545 0.05348842 0.05434821 0.05505785 ... 0.02044556 0.01895374 0.01757505 0.01632459 0.01521202 0.01423846 0.01339534 0.01266458 0.01201747 0.01141021
998 0.04404028 0.04600789 0.0478096 0.0494466 0.05092006 0.05223134 0.05338222 0.05437494 0.05521228 0.05589747 ... 0.02058768 0.01911402 0.01775813 0.01653517 0.01545483 0.01451822 0.01371676 0.01303234 0.01243616 0.01188467
999 0.04461672 0.0465212 0.04826676 0.04985433 0.05128478 0.05255922 0.05367915 0.05464654 0.05546388 0.05613414 ... 0.02133564 0.0198577 0.01849315 0.01725676 0.01615775 0.01519675 0.0143647 0.01364301 0.01300227 0.01239866
1000 0.04394689 0.0458364 0.047571 0.04915141 0.05057833 0.05185267 0.05297574 0.05394932 0.05477572 0.05545769 ... 0.02124505 0.01975619 0.01837673 0.01712111 0.01599818 0.01500817 0.01414162 0.01337953 0.012692 0.01203514

1001 rows × 51 columns

#封装为一个方程
def YieldCurve_Simulator(N,yc0,df,Nstep=1000,dt=0.01):
    yc_list=[]
    tenor=list(yc0.columns)

    for i in range(N):
    # Initialize and preallocate the yield curve matrix  
        yc=pd.DataFrame(index=arange(Nstep+1),columns=tenor)
        phi = np.random.normal(0,1,3*Nstep).reshape((Nstep,3))
        yc.iloc[0,:]=yc0.values   
        Row=list(range(len(yc.index)))[:-1]
        Column=list(range(len(yc.columns)))
    # Calculate the Yield Curve    
        for t in Row:
            for tau in Column:
                m=df.iloc[0,tau]
                Vol1=df.iloc[1,tau]
                Vol2=df.iloc[2,tau]
                Vol3=df.iloc[3,tau]
                A=yc.iloc[t,tau]
                B=m*dt+(Vol1*phi[t,0]+Vol2*phi[t,1]+Vol3*phi[t,2])*sqrt(dt)        
                if tau==list(range(len(yc.columns)))[-1]:
                    C=(yc.iloc[t,tau]-yc.iloc[t,tau-1])/(tenor[tau]-tenor[tau-1])*dt
                else:
                    C=(yc.iloc[t,tau+1]-yc.iloc[t,tau])/(tenor[tau+1]-tenor[tau])*dt
                yc.iloc[t+1,tau]=A+B+C
        yc_list.append(yc)
        
    return yc_list
N=1
YieldCurve_Simulator(N,yc0,df)
 

全部回复

0/140

达人推荐

量化课程

    移动端课程