The math behind AI, from counting to gradients
AI uses four kinds of math. Counting and comparing let a machine sort things. Weighted sums and probability let it score choices and pick a likely answer. Vectors and functions turn words and pictures into numbers. Statistics and gradients test a model and help it learn, by making its mistakes a little smaller each step.
Last reviewed: 20 October 2026 11 min read
What math does AI use?
AI uses ordinary math, stacked in layers. Nothing in it is magic: every step is counting, adding, multiplying or comparing, done very many times.
| Math idea | What it does inside AI | A tiny example |
|---|---|---|
| Counting and comparing | Turns a clue into a number and checks it against a line | 6 brown spots, and the line is 5, so "ripe" |
| Weighted sums | Adds up clues, each multiplied by how much it counts | (3 × 1) + (−4 × 1) = −1 |
| Percentages | Reports how often a model was right | 18 right out of 20 = 90% |
| Probability | Says how likely each answer is | 6 out of 10 = 0.6 |
| Functions | Describe a rule that turns an input into an output | y = 2x + 1 |
| Vectors | Store a word or a picture as a list of numbers | "cat" = (0.9, 0.2) |
| Distributions | Spread chances over many possible answers | 0.67, 0.24, 0.09 |
| Statistics | Sum up how a model did on many test cases | mean error = 10 ÷ 5 = 2 |
| Linear algebra | Does many weighted sums at once | W × x + b |
| Gradients | Show which way to nudge each number to make mistakes smaller | new w = 5 − 0.1 × 4 = 4.6 |
The sections below take one level at a time, with one formula and one worked AI example each. Every number in the examples is an illustration made up for this guide.
Ages 8 to 10: how do counting and comparing help a machine decide?
A machine can decide by counting something and comparing the count with a line. That is a rule, and a rule is the simplest kind of machine.
The formula: if count ≥ line, then "yes".
Worked example: a banana sorter. A machine counts the brown spots on a banana. Its line is 5.
- A banana with 6 spots: 6 ≥ 5, so the machine says "ripe".
- A banana with 2 spots: 2 is less than 5, so it says "not yet".
Clues with strengths. Now give the machine two clues, each with a strength. Brown spots count 1 point each. A soft banana gets 3 extra points. The line moves to 6.
- 4 spots, and soft: 4 + 3 = 7. Seven is at least 6, so "ripe".
- 4 spots, and hard: 4 + 0 = 4. Four is less than 6, so "not yet".
The machine never "knows" what a banana is. It adds and compares. The strengths decide how much each clue matters.
Where this sits in the learning path. For ages 8 to 10, the math strand of the learning path the curriculum is built around is "counts, comparisons and small input-by-strength examples."
Ages 11 to 13: what are weighted sums, percentages and probability in AI?
At 11 to 13, the same adding becomes a weighted sum, and the machine's results become percentages and probabilities.
Weighted sum. Each input is multiplied by its weight, and the results are added:
score = (w₁ × x₁) + (w₂ × x₂)
Weights can be negative. A negative weight pushes the score down. This multiply-and-add step is the basic step inside a neural network: each artificial neuron "performs a dot product with the input and its weights, adds the bias and applies the non-linearity."
Worked example: a message filter. A filter scores each message. If the score is above 0, it marks the message as junk.
| Input | Value (1 = yes, 0 = no) | Weight | Input × weight |
|---|---|---|---|
| Contains the word "prize" | 1 | +3 | +3 |
| Comes from a known sender | 1 | −4 | −4 |
| Score | −1 |
The score is −1, which is not above 0, so the message goes through. Change the sender to someone unknown (value 0) and the score becomes +3, so the message is marked as junk.
Percentages. To see how good the filter is, test it on messages it has never seen and count. If it gets 18 of 20 right:
accuracy = right ÷ total × 100 = 18 ÷ 20 × 100 = 90%
Probability. A probability is a number from 0 to 1. When every outcome is equally likely, you count the outcomes you care about and divide by the total number of outcomes. Machines that write text use probability to choose the next word.
Worked example: guessing the next word. Suppose a tiny text machine has seen "the cat sat on the" 10 times. It was followed by "mat" 6 times, "sofa" 3 times and "roof" once.
P(mat) = 6 ÷ 10 = 0.6. P(sofa) = 0.3. P(roof) = 0.1. Together they make 1.
The machine usually picks "mat", but it sometimes picks another word. A likely word is not the same as a true one.
Where this sits in the learning path. For ages 11 to 13, the math strand of the learning path the curriculum is built around is "ratios, percentages, signed numbers, basic probability and weighted sums."
Ages 14 to 15: how do functions, vectors and distributions describe AI?
At 14 to 15, a model is a function: a rule that turns inputs into an output. Its inputs are vectors, its slopes say how it changes, and its outputs are often a distribution of chances.
Function. y = f(x). In a model, x is the input (a list of numbers), f is the rule with its weights, and y is the output (a score or a set of chances).
Vector. A vector is a list of numbers. AI stores words and pictures this way. In this made-up example, each word gets two numbers: how much it is an animal, and how big it is (0 to 1).
| Word | Animal | Big |
|---|---|---|
| kitten | 0.9 | 0.1 |
| cat | 0.9 | 0.2 |
| truck | 0.0 | 0.8 |
"Kitten" and "cat" sit close together; "truck" sits far away. Closeness in numbers stands in for closeness in meaning. Real systems use far longer lists, and nobody chooses what each number means by hand.
Rate of change. A slope tells you how much the output changes when one input changes a little:
slope = change in output ÷ change in input
In AI, the important slope is how much the model's error changes when one weight changes. That slope is the start of learning.
Distribution. A model often gives a score to every possible answer. A function called softmax turns those scores into chances. It takes a list of any scores and "squashes it to a vector of values between zero and one that sum to one." It raises the number e (about 2.718) to the power of each score, then divides each result by their total.
Worked example. Three answers score 2, 1 and 0.
- e² ≈ 7.389, e¹ ≈ 2.718, e⁰ = 1. Total ≈ 11.107.
- Chances: 7.389 ÷ 11.107 ≈ 0.67; 2.718 ÷ 11.107 ≈ 0.24; 1 ÷ 11.107 ≈ 0.09.
The chances add up to 1. A 0.67 chance is the model's own number. It says how the model ranks the answers, not how often it is right.
Where this sits in the learning path. For ages 14 to 15, the math strand of the learning path the curriculum is built around is "functions, vectors, rates of change and distributions."
Ages 16 to 18: how do statistics, linear algebra and gradients make AI learn?
At 16 to 18, the math explains how a model is tested, how it does millions of weighted sums at once, and how it learns by reducing its error.
Statistics. One test case says little. Statistics sums up many. The mean is "the sum of the data points divided by the number of data points."
Worked example. A model's errors on five fresh test cases are 2, 0, 1, 3 and 4. Mean error = (2 + 0 + 1 + 3 + 4) ÷ 5 = 10 ÷ 5 = 2. A second set of five cases could give a different mean, which is why careful tests use many cases and report how much the number could move.
Linear algebra. A matrix is a grid of weights. Multiplying a matrix by a vector does one weighted sum per row, all at once. A simple classifier's score is often written as f(x) = W x + b.
Worked example. W has two rows, (1, 2) and (0, −1). The input x is (3, 1). The bias b is (0, 1).
- Row 1: (1 × 3) + (2 × 1) + 0 = 5
- Row 2: (0 × 3) + (−1 × 1) + 1 = 0
Output: (5, 0). A large model repeats this with grids of many thousands of rows.
Gradients. The gradient is "a vector of slopes": one slope for each weight. Learning moves each weight a small step against its slope, "in the negative direction of the gradient", so the error goes down. The size of the step is called the learning rate.
new weight = old weight − learning rate × slope
Worked example. Say the error is L = (w − 3)², so the best weight is 3. Start at w = 5.
- Error now: (5 − 3)² = 4.
- Slope of L at w = 5: 2 × (5 − 3) = 4.
- Learning rate 0.1: new w = 5 − 0.1 × 4 = 4.6.
- Error after one step: (4.6 − 3)² = 2.56.
The error fell from 4 to 2.56. Repeat the step and w keeps moving toward 3. Training a real model is this same step, repeated for a great many weights.
Where this sits in the learning path. For ages 16 to 18, the math strand of the learning path the curriculum is built around is "statistics, linear algebra and gradients where ready."
What does each formula explain, and what does it leave out?
Each formula explains one step. None of them explains a whole AI system on its own.
| Formula | It explains | It leaves out |
|---|---|---|
| count ≥ line | How a rule turns one clue into a decision | Where the line came from, and cases the rule never expected |
| Weighted sum | How several clues combine into one score | How the weights were chosen |
| Percentage right | How often a model was right on one test | Which cases it got wrong, and who those mistakes affect |
| Probability of the next word | Why a text machine picks likely words | Whether the words are true |
| Vectors | How words and pictures become numbers | What gets lost when something becomes a list of numbers |
| Softmax | How scores become chances that add to 1 | Whether the model's confidence matches how often it is right |
| Mean error | The typical size of a mistake | Rare, large mistakes hidden inside a good average |
| Gradient step | How a weight moves to reduce error | Whether the error measure itself was the right thing to reduce |
The math tells you how a machine computes. It does not tell you whether the goal was a good one, whether the examples were fair, or who should decide how the machine is used. Those are questions about people, and they need answers too.
Try it at home: a weighted-sum sorter (no device needed)
You need paper, a pencil and about 10 minutes.
- Pick a question, such as "Is this a good day for the park?"
- Choose three clues: sunny, warm, raining. Give each a weight: sunny +2, warm +1, raining −3.
- Pick a line: a score of 2 or more means "go".
- Score today. Write 1 for each clue that is true and 0 for each that is not. Multiply and add.
- Now test your machine on three made-up days. Does it agree with what your family would decide?
- Change one weight and test again. What changed?
For ages 11 and up: turn the scores for three answers into percentages of their total, and talk about why that is not the same as being sure.
Where does Mechna Academy fit?
Mechna Academy is a city of machines for curious children aged 8 to 18, opening first to ages 8 to 10. In each mission, a child guesses how a machine decides, then opens its Peek Hatch to check, changes one thing and tests it on a case it has never seen. Children learn how AI works this way, and they never chat with an AI.
At ages 8 to 10 there's no Python and no typing: answers are picture choices and sentence tiles.
Sources
Questions
AI uses counting and comparing, weighted sums, percentages and probability, functions and vectors, and statistics, linear algebra and gradients. Each one does a different job: sorting, scoring, turning words into numbers, testing results and helping a model learn by making its mistakes smaller.
Not to start. The core ideas begin with counting, comparing and adding clues with different strengths, which a child can try on paper. Building and analyzing large systems in depth uses statistics, linear algebra and calculus.
A weighted sum multiplies each input by a weight that says how much it counts, then adds the results. A machine compares the total with a line to decide. This multiply-and-add step is the basic step inside a neural network.
Probability lets a machine say how likely each possible answer is. Machines that write text use it to choose a likely next word. A likely answer is not always a true one, so it still needs checking.
A gradient is a list of slopes, one for each weight, showing how the model's error changes when that weight changes. Training moves each weight a small step against its slope, so the error goes down, and repeats that step many times.
No. At ages 8 to 10 there's no Python and no typing: answers are picture choices and sentence tiles.
Mechna Academy is a city of machines for curious children aged 8 to 18, opening first to ages 8 to 10.
Mechna Academy is a city of machines for curious children aged 8 to 18, opening first to ages 8 to 10.
For grown-ups: join the waitlistPlaces are limited. The waitlist is for parents and guardians 18 and older.