[SUMMARY] Internal Rate of Return (#156)

R

Ruby Quiz

Solving the IRR equation is essentially a matter of computational guesswork.
You pick an IRR and check to see if the NPV is zero, or very close to it. When
it is, you found the right IRR. If you didn't find the right IRR, it's time to
guess again.

How to make your guesses is the main issue with solving this problem. Most
people used a little calculus to refine their guess. Newton's method can be
used to solve equations like the one we have for NPV.

Alex Shulgin used a simpler guess metric that's probably a little more familiar
to us computer guys, especially if you have forgotten as much calculus as I
have. Alex did a binary search for the answer. This process is actually very
simple, once you find a good upper and lower bound for the search.

Let's have a look at how Alex solved the quiz. First, we need to be able to
calculate an NPV:

#!/usr/bin/env ruby

def npv(ct, i)
sum = 0
ct.each_with_index{ |c,t| sum += c/(1 + i.to_f)**t }
sum
end

# ...

That's a direct code translation of the equation given in the quiz. With it, we
can test our IRR guess to see what NPV value they return.

Now, for Alex's irr() method, let's actually start at the end of the code since
that makes it easier to understand:

# ...

def irr(ct)
# ... set l and r bounds in here ...

m = 0
loop do
m = (l + r)/2.0
v = npv(ct, m)

break if v.abs < 1e-8
if v*sign < 0
r = m
else
l = m
end
end
m
end

As my comment implies, the missing piece of code sets some bounds variables: l
for lower and r for roof, I think. It also sets the sign variable, which give
us the sign of the rate we are solving for.

If you can take those elements on faith for now, we're looking at a simple
binary search in this code. First we find the mid-point and check its NPV. If
that number is really close to zero, it's our answer. If the NPV tells us it is
too high, we drop the roof. Otherwise, we raise the lower bound. Rinse,
repeat. This will converge on the IRR we seek.

Now we need to know how Alex found those bounds:

# ...

def irr(ct)
l = -0.9999
sign = npv(ct, l)
r = 1.0
while npv(ct, r)*sign > 0 do
r *= 2
break if r > 1000
end
if r > 1000
l = -1.0001
sign = npv(ct, l)
r = -2.0
while npv(ct, r)*sign > 0 do
r *= 2
return 0.0/0.0 if r.abs > 1000
end
end

# ... binary search here ...
end

The first thing to understand here is why Alex is skirting around the -1 number
with values like -0.9999 and -1.0001. The reason is that NPV doesn't help us
there:
=> NaN

Given that, Alex's bounds checking code starts by assuming a lower bound of
-0.9999. The first while loop then walks the roof bound up from 1.0 to 1000
searching for a good upper bound. If those points don't work out, the code
pivots to -1.0001 and walking the other bound from -2.0 to -1000. If both
searches fail, we're in the area were IRR is undefined and Alex returns NaN to
indicate that. Otherwise, we have found suitable boundaries and the code moves
on to the binary search.

Again, that's a non-mathy way to solve this problem. Definitely browse through
the other solutions to get a feel for Newton's method based solutions as well.

Since this brings us to three full years of weekly quizzes and my retirement as
quizmaster, I want to thank not only those who solved this week's problem but
anyone who ever solved a quiz. If people hadn't supported the effort so well
these last three years, it never would have made it this far. I also need to
give a huge thanks to those who contributed quizzes and summaries during the
run. They carried me a good portion of the way and I'm eternally grateful for
that.

Sincerely, thank you all.

Tomorrow, I join most of you as a quiz solver. I'll leave it to the new team to
drop hints about their problems, but I for one am anxious to see them.
 
J

James Gray

Left and Right?

Never thought of that. I bet you are right too. It makes more sense
with the way the code handles negative numbers.

James Edward Gray II
 
E

Eric I.

Since this brings us to three full years of weekly quizzes and my retirement as
quizmaster, I want to thank not only those who solved this week's problem but
anyone who ever solved a quiz.  If people hadn't supported the effort sowell
these last three years, it never would have made it this far.  I also need to
give a huge thanks to those who contributed quizzes and summaries during the
run.  They carried me a good portion of the way and I'm eternally grateful for
that.

Sincerely, thank you all.

Thank you, James, and well done! You've given quite a lot to the Ruby
community.
Tomorrow, I join most of you as a quiz solver.  I'll leave it to the newteam to
drop hints about their problems, but I for one am anxious to see them.

Me too!

Eric
 
J

James Gray

Thank you, James, and well done! You've given quite a lot to the Ruby
community.

Thanks Eric. I think your solutions have given the community at least
as much. ;)

James Edward Gray II
 
B

Bill Kelly

From: "Ruby Quiz said:
Since this brings us to three full years of weekly quizzes and my retirement as
quizmaster, I want to thank not only those who solved this week's problem but
anyone who ever solved a quiz. If people hadn't supported the effort so well
these last three years, it never would have made it this far. I also need to
give a huge thanks to those who contributed quizzes and summaries during the
run. They carried me a good portion of the way and I'm eternally grateful for
that.

