-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtadJS.js
More file actions
558 lines (491 loc) · 15.7 KB
/
tadJS.js
File metadata and controls
558 lines (491 loc) · 15.7 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
var tadJS = {
/*****************************************
Author: Abraham A.
Copyright: (c) 2007-2014 tadxl.com
This financial functions library for JavaScript is based on financial
functions found in tadXL add-in for Excel 2007, 2010 and 2013.
tadXL functions were developed over an extended period of 6 years
based on authors research into algorithms for financial analysis
that were originally developed as online calculators at thinkanddone.com
The prefix "tad" in tadJS and tadXL is made from the initials of
authors flagship site thinkanddone.com
There are over 100 financial functions in this library that will be added
piecemeal to tadJS
*****************************************/
tadAEY: function(r, c)
{
if (r==0.0)
return 0.0;
if (c==0.0)
return Math.exp(r) - 1;
else
return Math.pow(1.0+r*c, 1/c) - 1;
},
tadEFFECT: function(r, c)
{
if (r==0.0)
return 0.0;
if (c==0.0)
return Math.exp(r)-1;
else
return Math.pow(1.0+r,1/c) - 1;
},
tadNOMINAL: function(r, c)
{
if (r==0.0)
return 0.0;
if (c==0.0)
return Math.log(1+r);
else
return (Math.pow(1.0+r,c) - 1)/c;
},
tadPVIF: function(r, n, c)
{
c = (typeof c !== "undefined") ? c : 1;
if (r==0.0)
return 1.0;
if (n==0.0)
return 1.0;
return Math.pow(1.0+this.tadAEY(r,c),-n);
},
tadPVIFbar: function(r, n, c)
{
c = (typeof c !== "undefined") ? c : 1;
if (r==0.0)
return -n;
if (n==0.0)
return 0.0;
return -n * this.tadPVIF(r,n+c,c);
},
tadFVIF: function(r, n, c)
{
c = (typeof c !== "undefined") ? c : 1;
if (r==0.0)
return 1.0;
if (n==0.0)
return 1.0;
return Math.pow(1.0+this.tadAEY(r,c),n);
},
tadFVIFbar: function(r, n, c)
{
c = (typeof c !== "undefined") ? c : 1;
if (r==0.0)
return n;
if (n==0.0)
return 0.0;
return n * this.tadFVIF(r,n-c,c);
},
tadPVIF2: function(r, n, c, p, d)
{
var t=0.0;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
t = (n-1)*p+d*p;
if (r==0.0)
return 1.0;
if (t==0.0)
return 1.0;
return this.tadPVIF(r,t,c);
},
tadPVIF2bar: function(r, n, c, p, d)
{
var t=0.0;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
t = (n-1)*p+d*p;
if (r==0.0)
return -t;
if (t==0.0)
return 0.0;
return -t * this.tadPVIF(r,t+c,c);
},
tadFVIF2: function(r, n, c, p, d)
{
var t=0.0;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
t = (n-1)*p+d*p;
if (r==0.0)
return 1.0;
if (t==0.0)
return 1.0;
return this.tadFVIF(r,t,c);
},
tadFVIF2bar: function(r, n, c, p, d)
{
var t=0.0;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
t = (n-1)*p+d*p;
if (r==0.0)
return t;
if (t==0.0)
return 0.0;
return t * this.tadFVIF(r,t-c,c);
},
DaysBetween: function(t1, t2) {
var oneDay = 24*60*60*1000;
return Math.round(Math.abs((t1.getTime() - t2.getTime())/oneDay));
},
tadBCR: function(r, cfs, atype, c, p, d) {
var bcr = 0.0;
var cf,dc=0.0,db=0.0;
var i,t;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
for (i=0; i< cfs.length; i++) {
if (atype == 0)
t = i*p+d*p;
else
{
if (i==0)
t = 0;
else
t = (i-1)*p+d*p;
}
cf = cfs[i];
if (cf < 0.0)
dc += Math.abs(cf) * this.tadPVIF(r, t, c);
else
db += cf * this.tadPVIF(r, t, c);
};
bcr = db / dc;
return bcr;
},
tadNPV: function(r, cfs, atype, c, p, d) {
var npv = 0.0;
var cf;
var i,t;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
for (i=0; i< cfs.length; i++) {
if (atype == 0)
t = i*p+d*p;
else
{
if (i==0)
t = 0;
else
t = (i-1)*p+d*p;
}
cf = cfs[i];
npv += cf * this.tadPVIF(r, t, c);
};
return npv;
},
tadNPVbar: function(r, cfs, atype, c, p, d) {
var npv = 0.0;
var cf;
var i,t;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
for (i=0; i< cfs.length; i++) {
if (atype == 0)
t = i*p+d*p;
else
{
if (i==0)
t = 0;
else
t = (i-1)*p+d*p;
}
cf = cfs[i];
npv += cf * this.tadPVIFbar(r, t, c);
};
return npv;
},
tadNFV: function(r, cfs, atype, c, p, d) {
var n;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
n = cfs.length;
npv = this.tadNPV(r, cfs, atype, c, p, d);
nfv = npv * this.tadFVIF(r, n, c);
return nfv;
},
tadNFVbar: function(r, cfs, atype, c, p, d) {
var n;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
n = cfs.length;
npv = this.tadNPVbar(r, cfs, atype, c, p, d);
nfv = npv * this.tadFVIF(r, n, c);
return nfv;
},
tadMIRR: function(fr, rr, cfs, atype, c, p, d) {
var mirr = 0.0;
var cf,db=0.0,dc=0.0;
var i,t,T;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
T = (cfs.length-1.0-atype) * p + p*d;
for (i=0; i< cfs.length; i++) {
if (atype == 0)
t = i*p+d*p;
else
{
if (i==0)
t = 0;
else
t = (i-1)*p+d*p;
}
cf = cfs[i];
if (cf < 0.0)
dc += Math.abs(cf) * this.tadPVIF(fr, t, c);
else
db += cf * this.tadFVIF(rr, t, c);
};
mirr = Math.pow(db / dc, 1.0/T) - 1.0;
return mirr;
},
tadPI: function(r, cfs, atype, c, p, d) {
var pi = 0.0;
var cf,de=0.0,dr=0.0;
var i,t;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
for (i=0; i< cfs.length; i++) {
if (atype == 0)
t = i*p+d*p;
else
{
if (i==0)
t = 0;
else
t = (i-1)*p+d*p;
}
cf = cfs[i];
if (cf < 0.0)
de += Math.abs(cf) * this.tadPVIF(r, t, c);
else
dr += cf * this.tadPVIF(r, t, c);
};
pi = dr / de;
return pi;
},
tadXNPV: function(r, data_values, c) {
var npv = 0.0;
c = (typeof c !== "undefined") ? c : 1;
var t0 = new Date(data_values[0].T);
for (var key in data_values) {
var current = data_values[key];
var cf = current.CF;
var t = new Date(current.T);
npv += cf * this.tadPVIF(r, this.DaysBetween(t0, t)/365, c);
};
return npv;
},
tadXNPVbar: function(r, data_values, c) {
var npv = 0.0;
c = (typeof c !== "undefined") ? c : 1;
var t0 = new Date(data_values[0].T);
for (var key in data_values) {
var current = data_values[key];
var cf = current.CF;
var t = new Date(current.T);
npv += cf * this.tadPVIFbar(r, this.DaysBetween(t0, t)/365, c);
};
return npv;
},
tadXIRR: function(data_values, guess,c) {
guess = (typeof guess === "undefined") ? 0.10 : guess;
c = (typeof c !== "undefined") ? c : 1;
var i;
var x = 0.0;
var x0 = guess;
var f;
var fbar;
for (i=0; i<100; i++)
{
f = this.tadXNPV(x0, data_values,c);
fbar = this.tadXNPVbar(x0, data_values,c);
if (fbar == 0.0)
return null;
else
x = x0 - f/fbar;
if ( Math.abs(x-x0) < 0.000001 )
return x;
x0 = x;
};
return null;
},
tadIRR: function(cfs, atype, guess, c, p, d) {
guess = (typeof guess === "undefined") ? 0.10 : guess;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
var i;
var x = 0.0;
var x0 = guess;
var f;
var fbar;
for (i=0; i<100; i++)
{
f = this.tadNPV(x0, cfs, atype, c, p, d);
fbar = this.tadNPVbar(x0, cfs, atype, c, p, d);
if (fbar == 0.0)
return null;
else
x = x0 - f/fbar;
if ( Math.abs(x-x0) < 0.000001 )
return x;
x0 = x;
};
return null;
},
tadIRR2: function(cfs, atype, guess, c, p, d) {
guess = (typeof guess === "undefined") ? 0.10 : guess;
atype = (typeof atype !== "undefined") ? atype : 1;
c = (typeof c !== "undefined") ? c : 1;
p = (typeof p !== "undefined") ? p : 1;
d = (typeof d !== "undefined") ? d : 1;
var i;
var x = 0.0;
var x0 = guess;
var f;
var fbar;
for (i=0; i<100; i++)
{
f = this.tadNFV(x0, cfs, atype, c, p, d);
fbar = this.tadNFVbar(x0, cfs, atype, c, p, d);
if (fbar == 0.0)
return null;
else
x = x0 - f/fbar;
if ( Math.abs(x-x0) < 0.000001 )
return x;
x0 = x;
};
return null;
}
};
// Sample tests for tadJS functions
// Following statements allow you to see test results for the functions
// in tadJS by opening the accompanying tadJS.html file in your browser
// To see the result, please remove the outer comment delimeters
/*
var data_values = [];
data_values.push({CF: -1000, T: "2012-01-01"});
data_values.push({CF: 500, T: "2013-01-01"});
data_values.push({CF: 400, T: "2014-01-01"});
data_values.push({CF: 300, T: "2015-01-01"});
data_values.push({CF: 100, T: "2016-01-01"});
var cfs = [-1000,500,400,300,100];
document.write( "PVIF(10%, 10, 1, 1, 1) = " + tadJS.tadPVIF2(0.10,10) );
document.write("<br/>");
document.write( "PVIF(10%, 10, 1, 1/2, 1) = " + tadJS.tadPVIF2(0.10,10,undefined,0.5) );
document.write("<br/>");
document.write( "PVIF(10%, 10, 1/2, 1, 1) = " + tadJS.tadPVIF2(0.10,10,0.5) );
document.write("<br/>");
document.write( "PVIF(10%, 10, 1/2, 1/2, 1) = " + tadJS.tadPVIF2(0.10,10,0.5,0.5) );
document.write("<br/>");
document.write( "PVIF(10%, 10, 1/2, 1/2, 1/2) = " + tadJS.tadPVIF2(0.10,10,0.5,0.5,0.5) );
document.write("<br/>");
document.write( "FVIF(10%, 10, 1, 1, 1) = " + tadJS.tadFVIF2(0.10,10) );
document.write("<br/>");
document.write( "FVIF(10%, 10, 1, 1/2, 1) = " + tadJS.tadFVIF2(0.10,10,undefined,0.5) );
document.write("<br/>");
document.write( "FVIF(10%, 10, 1/2, 1, 1) = " + tadJS.tadFVIF2(0.10,10,0.5) );
document.write("<br/>");
document.write( "FVIF(10%, 10, 1/2, 1/2, 1) = " + tadJS.tadFVIF2(0.10,10,0.5,0.5) );
document.write("<br/>");
document.write( "FVIF(10%, 10, 1/2, 1/2, 1/2) = " + tadJS.tadFVIF2(0.10,10,0.5,0.5,0.5) );
document.write("<br/>");
document.write( "BCR(10%, [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadBCR(0.10,cfs) );
document.write("<br/>");
document.write( "BCR(10%, [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadBCR(0.10,cfs,0) );
document.write("<br/>");
document.write( "BCR(10%, [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadBCR(0.10,cfs,undefined, 0.5) );
document.write("<br/>");
document.write( "BCR(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadBCR(0.10,cfs,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "BCR(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadBCR(0.10,cfs,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "MIRR(10%, 8%, [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadMIRR(0.10,0.08,cfs) );
document.write("<br/>");
document.write( "MIRR(10%, 8%, [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadMIRR(0.10,0.08,cfs,0) );
document.write("<br/>");
document.write( "MIRR(10%, 8%, [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadMIRR(0.10,0.08,cfs,undefined, 0.5) );
document.write("<br/>");
document.write( "MIRR(10%, 8%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadMIRR(0.10,0.08,cfs,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "MIRR(10%, 8%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadMIRR(0.10,0.08,cfs,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "NPV(10%, [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadNPV(0.10,cfs) );
document.write("<br/>");
document.write( "NPV(10%, [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadNPV(0.10,cfs,0) );
document.write("<br/>");
document.write( "NPV(10%, [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadNPV(0.10,cfs,undefined, 0.5) );
document.write("<br/>");
document.write( "NPV(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadNPV(0.10,cfs,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "NPV(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadNPV(0.10,cfs,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "NFV(10%, [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadNFV(0.10,cfs) );
document.write("<br/>");
document.write( "NFV(10%, [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadNFV(0.10,cfs,0) );
document.write("<br/>");
document.write( "NFV(10%, [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadNFV(0.10,cfs,undefined, 0.5) );
document.write("<br/>");
document.write( "NFV(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadNFV(0.10,cfs,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "NFV(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadNFV(0.10,cfs,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "PI(10%, [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadPI(0.10,cfs) );
document.write("<br/>");
document.write( "PI(10%, [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadPI(0.10,cfs,0) );
document.write("<br/>");
document.write( "PI(10%, [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadPI(0.10,cfs,undefined, 0.5) );
document.write("<br/>");
document.write( "PI(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadPI(0.10,cfs,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "PI(10%, [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadPI(0.10,cfs,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "IRR( [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadIRR(cfs) );
document.write("<br/>");
document.write( "IRR( [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadIRR(cfs,0) );
document.write("<br/>");
document.write( "IRR( [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadIRR(cfs,undefined,undefined, 0.5) );
document.write("<br/>");
document.write( "IRR( [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadIRR(cfs,undefined,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "IRR( [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadIRR(cfs,undefined,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "IRR2( [-1000,500,400,300,100], 1, 1, 1, 1) = " + tadJS.tadIRR2(cfs) );
document.write("<br/>");
document.write( "IRR2( [-1000,500,400,300,100], 0, 1, 1, 1) = " + tadJS.tadIRR2(cfs,0) );
document.write("<br/>");
document.write( "IRR2( [-1000,500,400,300,100], 1, 1/2, 1, 1) = " + tadJS.tadIRR2(cfs,undefined,undefined, 0.5) );
document.write("<br/>");
document.write( "IRR2( [-1000,500,400,300,100], 1, 1/2, 1/2, 1) = " + tadJS.tadIRR2(cfs,undefined,undefined, 0.5, 0.5) );
document.write("<br/>");
document.write( "IRR2( [-1000,500,400,300,100], 1, 1/2, 1/2, 1/2) = " + tadJS.tadIRR2(cfs,undefined,undefined, 0.5, 0.5,0.5) );
document.write("<br/>");
document.write( "XIRR = " + tadJS.tadXIRR(data_values,0.10) );
document.write("<br/>");
document.write( "XIRR = " + tadJS.tadXIRR(data_values,0.10,0.5) );
document.write("<br/>");
document.write( "XNPV = " + tadJS.tadXNPV(0.10,data_values) );
document.write("<br/>");
document.write( "XNPV = " + tadJS.tadXNPV(0.10,data_values,0.5) );
document.write("<br/>");
*/