forked from SciSharp/Numpy.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumPy_financial.tests.cs
More file actions
329 lines (281 loc) · 12.6 KB
/
NumPy_financial.tests.cs
File metadata and controls
329 lines (281 loc) · 12.6 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) 2019 by the SciSharp Team
// Code generated by CodeMinion: https://github.com/SciSharp/CodeMinion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using Python.Runtime;
using Numpy.Models;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Assert = NUnit.Framework.Assert;
namespace Numpy.UnitTest
{
[TestClass]
public class NumPy_financialTest : BaseTestCase
{
[TestMethod]
public void fvTest()
{
// What is the future value after 10 years of saving $100 now, with
// an additional monthly savings of $100. Assume the interest rate is
// 5% (annually) compounded monthly?
// >>> np.fv(0.05/12, 10*12, -100, -100)
// 15692.928894335748
//
#if TODO
var given= np.fv(0.05/12, 10*12, -100, -100);
var expected=
"15692.928894335748";
Assert.AreEqual(expected, given.repr);
#endif
// By convention, the negative sign represents cash flow out (i.e. money not
// available today). Thus, saving $100 a month at 5% annual interest leads
// to $15,692.93 available to spend in 10 years.
// If any input is array_like, returns an array of equal shape. Let’s
// compare different interest rates from the example above.
// >>> a = np.array((0.05, 0.06, 0.07))/12
// >>> np.fv(a, 10*12, -100, -100)
// array([ 15692.92889434, 16569.87435405, 17509.44688102])
//
#if TODO
given= a = np.array((0.05, 0.06, 0.07))/12;
given= np.fv(a, 10*12, -100, -100);
expected=
"array([ 15692.92889434, 16569.87435405, 17509.44688102])";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void pvTest()
{
// What is the present value (e.g., the initial investment)
// of an investment that needs to total $15692.93
// after 10 years of saving $100 every month? Assume the
// interest rate is 5% (annually) compounded monthly.
// >>> np.pv(0.05/12, 10*12, -100, 15692.93)
// -100.00067131625819
//
#if TODO
var given= np.pv(0.05/12, 10*12, -100, 15692.93);
var expected=
"-100.00067131625819";
Assert.AreEqual(expected, given.repr);
#endif
// By convention, the negative sign represents cash flow out
// (i.e., money not available today). Thus, to end up with
// $15,692.93 in 10 years saving $100 a month at 5% annual
// interest, one’s initial deposit should also be $100.
// If any input is array_like, pv returns an array of equal shape.
// Let’s compare different interest rates in the example above:
// >>> a = np.array((0.05, 0.04, 0.03))/12
// >>> np.pv(a, 10*12, -100, 15692.93)
// array([ -100.00067132, -649.26771385, -1273.78633713])
//
#if TODO
given= a = np.array((0.05, 0.04, 0.03))/12;
given= np.pv(a, 10*12, -100, 15692.93);
expected=
"array([ -100.00067132, -649.26771385, -1273.78633713])";
Assert.AreEqual(expected, given.repr);
#endif
// So, to end up with the same $15692.93 under the same $100 per month
// “savings plan,” for annual interest rates of 4% and 3%, one would
// need initial investments of $649.27 and $1273.79, respectively.
}
[TestMethod]
public void npvTest()
{
// >>> np.npv(0.281,[-100, 39, 59, 55, 20])
// -0.0084785916384548798
//
#if TODO
var given= np.npv(0.281,{-100, 39, 59, 55, 20});
var expected=
"-0.0084785916384548798";
Assert.AreEqual(expected, given.repr);
#endif
// (Compare with the Example given for numpy.lib.financial.irr)
}
[TestMethod]
public void pmtTest()
{
// What is the monthly payment needed to pay off a $200,000 loan in 15
// years at an annual interest rate of 7.5%?
// >>> np.pmt(0.075/12, 12*15, 200000)
// -1854.0247200054619
//
#if TODO
var given= np.pmt(0.075/12, 12*15, 200000);
var expected=
"-1854.0247200054619";
Assert.AreEqual(expected, given.repr);
#endif
// In order to pay-off (i.e., have a future-value of 0) the $200,000 obtained
// today, a monthly payment of $1,854.02 would be required. Note that this
// example illustrates usage of fv having a default value of 0.
}
[TestMethod]
public void ipmtTest()
{
// What is the amortization schedule for a 1 year loan of $2500 at
// 8.24% interest per year compounded monthly?
// >>> principal = 2500.00
//
#if TODO
var given= principal = 2500.00;
#endif
// The ‘per’ variable represents the periods of the loan. Remember that
// financial equations start the period count at 1!
// >>> per = np.arange(1*12) + 1
// >>> ipmt = np.ipmt(0.0824/12, per, 1*12, principal)
// >>> ppmt = np.ppmt(0.0824/12, per, 1*12, principal)
//
#if TODO
given= per = np.arange(1*12) + 1;
given= ipmt = np.ipmt(0.0824/12, per, 1*12, principal);
given= ppmt = np.ppmt(0.0824/12, per, 1*12, principal);
#endif
// Each element of the sum of the ‘ipmt’ and ‘ppmt’ arrays should equal
// ‘pmt’.
// >>> pmt = np.pmt(0.0824/12, 1*12, principal)
// >>> np.allclose(ipmt + ppmt, pmt)
// True
//
#if TODO
given= pmt = np.pmt(0.0824/12, 1*12, principal);
given= np.allclose(ipmt + ppmt, pmt);
var expected=
"True";
Assert.AreEqual(expected, given.repr);
#endif
// >>> fmt = '{0:2d} {1:8.2f} {2:8.2f} {3:8.2f}'
// >>> for payment in per:
// ... index = payment - 1
// ... principal = principal + ppmt[index]
// ... print(fmt.format(payment, ppmt[index], ipmt[index], principal))
// 1 -200.58 -17.17 2299.42
// 2 -201.96 -15.79 2097.46
// 3 -203.35 -14.40 1894.11
// 4 -204.74 -13.01 1689.37
// 5 -206.15 -11.60 1483.22
// 6 -207.56 -10.18 1275.66
// 7 -208.99 -8.76 1066.67
// 8 -210.42 -7.32 856.25
// 9 -211.87 -5.88 644.38
// 10 -213.32 -4.42 431.05
// 11 -214.79 -2.96 216.26
// 12 -216.26 -1.49 -0.00
//
#if TODO
given= fmt = '{0:2d} {1:8.2f} {2:8.2f} {3:8.2f}';
given= for payment in per:;
expected=
"... index = payment - 1\n" +
"... principal = principal + ppmt[index]\n" +
"... print(fmt.format(payment, ppmt[index], ipmt[index], principal))\n" +
" 1 -200.58 -17.17 2299.42\n" +
" 2 -201.96 -15.79 2097.46\n" +
" 3 -203.35 -14.40 1894.11\n" +
" 4 -204.74 -13.01 1689.37\n" +
" 5 -206.15 -11.60 1483.22\n" +
" 6 -207.56 -10.18 1275.66\n" +
" 7 -208.99 -8.76 1066.67\n" +
" 8 -210.42 -7.32 856.25\n" +
" 9 -211.87 -5.88 644.38\n" +
"10 -213.32 -4.42 431.05\n" +
"11 -214.79 -2.96 216.26\n" +
"12 -216.26 -1.49 -0.00";
Assert.AreEqual(expected, given.repr);
#endif
// >>> interestpd = np.sum(ipmt)
// >>> np.round(interestpd, 2)
// -112.98
//
#if TODO
given= interestpd = np.sum(ipmt);
given= np.round(interestpd, 2);
expected=
"-112.98";
Assert.AreEqual(expected, given.repr);
#endif
}
[TestMethod]
public void irrTest()
{
// >>> round(irr([-100, 39, 59, 55, 20]), 5)
// 0.28095
// >>> round(irr([-100, 0, 0, 74]), 5)
// -0.0955
// >>> round(irr([-100, 100, 0, -7]), 5)
// -0.0833
// >>> round(irr([-100, 100, 0, 7]), 5)
// 0.06206
// >>> round(irr([-5, 10.5, 1, -8, 1]), 5)
// 0.0886
//
#if TODO
var given= round(irr([-100, 39, 59, 55, 20]), 5);
var expected=
"0.28095";
Assert.AreEqual(expected, given.repr);
given= round(irr([-100, 0, 0, 74]), 5);
expected=
"-0.0955";
Assert.AreEqual(expected, given.repr);
given= round(irr([-100, 100, 0, -7]), 5);
expected=
"-0.0833";
Assert.AreEqual(expected, given.repr);
given= round(irr([-100, 100, 0, 7]), 5);
expected=
"0.06206";
Assert.AreEqual(expected, given.repr);
given= round(irr([-5, 10.5, 1, -8, 1]), 5);
expected=
"0.0886";
Assert.AreEqual(expected, given.repr);
#endif
// (Compare with the Example given for numpy.lib.financial.npv)
}
[TestMethod]
public void nperTest()
{
// If you only had $150/month to pay towards the loan, how long would it take
// to pay-off a loan of $8,000 at 7% annual interest?
// >>> print(round(np.nper(0.07/12, -150, 8000), 5))
// 64.07335
//
#if TODO
var given= print(round(np.nper(0.07/12, -150, 8000), 5));
var expected=
"64.07335";
Assert.AreEqual(expected, given.repr);
#endif
// So, over 64 months would be required to pay off the loan.
// The same analysis could be done with several different interest rates
// and/or payments and/or total amounts to produce an entire table.
// >>> np.nper(*(np.ogrid[0.07/12: 0.08/12: 0.01/12,
// ... -150 : -99 : 50 ,
// ... 8000 : 9001 : 1000]))
// array([[[ 64.07334877, 74.06368256],
// [ 108.07548412, 127.99022654]],
// [[ 66.12443902, 76.87897353],
// [ 114.70165583, 137.90124779]]])
//
#if TODO
given= np.nper(*(np.ogrid{0.07/12: 0.08/12: 0.01/12,;
expected=
"... -150 : -99 : 50 ,\n" +
"... 8000 : 9001 : 1000]))\n" +
"array([[[ 64.07334877, 74.06368256],\n" +
" [ 108.07548412, 127.99022654]],\n" +
" [[ 66.12443902, 76.87897353],\n" +
" [ 114.70165583, 137.90124779]]])";
Assert.AreEqual(expected, given.repr);
#endif
}
}
}