Powers, Logarithms, and Exponentials
A change from a small abundance to twice that abundance has the same ratio as a change between two much larger values. Logarithms express those ratios as differences. They appear throughout expression analysis because biological measurements often span several orders of magnitude.
Read algebra and notation first. The examples use invented positive abundances rather than raw counts from an experiment.
Powers and their inverses
Section titled “Powers and their inverses”In b**x, the base is b and the exponent is x. A logarithm asks which exponent produces a value. Thus log_b(b**x) = x for a positive base other than one.
Use base two when the interpretation concerns doubling. Use base ten for orders of magnitude. The natural logarithm, written ln, uses the constant e as its base. NumPy calls the natural logarithm np.log.
import numpy as np
control = np.array([10.0, 20.0, 80.0])treated = np.array([20.0, 10.0, 80.0])fold_change = treated / controllog2_fold_change = np.log2(fold_change)print("Fold change:", fold_change)print("Log2 fold change:", log2_fold_change)assert np.allclose(2.0 ** log2_fold_change, fold_change)Fold change: [2. 0.5 1. ]Log2 fold change: [ 1. -1. 0.]A positive log fold change indicates an increase relative to the denominator. A negative value indicates a decrease. Reversing the comparison reverses the sign.
The log rules
Section titled “The log rules”The base determines the unit of a logarithmic difference. A difference of one on a base-two scale represents a doubling. A difference of one on a base-ten scale represents multiplication by ten. Changing the base changes the numerical value, so a plot or table must identify its base.
There is a direct conversion between bases: log_b(x) = ln(x)/ln(b). The underlying ratio stays the same. This is why a model can use natural logarithms internally and report an effect on a base-two scale.
For positive a and b, log(a*b) = log(a) + log(b) and log(a/b) = log(a) - log(b). The power rule gives log(a**k) = k*log(a). These identities explain why likelihood calculations later replace products with sums.
There is no corresponding rule that replaces log(a+b) with log(a)+log(b). A log of a mean also differs from a mean of logs.
values = np.array([1.0, 9.0])print(f"Log of arithmetic mean: {np.log(values.mean()):.4f}")print(f"Mean of logs: {np.log(values).mean():.4f}")print(f"Geometric mean: {np.exp(np.log(values).mean()):.4f}")Log of arithmetic mean: 1.6094Mean of logs: 1.0986Geometric mean: 3.0000Exponentiating the mean of logs gives the geometric mean. It summarizes multiplicative scale, while the arithmetic mean summarizes additive scale.
In the example above, the arithmetic mean gives equal weight to differences on the original scale. The geometric mean gives equal weight to differences on the log scale. Neither summary is automatically preferable; the measurement and the scientific question determine which interpretation is useful.
A transformation also changes the meaning of an average. Averaging transformed expression across samples and then reversing the transformation does not generally recover the average original expression. Keep the order of operations explicit when comparing results from different tools.
Zero changes the question
Section titled “Zero changes the question”The real logarithm is undefined at zero. Adding a pseudocount makes the calculation finite, but changes the ratio. Its influence is strongest near zero.
low_control = 0.0low_treated = 2.0for pseudocount in [0.5, 1.0, 2.0]: adjusted = (low_treated + pseudocount) / (low_control + pseudocount) print(f"Pseudocount {pseudocount:.1f}: log2 ratio {np.log2(adjusted):.3f}")Pseudocount 0.5: log2 ratio 2.322Pseudocount 1.0: log2 ratio 1.585Pseudocount 2.0: log2 ratio 1.000Do not interpret these adjusted ratios as estimates from a differential expression model. Library size, dispersion, and biological replication still need treatment. The DESeq2 guide covers that analysis.
Exponential growth
Section titled “Exponential growth”The model N(t) = N(0)*exp(r*t) describes constant proportional growth. Its logarithm is a straight line with slope r. If time is measured in hours, r has units of inverse hours.
from pathlib import Path
import matplotlib.pyplot as plt
time = np.linspace(0.0, 6.0, 100)population = 100.0 * np.exp(0.4 * time)fig, axes = plt.subplots(1, 2, figsize=(9, 3.5))axes[0].plot(time, population)axes[0].set(xlabel="Time (hours)", ylabel="Population")axes[1].plot(time, np.log(population))axes[1].set(xlabel="Time (hours)", ylabel="Natural log of population")fig.tight_layout()Path("outputs").mkdir(exist_ok=True)fig.savefig("outputs/exponential_growth.png", dpi=160)plt.close(fig)
This model assumes that the proportional growth rate stays constant. Nutrient depletion and cell death can invalidate that assumption. A straight segment on a log plot does not establish that the same rate holds at every time.
Check your understanding
Section titled “Check your understanding”If the treatment and control measurements exchange places, what happens to their log fold change?
The ratio becomes its reciprocal. The quotient rule then reverses the sign of the log fold change while preserving its magnitude. This reasoning requires positive measurements and the same transformation in both directions.
For a second check, distinguish a logarithmic axis from transformed data. A logarithmic axis changes the positions at which original values appear. Applying a logarithm to the data changes the values that later calculations receive.
Further reading
Section titled “Further reading”-
The reference distinguishes natural logarithms, logarithms with a specified base, and exponentials. It also describes log1p for values close to zero.
-
The universal-function and array-arithmetic examples explain elementwise calculations. Use those operations when you extend the abundance and growth vectors.
Continue with sets, logic, and combinations.