January 30, 2013

Programming Collective Intelligence Chapter 8 Optimization

I've been reading the book Programming Collective Intelligence by Toby Segaran. The book gives you a good introduction to the art of Machine Learning and Data Mining. But if you are reading Chapter 8 on optimization, you may have found an error in the code from the section labelled Simulated Annealing (page 95-96), where you are calculating the probability.

This is the code in the book:
p = pow(math.e,(-eb-ea)/T)

But according to the unconfirmed errata and Wikipedia, the code should be:
p = pow(math.e,-(eb-ea)/T)

If you have changed the code and run the optimization program, you will soon discover that this new code will give you a nice "OverflowError: math range error", because -(eb-ea)/T will in the end be a large number. To solve this, you will need to change the code to this:

import decimal
exponent = -(eb-ea)/T
e = decimal.Decimal(math.e)
p = e**decimal.Decimal(exponent)

No comments:

Post a Comment