Calculating IRR in Julia

769 views Asked by At

I can calculate NPV using

tvmnpv(i,cfo,cfall)=begin
n=collect(1:length(cfall));
cfo + sum(cfall./(1+i).^n)
end

where cfo is the initial cashflow at t=0, and cfall represents the following cashflows and i is the discount rate being used.

However, I can not find a way to calculate the IRR given the cashflows. I believe excel uses a function that scrolls through possible values until a value where cfo plus the discounted following cashflows equals zero is found. Can anyone point me in the right direction?

An example of desired output is as follows:

cfo=[-100];cfall=[30,30,30,30]

Out: 0.07713847

Therefore, the IRR is 7.713847%.

Thank you for your help.

2

There are 2 answers

1
Gomiero On BEST ANSWER

Calculate the IRR is a Root-finding problem (find i for NPV=0).

One way to do this calculation, is to use the Roots.jl package (Pkg.add("Roots")), as following:

julia> using Roots

julia> tvmnpv(i,cfo,cfall)=begin
         n=collect(1:length(cfall));
         cfo + sum(cfall./(1+i).^n)
       end
tvmnpv (generic function with 1 method)

julia> f(x)=tvmnpv(x, cfo, cfall)
f (generic function with 1 method)

julia> cfo=-100.0
-100.0

julia> cfall=[30, 30, 30, 30];

julia> fzero(f, [0.0, 1.0])
0.07713847295208355

The interval [0.0, 1.0] could be changed for better performance.

If you don't want to install the package, I would recommend you to implement the Bisection method, which is simple and efficient.

tested with Julia Version 0.5.0

0
Alec On

The ActuaryUtilities package provides an internal_rate_of_return (also aliased asirr) function.