deno.land / x / financial@v0.1.3 / test / financial.test.ts

financial.test.ts
نووسراو ببینە
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { fv, pmt, nper, ipmt, ppmt, pv, rate, irr, npv, mirr, PaymentDueTime } from '../src/financial'
// Mostly based on// https://github.com/numpy/numpy-financial/blob/master/numpy_financial/tests/test_financial.py
describe('fv()', () => { it('calculates float when is end', () => { expect(fv(0.075, 20, -2000, 0, PaymentDueTime.End)).toBeCloseTo(86609.362673042924, 6) })
it('calculates float when is begin', () => { expect(fv(0.075, 20, -2000, 0, PaymentDueTime.Begin)).toBeCloseTo(93105.064874, 6) })
it('calculates float with rate 0', () => { expect(fv(0, 5, 100, 0)).toBeCloseTo(-500, 6) expect(fv(0.1, 5, 100, 0)).toBeCloseTo(-610.51, 6) })})
describe('pmt()', () => { it('calculates float when is end', () => { expect(pmt(0.08 / 12, 5 * 12, 15000)).toBeCloseTo(-304.145914, 6) })
it('calculates float when is begin', () => { expect(pmt(0.08 / 12, 5 * 12, 15000, 0, PaymentDueTime.Begin)).toBeCloseTo(-302.13170297305413, 6) })
it('calculates float with rate 0', () => { expect(pmt(0.0, 5 * 12, 15000)).toBeCloseTo(-250, 6) })})
describe('nper()', () => { it('calculates float when is end', () => { expect(nper(0, -2000, 0, 100000)).toBeCloseTo(50, 6) expect(nper(0.075, -2000, 0, 100000)).toBeCloseTo(21.544944, 6) expect(nper(0.1, 0, -500, 1500)).toBeCloseTo(11.52670461, 6) expect(nper(0.075, -2000, 0, 100000)).toBeCloseTo(21.5449442) })
it('calculates float when is begin', () => { expect(nper(0.075, -2000, 0, 100000, PaymentDueTime.Begin)).toBeCloseTo(20.76156441) })
it('deals with infinite payments', () => { expect(nper(0, -0.0, 1000)).toBe(Number.POSITIVE_INFINITY) })
it('calculates float with rate 0', () => { expect(nper(0, -100, 1000)).toBeCloseTo(10, 6) })})
describe('ipmt()', () => { it('calculates float when is end', () => { expect(ipmt(0.1 / 12, 1, 24, 2000)).toBeCloseTo(-16.666667, 6) expect(ipmt(0.1 / 12, 2, 24, 2000)).toBeCloseTo(-16.03647345, 6) expect(ipmt(0.1 / 12, 3, 24, 2000)).toBeCloseTo(-15.40102862, 6) expect(ipmt(0.1 / 12, 4, 24, 2000)).toBeCloseTo(-14.76028842, 6) })
it('calculates float when is begin', () => { expect(ipmt(0.1 / 12, 1, 24, 2000, 0, PaymentDueTime.Begin)).toBe(0) expect(ipmt(0.001988079518355057, 0, 360, 300000, 0, PaymentDueTime.Begin)).toBe(Number.NaN) expect(ipmt(0.001988079518355057, 1, 360, 300000, 0, PaymentDueTime.Begin)).toBe(0) expect(ipmt(0.001988079518355057, 2, 360, 300000, 0, PaymentDueTime.Begin)).toBeCloseTo(-594.107158, 6) expect(ipmt(0.001988079518355057, 3, 360, 300000, 0, PaymentDueTime.Begin)).toBeCloseTo(-592.971592, 6) })})
describe('ppmt()', () => { it('calculates float when is end', () => { expect(ppmt(0.1 / 12, 1, 60, 55000)).toBeCloseTo(-710.25412578642, 6) expect(ppmt(0.08 / 12, 1, 60, 15000)).toBeCloseTo(-204.145914, 6) })
it('calculates float when is begin', () => { expect(ppmt(0.1 / 12, 1, 60, 55000, 0, PaymentDueTime.Begin)).toBeCloseTo(-1158.929712, 6) expect(ppmt(0.08 / 12, 1, 60, 15000, 0, PaymentDueTime.Begin)).toBeCloseTo(-302.131703, 6) })})
describe('pv()', () => { it('calculates float when is end', () => { expect(pv(0.07, 20, 12000)).toBeCloseTo(-127128.1709461939, 6) expect(pv(0.07, 21, 12000)).toBeCloseTo(-130026.3279870970, 6) expect(pv(0.07, 22, 12000)).toBeCloseTo(-132734.8859692500, 6) expect(pv(0.07, 23, 12000)).toBeCloseTo(-135266.2485693920, 6) expect(pv(0.07, 24, 12000)).toBeCloseTo(-137632.0080087780, 6) expect(pv(0.07, 25, 12000)).toBeCloseTo(-139842.9981390450, 6) })
it('calculates float when is begin', () => { expect(pv(0.07, 20, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-136027.1429124, 6) expect(pv(0.07, 21, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-139128.1709461, 6) expect(pv(0.07, 22, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-142026.3279870, 6) expect(pv(0.07, 23, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-144734.8859692, 6) expect(pv(0.07, 24, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-147266.2485693, 6) expect(pv(0.07, 25, 12000, 0, PaymentDueTime.Begin)).toBeCloseTo(-149632.0080087, 6) })
it('calculates float when the rate is 0', () => { expect(pv(0, 20, 12000, 0)).toBeCloseTo(-240000, 6) })
it('calculates float when fv != 0', () => { expect(pv(0, 20, 12000, 1000)).toBeCloseTo(-241000, 6) })})
describe('rate()', () => { it('calculates float when is end', () => { expect(rate(10, 0, -3500, 10000)).toBeCloseTo(0.1106908, 6) })
it('calculates float when is begin', () => { expect(rate(10, 0, -3500, 10000, PaymentDueTime.Begin)).toBeCloseTo(0.1106908, 6) })
it('Should return NaN for infeasible solution', () => { expect(rate(12, 400, 10000, 5000, PaymentDueTime.End)).toBeNaN() expect(rate(12, 400, 10000, 5000, PaymentDueTime.Begin)).toBeNaN() })
it('calculates float with a custom guess, tolerance and maxIter', () => { expect(rate(10, 0, -3500, 10000, PaymentDueTime.Begin, 0.2, 1e-5, 200)).toBeCloseTo(0.1106908, 6) })})
describe('irr()', () => { it('calculates basic values', () => { expect(irr([-150000, 15000, 25000, 35000, 45000, 60000])).toBeCloseTo(0.052433, 6) expect(irr([-100, 0, 0, 74])).toBeCloseTo(-0.095496, 6) expect(irr([-100, 39, 59, 55, 20])).toBeCloseTo(0.2809484, 6) expect(irr([-100, 100, 0, -7])).toBeCloseTo(-0.083300, 6) expect(irr([-100, 100, 0, 7])).toBeCloseTo(0.0620585, 6) expect(irr([-5, 10.5, 1, -8, 1])).toBeCloseTo(0.088598, 6) })
it('calculates trailing zeroes correctly', () => { expect(irr([-5, 10.5, 1, -8, 1, 0, 0, 0])).toBeCloseTo(0.088598, 6) })
it('returns NaN if there is no solution', () => { expect(irr([-1, -2, -3])).toBeNaN() })
it('calculates with custom guess, tol and maxIter', () => { expect(irr([-5, 10.5, 1, -8, 1], 0.1, 1e-10, 10)).toBeCloseTo(0.08859833852439172, 9) })
it('returns null if can\'t calculate the result within the given number of iterations', () => { expect(irr([-5, 10.5, 1, -8, 1], 0.1, 1e-10, 2)).toBeNaN() })})
describe('npv()', () => { it('calculates float', () => { expect(npv(0.05, [-15000, 1500, 2500, 3500, 4500, 6000])).toBeCloseTo(122.894855, 6) })})
describe('mirr()', () => { it('calculates float', () => { expect(mirr([-4500, -800, 800, 800, 600, 600, 800, 800, 700, 3000], 0.08, 0.055)).toBeCloseTo(0.066597, 6) expect(mirr([-120000, 39000, 30000, 21000, 37000, 46000], 0.10, 0.12)).toBeCloseTo(0.126094, 6) expect(mirr([100, 200, -50, 300, -200], 0.05, 0.06)).toBeCloseTo(0.342823, 6) })
it('returns NaN if mirr() cannot be calculated', () => { expect(mirr([39000, 30000, 21000, 37000, 46000], 0.10, 0.12)).toBeNaN() expect(mirr([-39000, -30000, -21000, -37000, -46000], 0.10, 0.12)).toBeNaN() })})
financial

Version Info

Tagged at
3 years ago