finance_math 0.0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -1
- data/finance_math.gemspec +1 -1
- data/lib/finance_math/loan.rb +55 -2
- data/lib/finance_math/version.rb +1 -1
- data/spec/lib/loan_spec.rb +38 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbdffc4856090cc7d1e509ba4e9d0489017a7e44
|
4
|
+
data.tar.gz: 577779f78950688be481d7446c4fda4135f4e027
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c12c0a09ab26a16c7e8a7ef3bfa2b7964e3a284f1b371607fe7552dfc0fd90230c2b911cff9c3956534fd7ce7993b4012c820ccb03bed40d2617fd73814e26e
|
7
|
+
data.tar.gz: d31f8f44e884db52c0ac328391fdf9eadcc36f33222bdab02352b079441b16186b616c0c277db5abd7027f5b2a5496ac44e2afece6741294ae13930fbd1eb2d6
|
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
![Gem Version](https://img.shields.io/badge/finance_math-0.2-blue.svg)
|
2
|
-
![Build Status](https://
|
2
|
+
[![Build Status](https://semaphoreapp.com/api/v1/projects/869d7630-55d3-46e5-9dc2-03d0d1cfecfe/363108/shields_badge.svg)](https://semaphoreapp.com/kolosek/finance_math)
|
3
|
+
[![Code Climate](https://codeclimate.com/github/kolosek/finance_math/badges/gpa.svg)](https://codeclimate.com/github/kolosek/finance_math)
|
3
4
|
|
4
5
|
|
5
6
|
## What is FinanceMath?
|
@@ -41,6 +42,17 @@ loan = Loan.new(10.5, 12, 15000)
|
|
41
42
|
loan.pmt
|
42
43
|
```
|
43
44
|
|
45
|
+
## APR
|
46
|
+
|
47
|
+
Calculates the Annual Percentage Rate.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
|
51
|
+
loan = Loan.new(10.5, 12, 15000)
|
52
|
+
|
53
|
+
loan.apr
|
54
|
+
```
|
55
|
+
|
44
56
|
## Contributing
|
45
57
|
|
46
58
|
1. Fork it ( https://github.com/kolosek/finance_math/fork )
|
@@ -60,3 +72,5 @@ MIT License. See LICENSE for details.
|
|
60
72
|
## Copyright
|
61
73
|
|
62
74
|
Copyright (c) 2014-2015 Nebojsa Zoric, and Kolosek, Inc. (http://kolosek.com)
|
75
|
+
|
76
|
+
For any Ruby on Rails related work, please contact directly via form on http://kolosek.com
|
data/finance_math.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Nebojsa Zoric"]
|
10
10
|
spec.email = ["office@kolosek.com"]
|
11
11
|
spec.summary = %q{Finance library for Ruby.}
|
12
|
-
spec.description = %q{Implementation of
|
12
|
+
spec.description = %q{Implementation of Loan/Mortgage functions in Ruby language}
|
13
13
|
spec.homepage = ""
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/finance_math/loan.rb
CHANGED
@@ -18,6 +18,18 @@ module FinanceMath
|
|
18
18
|
# @api public
|
19
19
|
attr_reader :monthly_rate
|
20
20
|
|
21
|
+
# @return [DecNum] the currency protection
|
22
|
+
# @api public
|
23
|
+
attr_reader :currency_protection
|
24
|
+
|
25
|
+
# @return [DecNum] the fee for the bank/market
|
26
|
+
# @api public
|
27
|
+
attr_reader :structure_fee
|
28
|
+
|
29
|
+
# @return [DecNum] P principal
|
30
|
+
# @api public
|
31
|
+
attr_reader :principal
|
32
|
+
|
21
33
|
|
22
34
|
|
23
35
|
# create a new Loan instance
|
@@ -25,13 +37,16 @@ module FinanceMath
|
|
25
37
|
# @param [Numeric] decimal value of the interest rate
|
26
38
|
# @param [Integer] Duration of the loan period
|
27
39
|
# @param [Float] Loan amount
|
40
|
+
# @param [Float] structure fee - fee for the market in percentages
|
41
|
+
# @param [Float] currency protection - Protection for currency changes - usually 3%, default to 0%
|
28
42
|
# @example create a 10.5% Nominal rate
|
29
43
|
# Loan.new(10.5, 12, 1000)
|
30
44
|
# @see http://en.wikipedia.org/wiki/Nominal_interest_rate
|
31
45
|
# @api public
|
32
46
|
|
33
|
-
def initialize(nominal_rate, duration, amount)
|
34
|
-
@nominal_rate, @amount, @duration = nominal_rate.to_f, amount, duration
|
47
|
+
def initialize(nominal_rate, duration, amount, structure_fee=5, currency_protection=3)
|
48
|
+
@nominal_rate, @amount, @duration, @structure_fee, @currency_protection = nominal_rate.to_f, amount, duration, structure_fee.to_f, currency_protection.to_f
|
49
|
+
@principal = principal_calculation
|
35
50
|
@monthly_rate = @nominal_rate / 100 / 12
|
36
51
|
end
|
37
52
|
|
@@ -39,6 +54,38 @@ module FinanceMath
|
|
39
54
|
((@amount * interest(@monthly_rate, @duration) - future_value ) / ((1.0 + @monthly_rate * type) * fvifa(@monthly_rate, duration)))
|
40
55
|
end
|
41
56
|
|
57
|
+
def apr
|
58
|
+
pmt_base = pmt
|
59
|
+
n = duration.to_f / 12
|
60
|
+
q = (duration > 12) ? 12 : duration
|
61
|
+
i = pmt_base * n * q / principal - 1
|
62
|
+
#puts "n: #{n} q: #{q} i: #{i} pmt: #{pmt_base}"
|
63
|
+
find(pmt_base, n, q, i)
|
64
|
+
end
|
65
|
+
|
66
|
+
def principal_base(m, n, q, i)
|
67
|
+
m * ( 1 - ( 1 + ( i / q ) ) ** (- n * q) ) * q / i
|
68
|
+
end
|
69
|
+
|
70
|
+
def near(a, b)
|
71
|
+
r = 0.1
|
72
|
+
return ((a - r) < b && (a + r) > b) || ((b - r) < a && (b + r) > a)
|
73
|
+
end
|
74
|
+
|
75
|
+
def find(pmt_base, n, q, i)
|
76
|
+
p = principal_base(pmt_base, n, q, i)
|
77
|
+
|
78
|
+
return i if near(p, principal)
|
79
|
+
|
80
|
+
if p < principal
|
81
|
+
i -= 0.00001
|
82
|
+
elsif p > principal
|
83
|
+
i += 0.00001
|
84
|
+
end
|
85
|
+
|
86
|
+
find(pmt_base, n, q, i)
|
87
|
+
end
|
88
|
+
|
42
89
|
protected
|
43
90
|
|
44
91
|
def pow1pm1(x, y)
|
@@ -56,5 +103,11 @@ module FinanceMath
|
|
56
103
|
def fvifa(monthly_rate, duration)
|
57
104
|
(monthly_rate == 0) ? duration : pow1pm1(monthly_rate, duration) / monthly_rate
|
58
105
|
end
|
106
|
+
|
107
|
+
private
|
108
|
+
|
109
|
+
def principal_calculation
|
110
|
+
amount * (1 - currency_protection/100 - structure_fee / 100 )
|
111
|
+
end
|
59
112
|
end
|
60
113
|
end
|
data/lib/finance_math/version.rb
CHANGED
data/spec/lib/loan_spec.rb
CHANGED
@@ -27,5 +27,43 @@ describe Loan do
|
|
27
27
|
loan = Loan.new(0, 12, 1200)
|
28
28
|
expect(loan.pmt).to eq(100)
|
29
29
|
end
|
30
|
+
|
31
|
+
it "should return correct pmt value" do
|
32
|
+
loan = Loan.new(0, 36, 10000)
|
33
|
+
expect(loan.pmt).to eq(277.77777777777777)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return correct pmt value" do
|
37
|
+
loan = Loan.new(0, 6, 10000)
|
38
|
+
expect(loan.pmt).to eq(1666.6666666666667)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context ".apr" do
|
43
|
+
|
44
|
+
it "should return correct apr value" do
|
45
|
+
loan = Loan.new(16, 24, 10000)
|
46
|
+
expect(loan.apr).to eq(0.24699853524196447)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should return correct apr value" do
|
50
|
+
loan = Loan.new(13, 24, 10000)
|
51
|
+
expect(loan.apr).to eq(0.2159014588291408)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should return correct apr value" do
|
55
|
+
loan = Loan.new(13, 18, 10000)
|
56
|
+
expect(loan.apr).to eq(0.2418055150965281)
|
57
|
+
end
|
58
|
+
|
59
|
+
# it "should return correct apr value" do
|
60
|
+
# loan = Loan.new(13, 6, 10000)
|
61
|
+
# expect(loan.apr).to eq(0.2418055150965281)
|
62
|
+
# end
|
63
|
+
|
64
|
+
# it "should return correct apr value" do
|
65
|
+
# loan = Loan.new(13, 36, 10000)
|
66
|
+
# expect(loan.apr).to eq(0.2418055150965281)
|
67
|
+
# end
|
30
68
|
end
|
31
69
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: finance_math
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nebojsa Zoric
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,7 +52,7 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Implementation of
|
55
|
+
description: Implementation of Loan/Mortgage functions in Ruby language
|
56
56
|
email:
|
57
57
|
- office@kolosek.com
|
58
58
|
executables: []
|