#define durations
coupon_duration = [0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5]
#hold zero coupon rates for each day
all_zero_rates = []
#for each day find zero coupon rates
for index in range (0,len(yield_curve)):
coupon_rate = np.array(yield_curve.iloc[index,:])
coupon_rate = coupon_rate / 100 #convert yield_curve rates to numpy and divide by 100
##############################################################################################################
#cash flow calculation for yield curve rates
cash_flow = []
for i in range (1,11):
cash = []
for j in coupon_duration:
#every 6 months coupon payment is relized until maturity date
if i/2 > j:
cash.append(coupon_rate[i-1]/2*100)
#at maturity coupon payment with face value is realized
if i/2 == j:
cash.append(100*(coupon_rate[i-1]/2+1))
#for each coupon duration append cash flow to main cash_flow array
cash_flow.append(cash)
##############################################################################################################
#define zero_coupon_rate
zero_coupon_rate=[coupon_rate[0]] #for 0.5 year maturity zero_coupon and yield curve rates are same
#calculate zero_rates for other maturities
for i in range (1,10):
#create array for present_values
pv = []
for j in range (0,10):
#until maturity calculate present values by using previous zero_coupon_rates
if i > j:
pv.append(cash_flow[i][j] / ((1 + zero_coupon_rate[j]/2)**(coupon_duration[j]*2)))
#at maturity find new zero_coupon rates by equating sum of present valus to 100
if i == j:
#define problem
def rate (r):
remaining = 100 - sum(pv)
p_val = cash_flow[i][j] / ((1 + r/2)**(coupon_duration[j]*2))
return remaining - p_val
#solve problem and find zero_coupon_rates
r = fsolve(rate, [0])[0]
#append new coupon rates
zero_coupon_rate.append(r)
#append daily zero_coupon_rates to all_zero_rates array.
all_zero_rates.append(zero_coupon_rate)
##############################################################################################################
#create dataframe with all_zero_rates
rates_df = pd.DataFrame(all_zero_rates)