Ratios compare amounts, rates turn "per" into a number, and proportion says how one amount moves when another does. Together they scale recipes, plan trips and share money. In machine learning they split a dataset into training, validation and test sets, and they price and time every job a language model runs, in tokens per second and dollars per thousand tokens.
What you'll be able to do
Read a ratio part to part and part to whole, simplify it, and share an amount in it
Find a rate by dividing, and use it to scale up and down
Tell direct proportion (y = kx) from inverse proportion (y = k/x), and find k
Split a dataset 80/10/10 in whole examples, and work out speed and cost from a rate
Quick check
Have a guess before you start. Pancakes for 4 people take 3 cups of flour. How much flour for 6 people?
2 cups
5 cups
6 cups
4.5 cups
Show the answer
4.5 cups
Each person needs 3 ÷ 4 = 0.75 cups, so 6 people need 6 × 0.75 = 4.5 cups. Or: 6 people is 1.5 times as many, so 1.5 times the flour.
The math: comparing amounts
Purpose. A ratio compares two or more amounts by how many times bigger one is than another. Recipes, maps, mixes and money all use them.
The ratio 2:3, read "2 to 3", says: for every 2 of the first, there are 3 of the second. That's part to part. The same mix can be read part to whole: there are 2 + 3 = 5 parts in all, so the first is 2/5 of the whole and the second is 3/5.
Like fractions, ratios simplify. 6:9 is the same mix as 2:3, since both sides divide by 3.
Sharing in a ratio. Add the ratio's numbers to count the portions. Divide the total to find one portion. Then give each part its count of portions.
Watch it for 450 dollars shared by Ana, Ben and Cai in the ratio 2:3:4. In the picture, the top bar is the money cut into equal portions. The bar under it groups those same portions by person, so each share sits right under the portions that make it.
Try it now
Each share is its part's count of portions. Then set the amount to share to 1000 and the ratio to 8, 1, 1 and press Use these numbers: the bar is cut 8:1:1, the split you'll use on data in the AI part.
Quick check
From the last lesson: what is 80% of 1,250?
1,170
100
1,000
1,562.5
Show the answer
1,000
80% is 0.8, and 0.8 × 1,250 = 1,000. You'll meet this exact split again in the AI part.
Symbols and formula. For a ratio a:b:c of a total T:
one portion=a+b+cTshare of a=a×one portion
In plain words: count the portions, divide the total by that count, then hand out each part's portions.
A quick check: the shares must add back to the total. 100 + 150 + 200 = 450.
Rates. A rate is a ratio with units, read with "per": 60 miles per hour, 3 dollars per pound. You find a rate by dividing. 150 miles in 3 hours is 150 ÷ 3 = 50 miles per hour. To scale, multiply a rate by an amount: in 5 hours at 50 miles per hour you go 250 miles.
Quick check
Share 60 in the ratio 1:2. What does the 2 part get?
120
40
20
30
Show the answer
40
1 + 2 = 3 portions, so one portion is 60 ÷ 3 = 20, and the 2 part gets 2 × 20 = 40.
Direct proportion. Two amounts are in direct proportion when one is always the same multiple of the other. Double one and the other doubles too. The multiple, k, is the constant of proportion, found by dividing. For the pancakes, k = 3 ÷ 4 = 0.75 cups per person, so 6 people need 0.75 × 6 = 4.5 cups.
y=kxy=xk
In plain words: on the left, y grows in step with x. On the right, y shrinks as x grows.
Inverse proportion is the right-hand rule: double x and y halves. If 4 painters take 6 hours, k = 4 × 6 = 24 painter-hours, so 8 painters take 24 ÷ 8 = 3 hours. Here k is found by multiplying: the total work stays the same.
Quick check
Which pair is in inverse proportion?
Cups of flour and the pancakes they make
Painters on a job and its hours
Hours worked and pay at a fixed hourly rate
Miles driven and the gas a car uses up
Show the answer
Painters on a job and its hours
More painters share the same work, so the hours fall: double the painters, half the time.
Scale is a ratio too. A map's scale of 1:63,360 means 1 inch on the map stands for 63,360 inches on the ground, which is exactly a mile. Scaling works both ways: map to ground, multiply; ground to map, divide.
Recipes scale the same way. To serve 2 instead of 4, halve every amount, so the 3 cups of flour become 1.5. A ratio stays the same mix however big or small you make it, as long as every part is scaled by the same number. Scale one part and not the others, and the mix changes.
In your own words
In your own words: how can you tell direct proportion from inverse proportion?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: shares and rates
Purpose. Sharing and rates are a few lines of division and multiplication. Count the portions, find one, then hand them out:
It runs right here in your browser, with nothing to install.
Each share is a float, because / always gives one.
Code flow. Step through a share, then a rate, one line at a time. The first half is the cell above with each share given a name; the second is the math part's 150 miles in 3 hours. Watch portion appear first, then the three shares built from it.
The shares must add back to the total, and that check is worth running every time:
import math
total = 1000
portion = total / 10
a = portion * 8
b = portion * 1
c = portion * 1print(math.isclose(a + b + c, total))
Whole things can't be cut. You can't put 87.4 pictures in a set. When a total doesn't share out evenly, round the first parts down with // and give what's left to the last part, so nothing is lost:
total = 437
first = total * 8 // 10
second = total * 1 // 10
third = total - first - second
print(first, second, third)
A named mistake: dividing by the number of parts. The ratio 8:1:1 has three parts but 10 portions. Dividing by 3 ignores the ratio:
total = 1000print(total / 3 * 8) # wrong: more than the whole totalprint(total / 10 * 8) # right: 8 of the 10 portions
The first answer is bigger than the 1,000 being shared, a sure sign of a slip.
Worked example
Worked example: a road trip
A drive of 336 miles takes 6 hours and uses 12 gallons of gas. Find the speed, the miles per gallon, and the gas needed for a 434-mile trip.
miles = 336
hours = 6
gallons = 12
speed = miles / hours
mpg = miles / gallons
print("speed:", speed)
print("miles per gallon:", mpg)
print("gas for 434 miles:", 434 / mpg)
In the lesson you can change this and run it.
Both rates come from dividing by the thing after "per": miles per hour divides by hours, and miles per gallon divides by gallons. The last line runs a rate backward. Each gallon covers 28 miles, so 434 miles needs 434 ÷ 28 = 15.5 gallons. A sense check: 434 is a bit more than 336, so it should need a bit more than 12 gallons.
Quick check
What does this print?
total = 90
portion = total / (1 + 2)
print(portion, portion * 2)
45.0 90.0
30 60
30.0 45.0
30.0 60.0
Show the answer
30.0 60.0
1 + 2 = 3 portions, so one portion is 90 / 3 = 30.0, a float because / always gives one. The 2 part is 60.0.
Quick check
Which line gives a speed in miles per hour?
hours / miles
miles - hours
miles * hours
miles / hours
Show the answer
miles / hours
Per hour means divide by the hours.
Drill
Share an amount, price a rate
Set a, b and c to total shared in the ratio 8:1:1, and cost to the price of 15 pounds of apples at 2.40 dollars per pound. Each starter line has a slip.
total = 1000
portion = total / 3
a = portion * 8
b = portion * 1
c = portion * 1
cost = 15 / 2.40print(a, b, c, cost)
In the lesson, your code is checked as you write it.
In your own words
Why give what's left over to the last part, instead of rounding every part?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: splitting data, and rates for tokens
Purpose. Before training, a dataset is split three ways: a training set to learn from, a validation set to tune choices on, and a test set kept aside for a final, honest score.
A common split is 80/10/10, the ratio 8:1:1. For n examples:
train=0.8nvalidation=0.1ntest=0.1n
In plain words: 8 of every 10 examples go to training, 1 to validation and 1 to test.
n = 1250
train = n * 8 // 10
val = n * 1 // 10
test = n - train - val
print(train, val, test)
Quick check
What does this print?
n = 500
train = n * 8 // 10print(train, n - train)
400.0 100.0
400 100
80 420
4000 -3500
Show the answer
400 100
500 × 8 = 4000, and 4000 // 10 = 400, a whole number because // rounds down to one. The rest, 100, is left for validation and test.
In the real tools. In PyTorch, the library most models are built with, a dataset sits in a tensor, a block of numbers, one entry per example. Splitting it 8:1:1 is three slices, cut where the shares end: X[:80] takes the examples before number 80, X[80:90] those from 80 up to 90, and X[90:] the rest. Here are 100 examples, numbered from 0. Real code shuffles the examples first, so each set gets a fair mix: random_split, the name on the map, shuffles and cuts in one call.
Rates for language models. Language models read and write text in small pieces called tokens, often about three-quarters of a word each. Their speed is a rate, tokens per second. Their price is another, dollars per thousand tokens (or per million).
To price a job, first count the thousands of tokens, then multiply by the price of each thousand. The same steps work for any rate: count the units the rate is "per", then multiply.
Quick check
A model writes 120 tokens per second. How long does it take to write 3,000 tokens?
2,880 seconds
25 seconds
0.04 seconds
360,000 seconds
Show the answer
25 seconds
Time is the amount divided by the rate: 3,000 ÷ 120 = 25 seconds.
Ratios hide in the data, too. Say 900 of 1,000 training signs are stop signs and 100 are yield signs: a ratio of 9:1. A lazy model that always answers "stop" is right 900 times in 1,000, so it scores 90% accuracy without learning anything.
stop = 900
yield_signs = 100print(stop / yield_signs) # 9 stop signs for every yield signprint(stop / (stop + yield_signs)) # accuracy of always guessing stop
So before trusting an accuracy, check the ratio of the classes. A score is only good if it beats always guessing the most common answer. (The name yield_signs is used because yield is a word Python keeps for itself.)
Drill
Plan a dataset and a job
Split n examples 80/10/10: train and val rounded down with //, and test getting the rest. Then set seconds to the time for a job of 9,000 tokens at 150 tokens per second, and dollars to its cost at 0.002 dollars per thousand tokens.
n = 3456
train = n * 8 / 10
val = n * 1 / 10
test = n * 1 / 10
seconds = 9000 * 150
dollars = 9000 * 0.002print(train, val, test, seconds, dollars)
In the lesson, your code is checked as you write it.
In your own words
Why does a model's test set get split off before training, in a fixed share?
Write a sentence or two, then compare it with a good answer in the lesson.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write