Logarithmic functions

Level FundamentalDifficulty ★★★★★Concept⌖ Open in the map

What is it?

The inverse of the exponential: log⁡ax\log_a x is the power you raise aa to in order to get xx. Logarithms turn products into sums, which is why they appear in algorithm costs, information theory and every loss function based on likelihood.

Intuition

log⁡2n\log_2 n counts how many times you can halve nn before reaching 1 — the number of steps of binary search, the depth of a balanced tree, the number of bits needed to write nn.

Formulas

log⁡(xy)=log⁡x+log⁡y,ddxln⁡x=1x\log(xy) = \log x + \log y, \qquad \frac{\dd}{\dd x}\ln x = \frac1x
log⁡∏ip(xi)=∑ilog⁡p(xi)\log \prod_i p(x_i) = \sum_i \log p(x_i)
products of probabilities become sums

Where it shows up in computing

  • Algorithm analysis and complexity★★★★★fundamentalScientific computing and algorithms

    Binary search is O(log⁡n)O(\log n) and comparison sorting Θ(nlog⁡n)\Theta(n \log n).

  • Floating point (IEEE 754)★★★★★frequentScientific computing and algorithms

    Multiplying many small probabilities underflows to 0; summing their logs (log-sum-exp) does not.

Where it shows up in AI

  • Loss function★★★★★fundamentalAI and machine learning

    Cross-entropy is a negative log-likelihood; the log turns a product over samples into a sum.

Where is it used?

Computing topics reachable from here, through the chain of ideas that leads to them:

What depends on it

Exercises

1Computing

Why do ML libraries compute log⁡∑iezi\log \sum_i e^{z_i} as m+log⁡∑iezi−mm + \log\sum_i e^{z_i - m} with m=max⁡izim = \max_i z_i?

Solution

Both are equal since ezi=emezi−me^{z_i} = e^m e^{z_i - m}. But e1000e^{1000} overflows a double, while every ezi−m≤1e^{z_i - m} \le 1 and at least one equals 1, so the sum is in [1,n][1, n] and safe.

↑ ↓ to navigate · ↵ · Esc