C# IRR Code

As I said earlier here is the code to calculate IRR in C#. This is a rather long entry so you will have to be prepared to spend some time. While it’s not critical that you read it, I think it is important for those who have never written an IRR formula to understand how it works and what goes into it. This way, if you decide to use it and something goes wrong, you should be able to fix it. But don’t fear, the code is in here.

I think a good place to start is redefining the IRR calculation to be a bit more code friendly. You will remember that this is the IRR formula:

IRR Formula

BV Beginning Value
EV Ending Value
n The number of cash flows
cf Cash flow
cf[i] The ith cash flow
tt The total time to the ending value
r The rate of growth per period

Let’s analyze this formula and figure out how to turn it into some code. But first, let’s lay down some ground rules so we are on the same page. The term i will be measured in days not months or years (excel uses years or periods and it’s really annoying). We won’t lump all the cash flows during the month into one in or outflow (you can if you want outside the function).

Our goal when doing this is to create function that calculates IRR relatively quick. IRR by nature is iterative so we want to make sure that we do as little else as possible inside the IRR function. With this in mind we need a loop that is very clean and straight forward.

–BV is a constant term; we could move it to the left side of the equation, leave it where it is or move it into the summation series. Did you catch that, move it into the summation series. How are we going to do that? It’s simpler than you think and works because of how we use the IRR. The beginning value is zero days from the beginning of the period right?

Recall that any number raised to the zero power is 1 even zero. So it’s inside the summation series and makes it easier to use.

So if we call it the 0th cash flow and put it in the summation series we get:

Reducing BV

As you might have suspected, we are going to do the same thing with the ending value. It’s always the last day of the calculation so really the tt term is just a special form of t[i]. We are just going to define it as a cash flow and move it back into the summation series.

After these two manipulations our formula ends up looking like this:

Reducing BV

The last thing we are going to do is simplify this equation: recall this rule Math Rule

Applying that rule to our equation gives us this:

IRR Simplified

For simplicity sake, going forward I will refer to this formula as f(x) where x is the return for the entire period. Inside the code we will convert x to r by turning x into a daily return number r.

Understanding how to solve for r in f(x) is really the hard part of this. I will give a somewhat cursory explanation for some of the methods to do this, but you really need to study root finding algorithms to understand this.

Basically if you graph f(x), you are looking for the value of x where f(x) crosses the x axis.

f(x)

Bisection method
Make a guess that’s too low and a guess that’s too high and then try the middle number. If that number is too low you throw away the lower portion which splits your search space in half.

For example, low guess -.05 high guess .1. Middle guess is .25. It comes back as too low so now your bracket is .25, .1. Split in half and.. well you get the idea. The problem with this method is that it is very slow. It’s linear in nature for finding the solution.

Newton-Rhapson method
I have found that most coders will then read up on the various methods and jump to Newton-Rhapson because everyone says its faster at converging. The Newton-Rhapson method uses a tangent line by using the first derivative of f(x) to help with guessing.

On paper this method is very quick; however, in code it requires the calculation of the first derivative for every cash flow on every pass. This makes it slower and I have seen many times with Newton-Rhapson that the speed is close to linear, this is especially the case when f(x) is not a curved line. Just in case you want to try it, here is the first derivative of f(x):

f'(x)

Secant method
This method is generally good for our purposes. However, for highly variable cash flows it can lose the solution because it does not keep a bracket around the solution area. So we need a slight modification.

False Position method
This is the method we are going to use. It converges very quickly on a solution, performs well when f(x) is a straight line and keeps the solution bracketed at all times.

One of the assumptions we are going to make is that if f(a) is less than 0 and f(b) is greater than 0 then a solution lies somewhere between f(a) and f(b). That is, f(x) is continuous between f(a) and f(b). This may not always be the case, and in these cases an IRR may not exist.

One last note before you download the code; I have not run this code through a solid unit test. I wrote it just for this post as an illustrative example of how it “might” be written. Let me say that again, although the concept has been in a production environment, this specific code has never been in a production environment. In other words if this fails on you dont sue me, I’m not responsible for any failure. I am licensing this code under the GPL.

If all else fails, don’t forget the Financial.IRR method in Visual Basic.

One Response to “C# IRR Code”

  1. Dale Says:

    Mike, thanks for writing this as well as providing an excellent code reference implementation. I’ve ported your implementation to ruby. If you’re interested in a copy, email me and I’ll send it to you.

Leave a Reply