finfast 0.0.3 → 0.0.4

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.
Files changed (3) hide show
  1. data/lib/finfast.rb +92 -1
  2. data/lib/finfast/version.rb +1 -1
  3. metadata +7 -7
@@ -21,6 +21,97 @@ module Finfast
21
21
  newton(pmt_array, exp_array, guess, tolerance, iterations)
22
22
  end
23
23
 
24
+ # Future value of an annuity.
25
+ #
26
+ # Params:
27
+ # +i+:: The interest rate per period
28
+ # +n+:: The number of periods
29
+ # +pmt+:: The payment made in each period
30
+ #
31
+ def fvan(i, n, pmt)
32
+ return pmt * (((1 + i) ** n) - 1) / i
33
+ end
34
+
35
+ # Future value of a lump amount.
36
+ #
37
+ # Params:
38
+ # +i+:: The interest rate per period
39
+ # +n+:: The number of periods
40
+ # +amt+:: The amount in today's terms (present value)
41
+ #
42
+ def fv(i, n, amt)
43
+ return ((1 + i) ** n) * amt
44
+ end
45
+
46
+ # Calculates the payment for a loan based on constant payments and a
47
+ # constant interest rate.
48
+ #
49
+ # Params:
50
+ # +i+:: The interest rate per period
51
+ # +n+:: The number of periods
52
+ # +pv+:: The present value, or principal
53
+ #
54
+ def pmt(i, n, pv)
55
+ return pv / ((1 - (1 / (1 + i) ** n )) / i)
56
+ end
57
+
58
+ # The interest payment for a given period in an investment.
59
+ #
60
+ # Params:
61
+ # +i+:: The interest rate per period
62
+ # +n+:: The number of periods
63
+ # +per+:: The period for which to calculate the interest payment
64
+ # +pv+:: The present value, or principal
65
+ #
66
+ def ipmt(i, per, n, pv)
67
+ pmt = pmt(i, n, pv)
68
+ return ipmtp(i, per, pv, pmt)
69
+ end
70
+
71
+ # The interest payment for a loan, given that you are making a fixed
72
+ # payment different from the one dictated by the terms of the loan.
73
+ #
74
+ # Params:
75
+ # +i+:: The interest rate per period
76
+ # +per+:: The period for which to calculate the interest payment
77
+ # +pv+:: The present value, or principal
78
+ # +pmt+:: The amount of the fixed payment
79
+ #
80
+ def ipmtp(i, per, pv, pmt)
81
+ fv_orig = fv(i, per - 1, pv) # FV at the beginning of the period
82
+ fvan_pmts = fvan(i, per - 1, pmt) # FV of what we've been paying
83
+ balance_in_period = fv_orig - fvan_pmts # FV of what's left
84
+ return balance_in_period * i
85
+ end
86
+
87
+ def ipmts(i, pers, n, pv)
88
+ result = []
89
+ pers.each do |p|
90
+ result.push(ipmt(i, p, n, pv))
91
+ end
92
+ return result
93
+ end
94
+
95
+ def ipmtsp(i, pers, pv, pmt)
96
+ result = []
97
+ pers.each do |p|
98
+ result.push(ipmtp(i, p, pv, pmt))
99
+ end
100
+ return result
101
+ end
102
+
103
+ def pv_stream(i, stream)
104
+ result = 0
105
+ stream.each_with_index do |x, p|
106
+ result += x / (1 + i) ** (p + 1)
107
+ end
108
+ return result
109
+ end
110
+
111
+ def date_difference_months(d1, d2)
112
+ return d1.year * 12 - d2.year * 12 + d1.month - d2.month
113
+ end
114
+
24
115
  private
25
116
 
26
117
  def self.exp(d_i, d_0)
@@ -31,6 +122,6 @@ module Finfast
31
122
  end
32
123
  end
33
124
 
34
- module_function :xirr, :newton
125
+ module_function :xirr, :pmt, :ipmt, :ipmtp, :ipmts, :fv, :fvan, :newton
35
126
 
36
127
  end
@@ -1,6 +1,6 @@
1
1
  module Finfast
2
2
  VERSION_MAJOR = 0
3
3
  VERSION_MINOR = 0
4
- VERSION_PATCH = 3
4
+ VERSION_PATCH = 4
5
5
  VERSION = [VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH].join('.')
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: finfast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-05 00:00:00.000000000 Z
12
+ date: 2013-01-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70204428820160 !ruby/object:Gem::Requirement
16
+ requirement: &70239648070620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70204428820160
24
+ version_requirements: *70239648070620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70204428819740 !ruby/object:Gem::Requirement
27
+ requirement: &70239648070200 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70204428819740
35
+ version_requirements: *70239648070200
36
36
  description: Fast Ruby financial math operations, using native code
37
37
  email:
38
38
  - mike.grant@jhu.edu
@@ -54,7 +54,7 @@ rdoc_options:
54
54
  - --main
55
55
  - README.md
56
56
  - --title
57
- - finfast-0.0.3 Documentation
57
+ - finfast-0.0.4 Documentation
58
58
  require_paths:
59
59
  - lib
60
60
  required_ruby_version: !ruby/object:Gem::Requirement