-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from randallreedjr/add-interest-rate
Add ability to calculate interest rate
- Loading branch information
Showing
11 changed files
with
155 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Changelog | ||
|
||
## 0.1.0 | ||
|
||
* 1 minor enhancement | ||
* Update docs, add License and Changelog | ||
* 2 major enhancement: | ||
* Add ability to calculate interest rate (I) | ||
|
||
## 0.1.0 | ||
|
||
Initial project; ability to calculate present value (PV), payments (PMT), future value (FV), and number of periods (N) given other values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
LICENSE | ||
|
||
The MIT License | ||
|
||
Copyright (c) 2014-2016 Randall Reed, Jr. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
Time Value of Money calculator | ||
# Time Value of Money calculator | ||
|
||
Performs financial calculatins for: | ||
-n (number of periods) | ||
-pv (present value) | ||
-pmt (payment amount) | ||
-fv (future value) | ||
## Performs financial calculations for: | ||
* n (number of periods) | ||
* pv (present value) | ||
* pmt (payment amount) | ||
* fv (future value) | ||
* i (interest rate) | ||
|
||
Does not currently solve for interest rate. Only supports annuity due. | ||
**NOTE:** Only supports annuity due. | ||
|
||
## Usage | ||
|
||
Usage | ||
Initialize with values: | ||
time_value = TimeValue.new(n, i, pv, pmt, fv) | ||
``` | ||
time_value = TimeValue.new(n, i, pv, pmt, fv) | ||
``` | ||
|
||
Or set values after initialization: | ||
time_value = TimeValue.new() | ||
time_value.n = my_n | ||
time_value.i = my_i | ||
time_value.pv = my_pv | ||
time_value.pmt = my_pmt | ||
my_fv = time_value.calc_fv() | ||
``` | ||
time_value = TimeValue.new() | ||
time_value.n = my_n | ||
time_value.i = my_i | ||
time_value.pv = my_pv | ||
time_value.pmt = my_pmt | ||
my_fv = time_value.calc_fv() | ||
``` | ||
|
||
Remember to follow a convention for signs of values: | ||
-Money being paid should have a negative value (-) | ||
-Money being received should have a positive value (+) | ||
|
||
* Money being paid should have a negative value (-) | ||
* Money being received should have a positive value (+) | ||
|
||
Failing to follow this convention may yield unexpected results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require_relative '../lib/timevalue.rb' | ||
require_relative '../lib/time_value.rb' | ||
require_relative '../lib/solver.rb' | ||
require 'rspec' | ||
require_relative '../spec/spec_helper.rb' | ||
require_relative '../spec/spec_helper.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class Solver | ||
attr_reader :time_value, :goal | ||
attr_accessor :guess, :lower_bound, :upper_bound, :upper_cap_met | ||
|
||
def initialize(time_value, guess = 10.00, lower_bound = 0.00, upper_bound = 10.00) | ||
@upper_bound = upper_bound | ||
@lower_bound = lower_bound | ||
@guess = guess | ||
@time_value = time_value | ||
@goal = time_value.fv | ||
@upper_cap_met = false | ||
end | ||
|
||
def solve! | ||
iteration_count = 0 | ||
time_value.i = guess | ||
while (upper_bound - lower_bound).round(2) > 0.01 && iteration_count < 100 | ||
iteration_count += 1 | ||
begin | ||
result = time_value.calc_fv | ||
rescue FloatDomainError => error | ||
return nil | ||
end | ||
adjust_bounds(result) | ||
time_value.i = guess | ||
end | ||
guess if iteration_count < 100 | ||
end | ||
|
||
def adjust_bounds(result) | ||
if result > goal | ||
# interest rate too high | ||
self.upper_cap_met = true | ||
self.upper_bound = guess | ||
elsif result < goal | ||
# interest rate too low | ||
self.upper_bound *= 2 unless upper_cap_met | ||
self.lower_bound = guess | ||
end | ||
self.guess = ((upper_bound + lower_bound) / 2).round(2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'time_value' | ||
s.version = '0.1.0' | ||
s.date = '2016-03-27' | ||
s.summary = "Time value of money financial calculations" | ||
s.description = "Perform time value of money calculations using financial calculator variables n, i, PV, PMT, and FV" | ||
s.authors = ["Randall Reed, Jr."] | ||
s.email = 'randallreedjr@gmail.com' | ||
s.add_development_dependency "rspec", '~> 3.2' | ||
s.files = Dir.glob("{bin,lib}/**/*") + %w(LICENSE README.md CHANGELOG.md) | ||
s.homepage = 'http://rubygems.org/gems/timevalue' | ||
s.license = 'MIT' | ||
end |
Binary file not shown.
This file was deleted.
Oops, something went wrong.