finance 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README +47 -0
  2. data/lib/finance.rb +93 -0
  3. metadata +66 -0
data/README ADDED
@@ -0,0 +1,47 @@
1
+ = ABOUT
2
+
3
+ The goal of the _finance_ library is to provide Ruby implementations
4
+ of common financial formulas, with a function syntax similar to most
5
+ modern spreadsheet programs. As the project matures, consideration will
6
+ also be given to providing useful, but more advanced functions, which
7
+ may not be suitable for a spreadsheet environment but are useful when
8
+ programming.
9
+
10
+ I began developing _finance_ while writing a Ruby script for analyzing
11
+ mortgages. I couldn't find an existing resource for these tools, I am
12
+ hoping to save other folks some time by releasing what I have as a gem.
13
+
14
+ = EXAMPLES
15
+
16
+
17
+
18
+ = INSTALL
19
+
20
+ + $ sudo gem install finance +
21
+
22
+ = RESOURCES
23
+
24
+ Source code and bug tracking is available via github:
25
+
26
+ Project Page:: + http://github.com/wkranec/finance +
27
+ Repository:: + git://github.com/wkranec/finance.git +
28
+
29
+ = COPYRIGHT
30
+
31
+ This library is released under the terms of the LGPL license.
32
+
33
+ Copyright (c) 2011, William Kranec.
34
+ All rights reserved.
35
+
36
+ This program is free software: you can redistribute it and/or modify it
37
+ under the terms of the GNU General Public License as published by the
38
+ Free Software Foundation, either version 3 of the License, or (at your
39
+ option) any later version.
40
+
41
+ This program is distributed in the hope that it will be useful,
42
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
43
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
44
+ General Public License for more details.
45
+
46
+ You should have received a copy of the GNU General Public License along
47
+ with this program. If not, see <http://www.gnu.org/licenses/>.
data/lib/finance.rb ADDED
@@ -0,0 +1,93 @@
1
+ # The *Finance* module adheres to the following conventions for
2
+ # financial calculations:
3
+ #
4
+ # * Positive values represent cash inflows (money received); negative
5
+ # values represent cash outflows (payments).
6
+ # * *principal* represents the outstanding balance of a loan or annuity.
7
+ # * *rate* represents the interest rate _per period_.
8
+ module Finance
9
+
10
+ # Return the internal rate of return for a given sequence of cashflows.
11
+ #
12
+ # References:
13
+ # * http://en.wikipedia.org/wiki/Internal_rate_of_return
14
+ def Finance.irr(cashflows, iterations=100)
15
+ rate = 1.0
16
+ investment = cashflows[0]
17
+ for i in 1..iterations+1
18
+ rate = rate * (1 - npv(rate, cashflows) / investment)
19
+ end
20
+ rate
21
+ end
22
+
23
+ def Finance.payments(principal, rates)
24
+ p_total = rates.inject(0) { |sum, n| sum + n[0] }
25
+ p_current = 0
26
+ payments = []
27
+
28
+ rates.each do |periods, rate|
29
+ payment = Finance.pmt(principal, rate, p_total-p_current)
30
+
31
+ begin
32
+ payment = payment.round(2)
33
+ rescue ArgumentError
34
+ payment = (payment * 100.0).round / 100.0
35
+ end
36
+
37
+ if block_given?
38
+ payment = yield(payment)
39
+ end
40
+
41
+ periods.times do
42
+ interest = principal * rate
43
+
44
+ if payment > principal + interest
45
+ payment = principal + interest
46
+ end
47
+
48
+ principal = principal + interest - payment
49
+
50
+ begin
51
+ principal = principal.round(2)
52
+ rescue ArgumentError
53
+ principal = (principal * 100.0).round / 100.0
54
+ end
55
+
56
+ payments << payment
57
+ break if principal == 0
58
+
59
+ p_current = p_current + 1
60
+ end
61
+ end
62
+
63
+ payments
64
+ end
65
+
66
+ # Return the periodic payment due on a loan, based on the amortization
67
+ # process.
68
+ #
69
+ # References:
70
+ # * http://en.wikipedia.org/wiki/Amortization_calculator
71
+ def Finance.pmt(principal, rate, periods)
72
+ principal * (rate + (rate / ((1 + rate) ** periods - 1)))
73
+ end
74
+
75
+ # Return the net present value of a sequence of cash flows given
76
+ # the discount rate _rate_.
77
+ #
78
+ # References:
79
+ # * http://en.wikipedia.org/wiki/Net_present_value
80
+ def Finance.npv(rate, cashflows)
81
+ total = 0.0
82
+ cashflows.each_with_index do |cashflow, index|
83
+ total = total + cashflow / (1+rate) ** index
84
+ end
85
+ total
86
+ end
87
+
88
+ # Return the number of periods needed to pay off a loan with the
89
+ # given payment.
90
+ def Finance.nper(payment, rate, principal)
91
+ -(Math.log(1-((principal/payment)*rate))) / Math.log(1+rate)
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: finance
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Bill Kranec
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-15 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: The goal of the finance library is to provide Ruby implementations of common financial formulas, with a function syntax similar to most modern spreadsheet programs.
22
+ email: wkranec@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - lib/finance.rb
31
+ - README
32
+ homepage: http://finance.rubyforge.org
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ hash: 3
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project: finance
61
+ rubygems_version: 1.8.5
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Ruby implementations of common financial formulas.
65
+ test_files: []
66
+