Skip to content

update to python 3.x #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions B03898_05_Codes/bond_ytm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@
README
======
This file contains Python codes.
Python 3.x
======
"""

""" Get yield-to-maturity of a bond """
import scipy.optimize as optimize


def bond_ytm(price, par, T, coup, freq=2, guess=0.05):
def bond_ytm(price, par, maturity, coup, freq=2, guess=0.05):
freq = float(freq)
periods = T*freq
coupon = coup/100.*par/freq
periods = maturity*freq
coupon = coup*par/freq
dt = [(i+1)/freq for i in range(int(periods))]
ytm_func = lambda(y): \
sum([coupon/(1+y/freq)**(freq*t) for t in dt]) + \
par/(1+y/freq)**(freq*t) - price
ytm_func = lambda y: sum([coupon/(1+y/freq)**(freq*t) for t in dt]) + par/(1+y/freq)**(freq*maturity) - price

return optimize.newton(ytm_func, guess)

if __name__ == "__main__":
ytm = bond_ytm(95.0428, 100, 1.5, 5.75, 2)
print ytm
print (ytm)