finance_rb 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +31 -3
- data/lib/finance/loan.rb +41 -1
- data/lib/finance/version.rb +1 -1
- data/spec/finance/loan_spec.rb +32 -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: 98c8d72b54d7c7342331cf06086b5e5b1c5ec7c9fe9e3f60315fc78a94254885
|
4
|
+
data.tar.gz: a5ce132889634c03e72dc3c884e3549071867e3a297de2ab1a040c2627b876bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8938c6f7d84419716cc1ee728a372388f1956f9ab1be75c80bbbed70e744711a505aa252b19269c3655db04c1f73884280cf11cc1a483f2d4a24b64d231fbbcf
|
7
|
+
data.tar.gz: a75066c79654c3eb3812db62f5e59bd130c12b87de2229da69a3ad6ba82d116089e95807c3a204c9c93780c243cc21b92953d224c4c532a400c4e30e22986af1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,14 +4,14 @@ This package is a ruby native port of the numpy-financial package with some help
|
|
4
4
|
|
5
5
|
The functions in this package are a scalar version of their vectorised counterparts in the [numpy-financial](https://github.com/numpy/numpy-financial) library.
|
6
6
|
|
7
|
-
[](https://github.com/wowinter13/finance_rb/releases) [](http://makeapullrequest.com)
|
7
|
+
[](https://github.com/wowinter13/finance_rb/releases) [](http://makeapullrequest.com) [](https://codeclimate.com/github/wowinter13/finance_rb/maintainability)
|
8
8
|
|
9
9
|
Currently, only some functions are ported,
|
10
10
|
which are as follows:
|
11
11
|
|
12
12
|
| numpy-financial function | ruby native function ported? | info|
|
13
13
|
|:------------------------: |:------------------: | :------------------|
|
14
|
-
| fv |
|
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
17
|
| ppmt | | Computes principal payment for a loan|
|
@@ -20,4 +20,32 @@ which are as follows:
|
|
20
20
|
| rate | | Computes the rate of interest per period|
|
21
21
|
| irr | ✅ | Computes the internal rate of return|
|
22
22
|
| npv | ✅ | Computes the net present value of a series of cash flow|
|
23
|
-
| mirr | ✅ | Computes the modified internal rate of return|
|
23
|
+
| mirr | ✅ | Computes the modified internal rate of return|
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
finance_rb is available as a gem, to install it just install the gem:
|
28
|
+
|
29
|
+
gem install finance_rb
|
30
|
+
|
31
|
+
If you're using Bundler, add the gem to Gemfile.
|
32
|
+
|
33
|
+
gem 'finance_rb'
|
34
|
+
|
35
|
+
Run `bundle install`.
|
36
|
+
|
37
|
+
## Running tests
|
38
|
+
|
39
|
+
bundle exec rspec spec/
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it ( https://github.com/wowinter13/finance_rb/fork )
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create a new Pull Request
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
MIT License. See LICENSE for details.
|
data/lib/finance/loan.rb
CHANGED
@@ -5,6 +5,7 @@ module Finance
|
|
5
5
|
PAYMENT_TYPE_MAPPING = { end: 0, beginning: 1 }.freeze
|
6
6
|
|
7
7
|
# @return [Float] The amount of loan request (I.e. a present value)
|
8
|
+
# You can use #pv method to calculate value if param is not defined.
|
8
9
|
# Defaults to 0.
|
9
10
|
attr_accessor :amount
|
10
11
|
|
@@ -27,16 +28,23 @@ module Finance
|
|
27
28
|
attr_accessor :duration
|
28
29
|
|
29
30
|
# @return [Float] Future value.
|
31
|
+
# You can use #fv method to calculate value if param is not defined.
|
30
32
|
# Defaults to 0.
|
31
33
|
attr_accessor :future_value
|
32
34
|
|
35
|
+
# @return [Float] The (fixed) periodic payment.
|
36
|
+
# You can use #pmt method to calculate value if param is not defined.
|
37
|
+
attr_accessor :payment
|
38
|
+
|
39
|
+
# Create a new Loan instance.
|
33
40
|
def initialize(**options)
|
34
41
|
initialize_payment_type(options[:ptype])
|
35
42
|
@nominal_rate = options.fetch(:nominal_rate, 0).to_f
|
36
43
|
@duration = options.fetch(:duration, 1).to_f
|
37
44
|
@amount = options.fetch(:amount, 0).to_f
|
38
45
|
@future_value = options.fetch(:future_value, 0).to_f
|
39
|
-
@
|
46
|
+
@payment = options[:payment]
|
47
|
+
@monthly_rate = @nominal_rate / 12
|
40
48
|
end
|
41
49
|
|
42
50
|
# Pmt computes the payment against a loan principal plus interest (future_value = 0).
|
@@ -44,6 +52,8 @@ module Finance
|
|
44
52
|
# a certain future value given an initial deposit,
|
45
53
|
# a fixed periodically compounded interest rate, and the total number of periods.
|
46
54
|
#
|
55
|
+
# Required Loan arguments: nominal_rate, duration, amount, future_value*
|
56
|
+
#
|
47
57
|
# @return [Numeric] The (fixed) periodic payment.
|
48
58
|
#
|
49
59
|
# @example
|
@@ -69,6 +79,36 @@ module Finance
|
|
69
79
|
(-future_value + amount * factor) / second_factor
|
70
80
|
end
|
71
81
|
|
82
|
+
# Fv computes future value at the end of some periods (duration).
|
83
|
+
# Required Loan arguments: nominal_rate, duration, payment, amount*
|
84
|
+
#
|
85
|
+
# @param payment [Float] The (fixed) periodic payment.
|
86
|
+
# In case you don't want to modify the original loan, use this parameter to recalculate fv.
|
87
|
+
#
|
88
|
+
# @return [Float] The value at the end of the `duration` periods.
|
89
|
+
#
|
90
|
+
# @example
|
91
|
+
# require 'finance_rb'
|
92
|
+
# Finance::Loan.new(nominal_rate: 0.05, duration: 120, amount: -100, payment: -200).fv
|
93
|
+
# #=> 15692.928894335748
|
94
|
+
#
|
95
|
+
# @see http://www.oasis-open.org/committees/documents.php?wg_abbrev=office-formulaOpenDocument-formula-20090508.odt
|
96
|
+
# @see [WRW] Wheeler, D. A., E. Rathke, and R. Weir (Eds.) (2009, May).
|
97
|
+
# Open Document Format for Office Applications (OpenDocument)v1.2,
|
98
|
+
# Part 2: Recalculated Formula (OpenFormula) Format - Annotated Version,
|
99
|
+
# Pre-Draft 12. Organization for the Advancement of Structured Information
|
100
|
+
# Standards (OASIS). Billerica, MA, USA. [ODT Document].
|
101
|
+
def fv(payment: nil)
|
102
|
+
raise ArgumentError, 'no payment given' if self.payment.nil? && payment.nil?
|
103
|
+
|
104
|
+
final_payment = payment || self.payment
|
105
|
+
|
106
|
+
factor = (1.0 + monthly_rate)**duration
|
107
|
+
second_factor = (factor - 1) * (1 + monthly_rate * ptype) / monthly_rate
|
108
|
+
|
109
|
+
-((amount * factor) + (final_payment.to_f * second_factor))
|
110
|
+
end
|
111
|
+
|
72
112
|
private
|
73
113
|
|
74
114
|
def initialize_payment_type(ptype)
|
data/lib/finance/version.rb
CHANGED
data/spec/finance/loan_spec.rb
CHANGED
@@ -38,4 +38,36 @@ RSpec.describe Finance::Loan do
|
|
38
38
|
expect(loan.pmt).to eq(17_449.90775727763)
|
39
39
|
end
|
40
40
|
end
|
41
|
+
|
42
|
+
describe '#fv' do
|
43
|
+
context 'with loan arguments' do
|
44
|
+
it 'calculates correct fv value' do
|
45
|
+
loan = Finance::Loan.new(nominal_rate: 0.05, duration: 120, amount: -100, payment: -100)
|
46
|
+
expect(loan.fv).to eq(15_692.928894335748)
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'with :ptype' do
|
50
|
+
it 'calculates correct fv value' do
|
51
|
+
loan = Finance::Loan.new(
|
52
|
+
nominal_rate: 0.9, duration: 20, amount: 0, payment: -2000, ptype: :beginning
|
53
|
+
)
|
54
|
+
expect(loan.fv).to eq(93_105.06487352113)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with an optional :payment argument' do
|
60
|
+
it 'calculates correct fv value' do
|
61
|
+
loan = Finance::Loan.new(nominal_rate: 0.05, duration: 120, amount: -100, payment: -200)
|
62
|
+
expect(loan.fv(payment: -100)).to eq(15_692.928894335748)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'w/o any payments' do
|
67
|
+
it 'raises an ArgumentError exception w/o loan arguments' do
|
68
|
+
loan = Finance::Loan.new(nominal_rate: 0.05, duration: 120, amount: -100)
|
69
|
+
expect { loan.fv }.to raise_error(ArgumentError, "no payment given")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
41
73
|
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.1
|
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-
|
11
|
+
date: 2021-03-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
|