Exponential and Logarithmic Functions
Exponential functions describe quantities that grow or shrink by the same factor in every time step: compound interest, population growth, radioactive decay, cooling coffee. Logarithms are their inverses and answer "how many steps does it take?". Both are central to calculus and to machine learning.
Exponential functions
An exponential function has the variable in the exponent:
| Base | Behaviour | Example |
|---|---|---|
| growth: increases faster and faster | doubles every step | |
| decay: shrinks toward 0 | halves every step |
For any base: the domain is all reals, always (the graph never touches the -axis, which is a horizontal asymptote), and , so the graph passes through .
Exponential growth eventually beats every polynomial, however large its degree:
for x in [5, 10, 20, 30, 40]:
print(f"x = {x:2}: x^5 = {x**5:>12,} 2^x = {2**x:>16,}")x = 5: x^5 = 3,125 2^x = 32
x = 10: x^5 = 100,000 2^x = 1,024
x = 20: x^5 = 3,200,000 2^x = 1,048,576
x = 30: x^5 = 24,300,000 2^x = 1,073,741,824
x = 40: x^5 = 102,400,000 2^x = 1,099,511,627,776
leads at first, but overtakes it shortly before and then leaves it far behind. That is why an algorithm with running time is hopeless for large even when is still workable.
The number e
One base is special in calculus: . It appears when growth is compounded continuously:
import math
for n in [1, 10, 100, 10_000, 1_000_000]:
print(f"n = {n:>9,}: (1 + 1/n)^n = {(1 + 1/n)**n:.8f}")
print(f"math.e = {math.e:.8f}")n = 1: (1 + 1/n)^n = 2.00000000
n = 10: (1 + 1/n)^n = 2.59374246
n = 100: (1 + 1/n)^n = 2.70481383
n = 10,000: (1 + 1/n)^n = 2.71814593
n = 1,000,000: (1 + 1/n)^n = 2.71828047
math.e = 2.71828183
What makes special is that its slope at every point equals its own value: the derivative of is . You will prove this in Derivatives of Exponentials and Logs. Any exponential can be rewritten with base , since .
Logarithms
The logarithm base is the inverse of :
because . The natural logarithm uses base ; is the common log. In Python, math.log(x) is , and math.log2 and math.log10 give the other two.
| Law | Formula |
|---|---|
| product | |
| quotient | |
| power | |
| change of base | |
| inverse | , |
Logs turn multiplication into addition. That is why the graph of grows so slowly: going from 1 to 1,000,000 only raises from 0 to about 13.8.
Solving with logs
How long until an investment growing 6% a year doubles?
Growth and decay models
With a half-life , the decay constant is :
import numpy as np
N0, half_life = 800.0, 5.0 # grams, years
k = -np.log(2) / half_life
for t in [0, 5, 10, 15, 20]:
print(f"t = {t:2} years: {N0 * np.exp(k * t):6.1f} g")
t_10g = np.log(10 / N0) / k
print(f"down to 10 g after {t_10g:.2f} years")t = 0 years: 800.0 g
t = 5 years: 400.0 g
t = 10 years: 200.0 g
t = 15 years: 100.0 g
t = 20 years: 50.0 g
down to 10 g after 31.61 years
In software: softmax and log-loss
A classifier produces raw scores (logits) . Softmax turns them into probabilities with the exponential, which makes every value positive and exaggerates differences:
Training then minimises the log-loss (cross-entropy) , where is the probability the model gave to the correct class. A confident correct answer ( near 1) costs almost nothing; a confident wrong one costs a lot.
import numpy as np
def softmax(z):
z = np.asarray(z, dtype=float)
e = np.exp(z - z.max()) # subtract the max: same result, no overflow
return e / e.sum()
logits = [2.0, 1.0, 0.1]
p = softmax(logits)
print("probabilities:", np.round(p, 4), " sum:", round(p.sum(), 6))
print("loss if class 0 is correct:", round(-np.log(p[0]), 4))
print("loss if class 2 is correct:", round(-np.log(p[2]), 4))
print("huge logits still fine:", np.round(softmax([1000, 999, 998]), 4))probabilities: [0.659 0.2424 0.0986] sum: 1.0
loss if class 0 is correct: 0.417
loss if class 2 is correct: 2.317
huge logits still fine: [0.6652 0.2447 0.09 ]
Subtracting the largest logit works because : the factor cancels. Without it, np.exp(1000) overflows to infinity. LLMs apply softmax over vocabularies of 100,000+ tokens at every step (Sampling).
Logs show up elsewhere in computing too: is the number of halvings in binary search, the bits needed to store values, and the height of a balanced tree.
Exercises
Exercise 1: Evaluate logs
Without a calculator, find , , and .
Answer
5, −3, 5, 0.
Solution
Exercise 2: Simplify with log laws
Write as a sum and difference of simple logs.
Answer
Solution
import sympy as sp
x, y, z = sp.symbols('x y z', positive=True)
print(sp.expand_log(sp.log(x**3 * sp.sqrt(y) / z**2)))3*log(x) + log(y)/2 - 2*log(z)
Exercise 3: Solve an exponential equation
Solve .
Answer
Solution
import math
t = 5 * math.log(8)
print(round(t, 3), round(5 * math.exp(0.2 * t), 6))10.397 40.0
Exercise 4: Binary search steps
Binary search halves the range each step. How many steps does it need, at most, for a sorted list of 1,000,000 items? Use .
Answer
20 steps.
Solution
After halvings a range of items has left. We need , so .
import math
n = 1_000_000
print(math.log2(n), math.ceil(math.log2(n)))19.931568569324174 20
Exercise 5: Softmax by hand
Compute softmax of the logits exactly.
Answer
Solution
The difference of between the logits means the second class is 3 times as likely as the first.
Test yourself
Which expression equals ?
Why does a stable softmax implementation subtract from every logit first?
- To make the probabilities sum to 1
- To avoid overflow in exp; the result is mathematically identical
- To make all probabilities equal
- Because exp only accepts negative numbers
A substance has a half-life of 3 hours. What fraction is left after 12 hours?