Fin123
Fin123

Reputation: 11

How to discount Cashflows with variable discount rates and sum all cashflows

I am trying to price a bond that will pay coupons (c) semiannually for 4 years (which means 8 coupon payments in total) and return the principal (p) amount along with the 8th payment (c+p). The discount rate (dr) to discount each cashflows will be different.

inputs:

dr = [0.10, 0.12, 0.15, 0.22, 0.37, 0.6, 0.8, 0.85], p = 1000, c = 2, T = 4, freq = 2

I found the below code in stackoverflow but this does not use different 'dr' to discount each cashflows and also do not sum all the cashflows after discounting. Can someone please help? '''

par = 1000
coupon_rate = 3
T = 5
freq = 2


def cf_calculator(par, r, T, freq):
    for i in range(0,(T * freq)+1):
        if i < (T * freq):
            coupon = ((r/100) * par) / freq
            print(coupon)
        else: 
            coupon = (((r/100) * par) / freq) + par
            print(coupon)

print(cf_calculator(par,coupon_rate,T,freq))

'''

Bond CF for loop and if else loop

Upvotes: 1

Views: 326

Answers (1)

agarg
agarg

Reputation: 418

I have tried to avoid loops as much as possible:

p = 1000
c = 2 
T = 4
freq = 2
dr = [0.10, 0.12, 0.15, 0.22, 0.37, 0.6, 0.8, 0.85]

import numpy as np
cashflows = np.dot(p,[(c/100 + (i==freq*T-1)) for i in range(freq*T) ])
print(cashflows)

dcf = sum([cf[0]/((1+cf[1])**(i+1)) for i,cf in enumerate(zip(cashflows,dr))])
print(dcf)

Output:

[20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 1020.0]
69.40091054501804

I hope the math is correct. And the code assumes that the length of the list dr is equal to T*freq. For a production level code you may need to add try except blocks, checking the assumption mentioned above.

Upvotes: 0

Related Questions