-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkmv3.py
83 lines (79 loc) · 3.21 KB
/
kmv3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: utf-8 -*-
import commons
import math, time, linecache
def get_fund1(sigmaP, sigmaVi, nd1, d1):
temp = math.log((sigmaP * commons.EQUITY) / (nd1 * sigmaVi * commons.D))
numerator = temp + (commons.R + 0.5 * sigmaVi * sigmaVi) * commons.T
denominator = sigmaVi * math.sqrt(commons.T)
fund1i = (d1 - numerator / denominator)
if fund1i < 0 :
fund1i = -fund1i
# print fund1i
return fund1i
def get_v(sigmaP, nd1, sigmaV):
numerator = sigmaP * commons.EQUITY
denominator = nd1 * sigmaV
return numerator / denominator
def main():
input_file = commons.get_input_file()
data_file = "data/stock-price.data"
out_file = "out.data"
outputfile = open(out_file, "w")
outputfile.write("KMV3 begin: " + time.strftime('%Y-%m-%d %H:%M:%S') + "\n")
outputfile.write("+++++++++++++++++++++++++++\n")
if input_file != "" :
inputfile = open(input_file, "r")
while True :
line = inputfile.readline()
if not line :
break
outputfile.write(line)
list_values = line.split(',')
print line.split(',')
commons.R = float(list_values[0])
commons.D = float(list_values[1])
commons.EQUITY = float(list_values[2])
data_file = list_values[3].rstrip()
# read price of stock from file "stock-price.data"
listP = commons.get_listP(data_file)
commons.N = len(listP)
sigmaP = commons.get_sigmaP(listP)
# print "The volatility of the stock's price: sigmaP = %10.12f" % sigmaP
sigmaVi = sigmaV0 = commons.get_sigmaV0(sigmaP) # sigmaV0 is the down limit
# print sigmaV0
sigmaV = nd1 = 0.0
fund1 = 99999
nxxcache = linecache.getlines("nx-x")
xnxcache = linecache.getlines("x-nx")
while True :
sigmaVi += 0.0001
if sigmaVi > sigmaP :
break
nd2i = commons.get_nd2(sigmaP, sigmaVi)
d2i = commons.get_d2(nd2i, nxxcache)
if d2i == 9999 :
continue
d1i = sigmaVi * commons.T + d2i
nd1i = commons.get_nd1(d1i, xnxcache)
fund1i = get_fund1(sigmaP, sigmaVi, nd1i, d1i)
if fund1 > fund1i :
fund1 = fund1i
sigmaV = sigmaVi
nd1 = nd1i
# print "The fund1i is %10.12f, and the sigmaVi is %10.12f" % (fund1i, sigmaVi)
# print "The fd1 is %10.12f, and the sigmaV is %10.12f" % (fund1, sigmaV)
v = get_v(sigmaP, nd1, sigmaV)
print "V is %.10f" % v
dd = (v - commons.D) / (v * sigmaV)
print "DD is %.10f" % dd
outputfile.write("DD is %.10f\n" % dd)
edf = commons.get_nd1(-dd, xnxcache)
print "EDF is %.10f\n" % edf
outputfile.write("EDF is %.10f\n\n" % edf)
inputfile.close()
outputfile.write("---------------------------\n")
outputfile.write("KMV3 end: " + time.strftime('%Y-%m-%d %H:%M:%S'))
outputfile.write("\n\n")
outputfile.close()
if __name__=="__main__":
main()