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