finance_rb 0.1.2 → 0.2.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
  SHA256:
3
- metadata.gz: 3f920db19ed5b406b8e263c09fac4aba9cd203ebdca989c096b44b441753757e
4
- data.tar.gz: 435cd054975eee4b5027245d0301551b1b0cfd889153879f02929f1885d55f77
3
+ metadata.gz: d76ff15f6be262332aa729708a548d775f1caaaefaa3135c1d545660c34717ca
4
+ data.tar.gz: 2d75e526b34bf1704686e3fb9bf5198b4b2d302f05e714207d8d06833ce96acf
5
5
  SHA512:
6
- metadata.gz: 15fa362a606eacc22aeff3c89312bbedbfbe2d78319b66cd3c146a985a6c5a68114fcb1601986facdc823fa14b03a798708faae3adf704aad8c8ee89c59ec07b
7
- data.tar.gz: c96d0f272ccee5c88cab312981f7795bac101e7bd3b229b88cfb34f05db3334b63bc61d9f41f13404105c6b6ab6bd4d318ecf3ad98ac89dbe443f1cc920a453a
6
+ metadata.gz: 9545eebb0c540d6e5f3d75cdbf8a5b7fabe0ed75860601dd278f2c0b13ca05285a9d0865d978837532c92c2fb70815c5f450a10d6ff7bd008330ae81ac4d915b
7
+ data.tar.gz: bffcb93c73279bea34db04dbc39d0b6747e9d609e91a3a5f8006aaefe6ca08be2a4109b033b622a3382a4232a4e3ffba4d1a2eb168fbf6f1e6fe73fd971ab20c
data/README.md CHANGED
@@ -14,7 +14,7 @@ which are as follows:
14
14
  | fv | ✅ | Computes the future value|
15
15
  | ipmt | ✅ | Computes interest payment for a loan|
16
16
  | pmt | ✅ | Computes the fixed periodic payment(principal + interest) made against a loan amount|
17
- | ppmt | | Computes principal payment for a loan|
17
+ | ppmt || Computes principal payment for a loan|
18
18
  | nper | | Computes the number of periodic payments|
19
19
  | pv | | Computes the present value of a payment|
20
20
  | rate | | Computes the rate of interest per period|
data/lib/finance/loan.rb CHANGED
@@ -80,7 +80,7 @@ module Finance
80
80
  (factor - 1) * (1 + monthly_rate * ptype) / monthly_rate
81
81
  end
82
82
 
83
- (-future_value + amount * factor) / second_factor
83
+ -((future_value + amount * factor) / second_factor)
84
84
  end
85
85
 
86
86
  # IPmt computes interest payment for a loan under a given period.
@@ -111,6 +111,27 @@ module Finance
111
111
  end
112
112
  end
113
113
 
114
+ # PPmt computes principal payment for a loan under a given period.
115
+ #
116
+ # Required Loan arguments: period, nominal_rate, duration, amount, future_value*
117
+ #
118
+ # @return [Float] Principal payment for a loan under a given period.
119
+ #
120
+ # @example
121
+ # require 'finance_rb'
122
+ # Finance::Loan.new(nominal_rate: 0.0824, duration: 12, amount: 2500, period: 1).ppmt
123
+ # #=> -200.58192368678277
124
+ #
125
+ # @see http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
126
+ # @see [WRW] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May).
127
+ # Open Document Format for Office Applications (OpenDocument)v1.2,
128
+ # Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version,
129
+ # Pre-Draft 12. Organization for the Advancement of Structured Information
130
+ # Standards (OASIS). Billerica, MA, USA. [ODT Document].
131
+ def ppmt
132
+ pmt - ipmt
133
+ end
134
+
114
135
  # Fv computes future value at the end of some periods (duration).
115
136
  # Required Loan arguments: nominal_rate, duration, payment, amount*
116
137
  #
@@ -156,7 +177,7 @@ module Finance
156
177
  self.class.new(
157
178
  nominal_rate: nominal_rate.to_f, duration: period - 1.0,
158
179
  amount: amount.to_f, ptype: PAYMENT_TYPE_MAPPING.key(ptype)
159
- ).fv(payment: -pmt)
180
+ ).fv(payment: pmt)
160
181
  end
161
182
  end
162
183
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Finance
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -5,37 +5,37 @@ RSpec.describe Finance::Loan do
5
5
  context 'w/o a full set of params' do
