Modify code to compute APR based on multiple payment stream

Scot King 116 Reputation points
2022-10-15T03:02:36.243+00:00

I have a page that does complex amortization schedules and I need to be able to compute the APR just exactly as the FFEIC website tool here: https://www.ffiec.gov/examtools/FFIEC-Calculators/APR/#/accountdata

If you click the Help link, there is a download pdf file of the APR tool formulas, and the formula is in the other calculations section at the bottom of the document. On page 6 of the document is an example of a loan amount of $975 (loan $1000-finance charge of $25) and 3 payments of $340. I was able to get the code below to compute the APR of 27.48% as it shows on page 9. This is one stream of 3 payments of $340. (the same payment amount)

The APR tool takes varying payments streams. It takes an amount and the number of the payments. So for example on a 30 year mortgage there are 360 months. There could be 360 different monthly payments and the code has to be able to accept payment streams and compute the APR. As I said above, the code below does in fact compute the correct answer for 1 stream of of payments. How can it be modified to accept multiple payment streams?

 Public Overridable Function generalEquation(ByVal period As Integer, ByVal payment As Double, ByVal initialPeriods As Double, ByVal fractions As Double, ByVal rate As Double) As Double  
        Dim retval As Double = 0  
        For x As Integer = 0 To period - 1  
            retval += payment / ((1.0 + fractions * rate) * Math.Pow(1 + rate, initialPeriods + x))  
        Next x  
        Return retval  
    End Function  
  
    '''   
    ''' <param name="amount"> The initial amount A </param>  
    ''' <param name="payment"> The periodic payment P </param>  
    ''' <param name="payments"> The total number of payments n </param>  
    ''' <param name="ppy"> The number of payment periods per year </param>  
    ''' <param name="APRGuess"> The guess to start estimating from, 10% is 0.1, not 0.001 </param>  
    ''' <param name="odddays"> Odd days, as a fraction of a pay period.  10 days of a month is 0.33333... </param>  
    ''' <param name="full"> Full pay periods before the first payment.  Usually 1. </param>  
    ''' <returns> The calculated APR </returns>  
    Public Overridable Function findAPRGEQ(ByVal amount As Double, ByVal payment As Double, ByVal payments As Integer, ByVal ppy As Double, ByVal APRGuess As Double, ByVal odddays As Double, ByVal full As Double) As Double  
        Dim result As Double = APRGuess  
        Dim tempguess As Double = APRGuess  
  
        Do  
            result = tempguess  
            'Step 1  
            Dim i As Double = tempguess / (100 * ppy)  
            Dim A1 As Double = generalEquation(payments, payment, full, odddays, i)  
            'Step 2  
            Dim i2 As Double = (tempguess + 0.1) / (100 * ppy)  
            Dim A2 As Double = generalEquation(payments, payment, full, odddays, i2)  
            'Step 3  
            tempguess = tempguess + 0.1 * (amount - A1) / (A2 - A1)  
            Console.WriteLine(tempguess)  
        Loop While Math.Abs(result * 10000 - tempguess * 10000) > 1  
        Return result  
    End Function
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,753 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Scot King 116 Reputation points
    2022-10-15T14:13:41.157+00:00

    Sure, if you click the link to the APR tool, click the continue button at the bottom and on the next page, enter 975 for the amount financed, and a number for the disclosed apr. Leave installment loan radio button and monthly as is and click continue. On this next page input 340 for the payment and 3 for the number of payments. This is the one and only payment stream for this loan. You will see that there is a link to add a payment stream. Unit periods will auto calc as you enter additional payment streams. Leave the odd days blank. This is for extra days for when a loan begins on a day that is not the 1st of the month or the last day of the month. Once you click continue the tool calculates the apr and displays the information on the next page. The response agrees with the calculation in the pdf document (page 5) which as I mentioned you can find through the help link.

    If you look at the code, the step 1 step 2 step 3 are found on page 39 of the document. It does interpolation to derive the apr using iteration to solve the general equation. So if you feed this into the code you will get the same answer 27.48% for the 1 payment stream of 3 payments of 340 on a loan of $1000 with a $25 finance charge.
    Dim apr1 As Double = 0
    Dim amount As Double = Convert.ToDouble(txtamtfin.Text)
    'apr1 = findAPRGEQ(amount, pmtarray(0), pmtterm(0), 12, (Noterate / 12) + 0.1, 0, 1)
    apr1 = findAPRGEQ(975,340, 3, 12, 10, 0, 1)

    In my code, I created arrays, pmtarray to store the payments and pmtterm to store the number of these payments. I could loop through these 2 arrays and feed each one into the findAPRGEQ but I'm unclear on how to calculate the final result. According to the general equation shown on page 8 and 9, you are supposed to solve for i by using 975=340/(1+i) power 1 + 340/(1+i) power 2 + 340/(1+i) power 3
    This is a simple example using the same payment 340 for 3 payments. Assuming the loan was for 6 year, the equation would be 975= payment 1/(1+i) power 1 +payment 2/(1+i) power 2 + ... payment12/(1+i) power 6

    This is the equation for the loan above shown on page 9 of the documents. Extending this out to 6 payments and changing the payments to $100 X 3 and $300 X 3 ($1200 total) it would like this:
    975 = 100/(1+i) power 1 + 100/(1+i) power 2 + 100/(1+i) power 3 +300/(1+i) power 4 + 300/(1+i) power 5 + 300/(1+i) power 6

    How can the code be modified to accept more than 1 payment stream? Should each payment stream be fed into the function with a loop and store each result into an array and when finished do another loop to add up all the numbers in the result array?

    The tool gives these results for these 2 payments streams:
    Amount Financed
    975. 00
    Finance Charge
    225. 00
    Total of Payments
    1,200.00
    Annual Percentage Rate
    60. 9555
    Loan Regularity
    Irregular


  2. Scot King 116 Reputation points
    2022-10-16T14:52:23.56+00:00

    "You are likely much more qualified to understand financial facts/figures than I am, so I would have hoped you would keep things orientated around the coding."

    You need to understand the issue in order to create code. And yes you need to look at the tool and read the document to understand the code I provided and how to amend it to work with more than 1 payment stream.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.