10.12.2019 Views

Python for Finance

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 13

The following Python program is for estimating total assets (A) and its volatility

(sigmA) for a given E (equity), D (debt), T (maturity), r (risk-free rate), and the

volatility of the equity (sigmaE). The basic logic of the program is that we input

a large number of pairs of (A, sigmaE). Then we estimate E and sigmaE based on

the preceding equation. Since both E and sigmaE are known, we could estimate

the differences, diff4E=estimatedE – knownE and diff4sigmaE = estimatedSigmaE –

knownSigmaE. The pair of (A, sigmaE) that minimizes the sum of those two absolute

differences will be our solution:

import scipy as sp

import pandas as pd

import scipy.stats as stats

from scipy import log,sqrt,exp

# input area

D=30.

# debt

E=70.

# equity

T=1.

# maturity

r=0.07

# risk-free

sigmaE=0.4 # volatility of equity

#

# define a function to siplify notations later

def N(x):

return stats.norm.cdf(x)

#

def KMV_f(E,D,T,r,sigmaE):

n=10000

m=2000

diffOld=1e6 # a very big number

for i in sp.arange(1,10):

for j in sp.arange(1,m):

A=E+D/2+i*D/n

sigmaA=0.05+j*(1.0-0.001)/m

d1 = (log(A/D)+(r+sigmaA*sigmaA/2.)*T)/(sigmaA*sqrt(T))

d2 = d1-sigmaA*sqrt(T)

diff4A= (A*N(d1)-D*exp(-r*T)*N(d2)-E)/A # scale by assets

diff4sigmaE= A/E*N(d1)*sigmaA-sigmaE # a small number

already

diffNew=abs(diff4A)+abs(diff4sigmaE)

if diffNew<diffOld:

diffOld=diffNew

output=(round(A,2),round(sigmaA,4),round(diffNew,5))

return output

#

print("KMV=", KMV_f(D,E,T,r,sigmaE))

print("KMV=", KMV_f(D=65e3,E=110e3,T=1,r=0.01,sigmaE=0.2))

[ 481 ]

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

Saved successfully!

Ooh no, something went wrong!