-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfin.test.py
121 lines (86 loc) · 3.08 KB
/
fin.test.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import unittest
import numpy as np
import fin
import math
class SI_Test(unittest.TestCase):
def test_SI(self):
actual = fin.SI(100, 0.10, 1)
expected = 10.0
#self.failUnless(expected == actual)
self.assertAlmostEqual(expected, actual)
class CI_Test(unittest.TestCase):
def test_CI(self):
actual = fin.CI(100, 0.10, 2)
expected = 21.0
self.assertAlmostEqual(expected, actual)
class EI_Test(unittest.TestCase):
def test_EI(self):
actual = fin.EI(100, 0.10, 10)
expected = 100*(math.e - 1)
self.assertAlmostEqual(expected, actual)
class FV_Test(unittest.TestCase):
def test_FV(self):
actual = fin.FV(100, 0.10, 10)
expected = 100*math.pow(1.1, 10)
self.assertAlmostEqual(expected, actual)
class PV_Test(unittest.TestCase):
def test_PV(self):
actual = fin.PV(110, 0.10, 1)
expected = 100.0
self.assertAlmostEqual(expected, actual)
class IRR_Test(unittest.TestCase):
@staticmethod
def referenceIRR(payments):
sortedRoots = sorted(np.roots(payments), key = lambda x : abs(np.imag(x)))
return np.real(sortedRoots[0]).item() - 1
def test_short_payment_stream(self):
payments = [-500, 300, 300]
actual = fin.IRR(payments)
expected = IRR_Test.referenceIRR(payments)
self.assertAlmostEqual(expected, actual)
def test_long_payment_stream(self):
payments = [-2500, 300, 300, -900, 1000, 500, -100, 700, 700, 2300]
actual = fin.IRR(payments)
expected = IRR_Test.referenceIRR(payments)
self.assertAlmostEqual(expected, actual)
class PI_Test(unittest.TestCase):
def test_PI(self):
actual = fin.PI([-10, 11], 0.1)
expected = 1.0
self.assertAlmostEqual(expected, actual)
def test_PI_2(self):
actual = fin.PI([-10, 11, 12.1], 0.1)
expected = 2.0
self.assertAlmostEqual(expected, actual)
class NPV_Test(unittest.TestCase):
def test_NPV(self):
actual = fin.NPV([-10, 11], 0.1)
expected = 0.0
self.assertAlmostEqual(expected, actual)
def test_NPV_2(self):
actual = fin.NPV([-10, 11, 12.1], 0.1)
expected = 10.0
self.assertAlmostEqual(expected, actual)
class MIRR_Test(unittest.TestCase):
def test_MIRR(self):
actual = fin.MIRR([-11, 11], 0.1)
expected = 0.0
self.assertAlmostEqual(expected, actual)
def test_MIRR_2(self):
actual = fin.MIRR([-10, 11, 12.1], 0.1)
expected = 1.1*math.sqrt(2.0) - 1
self.assertAlmostEqual(expected, actual)
class PaybackPeriod_Test(unittest.TestCase):
def test_PaybackPeriod(self):
actual = fin.PaybackPeriod([-1000, 550, 605, 700], 0.1)
expected = 2.0
self.assertAlmostEqual(expected, actual)
def test_PaybackPeriod_2(self):
p = 1.1
actual = fin.PaybackPeriod([-1000, 500*p, 250*p*p, 500*p*p*p], p - 1)
expected = 2.5
self.assertAlmostEqual(expected, actual)
def main():
unittest.main()
if __name__ == '__main__':
main()