Algebra and Mathematical Notation
A formula for mean expression can fit on one line while a program needs several operations. The symbols name those operations. This guide translates between the two forms using counts for one invented gene.
You need arithmetic and the Python variables guide before this page. Run the Python blocks in order. The examples use small invented datasets, and the displayed output comes from the companion container.
Variables and equations
Section titled “Variables and equations”A variable represents a value. In y = a + b*x, x is an input, y is an output, a is an intercept, and b is a slope. A function describes the rule that maps an input to an output. We can write the same rule as f(x) = a + b*x.
Suppose a calibration line gives fluorescence as a function of concentration. Subtract the intercept and divide by the slope to recover concentration. This requires a nonzero slope and a measurement within the calibration range.
intercept = 5.0slope = 2.5fluorescence = 30.0concentration = (fluorescence - intercept) / slopeprint(f"Concentration: {concentration:.1f} ng/mL")print(f"Reconstructed signal: {intercept + slope * concentration:.1f}")Concentration: 10.0 ng/mLReconstructed signal: 30.0The units constrain the calculation. The intercept has fluorescence units, and the slope has fluorescence units per ng/mL. Subtraction requires compatible units. Division then leaves ng/mL.
Python uses = to assign a value and == to compare values. A mathematical equals sign states a relationship. It does not prescribe an assignment order.
Rearranging an equation preserves its solutions when the same valid operation applies to both sides. In the calibration example, first subtract a from both sides of y = a + b*x. This leaves y-a = b*x. Dividing both sides by a nonzero b then gives x = (y-a)/b.
The parentheses are part of the calculation. Writing y-a/b divides only a by b, so it describes a different expression. Substituting the calculated concentration into the original equation provides a check against that mistake.
A parameter and a variable can use the same notation but serve different roles. During this calculation, the calibration parameters stay fixed while the measured signal changes. When fitting the calibration line, the measurements stay fixed while the parameters change.
Indices identify observations
Section titled “Indices identify observations”In mathematical notation, x_i means observation i. The underscore represents a subscript here. For n samples, the sequence is x_1, ..., x_n. Python indexes the corresponding array from zero, so mathematical x_1 is Python counts[0].
The symbol Σ means a sum. The expression Σ(i=1 to n) x_i adds every observation once. The sample mean divides that sum by n.
import numpy as np
counts = np.array([12, 18, 9, 21])sample_count = counts.sizetotal = counts.sum()mean_count = total / sample_countprint(f"First observation: {counts[0]}")print(f"Total: {total}")print(f"Mean: {mean_count:.2f}")assert np.isclose(mean_count, counts.mean())First observation: 12Total: 60Mean: 15.00Parentheses decide which operations apply to which values. sum(counts) / n divides the total. sum(counts / n) divides each count first. These agree because division by a fixed nonzero number distributes over addition. That agreement does not extend to arbitrary functions.
The summation index is a local name for the position being visited. Replacing i with j throughout the sum does not change its value. Changing the lower or upper limit does, because it changes which observations contribute.
In a gene matrix, two indices are often necessary. Write x_ij for gene j in sample i. Summing over i holds the gene fixed and visits the samples. Summing over j holds the sample fixed and visits the genes. Labeling those axes before translating the equation prevents a common programming error.
Products and weighted means
Section titled “Products and weighted means”The symbol Π means a product. Later, probability models multiply probabilities for independent observations. A weighted mean instead multiplies each observation by a weight, adds the products, and divides by the total weight.
weights = np.array([1.0, 2.0, 1.0, 2.0])weighted_mean = np.sum(weights * counts) / np.sum(weights)print(f"Weighted mean: {weighted_mean:.2f}")print(f"Product of three values: {np.prod([2, 3, 4])}")Weighted mean: 16.50Product of three values: 24These weights are invented for arithmetic practice. They are not a recommendation to weight RNA sequencing samples by library size. A weight needs a scientific or statistical justification.
Check the meaning before the calculation
Section titled “Check the meaning before the calculation”An equation needs definitions for its variables, their units, and their allowed values. A denominator cannot be zero. An index must refer to an observation that exists. A mean of raw counts does not adjust for different sequencing depths.
For practice, replace the weights with equal positive weights. The weighted mean should equal the ordinary mean. Then change all concentrations from ng/mL to another unit and adjust the slope consistently.
Check your understanding
Section titled “Check your understanding”Suppose every observation receives the same positive weight. Should changing that shared weight change the weighted mean?
The shared factor appears in both the numerator and denominator, so it cancels. Relative weights determine the answer. Their common scale does not. This also explains why weights that sum to one are convenient but not required by the formula above.
Further reading
Section titled “Further reading”-
The basic operations and reduction examples explain array arithmetic, sums, and axis arguments. Compare those operations with the scalar calibration and weighted counts here.
-
MIT Mathematics for Computer Science
The course develops definitions, functions, and mathematical reasoning. Its textbook and problem sets extend the notation used in this lesson.
Continue with powers, logarithms, and exponentials.