finance_rb 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/lib/finance/loan.rb +39 -0
- data/lib/finance/version.rb +1 -1
- data/spec/finance/loan_spec.rb +29 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f920db19ed5b406b8e263c09fac4aba9cd203ebdca989c096b44b441753757e
|
4
|
+
data.tar.gz: 435cd054975eee4b5027245d0301551b1b0cfd889153879f02929f1885d55f77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15fa362a606eacc22aeff3c89312bbedbfbe2d78319b66cd3c146a985a6c5a68114fcb1601986facdc823fa14b03a798708faae3adf704aad8c8ee89c59ec07b
|
7
|
+
data.tar.gz: c96d0f272ccee5c88cab312981f7795bac101e7bd3b229b88cfb34f05db3334b63bc61d9f41f13404105c6b6ab6bd4d318ecf3ad98ac89dbe443f1cc920a453a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,7 @@ which are as follows:
|
|
12
12
|
| numpy-financial function | ruby native function ported? | info|
|
13
13
|
|:------------------------: |:------------------: | :------------------|
|
14
14
|
| fv | ✅ | Computes the future value|
|
15
|
-
| ipmt |
|
15
|
+
| ipmt | ✅ | Computes interest payment for a loan|
|
16
16
|
| pmt | ✅ | Computes the fixed periodic payment(principal + interest) made against a loan amount|
|
17
17
|
| ppmt | | Computes principal payment for a loan|
|
18
18
|
| nper | | Computes the number of periodic payments|
|
data/lib/finance/loan.rb
CHANGED
@@ -36,6 +36,9 @@ module Finance
|
|
36
36
|
# You can use #pmt method to calculate value if param is not defined.
|
37
37
|
attr_accessor :payment
|
38
38
|
|
39
|
+
# @return [Float] Period under consideration.
|
40
|
+
attr_accessor :period
|
41
|
+
|
39
42
|
# Create a new Loan instance.
|
40
43
|
def initialize(**options)
|
41
44
|
initialize_payment_type(options[:ptype])
|
@@ -43,6 +46,7 @@ module Finance
|
|
43
46
|
@duration = options.fetch(:duration, 1).to_f
|
44
47
|
@amount = options.fetch(:amount, 0).to_f
|
45
48
|
@future_value = options.fetch(:future_value, 0).to_f
|
49
|
+
@period = options[:period]
|
46
50
|
@payment = options[:payment]
|
47
51
|
@monthly_rate = @nominal_rate / 12
|
48
52
|
end
|
@@ -79,6 +83,34 @@ module Finance
|
|
79
83
|
(-future_value + amount * factor) / second_factor
|
80
84
|
end
|
81
85
|
|
86
|
+
# IPmt computes interest payment for a loan under a given period.
|
87
|
+
#
|
88
|
+
# Required Loan arguments: period, nominal_rate, duration, amount, future_value*
|
89
|
+
#
|
90
|
+
# @return [Float] Interest payment for a loan.
|
91
|
+
#
|
92
|
+
# @example
|
93
|
+
# require 'finance_rb'
|
94
|
+
# Finance::Loan.new(nominal_rate: 0.0824, duration: 12, amount: 2500, period: 1).ipmt
|
95
|
+
# #=> -17.166666666666668
|
96
|
+
#
|
97
|
+
# @see http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
|
98
|
+
# @see [WRW] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May).
|
99
|
+
# Open Document Format for Office Applications (OpenDocument)v1.2,
|
100
|
+
# Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version,
|
101
|
+
# Pre-Draft 12. Organization for the Advancement of Structured Information
|
102
|
+
# Standards (OASIS). Billerica, MA, USA. [ODT Document].
|
103
|
+
def ipmt
|
104
|
+
raise ArgumentError, 'no period given' if period.nil?
|
105
|
+
|
106
|
+
ipmt_val = remaining_balance * monthly_rate
|
107
|
+
if ptype == PAYMENT_TYPE_MAPPING[:beginning]
|
108
|
+
period == 1 ? 0.0 : (ipmt_val / 1 + monthly_rate)
|
109
|
+
else
|
110
|
+
ipmt_val
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
82
114
|
# Fv computes future value at the end of some periods (duration).
|
83
115
|
# Required Loan arguments: nominal_rate, duration, payment, amount*
|
84
116
|
#
|
@@ -119,5 +151,12 @@ module Finance
|
|
119
151
|
PAYMENT_TYPE_MAPPING[ptype]
|
120
152
|
end
|
121
153
|
end
|
154
|
+
|
155
|
+
def remaining_balance
|
156
|
+
self.class.new(
|
157
|
+
nominal_rate: nominal_rate.to_f, duration: period - 1.0,
|
158
|
+
amount: amount.to_f, ptype: PAYMENT_TYPE_MAPPING.key(ptype)
|
159
|
+
).fv(payment: -pmt)
|
160
|
+
end
|
122
161
|
end
|
123
162
|
end
|
data/lib/finance/version.rb
CHANGED
data/spec/finance/loan_spec.rb
CHANGED
@@ -70,4 +70,33 @@ RSpec.describe Finance::Loan do
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
end
|
73
|
+
|
74
|
+
describe '#ipmt' do
|
75
|
+
context 'when 1 period' do
|
76
|
+
it 'calculates correct ipmt value' do
|
77
|
+
loan = Finance::Loan.new(
|
78
|
+
nominal_rate: 0.0824, duration: 12, amount: 2500, period: 1
|
79
|
+
)
|
80
|
+
expect(loan.ipmt).to eq(-17.166666666666668)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'when 2 periods' do
|
85
|
+
it 'calculates correct ipmt value' do
|
86
|
+
loan = Finance::Loan.new(
|
87
|
+
nominal_rate: 0.0824, duration: 12, amount: 2500, period: 2
|
88
|
+
)
|
89
|
+
expect(loan.ipmt).to eq(-15.789337457350777)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when 3 periods' do
|
94
|
+
it 'calculates correct ipmt value' do
|
95
|
+
loan = Finance::Loan.new(
|
96
|
+
nominal_rate: 0.0824, duration: 12.0, amount: 2500.0, period: 3.0, fv: 0.0
|
97
|
+
)
|
98
|
+
expect(loan.ipmt).to eq(-14.402550587464257)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
73
102
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finance_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vlad Dyachenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A ruby port of numpy-financial functions. This library provides a Ruby
|
14
14
|
interface for working with interest rates, mortgage amortization, and cashflows
|