finance_rb 0.0.2 → 0.0.3
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 +4 -4
- data/CHANGELOG.md +4 -1
- data/README.md +7 -7
- data/lib/finance/calculations.rb +57 -0
- data/lib/finance/version.rb +1 -1
- data/spec/finance/calculations_spec.rb +21 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a0dc37279e3b577acb13a90c61ad4f232573a4d8c0eccdf19a267d42001a9ea
|
4
|
+
data.tar.gz: ca6db2175dd770056d7539d555b239127bcc5836ae79826edc6807a4f5bd4311
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 395383e7e7c9b2b564a738aae13bc6f95d7180652b951a2fa4cf90118e551cb0cba6e87badb4ca23c7b5ab790be2de0ff5bef0e3736c50c18a7246262b2ae496
|
7
|
+
data.tar.gz: 5c9a8d1e5b8726234fafa45ed747dcf881f7ae92a9157abf34e9186594775a4e651fc8c09395cdd9fd30e21426e217c4216f2b13644ee15f324a967325d4bf6a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -9,13 +9,13 @@ which are as follows:
|
|
9
9
|
|
10
10
|
| numpy-financial function | ruby native function ported? | info|
|
11
11
|
|:------------------------: |:------------------: | :------------------|
|
12
|
-
| fv |
|
13
|
-
| ipmt |
|
14
|
-
| pmt |
|
15
|
-
| ppmt |
|
16
|
-
| nper |
|
17
|
-
| pv |
|
18
|
-
| rate |
|
12
|
+
| fv | | Computes the future value|
|
13
|
+
| ipmt | | Computes interest payment for a loan|
|
14
|
+
| pmt | | Computes the fixed periodic payment(principal + interest) made against a loan amount|
|
15
|
+
| ppmt | | Computes principal payment for a loan|
|
16
|
+
| nper | | Computes the number of periodic payments|
|
17
|
+
| pv | | Computes the present value of a payment|
|
18
|
+
| rate | ✅ | Computes the rate of interest per period|
|
19
19
|
| irr | | Computes the internal rate of return|
|
20
20
|
| npv | ✅ | Computes the net present value of a series of cash flow|
|
21
21
|
| mirr | | Computes the modified internal rate of return|
|
data/lib/finance/calculations.rb
CHANGED
@@ -1,9 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'bigdecimal'
|
4
|
+
require 'bigdecimal/newton'
|
5
|
+
|
6
|
+
include Newton
|
7
|
+
|
3
8
|
module Finance
|
4
9
|
class Calculations
|
5
10
|
class << self
|
6
11
|
# Npv computes the Net Present Value of a cash flow series.
|
12
|
+
#
|
7
13
|
# @return [Numeric] The NPV of the input cash flow series `values` at the discount `rate`.
|
8
14
|
#
|
9
15
|
# @param [Numeric] :rate A discount rate applied once per period.
|
@@ -24,7 +30,58 @@ module Finance
|
|
24
30
|
npv_value
|
25
31
|
end
|
26
32
|
|
33
|
+
# IRR computes the Rate of Interest per period.
|
34
|
+
#
|
35
|
+
# @return [Float] Internal Rate of Return for periodic input values.
|
36
|
+
#
|
37
|
+
# @param [Array<Numeric>] :values Input cash flows per time period.
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# require 'finance_rb'
|
41
|
+
# Finance::Calculations.irr([-100, 0, 0, 74]) #=> 0.14299344106053188
|
42
|
+
#
|
43
|
+
# @see http://en.wikipedia.org/wiki/Internal_rate_of_return
|
44
|
+
# @see L. J. Gitman, "Principles of Managerial Finance, Brief," 3rd ed.,
|
45
|
+
# Addison-Wesley, 2003, pg. 348.
|
46
|
+
def irr(values)
|
47
|
+
inflows, outflows = values.partition{ |i| i >= 0 }
|
48
|
+
if inflows.empty? || outflows.empty?
|
49
|
+
return 0.0
|
50
|
+
end
|
51
|
+
|
52
|
+
func = BigDecimal.limit(100)
|
53
|
+
func = Function.new(values)
|
54
|
+
rate = [ func.one ]
|
55
|
+
nlsolve(func, rate)
|
56
|
+
rate[0].to_f
|
57
|
+
end
|
58
|
+
|
27
59
|
alias net_present_value npv
|
60
|
+
alias internal_return_rate irr
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Base class for working with Newton's Method.
|
65
|
+
# For more details, see Bigdecimal::Newton.
|
66
|
+
# @api private
|
67
|
+
class Function
|
68
|
+
def initialize(values)
|
69
|
+
@zero = BigDecimal("0.0")
|
70
|
+
@one = BigDecimal("1.0")
|
71
|
+
@two = BigDecimal("2.0")
|
72
|
+
@ten = BigDecimal("10.0")
|
73
|
+
@eps = BigDecimal("1.0e-16")
|
74
|
+
@values = values
|
75
|
+
end
|
76
|
+
|
77
|
+
def zero; @zero; end
|
78
|
+
def one ; @one; end
|
79
|
+
def two ; @two; end
|
80
|
+
def ten ; @ten; end
|
81
|
+
def eps ; @eps; end
|
82
|
+
|
83
|
+
def values(x); [Finance::Calculations.npv(x[0], @values)]; end
|
84
|
+
end
|
28
85
|
end
|
29
86
|
end
|
30
87
|
end
|
data/lib/finance/version.rb
CHANGED
@@ -12,10 +12,30 @@ RSpec.describe Finance::Calculations do
|
|
12
12
|
).to eq(-85.07888805409468)
|
13
13
|
end
|
14
14
|
|
15
|
-
it '
|
15
|
+
it 'calculates correct npv for zero rates' do
|
16
16
|
expect(
|
17
17
|
Finance::Calculations.net_present_value(0.0, [-2000, 55, 55, 55])
|
18
18
|
).to eq(-1835.0)
|
19
19
|
end
|
20
20
|
end
|
21
|
+
|
22
|
+
describe '#irr' do
|
23
|
+
it 'calculates correct irr value' do
|
24
|
+
expect(
|
25
|
+
Finance::Calculations.irr([-4000,1200,1410,1875,1050])
|
26
|
+
).to eq(0.14299344106053188)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'calculates zero value for cashflows w/o any inflows' do
|
30
|
+
expect(
|
31
|
+
Finance::Calculations.irr([100,500,200,50])
|
32
|
+
).to eq(0.0)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'is available through alias name' do
|
36
|
+
expect(
|
37
|
+
Finance::Calculations.internal_return_rate([-100, 0, 0, 74])
|
38
|
+
).to eq(-0.09549583035161031)
|
39
|
+
end
|
40
|
+
end
|
21
41
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finance_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vlad Dyachenko
|
@@ -33,9 +33,9 @@ licenses:
|
|
33
33
|
- MIT
|
34
34
|
metadata:
|
35
35
|
bug_tracker_uri: https://github.com/wowinter13/finance_rb/issues
|
36
|
-
changelog_uri: https://github.com/wowinter13/finance_rb/blob/
|
37
|
-
documentation_uri: https://www.rubydoc.info/wowinter13/finance_rb
|
38
|
-
source_code_uri: https://github.com/wowinter13/finance_rb
|
36
|
+
changelog_uri: https://github.com/wowinter13/finance_rb/blob/master/CHANGELOG.md
|
37
|
+
documentation_uri: https://www.rubydoc.info/github/wowinter13/finance_rb
|
38
|
+
source_code_uri: https://github.com/wowinter13/finance_rb
|
39
39
|
post_install_message:
|
40
40
|
rdoc_options: []
|
41
41
|
require_paths:
|