finrb 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/docs/api.md DELETED
@@ -1,1086 +0,0 @@
1
- # finrb - API and examples
2
-
3
- ## Utils
4
-
5
- Utils is a static class providing basic financial functions for modeling.
6
-
7
- Utils is based on R's [FinCal](https://github.com/felixfan/FinCal) library (ported to Ruby).
8
-
9
- Provides the following functions:
10
-
11
- - Basic Earnings Per Share
12
-
13
- - Bond-equivalent yield (BEY), 2 x the semiannual discount rate
14
-
15
- - Calculate the net increase in common shares from the potential exercise of stock options or warrants
16
-
17
- - Calculate weighted average shares - weighted average number of common shares
18
-
19
- - Cash ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
20
-
21
- - Computing Coefficient of variation
22
-
23
- - Computing HPR, the holding period return
24
-
25
- - Computing IRR, the internal rate of return
26
-
27
- - Computing NPV, the PV of the cash flows less the initial (time = 0) outlay
28
-
29
- - Computing Roy's safety-first ratio
30
-
31
- - Computing Sampling error
32
-
33
- - Computing Sharpe Ratio
34
-
35
- - Computing TWRR, the time-weighted rate of return
36
-
37
- - Computing bank discount yield (BDY) for a T-bill
38
-
39
- - Computing money market yield (MMY) for a T-bill
40
-
41
- - Computing the future value of an uneven cash flow series
42
-
43
- - Computing the present value of an uneven cash flow series
44
-
45
- - Computing the rate of return for each period
46
-
47
- - Convert a given continuous compounded rate to a norminal rate
48
-
49
- - Convert a given norminal rate to a continuous compounded rate
50
-
51
- - Convert holding period return to the effective annual rate
52
-
53
- - Convert stated annual rate to the effective annual rate (with continuous compounding)
54
-
55
- - Cost of goods sold and ending inventory under three methods (FIFO,LIFO,Weighted average)
56
-
57
- - Current ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
58
-
59
- - Debt ratio - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
60
-
61
- - Depreciation Expense Recognition - Straight-line depreciation (SL) allocates an equal amount of depreciation each year over the asset's useful life
62
-
63
- - Depreciation Expense Recognition - double-declining balance (DDB), the most common declining balance method, which applies two times the straight-line rate to the declining balance.
64
-
65
- - Diluted Earnings Per Share
66
-
67
- - Equivalent/proportional Interest Rates
68
-
69
- - Estimate future value (fv) (of a single sum)
70
-
71
- - Estimate future value of an annuity
72
-
73
- - Estimate period payment
74
-
75
- - Estimate present value (pv) (of a single sum) (of an annuity)
76
-
77
- - Estimate present value of a perpetuity
78
-
79
- - Estimate the number of periods
80
-
81
- - Financial leverage - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
82
-
83
- - Geometric mean return
84
-
85
- - Gross profit margin - Evaluate a company's financial performance
86
-
87
- - Harmonic mean, average price
88
-
89
- - Long-term debt-to-equity - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
90
-
91
- - Net profit margin - Evaluate a company's financial performance
92
-
93
- - Quick ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
94
-
95
- - Rate of return for a perpetuity
96
-
97
- - Total debt-to-equity - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
98
-
99
- - Weighted mean as a portfolio return
100
-
101
- ### Computing bank discount yield (BDY) for a T-bill
102
-
103
- - Param - d - the dollar discount, which is equal to the difference between the face value of the bill and the purchase price
104
-
105
- - Param - f - the face value (par value) of the bill
106
-
107
- - Param - t - number of days remaining until maturity
108
-
109
- Examples:
110
-
111
- ```ruby
112
- Finrb::Utils.bdy(d=1500,f=100000,t=120)
113
- ```
114
-
115
- ### Computing money market yield (MMY) for a T-bill
116
-
117
- - Param - bdy - bank discount yield
118
-
119
- - Param - t - number of days remaining until maturity
120
-
121
- Examples:
122
-
123
- ```ruby
124
- Finrb::Utils.bdy2mmy(bdy=0.045,t=120)
125
- ```
126
-
127
- ### Cash ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
128
-
129
- - Param - cash - cash
130
-
131
- - Param - ms - marketable securities
132
-
133
- - Param - cl - current liabilities
134
-
135
- Examples:
136
-
137
- ```ruby
138
- Finrb::Utils.cash_ratio(cash=3000,ms=2000,cl=2000)
139
- ```
140
-
141
- ### Computing Coefficient of variation
142
-
143
- - Param - sd - standard deviation
144
-
145
- - Param - avg - average value
146
-
147
- Examples:
148
-
149
- ```ruby
150
- Finrb::Utils.coefficient_variation(sd=0.15,avg=0.39)
151
- ```
152
-
153
- ### Cost of goods sold and ending inventory under three methods (FIFO,LIFO,Weighted average)
154
-
155
- - Param - uinv - units of beginning inventory
156
-
157
- - Param - pinv - prince of beginning inventory
158
-
159
- - Param - units - nx1 vector of inventory units. inventory purchased ordered by time (from first to last)
160
-
161
- - Param - price - nx1 vector of inventory price. same order as units
162
-
163
- - Param - sinv - units of sold inventory
164
-
165
- - Param - method - inventory methods: FIFO (first in first out, permitted under both US and IFRS), LIFO (late in first out, US only), WAC (weighted average cost,US and IFRS)
166
-
167
- Examples:
168
-
169
- ```ruby
170
- Finrb::Utils.cogs(uinv=2,pinv=2,units=[3,5],price=[3,5],sinv=7,method="FIFO")
171
- ```
172
-
173
- ```ruby
174
- Finrb::Utils.cogs(uinv=2,pinv=2,units=[3,5],price=[3,5],sinv=7,method="LIFO")
175
- ```
176
-
177
- ```ruby
178
- Finrb::Utils.cogs(uinv=2,pinv=2,units=[3,5],price=[3,5],sinv=7,method="WAC")
179
- ```
180
-
181
- ### Current ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
182
-
183
- - Param - ca - current assets
184
-
185
- - Param - cl - current liabilities
186
-
187
- Examples:
188
-
189
- ```ruby
190
- Finrb::Utils.current_ratio(ca=8000,cl=2000)
191
- ```
192
-
193
- ### Depreciation Expense Recognition - double-declining balance (DDB), the most common declining balance method, which applies two times the straight-line rate to the declining balance.
194
-
195
- - Param - cost - cost of long-lived assets
196
-
197
- - Param - rv - residual value of the long-lived assets at the end of its useful life. DDB does not explicitly use the asset's residual value in the calculations, but depreciation ends once the estimated residual value has been reached. If the asset is expected to have no residual value, the DB method will never fully depreciate it, so the DB method is typically changed to straight-line at some point in the asset's life.
198
-
199
- - Param - t - length of the useful life
200
-
201
- Examples:
202
-
203
- ```ruby
204
- Finrb::Utils.ddb(cost=1200,rv=200,t=5)
205
- ```
206
-
207
- ### Debt ratio - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
208
-
209
- - Param - td - total debt
210
-
211
- - Param - ta - total assets
212
-
213
- Examples:
214
-
215
- ```ruby
216
- Finrb::Utils.debt_ratio(td=6000,ta=20000)
217
- ```
218
-
219
- ### Diluted Earnings Per Share
220
-
221
- - Param - ni - net income
222
-
223
- - Param - pd - preferred dividends
224
-
225
- - Param - cpd - dividends on convertible preferred stock
226
-
227
- - Param - cdi - interest on convertible debt
228
-
229
- - Param - tax - tax rate
230
-
231
- - Param - w - weighted average number of common shares outstanding
232
-
233
- - Param - cps - shares from conversion of convertible preferred stock
234
-
235
- - Param - cds - shares from conversion of convertible debt
236
-
237
- - Param - iss - shares issuable from stock options
238
-
239
- Examples:
240
-
241
- ```ruby
242
- Finrb::Utils.diluted_eps(ni=115600,pd=10000,cdi=42000,tax=0.4,w=200000,cds=60000)
243
- ```
244
-
245
- ```ruby
246
- Finrb::Utils.diluted_eps(ni=115600,pd=10000,cpd=10000,w=200000,cps=40000)
247
- ```
248
-
249
- ```ruby
250
- Finrb::Utils.diluted_eps(ni=115600,pd=10000,w=200000,iss=2500)
251
- ```
252
-
253
- ```ruby
254
- Finrb::Utils.diluted_eps(ni=115600,pd=10000,cpd=10000,cdi=42000,tax=0.4,w=200000,cps=40000,cds=60000,iss=2500)
255
- ```
256
-
257
- ### Computing the rate of return for each period
258
-
259
- - Param - n - number of periods
260
-
261
- - Param - pv - present value
262
-
263
- - Param - fv - future value
264
-
265
- - Param - pmt - payment per period
266
-
267
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
268
-
269
- - Param - lower - the lower end points of the rate of return to be searched.
270
-
271
- - Param - upper - the upper end points of the rate of return to be searched.
272
- @importFrom stats uniroot
273
-
274
- Examples:
275
-
276
- ```ruby
277
- Finrb::Utils.discount_rate(n=5,pv=0,fv=600,pmt=-100,type=0)
278
- ```
279
-
280
- ### Convert stated annual rate to the effective annual rate
281
-
282
- - Param - r - stated annual rate
283
-
284
- - Param - m - number of compounding periods per year
285
-
286
- Examples:
287
-
288
- ```ruby
289
- Finrb::Utils.ear(r=0.12,m=12)
290
- ```
291
-
292
- ```ruby
293
- Finrb::Utils.ear(0.04,365)
294
- ```
295
-
296
- ### Convert stated annual rate to the effective annual rate with continuous compounding
297
-
298
- - Param - r - stated annual rate
299
-
300
- Examples:
301
-
302
- ```ruby
303
- Finrb::Utils.ear_continuous(r=0.1)
304
- ```
305
-
306
- ```ruby
307
- Finrb::Utils.ear_continuous(0.03)
308
- ```
309
-
310
- ### Bond-equivalent yield (BEY), 2 x the semiannual discount rate
311
-
312
- - Param - ear - effective annual rate
313
-
314
- Examples:
315
-
316
- ```ruby
317
- Finrb::Utils.ear2bey(ear=0.08)
318
- ```
319
-
320
- ### Computing HPR, the holding period return
321
-
322
- - Param - ear - effective annual rate
323
-
324
- - Param - t - number of days remaining until maturity
325
-
326
- Examples:
327
-
328
- ```ruby
329
- Finrb::Utils.ear2hpr(ear=0.05039,t=150)
330
- ```
331
-
332
- ### Equivalent/proportional Interest Rates
333
-
334
- @description An interest rate to be applied n times p.a. can be converted to an equivalent rate to be applied p times p.a.
335
-
336
- - Param - r - interest rate to be applied n times per year (r is annual rate!)
337
-
338
- - Param - n - times that the interest rate r were compounded per year
339
-
340
- - Param - p - times that the equivalent rate were compounded per year
341
-
342
- - Param - type - equivalent interest rates ('e',default) or proportional interest rates ('p')
343
-
344
- Examples:
345
-
346
- - monthly interest rat equivalent to 5% compounded per year
347
-
348
- ```ruby
349
- Finrb::Utils.eir(r=0.05,n=1,p=12)
350
- ```
351
-
352
- - monthly interest rat equivalent to 5% compounded per half year
353
-
354
- ```ruby
355
- Finrb::Utils.eir(r=0.05,n=2,p=12)
356
- ```
357
-
358
- - monthly interest rat equivalent to 5% compounded per quarter
359
-
360
- ```ruby
361
- Finrb::Utils.eir(r=0.05,n=4,p=12)
362
- ```
363
-
364
- - annual interest rate equivalent to 5% compounded per month
365
-
366
- ```ruby
367
- Finrb::Utils.eir(r=0.05,n=12,p=1)
368
- ```
369
-
370
- - this is equivalent to
371
- ear(r=0.05,m=12)
372
-
373
- - quarter interest rate equivalent to 5% compounded per year
374
-
375
- ```ruby
376
- Finrb::Utils.eir(r=0.05,n=1,p=4)
377
- ```
378
-
379
- - quarter interest rate equivalent to 5% compounded per month
380
-
381
- ```ruby
382
- Finrb::Utils.eir(r=0.05,n=12,p=4)
383
- ```
384
-
385
- - monthly proportional interest rate which is equivalent to a simple annual interest
386
-
387
- ```ruby
388
- Finrb::Utils.eir(r=0.05,p=12,type='p')
389
- ```
390
-
391
- ### Basic Earnings Per Share
392
-
393
- - Param - ni - net income
394
-
395
- - Param - pd - preferred dividends
396
-
397
- - Param - w - weighted average number of common shares outstanding
398
-
399
- Examples:
400
-
401
- ```ruby
402
- Finrb::Utils.eps(ni=10000,pd=1000,w=11000)
403
- ```
404
-
405
- ### Financial leverage - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
406
-
407
- - Param - te - total equity
408
-
409
- - Param - ta - total assets
410
-
411
- Examples:
412
-
413
- ```ruby
414
- Finrb::Utils.financial_leverage(te=16000,ta=20000)
415
- ```
416
-
417
- ### Estimate future value (fv)
418
-
419
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
420
-
421
- - Param - n - number of periods
422
-
423
- - Param - pv - present value
424
-
425
- - Param - pmt - payment per period
426
-
427
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
428
-
429
- Examples:
430
-
431
- ```ruby
432
- Finrb::Utils.fv(r=0.07,n=10,pv=1000,pmt=10)
433
- ```
434
-
435
- ### Estimate future value of an annuity
436
-
437
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
438
-
439
- - Param - n - number of periods
440
-
441
- - Param - pmt - payment per period
442
-
443
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
444
-
445
- Examples:
446
-
447
- ```ruby
448
- Finrb::Utils.fv_annuity(0.03,12,-1000)
449
- ```
450
-
451
- ```ruby
452
- Finrb::Utils.fv_annuity(r=0.03,n=12,pmt=-1000,type=1)
453
- ```
454
-
455
- ### Estimate future value (fv) of a single sum
456
-
457
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
458
-
459
- - Param - n - number of periods
460
-
461
- - Param - pv - present value
462
-
463
- Examples:
464
-
465
- ```ruby
466
- Finrb::Utils.fv_simple(0.08,10,-300)
467
- ```
468
-
469
- ```ruby
470
- Finrb::Utils.fv_simple(r=0.04,n=20,pv=-50000)
471
- ```
472
-
473
- ### Computing the future value of an uneven cash flow series
474
-
475
- - Param - r - stated annual rate
476
-
477
- - Param - cf - uneven cash flow
478
-
479
- Examples:
480
-
481
- ```ruby
482
- Finrb::Utils.fv_uneven(r=0.1, cf=[-1000, -500, 0, 4000, 3500, 2000])
483
- ```
484
-
485
- ### Geometric mean return
486
-
487
- - Param - r - returns over multiple periods
488
-
489
- Examples:
490
-
491
- ```ruby
492
- Finrb::Utils.geometric_mean(r=[-0.0934, 0.2345, 0.0892])
493
- ```
494
-
495
- ### Gross profit margin - Evaluate a company's financial performance
496
-
497
- - Param - gp - gross profit, equal to revenue minus cost of goods sold (cogs)
498
-
499
- - Param - rv - revenue (sales)
500
-
501
- Examples:
502
-
503
- ```ruby
504
- Finrb::Utils.gpm(gp=1000,rv=20000)
505
- ```
506
-
507
- ### Harmonic mean, average price
508
-
509
- - Param - p - price over multiple periods
510
-
511
- Examples:
512
-
513
- ```ruby
514
- Finrb::Utils.harmonic_mean(p=[8,9,10])
515
- ```
516
-
517
- ### Computing HPR, the holding period return
518
-
519
- - Param - ev - ending value
520
-
521
- - Param - bv - beginning value
522
-
523
- - Param - cfr - cash flow received
524
-
525
- Examples:
526
-
527
- ```ruby
528
- Finrb::Utils.hpr(ev=33,bv=30,cfr=0.5)
529
- ```
530
-
531
- ### Bond-equivalent yield (BEY), 2 x the semiannual discount rate
532
-
533
- - Param - hpr - holding period return
534
-
535
- - Param - t - number of month remaining until maturity
536
-
537
- Examples:
538
-
539
- ```ruby
540
- Finrb::Utils.hpr2bey(hpr=0.02,t=3)
541
- ```
542
-
543
- ### Convert holding period return to the effective annual rate
544
-
545
- - Param - hpr - holding period return
546
-
547
- - Param - t - number of days remaining until maturity
548
-
549
- Examples:
550
-
551
- ```ruby
552
- Finrb::Utils.hpr2ear(hpr=0.015228,t=120)
553
- ```
554
-
555
- ### Computing money market yield (MMY) for a T-bill
556
-
557
- - Param - hpr - holding period return
558
-
559
- - Param - t - number of days remaining until maturity
560
-
561
- Examples:
562
-
563
- ```ruby
564
- Finrb::Utils.hpr2mmy(hpr=0.01523,t=120)
565
- ```
566
-
567
- ### Computing IRR, the internal rate of return
568
-
569
- - Param - cf - cash flow,the first cash flow is the initial outlay
570
- @importFrom stats uniroot
571
-
572
- Examples:
573
-
574
- ```ruby
575
- Finrb::Utils.irr(cf=[-5, 1.6, 2.4, 2.8])
576
- ```
577
-
578
- ### Computing IRR, the internal rate of return
579
-
580
- @description This function is the same as irr but can calculate negative value. This function may take a very long time. You can use larger cutoff and larger step to get a less precision irr first. Then based on the result, change from and to, to narrow down the interval, and use a smaller step to get a more precision irr.
581
-
582
- - Param - cf - cash flow,the first cash flow is the initial outlay
583
-
584
- - Param - cutoff - threshold to take npv as zero
585
-
586
- - Param - from - smallest irr to try
587
-
588
- - Param - to - largest irr to try
589
-
590
- - Param - step - increment of the irr
591
-
592
- Examples:
593
-
594
- ```ruby
595
- Finrb::Utils.irr2(cf=[-5, 1.6, 2.4, 2.8])
596
- ```
597
-
598
- ```ruby
599
- Finrb::Utils.irr2(cf=[-200, 50, 60, -70, 30, 20])
600
- ```
601
-
602
- ### Calculate the net increase in common shares from the potential exercise of stock options or warrants
603
-
604
- - Param - amp - average market price over the year
605
-
606
- - Param - ep - exercise price of the options or warrants
607
-
608
- - Param - n - number of common shares that the options and warrants can be convened into
609
-
610
- Examples:
611
-
612
- ```ruby
613
- Finrb::Utils.iss(amp=20,ep=15,n=10000)
614
- ```
615
-
616
- ### Long-term debt-to-equity - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
617
-
618
- - Param - ltd - long-term debt
619
-
620
- - Param - te - total equity
621
-
622
- Examples:
623
-
624
- ```ruby
625
- Finrb::Utils.lt_d2e(ltd=8000,te=20000)
626
- ```
627
-
628
- ### Computing HPR, the holding period return
629
-
630
- - Param - mmy - money market yield
631
-
632
- - Param - t - number of days remaining until maturity
633
-
634
- Examples:
635
-
636
- ```ruby
637
- Finrb::Utils.mmy2hpr(mmy=0.04898,t=150)
638
- ```
639
-
640
- ### Estimate the number of periods
641
-
642
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
643
-
644
- - Param - pv - present value
645
-
646
- - Param - fv - future value
647
-
648
- - Param - pmt - payment per period
649
-
650
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
651
-
652
- Examples:
653
-
654
- ```ruby
655
- Finrb::Utils.n_period(0.1,-10000,60000000,-50000,0)
656
- ```
657
-
658
- ```ruby
659
- Finrb::Utils.n_period(r=0.1,pv=-10000,fv=60000000,pmt=-50000,type=1)
660
- ```
661
-
662
- ### Net profit margin - Evaluate a company's financial performance
663
-
664
- - Param - ni - net income
665
-
666
- - Param - rv - revenue (sales)
667
-
668
- Examples:
669
-
670
- ```ruby
671
- Finrb::Utils.npm(ni=8000,rv=20000)
672
- ```
673
-
674
- ### Computing NPV, the PV of the cash flows less the initial (time = 0) outlay
675
-
676
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
677
-
678
- - Param - cf - cash flow,the first cash flow is the initial outlay
679
-
680
- Examples:
681
-
682
- ```ruby
683
- Finrb::Utils.npv(r=0.12, cf=[-5, 1.6, 2.4, 2.8])
684
- ```
685
-
686
- ### Estimate period payment
687
-
688
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
689
-
690
- - Param - n - number of periods
691
-
692
- - Param - pv - present value
693
-
694
- - Param - fv - future value
695
-
696
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
697
-
698
- Examples:
699
-
700
- ```ruby
701
- Finrb::Utils.pmt(0.08,10,-1000,10)
702
- ```
703
-
704
- ```ruby
705
- Finrb::Utils.pmt(r=0.08,n=10,pv=-1000,fv=0)
706
- ```
707
-
708
- ```ruby
709
- Finrb::Utils.pmt(0.08,10,-1000,10,1)
710
- ```
711
-
712
- ### Estimate present value (pv)
713
-
714
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
715
-
716
- - Param - n - number of periods
717
-
718
- - Param - fv - future value
719
-
720
- - Param - pmt - payment per period
721
-
722
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
723
-
724
- Examples:
725
-
726
- ```ruby
727
- Finrb::Utils.pv(0.07,10,1000,10)
728
- ```
729
-
730
- ```ruby
731
- Finrb::Utils.pv(r=0.05,n=20,fv=1000,pmt=10,type=1)
732
- ```
733
-
734
- ### Estimate present value (pv) of an annuity
735
-
736
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
737
-
738
- - Param - n - number of periods
739
-
740
- - Param - pmt - payment per period
741
-
742
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
743
-
744
- Examples:
745
-
746
- ```ruby
747
- Finrb::Utils.pv_annuity(0.03,12,1000)
748
- ```
749
-
750
- ```ruby
751
- Finrb::Utils.pv_annuity(r=0.0425,n=3,pmt=30000)
752
- ```
753
-
754
- ### Estimate present value of a perpetuity
755
-
756
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
757
-
758
- - Param - g - growth rate of perpetuity
759
-
760
- - Param - pmt - payment per period
761
-
762
- - Param - type - payments occur at the end of each period (type=0); payments occur at the beginning of each period (type=1)
763
-
764
- Examples:
765
-
766
- ```ruby
767
- Finrb::Utils.pv_perpetuity(r=0.1,pmt=1000,g=0.02)
768
- ```
769
-
770
- ```ruby
771
- Finrb::Utils.pv_perpetuity(r=0.1,pmt=1000,type=1)
772
- ```
773
-
774
- ```ruby
775
- Finrb::Utils.pv_perpetuity(r=0.1,pmt=1000)
776
- ```
777
-
778
- ### Estimate present value (pv) of a single sum
779
-
780
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
781
-
782
- - Param - n - number of periods
783
-
784
- - Param - fv - future value
785
-
786
- Examples:
787
-
788
- ```ruby
789
- Finrb::Utils.pv_simple(0.07,10,100)
790
- ```
791
-
792
- ```ruby
793
- Finrb::Utils.pv_simple(r=0.03,n=3,fv=1000)
794
- ```
795
-
796
- ### Computing the present value of an uneven cash flow series
797
-
798
- - Param - r - discount rate, or the interest rate at which the amount will be compounded each period
799
-
800
- - Param - cf - uneven cash flow
801
-
802
- Examples:
803
-
804
- ```ruby
805
- Finrb::Utils.pv_uneven(r=0.1, cf=[-1000, -500, 0, 4000, 3500, 2000])
806
- ```
807
-
808
- ### Quick ratio - Liquidity ratios measure the firm's ability to satisfy its short-term obligations as they come due.
809
-
810
- - Param - cash - cash
811
-
812
- - Param - ms - marketable securities
813
-
814
- - Param - rc - receivables
815
-
816
- - Param - cl - current liabilities
817
-
818
- Examples:
819
-
820
- ```ruby
821
- Finrb::Utils.quick_ratio(cash=3000,ms=2000,rc=1000,cl=2000)
822
- ```
823
-
824
- ### Convert a given norminal rate to a continuous compounded rate
825
-
826
- - Param - r - norminal rate
827
-
828
- - Param - m - number of times compounded each year
829
-
830
- Examples:
831
-
832
- ```ruby
833
- Finrb::Utils.r_continuous(r=0.03,m=4)
834
- ```
835
-
836
- ### Convert a given continuous compounded rate to a norminal rate
837
-
838
- - Param - rc - continuous compounded rate
839
-
840
- - Param - m - number of desired times compounded each year
841
-
842
- Examples:
843
-
844
- ```ruby
845
- Finrb::Utils.r_norminal(0.03,1)
846
- ```
847
-
848
- ```ruby
849
- Finrb::Utils.r_norminal(rc=0.03,m=4)
850
- ```
851
-
852
- ### Rate of return for a perpetuity
853
-
854
- - Param - pmt - payment per period
855
-
856
- - Param - pv - present value
857
-
858
- Examples:
859
-
860
- ```ruby
861
- Finrb::Utils.r_perpetuity(pmt=4.5,pv=-75)
862
- ```
863
-
864
- ### Computing Sampling error
865
-
866
- - Param - sm - sample mean
867
-
868
- - Param - mu - population mean
869
-
870
- Examples:
871
-
872
- ```ruby
873
- Finrb::Utils.sampling_error(sm=0.45, mu=0.5)
874
- ```
875
-
876
- ### Computing Roy's safety-first ratio
877
-
878
- - Param - rp - portfolio return
879
-
880
- - Param - rl - threshold level return
881
-
882
- - Param - sd - standard deviation of portfolio retwns
883
-
884
- Examples:
885
-
886
- ```ruby
887
- Finrb::Utils.sf_ratio(rp=0.09,rl=0.03,sd=0.12)
888
- ```
889
-
890
- ### Computing Sharpe Ratio
891
-
892
- - Param - rp - portfolio return
893
-
894
- - Param - rf - risk-free return
895
-
896
- - Param - sd - standard deviation of portfolio retwns
897
-
898
- Examples:
899
-
900
- ```ruby
901
- Finrb::Utils.sharpe_ratio(rp=0.038,rf=0.015,sd=0.07)
902
- ```
903
-
904
- ### Depreciation Expense Recognition - Straight-line depreciation (SL) allocates an equal amount of depreciation each year over the asset's useful life
905
-
906
- - Param - cost - cost of long-lived assets
907
-
908
- - Param - rv - residual value of the long-lived assets at the end of its useful life
909
-
910
- - Param - t - length of the useful life
911
-
912
- Examples:
913
-
914
- ```ruby
915
- Finrb::Utils.slde(cost=1200,rv=200,t=5)
916
- ```
917
-
918
- ### Total debt-to-equity - Solvency ratios measure the firm's ability to satisfy its long-term obligations.
919
-
920
- - Param - td - total debt
921
-
922
- - Param - te - total equity
923
-
924
- Examples:
925
-
926
- ```ruby
927
- Finrb::Utils.total_d2e(td=6000,te=20000)
928
- ```
929
-
930
- ### Computing TWRR, the time-weighted rate of return
931
-
932
- - Param - ev - ordered ending value list
933
-
934
- - Param - bv - ordered beginning value list
935
-
936
- - Param - cfr - ordered cash flow received list
937
-
938
- Examples:
939
-
940
- ```ruby
941
- Finrb::Utils.twrr(ev=[120,260],bv=[100,240],cfr=[2,4])
942
- ```
943
-
944
- ### Calculate weighted average shares - weighted average number of common shares
945
-
946
- - Param - ns - n x 1 vector vector of number of shares
947
-
948
- - Param - nm - n x 1 vector vector of number of months relate to ns
949
-
950
- Examples:
951
-
952
- s=[10000,2000];m=[12,6];
953
-
954
- ```ruby
955
- Finrb::Utils.was(ns=s,nm=m)
956
- ```
957
-
958
- s=[11000,4400,-3000];m=[12,9,4];
959
-
960
- ```ruby
961
- Finrb::Utils.was(ns=s,nm=m)
962
- ```
963
-
964
- ### Weighted mean as a portfolio return
965
-
966
- - Param - r - returns of the individual assets in the portfolio
967
-
968
- - Param - w - corresponding weights associated with each of the individual assets
969
-
970
- Examples:
971
-
972
- ```ruby
973
- Finrb::Utils.wpr(r=[0.12, 0.07, 0.03],w=[0.5,0.4,0.1])
974
- ```
975
-
976
- ## Amortization
977
-
978
- You are interested in borrowing $250,000 under a 30 year, fixed-rate
979
- loan with a 4.25% APR.
980
-
981
- ```ruby
982
- rate = Finrb::Rate.new(0.0425, :apr, :duration => (30 * 12))
983
- amortization = Finrb::Amortization.new(250000, rate)
984
- ```
985
-
986
- Find the standard monthly payment:
987
-
988
- ```ruby
989
- amortization.payment
990
- => Flt::DecNum('-1229.91')
991
- ```
992
-
993
- Find the total cost of the loan:
994
-
995
- ```ruby
996
- amortization.payments.sum
997
- => Flt::DecNum('-442766.55')
998
- ```
999
-
1000
- How much will you pay in interest?
1001
-
1002
- ```ruby
1003
- amortization.interest.sum
1004
- => Flt::DecNum('192766.55')
1005
- ```
1006
-
1007
- How much interest in the first six months?
1008
-
1009
- ```ruby
1010
- amortization.interest[0,6].sum
1011
- => Flt::DecNum('5294.62')
1012
- ```
1013
-
1014
- If your loan has an adjustable rate, no problem. You can pass an
1015
- arbitrary number of rates, and they will be used in the amortization.
1016
- For example, we can look at an amortization of $250000, where the APR
1017
- starts at 4.25%, and increases by 1% every five years.
1018
-
1019
- ```ruby
1020
- values = %w{ 0.0425 0.0525 0.0625 0.0725 0.0825 0.0925 }
1021
- rates = values.collect { |value| Finrb::Rate.new( value, :apr, :duration => (5 * 12) }
1022
- arm = Finrb::Amortization.new(250000, *rates)
1023
- ```
1024
-
1025
- Since we are looking at an ARM, there is no longer a single "payment" value.
1026
-
1027
- ```ruby
1028
- arm.payment
1029
- => nil
1030
- ```
1031
-
1032
- But we can look at the different payments over time.
1033
-
1034
- ```ruby
1035
- arm.payments.uniq
1036
- => [Flt::DecNum('-1229.85'), Flt::DecNum('-1360.41'), Flt::DecNum('-1475.65'), Flt::DecNum('-1571.07'), ... snipped ... ]
1037
- ```
1038
-
1039
- The other methods previously discussed can be accessed in the same way:
1040
-
1041
- ```ruby
1042
- arm.interest.sum
1043
- => Flt::DecNum('287515.45')
1044
- arm.payments.sum
1045
- => Flt::DecNum('-537515.45')
1046
- ```
1047
-
1048
- Last, but not least, you may pass a block when creating an Amortization
1049
- which returns a modified monthly payment. For example, to increase your
1050
- payment by $150, do:
1051
-
1052
- ```ruby
1053
- rate = Finrb::Rate.new(0.0425, :apr, :duration => (30 * 12))
1054
- extra_payments = 250000.amortize(rate){ |period| period.payment - 150 }
1055
- ```
1056
-
1057
- Disregarding the block, we have used the same parameters as the first
1058
- example. Notice the difference in the results:
1059
-
1060
- ```ruby
1061
- amortization.payments.sum
1062
- => Flt::DecNum('-442745.98')
1063
- extra_payments.payments.sum
1064
- => Flt::DecNum('-400566.24')
1065
- amortization.interest.sum
1066
- => Flt::DecNum('192745.98')
1067
- extra_payments.interest.sum
1068
- => Flt::DecNum('150566.24')
1069
- ```
1070
-
1071
- You can also increase your payment to a specific amount:
1072
-
1073
- ```ruby
1074
- extra_payments_2 = 250000.amortize(rate){ -1500 }
1075
- ```
1076
-
1077
- ## IRR and XIRR
1078
-
1079
- ```ruby
1080
- guess = 0.1
1081
- transactions = []
1082
- transactions << Transaction.new(-10000, date: '2010-01-01'.to_time(:utc))
1083
- transactions << Transaction.new(123000, date: '2012-01-01'.to_time(:utc))
1084
- transactions.xirr(guess)
1085
- # => Finrb::Rate.new(2.507136, :apr)
1086
- ```