exonio 0.4.1 → 0.5.0

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
  SHA1:
3
- metadata.gz: d61396bb031ad61fccf7f19cc5cc1ef243bb78da
4
- data.tar.gz: 6de79892afc7edf4e19da4767e4ba5dacc2886aa
3
+ metadata.gz: ca4e5b35139265ba594d5146efee24fd9511a8b4
4
+ data.tar.gz: 31addf7c1d0e5d28a49b27d8537a713e2c12651d
5
5
  SHA512:
6
- metadata.gz: 37e0ed4a079fe9a7089c70b31cedd3c8e7dbbbcd044945c7b70ba06eba8145e0a90f61939cb4b2292907ede374a3a6c6bb1e43a79fa8c049f9ebe059d88384eb
7
- data.tar.gz: b54e1f1ef81daff292ddfcca5d6fbdb35adae1fc3b2d7e16deca55b2122dc8535a401e313e49ebd978644c987f26294b230ab587fab7b0b90866023d4b252e6e
6
+ metadata.gz: c082906456fc5cc853a11f6fc70f45886920053d662322a4b686122b849152875b19d011cbd4078229d17de419a7f939282b92270c9ed6834eefe7bf72b64b62
7
+ data.tar.gz: d98d28a9b7705a7dfe6343d9fb3bc26f6a689aed5e39272e24879349b75278ddaf25b616f0e75a7f426d612cdbe969d4f7f5edd6fded4b09b4740727b2524ea3
@@ -1,5 +1,13 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.5.0
4
+
5
+ * Implements `irr` and `npv` method
6
+
7
+ ## 0.4.1
8
+
9
+ * Fix infinite loop with big scale on `rate` method
10
+
3
11
  ## 0.4.0
4
12
 
5
13
  * Implements `rate` method
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- exonio (0.4.1)
4
+ exonio (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -43,6 +43,19 @@ By convention, the negative sign represents cash flow out (i.e. money not
43
43
  available today). Thus, saving $100 a month at 5% annual interest leads
44
44
  to $15,692.93 available to spend in 10 years.
45
45
 
46
+ ### IRR
47
+
48
+ Suppose one invests 100 units and then makes the following withdrawals at regular (fixed)
49
+ intervals: 39, 59, 55, 20. Assuming the ending value is 0, one's 100 unit investment
50
+ yields 173 units; however, due to the combination of compounding and the periodic
51
+ withdrawals, the "average" rate of return is neither simply 0.73/4 nor (1.73)^0.25-1.
52
+
53
+ ```ruby
54
+ Exonio.irr([-100, 39, 59, 55, 20]) # ==> 0.28095
55
+ ```
56
+
57
+ So, the internal rate of return is 28.09%
58
+
46
59
  ### IPMT
47
60
 
48
61
  What is the interest part of a payment in the 8th period (i.e., 8th month),
@@ -65,6 +78,14 @@ Exonio.nper(0.07 / 12, -150, 8000) # ==> 64.07334877066185
65
78
 
66
79
  So, over 64 months would be required to pay off the loan.
67
80
 
81
+ ### NPV
82
+
83
+ Calculates the Net Present Value of an investment
84
+
85
+ ```ruby
86
+ Exonio.npv(0.281, [-100, 39, 59, 55, 29]) # ==> -0.00661872883563408
87
+ ```
88
+
68
89
  ### PMT
69
90
 
70
91
  What is the monthly payment needed to pay off a $200,000 loan in 15
@@ -113,9 +134,7 @@ There's a lot of formulas to be implemented, including:
113
134
  * AMORLINC
114
135
  * DB
115
136
  * DDB
116
- * IRR
117
137
  * MIRR
118
- * NPV
119
138
  * PPMT
120
139
  * SLN
121
140
  * SYD
@@ -1,5 +1,11 @@
1
- require "exonio/version"
2
- require "exonio/financial"
1
+ require 'bigdecimal'
2
+ require 'bigdecimal/newton'
3
+
4
+ require 'exonio/version'
5
+ require 'exonio/financial'
6
+ require 'exonio/helpers'
7
+
8
+ include Newton
3
9
 
4
10
  module Exonio
5
11
  extend Financial
@@ -144,6 +144,45 @@ module Exonio
144
144
  next_guess
145
145
  end
146
146
 
147
+ # Calculates the net present value of an investment based on a
148
+ # series of periodic cash flows and a discount rate.
149
+ #
150
+ # @param discount [Float] The discount rate of the investment over one period
151
+ # @param cashflows [Array] The first future cash flow + additional future cash flows
152
+ #
153
+ # @return [Float]
154
+ #
155
+ # @example
156
+ # Exonio.npv(0.281, [-100, 39, 59, 55, 20]) # ==> -0.00661872883563408
157
+ #
158
+ def npv(discount, cashflows)
159
+ total = 0
160
+
161
+ cashflows.each_with_index do |cashflow, index|
162
+ total += cashflow / (1 + discount) ** (index + 1)
163
+ end
164
+
165
+ total
166
+ end
167
+
168
+ # Calculates the internal rate of return on an investment based on a
169
+ # series of periodic cash flows.
170
+ #
171
+ # @param cashflows [Array] An array containing the income or payments
172
+ # associated with the investment
173
+ #
174
+ # @return [Float]
175
+ #
176
+ # @example
177
+ # Exonio.irr([-100, 39, 59, 55, 20]) # ==> 0.28094842116...
178
+ #
179
+ def irr(values)
180
+ func = Helpers::IrrHelper.new(values)
181
+ guess = [ func.zero ]
182
+ nlsolve( func, guess)
183
+ guess[0]
184
+ end
185
+
147
186
  private
148
187
 
149
188
  # This method was borrowed from the NumPy rate formula
@@ -0,0 +1,28 @@
1
+ module Exonio
2
+ module Helpers
3
+ class IrrHelper
4
+ values = {
5
+ eps: '1.0e-16',
6
+ one: '1.0',
7
+ two: '2.0',
8
+ ten: '10.0',
9
+ zero: '0.0'
10
+ }
11
+
12
+ values.each do |key, value|
13
+ define_method key do
14
+ BigDecimal.new(value)
15
+ end
16
+ end
17
+
18
+ def initialize(transactions)
19
+ @transactions = transactions
20
+ end
21
+
22
+ def values(x)
23
+ value = Exonio.npv(x[0].to_f, @transactions)
24
+ [ value ]
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Exonio
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exonio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rafael Izidoro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-11 00:00:00.000000000 Z
11
+ date: 2016-04-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - exonio.gemspec
89
89
  - lib/exonio.rb
90
90
  - lib/exonio/financial.rb
91
+ - lib/exonio/helpers.rb
91
92
  - lib/exonio/version.rb
92
93
  homepage: http://github.com/noverde/exonio
93
94
  licenses: []