Giter VIP home page Giter VIP logo

exonio's Introduction

Exonio

This gem brings some useful Excel formulas to Ruby. For now, it just has financial formulas, but could have more (like statistical formulas) in the future.

Gem Version Tests

Installation

Add this line to your application's Gemfile:

gem 'exonio'

And then execute:

$ bundle

Or install it yourself as:

$ gem install exonio

Usage

To use Exonio you just have to call the method you like to use. Example:

 Exonio.pmt(0.075 / 12, 12 * 15, 200_000) # ==> -1854.0247200054619

Available Formulas

EFFECT

The Excel EFFECT function returns the effective annual interest rate, given a nominal interest rate and the number of compounding periods per year. Effective annual interest rate is the interest rate actually earned due to compounding. More about this function EFFECT

Exonio.effect(0.05, 10 * 12) # ==> 0.05126014873337037

NOMINAL

The Excel NOMINAL function returns the nominal interest rate when given an effective annual interest rate and the number of compounding periods per year. The effective rate is the actual rate due to compounding. The nominal rate is typically the stated rate. More about this function NOMINAL

Exonio.nominal(0.05, 12 / 6) # ==> 0.04939015319191986

FV

What is the future value after 10 years of saving $100 now, with an additional monthly savings of $100 with the interest rate at 5% (annually) compounded monthly?

Exonio.fv(0.05 / 12, 10 * 12, -100, -100) # ==> 15692.928894335748

By convention, the negative sign represents cash flow out (i.e. money not available today). Thus, saving $100 a month at 5% annual interest leads to $15,692.93 available to spend in 10 years.

IRR

Suppose one invests 100 units and then makes the following withdrawals at regular (fixed) intervals: 39, 59, 55, 20. Assuming the ending value is 0, one's 100 unit investment yields 173 units; however, due to the combination of compounding and the periodic withdrawals, the "average" rate of return is neither simply 0.73/4 nor (1.73)^0.25-1.

Exonio.irr([-100, 39, 59, 55, 20]) # ==> 0.28095

So, the internal rate of return is 28.09%

IPMT

What is the interest part of a payment in the 8th period (i.e., 8th month), having a $5,000 loan to be paid in 2 years at an annual interest rate of 7.5%?

Exonio.ipmt(0.075 / 12, 8, 12 * 2, 5_000.00) # ==> -22.612926783996798

So, in the 8th payment, $22.61 are the interest part.

NPER

If you only had $150/month to pay towards the loan, how long would it take to pay-off a loan of $8,000 at 7% annual interest?

Exonio.nper(0.07 / 12, -150, 8000) # ==> 64.07334877066185

So, over 64 months would be required to pay off the loan.

NPV

Calculates the Net Present Value of an investment

Exonio.npv(0.281, [-100, 39, 59, 55, 29]) # ==> -0.00661872883563408

PMT

What is the monthly payment needed to pay off a $200,000 loan in 15 years at an annual interest rate of 7.5%?

Exonio.pmt(0.075 / 12, 12 * 15, 200_000) # ==> -1854.0247200054619

In order to pay-off (i.e., have a future-value of 0) the $200,000 obtained today, a monthly payment of $1,854.02 would be required. Note that this example illustrates usage of fv (future value) having a default value of 0.

PV

What is the present value (e.g., the initial investment) of an investment that needs to total $20,000.00 after 10 years of saving $100 every month? Assume the interest rate is 5% (annually) compounded monthly.

Exonio.pv(0.05 / 12, 12 * 10, -100, 20_000) # ==> -2715.0857731569663

By convention, the negative sign represents cash flow out (i.e., money not available today). Thus, to end up with $20,000.00 in 10 years saving $100 a month at 5% annual interest, an initial deposit of $2715,09 should be made.

RATE

Suppose you take a loan of $50,000.00 to pay in 3 years with a monthly payment of $2,500.00. What is the rate applied to this loan?

Exonio.rate(12 * 3, 2_500, -50_000) # ==> 0.036006853458478955

So, the rate applied is 3.60%.

