Calculating interest rates using Newton’s method

Here’s a post I wrote for Microfinance Transparency on March 5, 2010

In a previous blog post, I described how to use a spreadsheet like Microsoft Excel to calculate interest rates. Again, interest rate calculations are at the core of MFTransparency’s ability to provide accurate data that can be compared across various products offered by numerous MFIs. In the last post we looked at Excel’s IRR and XIRR functions and concluded that XIRR is more accurate because it takes into account the actual payment dates of the loan and thus allows us to calculate annualized interest rates even with irregular repayment schedules.

But for the more technical among us, I realize that even this may not be sufficient. Today I’m going to demonstrate how to write a computer program that is as accurate as Excel 2007’s XIRR function. This article is likely to be of less broad interest, but it provides transparency into how we will calculate interest rates for future data collection trips; and it may be useful for MFIs that wish to automate interest rate calculations for a larger data set than can be handled with Excel.

Let’s start with the EIR formula and describe two techniques.

  • EIR = cf*(1+rate)^n

cf = cashflow, n=number of periods/year

Simple Guess and Check

The first technique is to make a guess about the interest rate and then run the numbers through the EIR formula to see how close you are. You then iterate, guessing somewhere in the middle of your previous guesses, or widening your area by doubling. The advantage of this technique is that it is simple and it gets you the right answer eventually (or at least fairly accurately given enough guesses).

Newton’s method

A more advanced way to solve the EIR formula is to use Calculus. It’s still a “guessing” technique, but it is much more efficient and elegant.

It’s easier to visualize this technique if we draw a graph and plot an initial guess, with the goal of finding the point on the graph where it crosses the x axis.

We start by making an initial guess and then figuring out what the “tangent” line at that point would be. This is the same thing as the derivative of the EIR calculation:

  • EIR = cf*(1+rate)^n
  • f’rate = cf*n*(rate+1)^(n-1) = pink tangent line is the derivative

cf = cashflow, n=number of periods/year

We can then figure out where the tangent line intersects the x axis and use that to make a much more accurate second guess.

The speed advantage the Newton-Raphson method has over a simple guess and check technique is quite remarkable. It is common to be able to achieve a result that matches Excel to within 8 decimal places in 5-10 iterations.

Implementing Newton-Raphson

Fortunately for me, I didn’t have to implement the entire Newton-Raphson algorithm myself because the programming language I use already has a library to do this. I just give it the EIR function and the derivative function and it does the rest.

For the programmers out there, here are a few links to implementations in various languages:

  • C#: uses bisection rather than Newton’s method
  • Java
  • Python

Related Information:

For those of you who would like to know more, please explore the following links:

Next Article:

Calculating Effective Interest Rates Using Cashflow Discounting

Here’s a post I wrote for Microfinance Transparency on October 26, 2010:

(Note: not all formatting carried over from the MFT Blog)

In a previous post, I described the technique that computer programs like Microsoft Excel use to calculate the XIRR (effective interest rate) as a very smart version of “guess and check.” The post on Newton’s Method described how the “guessing” part works, but it did not describe how the computer is able to finally verify when it has the correct EIR figure — the “check” part.

In today’s post, I’m going to describe the process that a computer program uses to generate a discounted cashflow, a method of calculating the value of a cashflow that uses the time-value of money. By adding up the discounted cashflows we are able to determine whether we have the correct EIR.

I’m going to start with a sample loan of $4,825.00 that was disbursed on the 28th of the month but is paid back on the 16th of the month every month for about a year. The fact that the disbursement date is not exactly one month prior to the first repayment means that a simple IRR formula can not be used for accurate results, and the calculation must take all the actual dates into account. The exact details are at right.

How to Check an EIR

The first step in the process is to come up with a guess. For my example, I’ll start with a guess of 30 percent. The next step is then to take that 30% rate and plug it into the discounting formula for each date of the loan:

Discounted cashflow = cashflow * (rate +1)^(-days/365)

This gives us the discounted value of the cashflow for each date period. We then add up all the discounted cashflows to produce a total. This total should be 0 if the rate is correct, indicating that discounted cashflow is equivalent to the nominal cashflow at the specified rate. If it is not 0, we guess again, until we get closer and closer to 0. (For our purposes we decided that 8 decimal places of accuracy is good enough!) Below, you will see that Newton’s method is able to guess the correct EIR in only 5 guesses! [Read more…]

