Exponentials and Logarithms

Intuition

The exponential exe^x grows by the same factor for each step in xx; the natural logarithm ln⁡x\ln x undoes it, answering "what power of ee gives xx?". Logs turn multiplication into addition, which is why ML works with log-probabilities: multiplying hundreds of small probabilities underflows to 0, but adding their logs is perfectly stable.

Precise explanation

  • e≈2.71828e \approx 2.71828; math.exp(x) is exe^x and math.log(x) is ln⁡x\ln x (only for x>0x > 0).
  • Rules: ln⁡(ab)=ln⁡a+ln⁡b\ln(ab) = \ln a + \ln b, ln⁡(ak)=kln⁡a\ln(a^k) = k\ln a, ln⁡1=0\ln 1 = 0, eln⁡x=xe^{\ln x} = x.
  • Change of base: log⁡bx=ln⁡xln⁡b\log_b x = \dfrac{\ln x}{\ln b}, e.g. log⁡28=3\log_2 8 = 3.
  • Compound growth: an amount growing by rate rr per step doubles after t=ln⁡2ln⁡(1+r)t = \dfrac{\ln 2}{\ln(1+r)} steps.
  • Log-likelihood: ln⁡∏ipi=∑iln⁡pi\ln \prod_i p_i = \sum_i \ln p_i.
  • Log-sum-exp: LSE(v)=ln⁡∑ievi\mathrm{LSE}(v) = \ln \sum_i e^{v_i}. Computing e1000e^{1000} overflows, so use LSE(v)=m+ln⁡∑ievi−m\mathrm{LSE}(v) = m + \ln\sum_i e^{v_i - m} with m=max⁡ivim = \max_i v_i; every exponent is then at most e0=1e^0 = 1.

Worked example

Probabilities [1e-5] * 1000: the product is 10−500010^{-5000}, which a float stores as 0.0, and math.log(0.0) raises ValueError. The sum of logs is 1000ln⁡10−5≈−11512.91000 \ln 10^{-5} \approx -11512.9. logsumexp([1000, 1000]) =1000+ln⁡2≈1000.693= 1000 + \ln 2 \approx 1000.693.

Common mistakes

  • math.log(x, 10) vs math.log10(x) — both work; math.log(x) alone is base ee, not 10.
  • Taking the log of 0 or a negative number.
  • Implementing log-sum-exp directly, which overflows for large inputs or underflows to log(0) for very negative ones.

Why this matters

Cross-entropy loss, perplexity, Naive Bayes and softmax (DL-02) all rely on these identities, and MA-03 is built from e−xe^{-x}.

Library implementation

The math module already provides math.log(x, base), math.log2, math.log10 and math.log1p(r) (an accurate ln⁡(1+r)\ln(1 + r) for small rr). For log-sum-exp, SciPy offers scipy.special.logsumexp, which uses the same max-subtraction trick (it also handles arrays and weights). The tests below compare your functions with these when they are available; the SciPy comparison is skipped if SciPy is not installed.

References