finance 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README CHANGED
@@ -1,30 +1,74 @@
1
- = ABOUT
1
+ _finance_ - a library for financial calculations in Ruby.
2
+
3
+ = INSTALL
4
+
5
+ $ sudo gem install finance
6
+
7
+ = OVERVIEW
8
+
9
+ == GETTING STARTED
10
+
11
+ >> require 'finance'
12
+
13
+ == AMORTIZATION
14
+
15
+ You are interested in borrowing $250,000 under a 30 year, fixed-rate loan with a 4.25% APR.
16
+
17
+ >> rate = Rate.new(0.0425, :apr, :duration => 30.years)
18
+ >> amortization = 250000.amortize(rate)
19
+
20
+ Find the standard monthly payment:
2
21
 
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.
22
+ >> amortization.payment
23
+ => DecNum('-1229.91')
24
+
25
+ Find the total cost of the loan:
26
+
27
+ >> amortization.payments.sum
28
+ => DecNum('-442766.55')
29
+
30
+ How much will you pay in interest?
31
+
32
+ >> amortization.interest.sum
33
+ => DecNum('192766.55')
34
+
35
+ How much interest in the first six months?
36
+
37
+ >> amortization.interest[0,6].sum
38
+ => DecNum('5294.62')
39
+
40
+ = ABOUT
9
41
 
10
42
  I began developing _finance_ while writing a Ruby script for analyzing
11
43
  mortgages. I couldn't find an existing resource for these tools, I am
12
44
  hoping to save other folks some time by releasing what I have as a gem.
13
45
 
14
- = EXAMPLES
46
+ If you have used _finance_ and find it useful, I would enjoy hearing
47
+ about it!
15
48
 
49
+ = FEATURES
16
50
 
51
+ Currently implemented features include:
17
52
 
18
- = INSTALL
53
+ * Fixed-rate mortgage amortization (30/360).
54
+ * Interest rates
55
+ * Various cash flow computations, such as NPV, IRR, and sum.
56
+
57
+ Planned features include:
19
58
 
20
- + $ sudo gem install finance +
59
+ * Adjustable rate mortgage amortization.
60
+ * Payment modifications (i.e., how does paying an additional $75 per
61
+ month affect the amortization?)
62
+ * Balloon payments.
63
+ * Support for amortization methods other than 30/360.
21
64
 
22
65
  = RESOURCES
23
66
 