How to Bring Neighbors Together with a Block Party

3rd Street Block Party, 2022

Lessons from our Street’s first Block Party in Decades

I’ve been wanting to hold a block party on my street to help neighbors get to know each other and build community spirit.  This post details how our street held our first block party in decades.  My hope is that other streets will learn from this on how to  hold their own block parties.

Walking the Neighborhood

I’ve being going on regular walks around Akron for over a decade and after a day spent working from home, I often feel like getting some social time.  While on my walks, if a resident seems open to talking I’ll often strike up a conversation.

When I’m on my own street and I see a neighbor, I test their receptivity  and mention the idea of the block party.  If I haven’t talked with them before I point out where I live on the street so they can tell that I’m one of their neighbors and this block party thing is “legit”.

We were able to get 49 attendees to the party, which is quite a high percentage of the neighborhood.

3rd Street Block Party, 2022

Talking to people in person

I talked with everyone on the street at least 2-3 times.  I don’t think I would have gotten the response I did without the personal touch.  I also got ideas from people on how to make the idea work.  I had the basic concept, but I didn’t have a location or date at the beginning.  The date and location came later after I spoke with people. [Read more…]

Monetary Policy Options (Notes for discussion)

  1. Babylonian Option (Code of Hammurabi): Year of Jubilee
    • Because Debt compounds (compound interest), it grows out of control
    • When it gets out of control, it must be reigned in
    • The Babylonians realized that compound interest would make their citizens “debt slaves”
      • A neighboring king could threaten to invade the indebted land and offer to forgive all the personal debts
      • Therefore, the king realized that he would have to forgive the debt himself, because otherwise his subjects would not fight to defend against the invader.
    • The nation of Israel was held captive in Babylonia and learned about this debt forgiveness.  This is where the “Year of Jubilee” comes from.
      • Roughly every 50 years, the personal debt is forgiven
      • This results in lenders choosing to “back off” and limit loan term lengths.
      • As the Year of Jubilee approached, lenders would stop making new (personal) loans that extended past the Year of Jubilee, resulting in a natural decline of leverage.
      • Business loans were not forgiven.
    • With the exception of the Romans, most other nations adopted the Babylonian debt forgiveness policy.  The US is following the Roman approach.
    • Professor Michael Hudson says that this attitude toward debt was part of the Roman dysfunction.  We have adopted the Roman policy that the debt always has to be repaid.
  2. The hard money standard (gold/bitcoin) tries to avoid governments debasing money by printing more fiat.  It tries to use a scarce asset like gold as a means to limit the power of governments to corrupt the money.
  3. Fiat Standard: allows governments to print as much as they want.  Stephanie Kelton says that MMT (modern monetary theory) enables monetarily sovereign countries to run big deficits, with the only limitation being inflation.  According to this school, governments should focus on inflation, rather than debt.
    • One question is what happens when the government issues more debt than the market wants to purchase without significantly higher interest rates.  Can the Federal Reserve buy up as much treasuries as the government wants? (monetize the debt)
  4. Soft Default: Critics of the Fiat Standard argue that monitoring debt level is very important.  With debt in the US ~130% of GDP, the government can’t fight inflation by raising interest rates without spending large amounts on interest.
      • In the late 1970s, Paul Volcker was able to reign in high inflation by raising interest rates into the double digits.  The US can’t afford to do this now because the debt levels are so much higher (130% of GDP vs 30%).
      • One way the government could deal with the debt is to inflate the money supply over an extended period of time (a decade) until the debt is roughly half its current level (in relation to GDP).
        • I assume this is the unstated US governmental policy.
  5. Hard Default: Another option would be to arrange for a sudden and dramatic debasement of the currency.  Prices would double overnight, making it easier to pay back debts with devalued currency.
    • A hard default happens regularly in other countries
    • A hard default happened in March of 1933, when FDR reset the price of gold.
      • In 1971, the US had a soft default when it went off the gold standard.
  6. Transition to new System (soft default)
    • A final option would be for the federal reserve to inflate the dollar while people voluntarily transition to a new system, be that the Bancor or Bitcoin, or something else.  Those who remain in the old system the longest get the worst exchange rate to the new system.
    • This would be like the Babylonian option in that the debt would be forgiven, but it would be a one-time event and not establish this as a regular tradition.