finance_rb 0.0.4 → 0.1.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: 82235bcca3f51afff5ec345707da9d1ed2e8472a20488bb3847a4967baf6932c
4
- data.tar.gz: '02029b9b9151d52ad860b1f985cc708199d734f8f210e574cda50fc7290f4231'
3
+ metadata.gz: 6a3ce7fbe55b1715dc8b2b636326a1bb99eb4b8638f2a3c111e0decfe1b4d126
4
+ data.tar.gz: a5568d0deadc00bdd55ffac1cb05dee7a7d8abb8bee87015d321916f59694705
5
5
  SHA512:
6
- metadata.gz: 38348b526019d3124c6cff67c4cd39427600d7a52ea584ea9b583328bedcfceb6680783bbbfdb8f9b45b686ff612e6295e670f1a09d882e09f4540fcf23a3aea
7
- data.tar.gz: 879a9b9b6e01efd654e32a1bbc77a9e07e8a9b1cebbbb1061f6d20f3e0aa723b8150fbf8a56e66dee87bf3eb69a14301dc4d9a51fd62f87e9414f69f47aab5d6
6
+ metadata.gz: cd349f4f6aa4cf20fab8db9064da6b77f1199b61b927af170f491dd447cfdb3889aeea97d07de34706ca12493cfae56d1089aa8cfb2ff80e351ddd951d3e92e9
7
+ data.tar.gz: fdef6ff2746503b68a2a83494b6a006f05a7216f87125a71967dfdf7de05d4dcae80563f4cfbac0cf8ce7b91c5510cfd4cc4111cbe49f6573e43dc37fc21d4f2
data/CHANGELOG.md CHANGED
@@ -1,12 +1,19 @@
1
+ ## [0.1.0] - 2021-03-28
2
+
3
+ ### Added
4
+ * Create a basic structure for `Finance::Loan`
5
+ * Implement `Finance::Loan#pmt`
6
+
7
+
1
8
  ## [0.0.4] - 2021-03-25
2
9
 
3
10
  ### Added
4
- * Implement Finance::Calculations#mirr
11
+ * Implement `Finance::Calculations#mirr`
5
12
 
6
13
  ## [0.0.3] - 2021-03-23
7
14
 
8
15
  ### Added
9
- * Implement Finance::Calculations#irr using Newton's method
16
+ * Implement `Finance::Calculations#irr` using Newton's method
10
17
 
11
18
  ## [0.0.2] - 2021-03-22
12
19
 
data/README.md CHANGED
@@ -13,7 +13,7 @@ which are as follows:
13
13
  |:------------------------: |:------------------: | :------------------|
14
14
  | fv | | Computes the future value|
15
15
  | ipmt | | Computes interest payment for a loan|
16
- | pmt | | Computes the fixed periodic payment(principal + interest) made against a loan amount|
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|
19
19
  | pv | | Computes the present value of a payment|
data/lib/finance/loan.rb CHANGED
@@ -2,24 +2,82 @@
2
2
 
3
3
  module Finance
4
4
  class Loan
5
- def initialize(**options)
6
- end
5
+ PAYMENT_TYPE_MAPPING = { end: 0, beginning: 1 }.freeze
7
6
 
8
- def pmt()
9
- end
7
+ # @return [Float] The amount of loan request (I.e. a present value)
8
+ # Defaults to 0.
9
+ attr_accessor :amount
10
10
 
11
- def ipmt()
12
- end
11
+ # @return [Integer] Specification of whether payment is made
12
+ # at the beginning (ptype = 1) or the end (ptype = 0) of each period.
13
+ # Defaults to {:end, 0}.
14
+ attr_accessor :ptype
15
+
16
+ # @return [Float] The nominal annual rate of interest as decimal (not per cent).
17
+ # (e.g., 13% -> 0.13)
18
+ # Defaults to 0.
19
+ attr_accessor :nominal_rate
20
+
21
+ # @return [Float] The monthly rate is the nominal annual rate divided by 12.
22
+ # Defaults to 0.
23
+ attr_reader :monthly_rate
13
24
 
14
- def ppmt()
25
+ # @return [Float] The number of periods to be compounded for. (I.e. Nper())
26
+ # Defaults to 1.
27
+ attr_accessor :duration
28
+
29
+ # @return [Float] Future value.
30
+ # Defaults to 0.
31
+ attr_accessor :future_value
32
+
33
+ def initialize(**options)
34
+ initialize_payment_type(options[:ptype])
35
+ @nominal_rate = options.fetch(:nominal_rate, 0).to_f
36
+ @duration = options.fetch(:duration, 1).to_f
37
+ @amount = options.fetch(:amount, 0).to_f
38
+ @future_value = options.fetch(:future_value, 0).to_f
39
+ @monthly_rate = @nominal_rate / 12
15
40
  end
