# Bootstraping Swedish government bonds from scipy.stats import * from numpy import * from matplotlib.pyplot import * from datetime import * # Parameter Values #BONDS #Today StartD=date(2015,11,18) N=360.0 Nominal=100.0 #Coupon C=[3.0, 3.75, 4.25, 5.0, 3.5, 1.5, 2.5, 1.0, 2.25, 3.5 ] #Yield to maturity (%) YTM=[-0.0051, -0.0044, -0.0021, 0.0014, 0.0041, 0.0066, 0.0084, 0.0112, 0.0162, 0.0199] #Maturity date D1=date(2016,7,12) D2=date(2017,8,12) D3=date(2019,3,12) D4=date(2020,12,1) D5=date(2022,6,1) D6=date(2023,11,13) D7=date(2025,5,12) D8=date(2026,11,12) D9=date(2032,6,1) D10=date(2039,3,30) #BILLS #Days Days=[30.0,60.0,90.0,120.0] #Yields Yields=[-0.0043, -0.0043, -0.0044, -0.0044] z=zip(Days,Yields) def bond(Coupon, ytm, Date,z): M=Date-StartD # Time to maturity M=M.days T=M PVC=[] while T>N: # while time to maturty is more than year T=T-N #the time when coupon payment will be made t=T // N #how many coupon payments have been made tt=T%N #the time when the first coupon payment will be made S1=min(Days, key=lambda x:abs(x-T)) #finding the nearest value n=Days.index(S1) #the index in the list for the nearest value #interpolation/extrapolation if S1S2: z.insert(n2+1,(M,ZCR)) Days.insert(n2+1,M) Yields.insert(n2+1, ZCR) else: z.insert(n2, (M,ZCR)) Days.insert(n2, M) Yields.insert(n2,ZCR) return z #calculating spot rate curve using government bonds a1=bond(C[0], YTM[0], D1,z) a2=bond(C[1],YTM[1], D2, a1) a3=bond(C[2], YTM[2], D3,a2) a4=bond(C[3], YTM[3], D4,a3) a5=bond(C[4], YTM[4], D5,a4) a6=bond(C[5], YTM[5], D6,a5) a7=bond(C[6], YTM[6], D7,a6) a8=bond(C[7], YTM[7], D8,a7) a9=bond(C[8], YTM[8], D9,a8) a10=bond(C[9], YTM[9], D10,a9) i=len(Yields)-1 F=(((1+Yields[i])**(Days[i]/N))/((1+Yields[i-1])**(Days[i-1]/N)))**(N/float(Days[i]-Days[i-1]))-1 # forward rate for the last given date e=25.0 # calculating spot rates for more 10 years R10=((1+Yields[i])**(Days[i]/N)*(1+F)**((e*N-Days[i])/N))**(1/e)-1 Yields.append(R25) Days.append(e*N) r=R25 for k in range(1,10): e=e+1 R=(((1+r)**(e-1))*(1+F))**(1/e)-1 Yields.append(R) Days.append(e*N) r=R s=len(Days)-1 D=[0]*(s+1) FR=[0]*s for q in range (0,s+1): # finding the discount function D[q]=1/(1+Yields[q])**(Days[q]/N) for q in range (0,s): # finding the forward rates FR[q]=(((1+Yields[q+1])**(Days[q+1]/N))/((1+Yields[q])**(Days[q]/N)))**(1/((Days[q+1]/N-Days[q]/N)))-1 #plotting figure(1) grid() plot(Days, Yields, 'g', label="Spot rate") title('Spot and Forward curves') xlabel('Days') ylabel('Yield') figure(2) grid() plot(Days, D, 'm') title('Discount function') xlabel('Days') ylabel('Discount factor') figure(1) plot(Days[0:s], FR, 'r', label="Forward rate") legend(bbox_to_anchor=(0.68, 0.17), loc=2, borderaxespad=0.) show()