10.12.2019 Views

Python for Finance

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5

sellingPrice/(1+R)**n

sellingPrice= d(n+1)/(r-g)

where g is long term growth rate

Example #1: >>> r=0.182

>>> g=0.03

>>> d=[1.8,2.07,2.277,2.48193,2.68,2.7877]

>>>pvValueNperiodModel(r,g,d)

17.472364312825711

"""

import scipy as sp

d=dividendNplus1

n=len(d)-1

g=longTermGrowthRate

pv=sp.npv(r,d[:-1])*(1+r)

sellingPrice=d[n]/(r-g)

pv+=sp.pv(r,n,0,-sellingPrice)

return pv

Appendix E – Python program to estimate the

duration for a bond

def durationBond(rate,couponRate,maturity):

"""Objective : estimte the durtion for a given bond

rate : discount rate

couponRate: coupon rate

maturity : number of years

Example 1: >>>discountRate=0.1

>>>couponRate=0.04

>>> n=4

>>>durationBond(rate,couponRate,n)

3.5616941835365492

"""

Example #2>>>durationBond(0.1,0.04,4)

3.7465335177625576

import scipy as sp

d=0

n=maturity

for i in sp.arange(n):

d+=(i+1)*sp.pv(rate,i+1,0,-couponRate)

d+=n*sp.pv(rate,nper,0,-1)

return d/sp.pv(rate,n,-couponRate,-1)

[ 181 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!