16
41
 
17
- def fv();end
42
+ # Pmt computes the payment against a loan principal plus interest (future_value = 0).
43
+ # It can also be used to calculate the recurring payments needed to achieve
44
+ # a certain future value given an initial deposit,
45
+ # a fixed periodically compounded interest rate, and the total number of periods.
46
+ #
47
+ # @return [Numeric] The (fixed) periodic payment.
48
+ #
49
+ # @example
50
+ # require 'finance_rb'
51
+ # Finance::Loan.new(nominal_rate: 0.1, duration: 12, amount: 1000, ptype: :end).pmt
52
+ # #=> 87.9158872300099
53
+ #
54
+ # @see http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
55
+ # @see [WRW] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May).
56
+ # Open Document Format for Office Applications (OpenDocument)v1.2,
57
+ # Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version,
58
+ # Pre-Draft 12. Organization for the Advancement of Structured Information
59
+ # Standards (OASIS). Billerica, MA, USA. [ODT Document].
60
+ def pmt
61
+ factor = (1.0 + monthly_rate)**duration
62
+ second_factor =
63
+ if monthly_rate.zero?
64
+ duration
65
+ else
66
+ (factor - 1) * (1 + monthly_rate * ptype) / monthly_rate
67
+ end
18
68
 
19
- def pv();end
69
+ (-future_value + amount * factor) / second_factor
70
+ end
20
71
 
21
- def nper();end
72
+ private
22
73
 
23
- def rate();end
74
+ def initialize_payment_type(ptype)
75
+ @ptype =
76
+ if ptype.nil? || !PAYMENT_TYPE_MAPPING.keys.include?(ptype)
77
+ PAYMENT_TYPE_MAPPING[:end]
78
+ else
79
+ PAYMENT_TYPE_MAPPING[ptype]
80
+ end
81
+ end
24
82
  end
25
83
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Finance
4
- VERSION = "0.0.4"
4
+ VERSION = "0.1.0"
5
5
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Finance::Loan do
4
+ describe '#pmt' do
5
+ context 'w/o a full set of params' do
6
+ it 'calculates correct pmt value w/o :ptype' do
7
+ loan = Finance::Loan.new(nominal_rate: 0.1, duration: 12, amount: 1000)
8
+ expect(loan.pmt).to eq(87.9158872300099)
9
+ end
10
+
11
+ it 'calculates correct pmt value w/o :nominal_rate' do
12
+ loan = Finance::Loan.new(duration: 12, amount: 1200, ptype: :end)
13
+ expect(loan.pmt).to eq(100)
14
+ end
15
+ end
16
+
17
+ context 'with zero rates' do
18
+ it 'calculates correct pmt value for 3 years' do
19
+ loan = Finance::Loan.new(nominal_rate: 0, duration: 36, amount: 10_000, ptype: :end)
20
+ expect(loan.pmt).to eq(277.77777777777777)
21
+ end
22
+
23
+ it 'calculates correct pmt value for 6 months' do
24
+ loan = Finance::Loan.new(nominal_rate: 0, duration: 6, amount: 10_000, ptype: :end)
25
+ expect(loan.pmt).to eq(1666.6666666666667)
26
+ end
27
+ end
28
+
29
+ context 'with :beginning ptype' do
30
+ it 'calculates correct pmt value' do
31
+ loan = Finance::Loan.new(nominal_rate: 0.12, duration: 6, amount: 1000, ptype: :beginning)
32
+ expect(loan.pmt).to eq(170.8399670404763)
33
+ end
34
+ end
35
+
36
+ it 'calculates correct pmt value' do
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)
39
+ end
40
+ end
41
+ 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.0.4
4
+ version: 0.1.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-03-24 00:00:00.000000000 Z
11
+ date: 2021-03-28 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
@@ -27,6 +27,7 @@ files:
27
27
  - lib/finance/version.rb
28
28
  - lib/finance_rb.rb
29
29
  - spec/finance/calculations_spec.rb
30
+ - spec/finance/loan_spec.rb
30
31
  - spec/spec_helper.rb
31
32
  homepage: https://github.com/wowinter13/finance_rb
32
33
  licenses:
@@ -57,4 +58,5 @@ specification_version: 4
57
58
  summary: A library for finance manipulations in Ruby.
58
59
  test_files:
59
60
  - spec/finance/calculations_spec.rb
61
+ - spec/finance/loan_spec.rb
60
62
  - spec/spec_helper.rb