finance_rb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fa908552e787182999bb782ed6c7f7f624307f0acd77ff969c0171e07dd0179
4
- data.tar.gz: e0492ef3dd7baa271a4d7893ff32c5c94de852d2328d1c49321bb1d78147f08e
3
+ metadata.gz: 3a0dc37279e3b577acb13a90c61ad4f232573a4d8c0eccdf19a267d42001a9ea
4
+ data.tar.gz: ca6db2175dd770056d7539d555b239127bcc5836ae79826edc6807a4f5bd4311
5
5
  SHA512:
6
- metadata.gz: ccc866a27aecf49b0f869f9ddf590ac58d7682bc35c30a5abfda526251b800960b143fa19b05063411e30061a96f950963c4f8c38d867da763b1654ec77bfb52
7
- data.tar.gz: 5ca7d064faf5bb38ad5cd70ddb1595d8983833e4ad9b46290f17b9f5ca8eee2abd4e4c99eedf852bbf79790c28d54e4aa4743beeffbf72921ff0dccc95f1cc94
6
+ metadata.gz: 395383e7e7c9b2b564a738aae13bc6f95d7180652b951a2fa4cf90118e551cb0cba6e87badb4ca23c7b5ab790be2de0ff5bef0e3736c50c18a7246262b2ae496
7
+ data.tar.gz: 5c9a8d1e5b8726234fafa45ed747dcf881f7ae92a9157abf34e9186594775a4e651fc8c09395cdd9fd30e21426e217c4216f2b13644ee15f324a967325d4bf6a
data/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
- ## [Unreleased]
1
+ ## [0.0.3] - 2021-03-23
2
+
3
+ ### Added
4
+ * Implement Finance::Calculations#irr using Newton's method
2
5
 
3
6
  ## [0.0.2] - 2021-03-22
4
7
 
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 | | 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|
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|
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Finance
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
@@ -12,10 +12,30 @@ RSpec.describe Finance::Calculations do
12
12
  ).to eq(-85.07888805409468)
13
13
  end
14
14
 
15
- it 'returns correct npv for zero rates' do
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.2
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/v0.0.2/CHANGELOG.md
37
- documentation_uri: https://www.rubydoc.info/wowinter13/finance_rb/0.0.2
38
- source_code_uri: https://github.com/wowinter13/finance_rb/tree/v0.0.2
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: