Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 917899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:17:06+00:00 2026-05-15T18:17:06+00:00

I’m making an application that gives clients and approximate loan offer (they are later

  • 0

I’m making an application that gives clients and approximate loan offer (they are later calculated by other back-office systems).
I have received some code from the financial firm that we are making the calculator for.
My problem is that I do not understand the part of the code that calculates the annual percentage rate (including startup and monthly fees).

It might be this method they are using, but I can’t really tell:
http://www.efunda.com/math/num_rootfinding/num_rootfinding.cfm#Newton_Raphson

The code works correctly, but I really hate building an application on code that I don’t fully understand and/or trust.
The ultimate reply would be source-code which does the same thing, but with comments and understandable variable names (I’m not really excepting that 🙂 All ideas are welcome – maybe someone has a link to an article that explains it.

(please note that I’m by no means a math or financial wiz)

[snip]
int n = numberOfPayments;
double a = (amount / (monthlyPayment * Math.Pow(n, 2)) - (monthlyPayment / amount));
double d = 0;
if (a == 0)
{
    d = 0;
}
else
{
    for (int qq = 0; qq < 20; qq++)
    {
        double b = amount + (monthlyPayment / a) * (1 - (Math.Pow((1 + a), -n)));
        double c = amount + (monthlyPayment / a) * ((n * (Math.Pow((1 + a), (-n - 1)))) - ((1 - (Math.Pow((1 + a), -n))) / a));
        d = a - (b / c);
        double aa = a;
        double dd = d;
        a = d;
        if (Math.Abs(aa - dd) < Math.Pow(10, -5)) { break; }
    }
}
double apr = ((Math.Pow((1 + d), 12)) - 1) * 100;
apr = Math.Round(apr * 100) / 100;
[/snip]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:17:06+00:00Added an answer on May 15, 2026 at 6:17 pm

    The code is indeed using the Newton-Raphson method although I have no idea what exactly it is calculating; you may have copied from the wrong section. If, indeed, you want to calculate the annual percentage rate given the loan amount, the monthly payment and the number of months then you have nearly completely solved this except that you probably don’t know what the function is whose roots are being searched for and this is, understandably, a stumbling block.

    The value that is being searched is called the internal rate of return (IRR) for which there is no closed form; you have to calculate it the hard way or use numerical methods. Calculating the annual percentage rate is a special case of the IRR where all the payments are equal and the loan runs to term. That means that the equation is the following:

    P is the principal/loan amount, m is monthly payment, i is the interest rate, N is number of months

    0 = P - Sum[k=1..N](m*(1+i)^(-k))
    

    And we have to solve for i. The above equation is equivalent to:

    P = Sum[k=1..N](m*(1+i)^(-k))
    P = m * Sum[k=1..N]((1+i)^(-k))  // monthly payments all the same
    P/m = Sum[k=1..N]((1+i)^(-k))
    

    There are some formulas to get the closed form for the sum on the right hand side which result in the following equation which relates all the quantities that we know already (term, loan, and monthly payment amount) and which is far more tractable:

    monthlyPayment = loanAmount * interestRate * ((1 + interestRate)^numberOfPayments)/(((1 + interestRate)^numberOfPayments) - 1)
    

    To reduce typing let:

    • P is the principal/loan amount
    • m is recurring payment amount
    • N is total number of payments

    So the equation whose roots we have to find is:

    F(x) = P * x * ((1 + x)^N)/(((1 + x)^N) - 1) - m 
    

    To use the Newton-Rhapson method we need the first derivative of F with respect to x:

    F_1(x) = P * ( (1 + x)^N/(-1 + (1 + x)^N) - ((N * x * (1 + x)^(-1 + 2*N))/(-1 + (1 + x)^N)^2) + (N * x * (1 + x)^(-1 + N))/(-1 + (1 + x)^N) )
    

    The following code in Groovy does the proper calculation:

    numPay = 360
    payment = 1153.7
    amount = 165000
    double error = Math.pow(10,-5)
    double approx = 0.05/12 // let's start with a guess that the APR is 5% 
    double prev_approx
    
    def F(x) {
      return amount * x * Math.pow(1 + x,numPay)/(Math.pow(1 + x,numPay) - 1) - payment
    }
    
    def F_1(x) {
      return amount * ( Math.pow(1 + x,numPay)/(-1 + Math.pow(1 + x,numPay)) - numPay * x * Math.pow(1 + x,-1 + 2*numPay)/Math.pow(-1 + Math.pow(1 + x,numPay),2) + numPay * x * Math.pow(1 + x,-1 + numPay)/(-1 + Math.pow(1 + x,numPay))) 
    }
    
    
    println "initial guess $approx"
    for (k=0;k<20;++k) {
           prev_approx = approx
           approx = prev_approx - F(prev_approx)/F_1(prev_approx)
           diff = Math.abs(approx-prev_approx)
           println "new guess $approx diff is $diff"
           if (diff < error) break
    }
    
    apr = Math.round(approx * 12 * 10000)/100 // this way we get APRs like 7.5% or 6.55%
    println "APR is ${apr}% final approx $approx "
    

    I did not use the provided code since it was a bit murky (plus it did not work for me). I derived this from the definitions of Newton-Rhapson and monthly mortage payments equation. The approximation converges very quickly (10^-5 within 2 or 3 iterations)

    NOTE: I am not able to get this link to be properly inserted for the text where the first derivative is first mentioned: http://www.wolframalpha.com/input/?i=d/dx(+x+*+((1+%2B+x)^n)/(((1+%2B+x)^n)+-+1)+-m+)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.