Professional Documents
Culture Documents
Nelson-Siegel Model
Nelson-Siegel Model
<pandas.io.formats.style.Styler at 0x16d17d72b10>
sf = df.copy()
sf = sf.dropna()
sf1 = sf.copy()
sf1['Y'] = round(sf['Yield']*100,4)
sf = sf.style.format({'Maturity': '{:,.2f}'.format,'Yield':
'{:,.4%}'})
import matplotlib.pyplot as plt
import matplotlib.markers as mk
import matplotlib.ticker as mtick
fontsize=15
fig = plt.figure(figsize=(7,4))
plt.title("Nelson-Siegel-Svensson Model - Unfitted Yield
Curve",fontsize=fontsize)
ax = plt.axes()
ax.set_facecolor("black")
fig.patch.set_facecolor('white')
X = sf1["Maturity"]
Y = sf1["Y"]
plt.scatter(X, Y, marker="o", c="blue")
plt.xlabel('Period',fontsize=fontsize)
plt.ylabel('Interest',fontsize=fontsize)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.xaxis.set_ticks(np.arange(0, 30, 5))
ax.yaxis.set_ticks(np.arange(0, 4, 0.5))
ax.legend(loc="lower right", title="Yield")
plt.grid()
plt.show()
df['NSS'] = (β0)+(β1*((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))+(β2*((((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))-(np.exp(-
df['Maturity']/λ0))))+(β3*((((1-np.exp(-df['Maturity']/λ1))/
(df['Maturity']/λ1)))-(np.exp(-df['Maturity']/λ1))))
df.style.format({'Maturity': '{:,.0f}'.format,'Yield':
'{:,.2%}','NSS': '{:,.2%}'})
<pandas.io.formats.style.Styler at 0x16d196d27d0>
df1 = df.copy()
df['Y'] = round(df['Yield']*100,4)
df['NSS'] =(β0)+(β1*((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))+(β2*((((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))-(np.exp(-
df['Maturity']/λ0))))+(β3*((((1-np.exp(-df['Maturity']/λ1))/
(df['Maturity']/λ1)))-(np.exp(-df['Maturity']/λ1))))
df['N'] = round(df['NSS']*100,4)
df2 = df.copy()
df2 = df2.style.format({'Maturity': '{:,.2f}'.format,'Y': '{:,.2%}',
'N': '{:,.2%}'})
import matplotlib.pyplot as plt
import matplotlib.markers as mk
import matplotlib.ticker as mtick
fontsize=15
fig = plt.figure(figsize=(13,7))
plt.title("Nelson-Siegel-Svensson Model - Unfitted Yield
Curve",fontsize=fontsize)
ax = plt.axes()
ax.set_facecolor("black")
fig.patch.set_facecolor('white')
X = df["Maturity"]
Y = df["Y"]
x = df["Maturity"]
y = df["N"]
ax.plot(x, y, color="orange", label="NSS")
plt.scatter(x, y, marker="o", c="orange")
plt.scatter(X, Y, marker="o", c="blue")
plt.xlabel('Period',fontsize=fontsize)
plt.ylabel('Interest',fontsize=fontsize)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.xaxis.set_ticks(np.arange(0, 30, 5))
ax.yaxis.set_ticks(np.arange(0, 4, 0.5))
ax.legend(loc="lower right", title="Yield")
plt.grid()
plt.show()
df['Residual'] = (df['Yield'] - df['NSS'])**2
df22 = df[['Maturity','Yield','NSS','Residual']]
df22.style.format({'Maturity': '{:,.0f}'.format,'Yield':
'{:,.2%}','NSS': '{:,.2%}','Residual': '{:,.9f}'})
<pandas.io.formats.style.Styler at 0x16d197517d0>
np.sum(df['Residual'])
0.02100226160082434
def myval(c):
df = dd.copy()
df['NSS'] =(c[0])+(c[1]*((1-np.exp(-
df['Maturity']/c[4]))/(df['Maturity']/c[4])))+(c[2]*((((1-np.exp(-
df['Maturity']/c[4]))/(df['Maturity']/c[4])))-(np.exp(-
df['Maturity']/c[4]))))+(c[3]*((((1-np.exp(-df['Maturity']/c[5]))/
(df['Maturity']/c[5])))-(np.exp(-df['Maturity']/c[5]))))
df['Residual'] = (df['Yield'] - df['NSS'])**2
val = np.sum(df['Residual'])
print("[β0, β1, β2, β3, λ0, λ1]=",c,", SUM:", val)
return(val)
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.01 0.01 0.01 1. 1. ] , SUM:
0.02100226160082434
[β0, β1, β2, β3, λ0, λ1]= [0.0105 0.01 0.01 0.01 1.
1. ] , SUM: 0.020297626475441165
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.0105 0.01 0.01 1.
1. ] , SUM: 0.02058563545376699
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.01 0.0105 0.01 1.
1. ] , SUM: 0.020890348643858147
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.01 0.01 0.0105 1.
1. ] , SUM: 0.020890348643858147
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.01 0.01 0.01 1.05 1. ] , SUM:
0.020909910342136163
[β0, β1, β2, β3, λ0, λ1]= [0.01 0.01 0.01 0.01 1. 1.05] , SUM:
0.02101874476529507
[β0, β1, β2, β3, λ0, λ1]= [0.01016667 0.01016667 0.01016667 0.01016667
1.01666667 0.95 ] , SUM: 0.02050432151686911
[β0, β1, β2, β3, λ0, λ1]= [0.01022222 0.01022222 0.01022222 0.01022222
1.02222222 0.98333333] , SUM: 0.020357080828789606
[β0, β1, β2, β3, λ0, λ1]= [0.0102963 0.0102963 0.0102963 0.0102963
0.96296296 0.97777778] , SUM: 0.020273050659472106
[β0, β1, β2, β3, λ0, λ1]= [0.01044444 0.01044444 0.01044444 0.01044444
0.91944444 0.96666667] , SUM: 0.01996947732752005
[β0, β1, β2, β3, λ0, λ1]= [0.01044444 0.01044444 0.01044444 0.00977778
0.98611111 0.96666667] , SUM: 0.019978597937272132
[β0, β1, β2, β3, λ0, λ1]= [0.01059259 0.01059259 0.00992593 0.0102037
0.98148148 0.95555556] , SUM: 0.01968045867002169
[β0, β1, β2, β3, λ0, λ1]= [0.01088889 0.01088889 0.00963889 0.01030556
0.97222222 0.93333333] , SUM: 0.019093793837322518
[β0, β1, β2, β3, λ0, λ1]= [0.01088889 0.01022222 0.01030556 0.01030556
0.97222222 0.93333333] , SUM: 0.019477263258251654
[β0, β1, β2, β3, λ0, λ1]= [0.01096296 0.01057407 0.01018519 0.01018519
0.94074074 0.97777778] , SUM: 0.019229329417675235
[β0, β1, β2, β3, λ0, λ1]= [0.01115432 0.0106358 0.01011728 0.01011728
0.90802469 0.94259259] , SUM: 0.019010430830308702
[β0, β1, β2, β3, λ0, λ1]= [0.01162037 0.01084259 0.01006481 0.01006481
0.85092593 0.92222222] , SUM: 0.01837963411473521
[β0, β1, β2, β3, λ0, λ1]= [0.01125 0.01113889 0.01036111 0.01036111
0.88055556 0.9 ] , SUM: 0.018445405076567226
[β0, β1, β2, β3, λ0, λ1]= [0.01157407 0.01092593 0.00988889 0.01077778
0.85925926 0.91111111] , SUM: 0.01824233116298362
[β0, β1, β2, β3, λ0, λ1]= [0.01213889 0.01116667 0.00961111 0.01127778
0.79583333 0.88333333] , SUM: 0.01743877229723573
[β0, β1, β2, β3, λ0, λ1]= [0.01213889 0.01116667 0.00961111 0.01038889
0.88472222 0.88333333] , SUM: 0.017405639397160496
[β0, β1, β2, β3, λ0, λ1]= [0.01298611 0.01152778 0.00919444 0.01036111
0.86736111 0.84166667] , SUM: 0.016207897300421818
[β0, β1, β2, β3, λ0, λ1]= [0.01239352 0.01182407 0.00937963 0.0105463
0.79699074 0.88611111] , SUM: 0.016859315440428534
[β0, β1, β2, β3, λ0, λ1]= [0.0127963 0.01188889 0.00923148 0.01078704
0.78055556 0.81111111] , SUM: 0.016316344309444047
[β0, β1, β2, β3, λ0, λ1]= [0.01350617 0.01190741 0.00964198 0.01082716
0.68518519 0.81481481] , SUM: 0.015636632945099267
[β0, β1, β2, β3, λ0, λ1]= [0.01481481 0.01241667 0.00964352 0.01108796
0.54166667 0.75555556] , SUM: 0.014277626055960167
[β0, β1, β2, β3, λ0, λ1]= [0.01433333 0.01208333 0.00868056 0.01101389
0.66388889 0.8 ] , SUM: 0.014775265199124827
[β0, β1, β2, β3, λ0, λ1]= [0.01486728 0.01279321 0.00851543 0.01162654
0.63117284 0.73703704] , SUM: 0.01377245870696868
[β0, β1, β2, β3, λ0, λ1]= [0.01649074 0.01376852 0.00774074 0.01240741
0.5212963 0.64444444] , SUM: 0.011977541559431974
[β0, β1, β2, β3, λ0, λ1]= [0.01579938 0.01333642 0.00834568 0.01079012
0.59475309 0.6962963 ] , SUM: 0.012785969752485188
[β0, β1, β2, β3, λ0, λ1]= [0.01668004 0.01318313 0.00823251 0.01160288
0.52618313 0.63024691] , SUM: 0.012082966906345511
[β0, β1, β2, β3, λ0, λ1]= [0.01757184 0.01354973 0.00804767 0.01163409
0.4578275 0.64495885] , SUM: 0.011386752609144797
[β0, β1, β2, β3, λ0, λ1]= [0.01995962 0.01438014 0.00745576 0.01205761
0.29646348 0.56188272] , SUM: 0.00993860381567425
[β0, β1, β2, β3, λ0, λ1]= [0.01970653 0.01486163 0.00750514 0.01262551
0.18072274 0.52114198] , SUM: 0.011219008710274418
[β0, β1, β2, β3, λ0, λ1]= [0.02015038 0.01523217 0.00762723 0.01250995
0.22313957 0.46985597] , SUM: 0.010154067229464468
[β0, β1, β2, β3, λ0, λ1]= [0.02144742 0.01583733 0.00599217 0.01290987
0.2391861 0.41906722] , SUM: 0.008920319121112196
[β0, β1, β2, β3, λ0, λ1]= [0.02476372 0.01754767 0.0041665 0.01382082
0.08794582 0.25082305] , SUM: 0.008615432368795955
[β0, β1, β2, β3, λ0, λ1]= [0.02345096 0.01632133 0.00589695 0.01421794
0.01716392 0.32983539] , SUM: 0.011883499189483558
[β0, β1, β2, β3, λ0, λ1]= [ 0.02482727 0.01752069 0.0052316
0.01427686 -0.08393919 0.2957476 ] , SUM: 8.575300168882298e+152
[β0, β1, β2, β3, λ0, λ1]= [0.01871685 0.01426752 0.00748228 0.01227138
0.37365255 0.54662209] , SUM: 0.010477509207898897
[β0, β1, β2, β3, λ0, λ1]= [ 0.02575861 0.01710163 0.00563721
0.01342699 -0.12826694 0.24894262] , SUM: 3.571572308046949e+98
[β0, β1, β2, β3, λ0, λ1]= [0.01880771 0.0146018 0.00721486 0.0126623
0.35890549 0.54556899] , SUM: 0.010358463388117342
[β0, β1, β2, β3, λ0, λ1]= [0.01725064 0.01397564 0.00792031 0.01109792
0.48977929 0.63546287] , SUM: 0.011472258697191033
[β0, β1, β2, β3, λ0, λ1]= [0.01880072 0.01456206 0.00741447 0.01187792
0.37162545 0.559056 ] , SUM: 0.010391522047954252
[β0, β1, β2, β3, λ0, λ1]= [0.02069313 0.01533549 0.00628189 0.01244115
0.38985471 0.45679429] , SUM: 0.008489951830820804
[β0, β1, β2, β3, λ0, λ1]= [0.02118643 0.01557243 0.00567026 0.01234896
0.4944207 0.42462045] , SUM: 0.007616643082471367
[β0, β1, β2, β3, λ0, λ1]= [0.02250601 0.01636457 0.00570074 0.01282114
0.23718095 0.39064697] , SUM: 0.008075848482823327
[β0, β1, β2, β3, λ0, λ1]= [0.02365723 0.01667086 0.00519731 0.013529
0.19439322 0.32207671] , SUM: 0.007662498410342418
[β0, β1, β2, β3, λ0, λ1]= [0.02526675 0.01732082 0.00472441 0.01303352
0.15227576 0.2610663 ] , SUM: 0.007230674134189127
[β0, β1, β2, β3, λ0, λ1]= [0.02849628 0.01868033 0.00347918 0.01321913
0.0489609 0.11881496] , SUM: 0.007618076975101157
[β0, β1, β2, β3, λ0, λ1]= [0.02562954 0.01738666 0.00334443 0.01336041
0.2644204 0.26718276] , SUM: 0.005970923314451738
[β0, β1, β2, β3, λ0, λ1]= [0.02836913 0.01846391 0.00120303 0.01378564
0.28506082 0.16584616] , SUM: 0.004832450708058188
[β0, β1, β2, β3, λ0, λ1]= [0.02862347 0.01959994 0.00143166 0.01438875
0.18729561 0.04314383] , SUM: 0.006105524027622133
[β0, β1, β2, β3, λ0, λ1]= [0.02510596 0.01711651 0.00380931 0.01281486
0.42892988 0.2849771 ] , SUM: 0.005335952689634192
[β0, β1, β2, β3, λ0, λ1]= [0.02823032 0.01855025 0.00164458 0.01381243
0.34361104 0.10992988] , SUM: 0.004729249889270004
[β0, β1, β2, β3, λ0, λ1]= [ 3.10924685e-02 1.96430897e-02 -
3.83496805e-04 1.43080793e-02
3.96826090e-01 -3.04286648e-02] , SUM: inf
[β0, β1, β2, β3, λ0, λ1]= [0.02860345 0.01887042 0.00096377 0.01319905
0.43613805 0.10778453] , SUM: 0.0042856246025029755
[β0, β1, β2, β3, λ0, λ1]= [ 0.03107656 0.01997021 -0.001153
0.01303408 0.55701046 0.00063843] , SUM: 0.004244613080855272
[β0, β1, β2, β3, λ0, λ1]= [ 0.03437096 0.02143478 -0.0017836
0.01460747 0.15697382 -0.13608655] , SUM: 3.0132522730480967e+93
[β0, β1, β2, β3, λ0, λ1]= [0.02448256 0.01703802 0.00380679 0.01291359
0.41005898 0.2844437 ] , SUM: 0.00575720455663108
[β0, β1, β2, β3, λ0, λ1]= [ 0.03002924 0.01959213 -0.00114362
0.01388293 0.58504651 0.03526007] , SUM: 0.003974782177182084
[β0, β1, β2, β3, λ0, λ1]= [ 0.03241049 0.02072778 -0.00407763
0.01430763 0.80143188 -0.07764305] , SUM: 3.9531380884194614e+166
[β0, β1, β2, β3, λ0, λ1]= [0.02714112 0.0173104 0.00129071 0.01235909
0.68261028 0.25055462] , SUM: 0.003995583121809797
[β0, β1, β2, β3, λ0, λ1]= [ 0.03216821 0.01996312 -0.00192312
0.01364942 0.55069735 -0.00204162] , SUM: inf
[β0, β1, β2, β3, λ0, λ1]= [0.02640398 0.01776929 0.00237431 0.01309755
0.44521857 0.21282237] , SUM: 0.004771028505380457
[β0, β1, β2, β3, λ0, λ1]= [ 0.03197749 0.02010222 -0.00240397
0.01384238 0.53725602 -0.02662659] , SUM: inf
[β0, β1, β2, β3, λ0, λ1]= [0.02682384 0.01786294 0.00225599 0.01307174
0.45601141 0.20707617] , SUM: 0.004566425471707755
[β0, β1, β2, β3, λ0, λ1]= [2.81992253e-02 1.85544936e-02 5.53292809e-
04 1.26336305e-02
7.38108610e-01 1.06247685e-01] , SUM: 0.0037401191647017124
[β0, β1, β2, β3, λ0, λ1]= [2.81142746e-02 1.85997853e-02 2.28423417e-
04 1.20576253e-02
9.64632505e-01 7.64484461e-02] , SUM: 0.0035831750292861847
[β0, β1, β2, β3, λ0, λ1]= [ 0.03073448 0.01952594 -0.00133329
0.01297508 0.75108883 0.0138135 ] , SUM: 0.00373697005986816
[β0, β1, β2, β3, λ0, λ1]= [ 0.02974286 0.01907022 -0.00159618
0.01198108 0.98852229 0.0846672 ] , SUM: 0.003276043769685881
[β0, β1, β2, β3, λ0, λ1]= [ 0.03049913 0.0193302 -0.00321656
0.0110654 1.31097791 0.07203586] , SUM: 0.0030819773881023307
[β0, β1, β2, β3, λ0, λ1]= [ 0.03237443 0.02024662 -0.00403177
0.012053 1.16111075 -0.05749254] , SUM: 1.3616018713964374e+226
[β0, β1, β2, β3, λ0, λ1]= [0.02821149 0.01845886 0.00068405 0.01281705
0.63228625 0.140934 ] , SUM: 0.003826891714688757
[β0, β1, β2, β3, λ0, λ1]= [ 2.71666817e-02 1.76355637e-02 -
1.04238167e-05 1.20183170e-02
1.08520363e+00 1.95710396e-01] , SUM: 0.0035548670782700412
[β0, β1, β2, β3, λ0, λ1]= [ 0.03111064 0.02040376 -0.00288785
0.01257971 1.09380159 -0.07248719] , SUM: 3.9611238944282096e+178
[β0, β1, β2, β3, λ0, λ1]= [2.81335005e-02 1.80837389e-02 2.46070017e-
04 1.24142465e-02
7.85408110e-01 1.69794163e-01] , SUM: 0.003599099313950892
[β0, β1, β2, β3, λ0, λ1]= [2.75906045e-02 1.76192338e-02 9.70918392e-
06 1.05663177e-02
1.25815257e+00 1.87652054e-01] , SUM: 0.003383639812259717
[β0, β1, β2, β3, λ0, λ1]= [ 0.0292014 0.01847263 -0.00204274
0.01088194 1.41953494 0.09755081] , SUM: 0.003104230270289831
[β0, β1, β2, β3, λ0, λ1]= [ 2.61673873e-02 1.70544407e-02 -
2.61885958e-04 1.00261997e-02
1.52354772e+00 2.52583741e-01] , SUM: 0.0036981692494611243
[β0, β1, β2, β3, λ0, λ1]= [ 2.73091595e-02 1.76723163e-02 -
5.29736355e-04 1.07634210e-02
1.33043300e+00 1.92891181e-01] , SUM: 0.0034089291169924103
[β0, β1, β2, β3, λ0, λ1]= [ 0.02849358 0.0183595 -0.00209985
0.01003676 1.67090341 0.10430208] , SUM: 0.0031209374327762383
[β0, β1, β2, β3, λ0, λ1]= [ 0.02863924 0.01776336 -0.00285829
0.00971976 1.72710265 0.20693235] , SUM: 0.0030413251997053812
[β0, β1, β2, β3, λ0, λ1]= [ 0.02890173 0.01734515 -0.00440164
0.00855083 2.10833772 0.2721743 ] , SUM: 0.002994245894488959
[β0, β1, β2, β3, λ0, λ1]= [ 0.03016519 0.01863078 -0.00408318
0.00860324 1.94757622 0.11315837] , SUM: 0.0029025971367471464
[β0, β1, β2, β3, λ0, λ1]= [ 0.03166444 0.01912839 -0.00611956
0.00689571 2.37876252 0.07188235] , SUM: 0.0029268922510996107
C:\Users\HP\anaconda3\Lib\site-packages\pandas\core\arraylike.py:402:
RuntimeWarning: overflow encountered in exp
result = getattr(ufunc, method)(*inputs, **kwargs)
β0 = c[0]
β1 = c[1]
β2 = c[2]
β3 = c[3]
λ0 = c[4]
λ1 = c[5]
print("[β0, β1, β2, β3, λ0, λ1]=", [c[0].round(2), c[1].round(2),
c[2].round(2), c[3].round(2), c[4].round(2), c[5].round(2)])
[β0, β1, β2, β3, λ0, λ1]= [0.28, -0.25, -0.16, 0.1, 103.29, 0.17]
df = df1.copy()
df['NSS'] =(β0)+(β1*((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))+(β2*((((1-np.exp(-
df['Maturity']/λ0))/(df['Maturity']/λ0)))-(np.exp(-
df['Maturity']/λ0))))+(β3*((((1-np.exp(-df['Maturity']/λ1))/
(df['Maturity']/λ1)))-(np.exp(-df['Maturity']/λ1))))
sf4 = df.copy()
sf5 = sf4.copy()
sf5['Y'] = round(sf4['Yield']*100,4)
sf5['N'] = round(sf4['NSS']*100,4)
sf4 = sf4.style.format({'Maturity': '{:,.2f}'.format,'Yield':
'{:,.2%}', 'NS': '{:,.2%}'})
M0 = 0.00
M1 = 3.50
import matplotlib.pyplot as plt
import matplotlib.markers as mk
import matplotlib.ticker as mtick
fontsize=15
fig = plt.figure(figsize=(7,4))
plt.title("Nelson-Siegel-Svensson Model - Fitted Yield
Curve",fontsize=fontsize)
ax = plt.axes()
ax.set_facecolor("black")
fig.patch.set_facecolor('white')
X = sf5["Maturity"]
Y = sf5["Y"]
x = sf5["Maturity"]
y = sf5["N"]
ax.plot(x, y, color="orange", label="NSS")
plt.scatter(x, y, marker="o", c="orange")
plt.scatter(X, Y, marker="o", c="blue")
plt.xlabel('Period',fontsize=fontsize)
plt.ylabel('Interest',fontsize=fontsize)
ax.yaxis.set_major_formatter(mtick.PercentFormatter())
ax.xaxis.set_ticks(np.arange(0, 30, 5))
ax.yaxis.set_ticks(np.arange(0, 4, 0.5))
ax.legend(loc="lower right", title="Yield")
plt.grid()
plt.show()
df.style.format({'Maturity': '{:,.0f}'.format,'Yield':
'{:,.2%}','NSS': '{:,.2%}'})
<pandas.io.formats.style.Styler at 0x16d1981c110>