Prerequisite: Logarithms, Or Why Bots Add Instead of Multiply

A short from-zero lesson for the arbitrage course. What a logarithm does to a chain of rates, why the sum of logs replaces the product, and how to read the minus sign. Tiny Python code with output you can check by hand.

A helper lesson for the course "Build your own arbitrage bot". Lessons 5 to 8 turn rates into a graph and use −log(rate). This page explains why, from zero.

The problem: chains multiply

You change dollars into euros at 0.90, euros into pounds at 0.88, pounds back into dollars at 1.30. Each step multiplies your money. After three steps: 1 × 0.90 × 0.88 × 1.30 = 1.0296. You gained 2.96 percent.

Multiplying works for three steps. The algorithms in lessons 6 to 8 were designed for something else: they add numbers along a path ("the cheapest path has the smallest total"). We need a trick that turns multiplication into addition.

The trick

The logarithm (written log, here the natural one) is a function with one special habit:

log(a × b) = log(a) + log(b)

Multiplying inputs adds outputs. Two more facts you need:

  • log(1) = 0. A rate of exactly 1 (no gain, no loss) becomes 0.
  • If a number is above 1, its log is positive. If it is below 1, its log is negative.

Check it with the chain above:

import math
rates = [0.9, 0.88, 1.30]
prod = 1.0
for r in rates:
    prod *= r
print("product", round(prod, 4))
logs = [math.log(r) for r in rates]
print("logs", [round(x, 4) for x in logs])
print("sum of logs", round(sum(logs), 4), "-> exp", round(math.exp(sum(logs)), 4))
print("negative logs sum", round(-sum(logs), 4))

Output:

product 1.0296
logs [-0.1054, -0.1278, 0.2624]
sum of logs 0.0292 -> exp 1.0296
negative logs sum -0.0292

The sum of the logs is 0.0292. The function exp is the undo button for log, and exp(0.0292) gives back 1.0296, the same answer as the product.

Why the minus sign

A path-finding algorithm looks for the smallest total. We want the circle that gains the most. Gain means the product above 1, which means the sum of logs above 0. To turn "biggest" into "smallest" we flip the sign: use −log(rate) as the length of each road.

Now: a circle with total length below 0 is a profitable circle. In the example the total is −0.0292, which is below 0, so the circle is profitable, and it is exactly the negative cycle of lesson 6.

Small numbers worth knowing

  • log(2) is 0.6931, and log(0.5) is −0.6931: doubling and halving are mirror images.
  • For small changes, log(1 + x) is close to x. A 1 percent gain, log(1.01), is 0.0100 (rounded). That is why log numbers read like percentages when they are small.

Your exercise

  1. Run the code. Change a rate so that the product falls below 1. What does the sum of logs become?
  2. By hand: three trades with rates 1.02, 1.03 and 0.95. First multiply, then add logs on a calculator, then compare.
  3. Why does a rate of exactly 1 not affect the total length of a path?

Next: the helper on graphs (arbitrage-prereq-2-graphs).