24
- Source code and bug tracking is available via github:
67
+ This gem and related documentation is available through
68
+ RubyGems[https://rubygems.org/gems/finance].
25
69
 
26
- Project Page:: + http://github.com/wkranec/finance +
27
- Repository:: + git://github.com/wkranec/finance.git +
70
+ Source code and bug tracking is available via
71
+ github[http://github.com/wkranec/finance].
28
72
 
29
73
  = COPYRIGHT
30
74
 
@@ -0,0 +1,91 @@
1
+ require 'cashflows'
2
+
3
+ class Period
4
+ attr_accessor :payment
5
+ attr_accessor :principal
6
+ attr_accessor :rate
7
+
8
+ # Return the remaining balance at the end of the period.
9
+ def balance
10
+ @principal + @payment + self.interest
11
+ end
12
+
13
+ def initialize(principal, rate, payment)
14
+ @principal = principal
15
+ @rate = rate
16
+ @payment = payment
17
+ end
18
+
19
+ # Return the interest charged for the period.
20
+ def interest
21
+ (@principal * @rate).round(2)
22
+ end
23
+ end
24
+
25
+ class Amortization
26
+ attr_accessor :balance
27
+ attr_accessor :duration
28
+ attr_accessor :payment
29
+ attr_accessor :periods
30
+ attr_accessor :principal
31
+ attr_accessor :rate
32
+
33
+ def compute(balance, rate)
34
+ duration = @duration - @periods.length
35
+ @payment = Amortization.payment balance, rate.monthly, duration
36
+
37
+ rate.duration.times do
38
+ if @payment > @balance
39
+ @payment = @balance
40
+ end
41
+
42
+ period = Period.new(@balance, rate.monthly, @payment)
43
+ @periods << period
44
+ @balance = period.balance
45
+
46
+ if @balance.zero?
47
+ break
48
+ end
49
+ end
50
+ end
51
+
52
+ def initialize(principal, rate)
53
+ @principal = principal
54
+ @balance = principal
55
+ @rate = rate
56
+ @duration = rate.duration
57
+ @periods = []
58
+
59
+ compute(@balance, @rate)
60
+
61
+ # Add any remaining balance due to rounding error to the last payment.
62
+ unless @balance.zero?
63
+ @periods[-1].payment -= @balance
64
+ @balance = 0
65
+ end
66
+ end
67
+
68
+ def inspect
69
+ "Amortization.new(#{@principal}, #{@rate}"
70
+ end
71
+
72
+ def interest
73
+ @periods.collect { |period| period.interest }
74
+ end
75
+
76
+ # Return the periodic payment due on a loan, based on the
77
+ #{http://en.wikipedia.org/wiki/Amortization_calculator amortization process}.
78
+ def Amortization.payment(balance, rate, periods)
79
+ -(balance * (rate + (rate / ((1 + rate) ** periods - 1)))).round(2)
80
+ end
81
+
82
+ def payments
83
+ @periods.collect { |period| period.payment }
84
+ end
85
+ end
86
+
87
+ class Numeric
88
+ def amortize(rate)
89
+ Amortization.new(self, rate)
90
+ end
91
+ end
@@ -0,0 +1,33 @@
1
+ module Cashflow
2
+ # Return the {http://en.wikipedia.org/wiki/Internal_rate_of_return
3
+ # internal rate of return} for a given sequence of cashflows.
4
+ def irr(iterations=100)
5
+ rate = 1.0
6
+ investment = self[0]
7
+ for i in 1..iterations+1
8
+ rate = rate * (1 - self.npv(rate) / investment)
9
+ end
10
+ rate
11
+ end
12
+
13
+ # Return the {http://en.wikipedia.org/wiki/Net_present_value net present value} of a sequence of cash flows given
14
+ # the discount rate _rate_.
15
+ def npv(rate)
16
+ total = 0.0
17
+ self.each_with_index do |cashflow, index|
18
+ total = total + cashflow / (1+rate) ** index
19
+ end
20
+ total
21
+ end
22
+
23
+ def sum
24
+ self.inject(:+)
25
+ end
26
+
27
+ def xirr
28
+ end
29
+ end
30
+
31
+ class Array
32
+ include Cashflow
33
+ end
@@ -1,3 +1,8 @@
1
+ require 'amortization'
2
+ require 'cashflows'
3
+ require 'rates'
4
+ require 'timespan'
5
+
1
6
  # The *Finance* module adheres to the following conventions for
2
7
  # financial calculations:
3
8
  #
@@ -7,19 +12,6 @@
7
12
  # * *rate* represents the interest rate _per period_.
8
13
  module Finance
9
14
 
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
15
  def Finance.payments(principal, rates)
24
16
  p_total = rates.inject(0) { |sum, n| sum + n[0] }
25
17
  p_current = 0
@@ -63,28 +55,6 @@ module Finance
63
55
  payments
64
56
  end
65
57
 
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
58
  # Return the number of periods needed to pay off a loan with the
89
59
  # given payment.
90
60
  def Finance.nper(payment, rate, principal)
@@ -0,0 +1,89 @@
1
+ require 'rubygems'
2
+ require 'flt'
3
+
4
+ class Rate
5
+ attr_accessor :duration
6
+ attr_accessor :periods
7
+ attr_accessor :nominal
8
+
9
+ # Alias method for *effective*.
10
+ def apr=(apr)
11
+ self.effective = apr
12
+ end
13
+
14
+ def apr
15
+ self.effective
16
+ end
17
+
18
+ # Alias method for *effective*.
19
+ def apy=(apy)
20
+ self.effective = apy
21
+ end
22
+
23
+ def apy
24
+ self.effective
25
+ end
26
+
27
+ def effective=(rate)
28
+ unless @periods == Flt::DecNum.infinity
29
+ @nominal = @periods * ((1 + rate) ** (1 / @periods) - 1)
30
+ else
31
+ @nominal = Math.log(rate + 1)
32
+ end
33
+ end
34
+
35
+ def effective
36
+ unless @periods == Flt::DecNum.infinity
37
+ (1 + @nominal / @periods) ** @periods - 1
38
+ else
39
+ @nominal.exp - 1
40
+ end
41
+ end
42
+
43
+ def initialize(rate, type, opts={})
44
+ # Make sure the rate is a decimal.
45
+ unless rate.class == Flt::DecNum
46
+ rate = Flt::DecNum rate.to_s
47
+ end
48
+
49
+ # Set the compounding interval.
50
+ compounding = opts.fetch(:compounds, :monthly)
51
+
52
+ translate = {
53
+ :annually => Flt::DecNum(1),
54
+ :continuously => Flt::DecNum.infinity,
55
+ :daily => Flt::DecNum(365),
56
+ :monthly => Flt::DecNum(12),
57
+ :quarterly => Flt::DecNum(4),
58
+ :semiannually => Flt::DecNum(2)
59
+ }
60
+
61
+ if translate.has_key? compounding
62
+ @periods = translate.fetch compounding
63
+ elsif compounding.kind_of? Numeric
64
+ @periods = Flt::DecNum compounding.to_s
65
+ end
66
+
67
+ # Set the rate in the proper way, based on the value of :type:.
68
+ if %w{apr apy effective}.include? type.to_s
69
+ self.effective = rate
70
+ else
71
+ @nominal = rate
72
+ end
73
+
74
+ # Set the remainder of the attributes provided in :opts:.
75
+ opts.each do |key, value|
76
+ unless key == :compounds
77
+ send("#{key}=", value)
78
+ end
79
+ end
80
+ end
81
+
82
+ def inspect
83
+ "Rate.new(#{self.apr.round(6)}, :apr)"
84
+ end
85
+
86
+ def monthly
87
+ (self.effective / 12).round(6)
88
+ end
89
+ end