Statistical formulas

Sum

Exonio.sum([1, 2, 3, 4, 5]) # ==> 15

Mean

Exonio.mean([1, 2, 3, 4, 5]) # ==> 3.0

Median

Exonio.median([1, 2, 3, 6, 5, 4]) # ==> 3.5

TODO

There's a lot of formulas to be implemented, including:

  • ACCRINT
  • ACCRINTM
  • AMORDEGRC
  • AMORLINC
  • DB
  • DDB
  • MIRR
  • PPMT
  • SLN
  • SYD
  • VDB

So feel free to pick one of those and open a pull request \o/.

Contributing

  1. Fork the repository
  2. Create a branch
  3. Hack hack hack...
  4. Create a spec
  5. Open a Pull Request ;)

License

Exonio is released under the MIT License.

Special Thanks

A special thanks goes to the python NumPy project, which was the source for most of the formulas.

exonio's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

exonio's Issues

pmt returns NaN if interest rate is 0

Hi there!

Thanks for all your work on this gem. It's saved me a lot of time in my project.

I'm trying to use Exonio.pmt with an interest rate of 0 and I'm getting an unexpected result:

(byebug) interest_rate.to_s
"0.0"
(byebug) Exonio.pmt(interest_rate / 12, loan.amortization_schedule - month + 1, 0 - beginning_contract_balance)
#<BigDecimal:7ffd1dc47350,'NaN',9(45)>

In excel, it seems to work if I use a rate of 0:

screen shot 2018-07-13 at 11 17 17 am

I don't know enough about these formulas so I don't know what's the best solution.

What could be the best patch for this?

Please let me know.

Thanks!

irr returns Failed to reduce function values.

The irr formula works fine most of the time, but for some specific cashflows nlsolve returns this error:

