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

By applying equation (8), we have a result for APR2:

>>>Rs=(1+0.05/2)**(2/12)-1

>>>Rs*2

0.008247830930288469

>>>

The corresponding -line Python program is shown here. To save space, the program

has no additional explanation or comments:

def APR2APR(APR1,m1,m2):

return m2*((1+APR1/m1)**(m1/m2)-1)

For a continuously compounded interest rate, different ways could be used to

explain this confusion concept. First, we apply the formula of Effective Annual Rate

(EAR) by increasing the compounding frequency of m:

For example, if APR is 10% and compounded semi-annually, EAR will be 10.25%:

>>> (1+0.1/2)**2-1

>>>

0.10250000000000004

Since this function is quite simple, we could write a Python function instead, see the

following program:

def EAR_f(APR,m):

return (1+APR/m)**m-1

Next, assume that the APR is 10% and let's increase the compounding frequency, see

the following program:

import numpy as np

d=365

h=d*24

m=h*60

s=m*60

ms=s*1000

x=np.array([1,2,4,12,d,h,m,s,ms])

APR=0.1

for i in x:

print(EAR_f(APR,i))

[ 155 ]

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

Saved successfully!

Ooh no, something went wrong!