Sincerely, thank you all.

Tomorrow, I join most of you as a quiz solver. I'll leave it to the new team to
drop hints about their problems, but I for one am anxious to see them.

Thanks, James for all the quizzes !

I only solved a few, but I followed the discussion on the rest with interest and
enjoyment.

Hard to believe it's been three years already. Over time, the summary on
Thursday and new quiz on Friday was something I'd really gotten to look
forward to each week.

You did a fantastic job. Enjoyed it.


Regards,

Bill
 
R

Robert Dober

Well I know I have said thanx already, cause I wanted to be the first ;).
But I guess that you might save this one single thread to show to your
grandchildren and so I repeat my expression of gratitude for what you
have done for this community and Ruby in general.

C U around
Robert
 
J

James Gray

Well I know I have said thanx already, cause I wanted to be the
first ;).
But I guess that you might save this one single thread to show to your
grandchildren and so I repeat my expression of gratitude for what you
have done for this community and Ruby in general.

<laughs>

My grandchildren, eh? Brilliant.

Thank you Robert.

James Edward Gray II
 
M

Morton Goldberg

Since this brings us to three full years of weekly quizzes and my
retirement as
quizmaster, I want to thank not only those who solved this week's
problem but
anyone who ever solved a quiz. If people hadn't supported the
effort so well
these last three years, it never would have made it this far. I
also need to
give a huge thanks to those who contributed quizzes and summaries
during the
run. They carried me a good portion of the way and I'm eternally
grateful for
that.

Sincerely, thank you all.

Thank _you_ so much.

I was a late comer to Ruby Quiz. My first submission was Quiz 90. And
I didn't solve all that many quizzes after that one. Still I always
followed the submissions and read the summaries from Quiz 90 on. And
I enjoyed and learned from each. I am particularly fond of your
summaries. They were always cogent and extremely well written --
amazingly so, when one considers how little time you had to write
each one.

Now that you're retired <grin>, James, why don't you use some newly
available free time <another grin> to get out a second "Best of Ruby
Quiz" book <serious>? Either an updated second edition, or a separate
second volume. I'd would really like to see this happen. They were
certainly enough great quizzes after the book came out to fill
another volume.

Regards, Morton
 
J

James Gray

Thank _you_ so much.

I was a late comer to Ruby Quiz. My first submission was Quiz 90. =20
And I didn't solve all that many quizzes after that one. Still I =20
always followed the submissions and read the summaries from Quiz 90 =20=
on. And I enjoyed and learned from each. I am particularly fond of =20
your summaries. They were always cogent and extremely well written =20
-- amazingly so, when one considers how little time you had to write =20=
each one.

Thanks for the high praise Morton.
Now that you're retired <grin>, James, why don't you use some newly =20=
available free time <another grin> to get out a second "Best of Ruby =20=
Quiz" book <serious>? Either an updated second edition, or a =20
separate second volume. I'd would really like to see this happen. =20
They were certainly enough great quizzes after the book came out to =20=
fill another volume.

I may very well do that at some point. I would like to and I agree =20
with you about there being a lot of good material for it now.

However, for just a little while first, I would like to have a little =20=

more time for programming=85 ;)

James Edward Gray II=
 
A

Alex Shulgin

[snip my solution]

Uh, I'm surprised to see that elaborate explanation of such a dumb
code of mine. Sorry, if that had taken you a lot of time--it was
really my job to explain it at least in a few words, but I'm too
lazy. :)
Sincerely, thank you all.

Thank you! I've joined the quiz very late, but found it very exciting
and watched every single problem since when. Solving little quizzies
in such a nice language like Ruby is really refreshing after hours of
routine job at work. :)
 
J

James Gray

[snip my solution]

Uh, I'm surprised to see that elaborate explanation of such a dumb
code of mine.

I personally found it to be pretty clever. I couldn't think of a way
to solve that problem without using the math, so it was neat to have
you show it too me.
Thank you! I've joined the quiz very late, but found it very exciting
and watched every single problem since when. Solving little quizzies
in such a nice language like Ruby is really refreshing after hours of
routine job at work. :)

Thanks Alex.

James Edward Gray II
 
C

Carl Porth

Hey James,

I'd like to mention my appreciation for all the hard work you've put
into Ruby Quiz. It has certainly proven to be a valuable resource for
learning Ruby. And it has brightened my day when my solution has been
featured in your weekly summary.

The quizmasters following you have very big shoes to fill.

Thanks again,
Carl
 
J

James Gray

I'd like to mention my appreciation for all the hard work you've put
into Ruby Quiz. It has certainly proven to be a valuable resource for
learning Ruby. And it has brightened my day when my solution has been
featured in your weekly summary.

Thanks for giving me those great solutions to talk about Carl.

James Edward Gray II
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top