[1] pry(#<Loan>)> Exonio.irr(cashflows)
RuntimeError: Failed to reduce function values.
from /Users/lubc/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/bigdecimal/newton.rb:68:in `nlsolve'
[2] pry(#<Loan>)> cashflows.map(&:to_f)
[-890000.0,9551.37725951421,9493.7890635608,9436.54808458176,9379.652229099,9323.09941625663,9266.88757774489,9211.0146577245,9155.47861275147,9100.27741170236,9045.40903569999,8990.87147803963,8936.66274411557,8882.78085134823,8829.22382911156,8775.98971866106,8723.07657306209,8670.48245711869,8618.20544730277,8566.24363168381,8514.59510985889,8463.25799288321,8412.23040320101,8361.51047457688,8311.09635202751,8260.98619175387,8211.17816107377,8161.67043835479,8112.46121294775,8063.54868512038,8014.93106599158,7966.60657746595,7918.57345216877,7870.8299333814,7823.37427497695,7776.2047413565,7729.31960738559,7682.71715833111,7636.39568979862,7590.35350766997,7544.58892804143,7499.10027716198,7453.88589137218,7408.94411704334,7364.27331051696,7319.8718380447,7275.73807572856,7231.87040946155,7188.26723486859,7144.92695724792,7101.84799151269,7059.02876213306,7016.46770307851,6974.16325776062,6932.11387897611,6890.31802885029,6848.77417878076,6807.48080938156,6766.43641042754,6725.63948079922,6685.08852842777,6644.78207024055,6604.7186321068,6564.89674878373,6525.314963863,6485.97182971736,6446.86590744777,6407.99576683076,6369.3599862661,6330.95715272485,6292.78586169762,6254.84471714328,6217.13233143784,6179.64732532373,6142.38832785933,6105.35397636888,6068.5429163926,6031.95380163715,5995.58529392643,5959.4360631526,5923.50478722747,5887.79015203411,5852.29085137879,5817.00558694325,5781.93306823719,5747.07201255106,5712.42114490917,5677.97919802305,5643.74491224511,5609.71703552255,5575.89432335158,5542.27553873193,5508.85945212155,5475.64484139172,5442.63049178227,5409.81519585722,5377.19775346058,5344.77697167248,5312.55166476553,5280.52065416144,5248.68276838795,5217.03684303597,5185.58172071697,5154.31625102068,5123.23929047304,5092.34970249429,5061.64635735751,5031.12813214721,5000.79391071834,4970.6425836554,4940.67304823194,4910.88420837014,4881.2749746008,4851.84426402346,4822.59100026679,4793.51411344926,4764.61254013994,4735.88522331969,4707.33111234248,4678.9491628969,4650.73833696805,4622.69760279952,4594.8259348557,4567.12231378422,4539.58572637873,4512.2151655418,4485.0096302481,4457.96812550779,4431.08966233013,4404.37325768731,4377.8179344785,4351.42272149411,4325.18665338028,4299.10877060355,4273.18811941579,4247.42375181932,4221.81472553222,4196.36010395387,4171.05895613072,4145.91035672223,4120.91338596701,4096.06712964919,4071.370679065,4046.82313098952,4022.42358764365,3998.17115666129,3974.06495105667,3950.10408919195,3926.28769474495,3902.61489667708,3879.08482920156,3855.69663175167,3832.44944894931,3809.34243057374,3786.37473153044,3763.54551182024,3740.85393650859,3718.29917569499,3695.8804044827,3673.59680294851,3651.4475561128,3629.43185390969,3607.54889115744,3585.79786752904,3564.17798752285,3542.68846043357,3521.3285003233,3500.09732599282,3478.99416095298,3458.01823339634,3437.16877616892,3416.44502674212,3395.8462271849,3375.37162413601,3355.02046877643,3334.79201680202,3314.68552839626,3294.70026820323,3274.83550530068,3255.09051317333,3235.46456968627,3215.9569570586,3196.56696183709,3177.29387487017,3158.13699128196,3139.09561044648,3120.16903596208,3101.35657562587,3082.65754140851,3064.07124942898,3045.59701992961,3027.23417725118,3008.98204980822,2990.83997006448,2972.80727450848,2954.88330362925,2937.06740189221,2919.35891771522,2901.75720344472,2884.26161533206,2866.87151350991,2849.58626196896,2832.40522853454,2815.32778484359,2798.35330632165,2781.48117215997,2764.71076529288,2748.04147237519,2731.47268375975,2715.00379347515,2698.63419920359,2682.3633022588,2666.19050756421,2650.11522363113,2634.13686253712,2618.25483990455,2602.46857487913,2586.77749010878,2571.18101172238,2555.67856930894,2540.26959589657,2524.9535279319,2509.72980525934,2494.59787110069,2479.55717203472,2464.60715797694,2449.74728215952,2434.97700111122,2420.29577463759,2405.70306580116,2391.19834090182,2376.78106945731,2362.45072418381,2348.20678097666,2334.04871889118,2319.97602012364,2305.98816999229,2292.08465691856,2278.26497240835,2264.52861103344,213065.861366743]

Any idea what can be the problem? Thanks!

Exonio.irr is slowly with Ruby 3.2.2

Hi!

I have the following gist code:

https://gist.github.com/gemitoytp/eca40ae2c264f43cbebf96b6eb7338e4

If I use Ruby 3.1.4 is more fastest that Ruby 3.2.2 with exonio-0.7.0, check the following benchmark result:

Ruby 3.1.4

$ ruby -v                                                                        
ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [arm64-darwin22]

$ ruby exonio_irr_benchmark.rb
       user     system      total        real
Exonio.irr  0.001008   0.000062   0.001070 (  0.001062)

Ruby 3.2.2

$ ruby -v
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [arm64-darwin22]

$ ruby exonio_irr_benchmark.rb
       user     system      total        real
Exonio.irr 86.791901   0.497656  87.289557 ( 87.318323)

I have an Apple M2 Pro with 16 GB of RAM

Error with nper function

I am using the nper function and works fine most of the times.

But for this specific values:
Exonio.nper(0.0575 / 12.0, -76.0, 943183.21)

I am having an error:
Math::DomainError Exception: Numerical argument is out of domain - "log"

Any idea about what can be the problem? Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.