6
6
  it 'calculates correct pmt value w/o :ptype' do
7
7
  loan = Finance::Loan.new(nominal_rate: 0.1, duration: 12, amount: 1000)
8
- expect(loan.pmt).to eq(87.9158872300099)
8
+ expect(loan.pmt).to eq(-87.9158872300099)
9
9
  end
10
10
 
11
11
  it 'calculates correct pmt value w/o :nominal_rate' do
12
12
  loan = Finance::Loan.new(duration: 12, amount: 1200, ptype: :end)
13
- expect(loan.pmt).to eq(100)
13
+ expect(loan.pmt).to eq(-100)
14
14
  end
15
15
  end
16
16
 
17
17
  context 'with zero rates' do
18
18
  it 'calculates correct pmt value for 3 years' do
19
19
  loan = Finance::Loan.new(nominal_rate: 0, duration: 36, amount: 10_000, ptype: :end)
20
- expect(loan.pmt).to eq(277.77777777777777)
20
+ expect(loan.pmt).to eq(-277.77777777777777)
21
21
  end
22
22
 
23
23
  it 'calculates correct pmt value for 6 months' do
24
24
  loan = Finance::Loan.new(nominal_rate: 0, duration: 6, amount: 10_000, ptype: :end)
25
- expect(loan.pmt).to eq(1666.6666666666667)
25
+ expect(loan.pmt).to eq(-1666.6666666666667)
26
26
  end
27
27
  end
28
28
 
29
29
  context 'with :beginning ptype' do
30
30
  it 'calculates correct pmt value' do
31
31
  loan = Finance::Loan.new(nominal_rate: 0.12, duration: 6, amount: 1000, ptype: :beginning)
32
- expect(loan.pmt).to eq(170.8399670404763)
32
+ expect(loan.pmt).to eq(-170.8399670404763)
33
33
  end
34
34
  end
35
35
 
36
36
  it 'calculates correct pmt value' do
37
37
  loan = Finance::Loan.new(nominal_rate: 0.13, duration: 90, amount: 1_000_000, ptype: :end)
38
- expect(loan.pmt).to eq(17_449.90775727763)
38
+ expect(loan.pmt).to eq(-17_449.90775727763)
39
39
  end
40
40
  end
41
41
 
@@ -99,4 +99,51 @@ RSpec.describe Finance::Loan do
99
99
  end
100
100
  end
101
101
  end
102
+
103
+ describe '#ppmt' do
104
+ context 'when 1 period' do
105
+ it 'calculates correct ppmt value' do
106
+ loan = Finance::Loan.new(
107
+ nominal_rate: 0.0824, duration: 12, amount: 2500, period: 1
108
+ )
109
+ expect(loan.ppmt).to eq(-200.58192368678277)
110
+ end
111
+ end
112
+
113
+ context 'when 2 periods' do
114
+ it 'calculates correct ppmt value' do
115
+ loan = Finance::Loan.new(
116
+ nominal_rate: 0.0824, duration: 12, amount: 2500, period: 2
117
+ )
118
+ expect(loan.ppmt).to eq(-201.95925289609866)
119
+ end
120
+ end
121
+
122
+ context 'when 3 periods' do
123
+ it 'calculates correct ppmt value' do
124
+ loan = Finance::Loan.new(
125
+ nominal_rate: 0.0824, duration: 12, amount: 2500, period: 3
126
+ )
127
+ expect(loan.ppmt).to eq(-203.34603976598518)
128
+ end
129
+ end
130
+
131
+ context 'when 4 periods' do
132
+ it 'calculates correct ppmt value' do
133
+ loan = Finance::Loan.new(
134
+ nominal_rate: 0.0824, duration: 12, amount: 2500, period: 4
135
+ )
136
+ expect(loan.ppmt).to eq(-204.7423492390449)
137
+ end
138
+ end
139
+
140
+ context 'when 5 periods' do
141
+ it 'calculates correct ppmt value' do
142
+ loan = Finance::Loan.new(
143
+ nominal_rate: 0.0824, duration: 12, amount: 2500, period: 5
144
+ )
145
+ expect(loan.ppmt).to eq(-206.1482467038197)
146
+ end
147
+ end
148
+ end
102
149
  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.2
4
+ version: 0.2.0
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-04-04 00:00:00.000000000 Z
11
+ date: 2021-04-30 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