marksweston / finance

A library for financial calculations in Ruby
https://rubygems.org/gems/finance
Other
217 stars 93 forks source link

Add Execl's RATE function. Adding annual_fee to payment calculations (dupe function though - didn't touch yours) #14

Closed wiseleyb closed 11 years ago

wiseleyb commented 11 years ago

Also made a few tweaks so this works out of the box in Rails/Rails-Console. I couldn't get your examples like 250000.amortize(rate) working (ruby 1.9.3, Rails 3.2.8).

Added tests. All tests pass.

Be happy to do whatever tweaks you want.

== Effective Interest Rate

Implementation of Execl's RATE function

RATE RATE(NPER, PMT, PV, FV, type, guess)
Returns the constant interest rate per period of an annuity. => NPER is the total number of periods, during which payments are made (payment period). => PMT is the constant payment (annuity) paid during each period. => PV is the cash value in the sequence of payments. => FV (optional) is the future value, which is reached at the end of the periodic payments. => Type (optional) defines whether the payment is due at the beginning (1) or the end (0) of a period. => Guess (optional) determines the estimated value of the interest with iterative calculation. see test/test_effective_interest_rate.rb for examples adapted from Java code found here: http://www.pcpros.com/software/programming/java/java.shtml Example: Finance::EffectiveInterestRate.calc_effective_interest_rate(360, -1433.39, 200000)

== Financial Payments with annual fees

This will return identical results that Finance::Amortization will given annual_fee = 0 Simply adds ability to use annual_fee

Finance::EffectiveInterestRate.calc_payment(principal, rate, periods, annual_fee = 0)
wkranec commented 11 years ago

Hi Ben,

First off, thanks a lot for sending this! There are several users who have asked about behavior with Rails, and I just haven't had time to look into it.

Just out of curiosity, which tweaks address the Rails issues? I noticed a #to_i method and a few Math. additions, but didn't see anything that seemed to specifically target Rails.

Most of my thoughts at this point are trivialities:

  1. Should EffectiveInterestRate be a subclass of Rate?
  2. Should we use the built in Ruby solver instead of the secant method?

Give me another day or two to digest the diffs; I'll probably release all of this sometime over the weekend.

Thanks again for contributing, and for being a finance user!

Bill

fairloan-devops commented 11 years ago

+1 This would be a great addition.

Would it be possible not to use single variable names, and split the code into more unit testable and readable code (don't gate integration on this).

mikel commented 11 years ago

Hi there Bill,

I work with Ben, we've actually found an error with this algorithm that fails on high interest rate values. Please close this pull request.

I am going to get one of my other team (@ab9) to implement a new effective interest rate method on the rates class and preferably using the ruby solver as you suggest, should have it done soon.

wkranec commented 11 years ago

Hi Mikel, Ben, & Aaron,

I've merged the metadata_cleanup pull request into a release branch for 1.1.3. I would also like to include some of the fixes in Ben's pull request related to the behavior of finance under Rails before releasing a new gem.

Is there any chance of opening a second pull request so that I can incorporate these fixes? Thanks!