# Help with IRR (Internal Rate of Return)

Hi guys!

If you are familiar with excel, there is this function called IRR(), that produces a percentage value. I am being ask if i can output this value since the raw values are in the database. When i ask them for the formula,they said it wasn’t just a formula, but a lot of work, and when i saw it, it really was.

If there is such a function coded in php, or if you know any alternative, please reply.

Thanks!

What exactly is the formula? I’m sure it is just simple math (to the computer.)

They econ guys here only showed it to me on a book, which entails a number of complicated steps. A shocking definition can be found here

http://www.rentalsoftware.com/internal_rate_of_return.htm

an a geeky explanation of how its done here (though it differs from the book a lot)

Not sure if this is correct or what you are looking for.
But it matches the output of: http://www.discountingcashflow.com/irr-en-irr-internal-rate-of-return.php
With the same data.

``````
<?php

\$investment = 100000;

\$flow = array(
array(
'cashflow' => 10000,
'temporal' => 1
),
array(
'cashflow' => 20000,
'temporal' => 2
),
array(
'cashflow' => 30000,
'temporal' => 3
),
array(
'cashflow' => 40000,
'temporal' => 4
),
array(
'cashflow' => 50000,
'temporal' => 5
)
);

function irr (\$investment, \$flow) {

for (\$n = 0; \$n < 100; \$n += 0.0001) {

\$pv = 0;
for (\$i = 0; \$i < count(\$flow); \$i++) {
\$pv = \$pv + \$flow[\$i]['cashflow'] / pow(1 + \$n, \$flow[\$i]['temporal']);
}

if (\$pv <= \$investment) {
return round(\$n * 10000) / 100;
}

}

}

var_dump(irr(\$investment, \$flow));

``````

12.01

According to this function temporal is just a count down starting at one to the number of cash flows.

Also produces 12.01

So on that logic. Smaller data.

``````
\$investment = 100000;
\$flow = array(10000, 20000, 30000, 40000, 50000);

function irr (\$investment, \$flow) {

for (\$n = 0; \$n < 100; \$n += 0.0001) {

\$pv = 0;
for (\$i = 0; \$i < count(\$flow); \$i++) {
\$pv = \$pv + \$flow[\$i] / pow(1 + \$n, \$i + 1);
}

if (\$pv <= \$investment) {
return round(\$n * 10000) / 100;
}

}

}

var_dump(irr(\$investment, \$flow));

``````

The definition here: http://www.rentalsoftware.com/internal_rate_of_return.htm the example given
an investmet of \$50 with a return of \$10 = \$60 resulting in an IRR of 20% (20.01)

``````
\$investment = 50;
\$flow = array(60);
# Output 20.01

``````

So I assume this IRR function is working.