Search

Search papers and lessons

Symbolic Regression

Level 3 · 13 min read

Symbolic regression searches for a readable formula that fits a set of measurements, instead of only fitting the numbers of a formula chosen in advance.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Probability
  • Optimization
  • Programming languages
  • ReadReady to readNot yet

TL;DR

Symbolic regression takes a table of inputs and outputs and searches for a mathematical formula that maps one to the other. Ordinary regression fixes the shape of the formula, such as a straight line, and only tunes its numbers. Symbolic regression also chooses the shape, so the result is a short expression a person can read and check. It matters when the goal is to understand a system, not only to predict it.

Builds on: Linear Regression, Abstract Syntax Tree, Genetic Algorithm · Leads to: Program Synthesis, SINDy

Where it's used

Recovering physical laws from experiments. Given measurements of how a moving system behaves, a symbolic search can propose the equation that governs it. Researchers have used this on simple mechanical systems and recovered relations that are known from physics, which is a check that the method works before it is pointed at unknown systems.

Scientific modelling where the formula must be inspected. In fields such as materials science, chemistry and ecology, a fitted neural network can predict well but says nothing a reader can verify. A short formula can be checked against units, limits and known theory, so a researcher can tell whether it is plausible.

Compact models for engineering. A formula with five terms can be evaluated in microseconds on a small controller, while a large model may not fit on the device. Engineers use symbolic search to trade a little accuracy for a model they can run and explain.

How it works

Take a tiny problem. The data are three pairs of inputs and outputs: (x, y) = (0, 1), (1, 3), (2, 5). We want a formula f(x) that gives the y value for each x.

The method keeps a set of candidate formulas and scores each one. It does not know the answer ahead of time.

  1. Write candidate formulas as expression trees. Two candidates are f₁(x) = 2x + 1 and f₂(x) = x² + 1. Each is stored as a tree: operators such as + and × are inner nodes, and x and the numbers are leaves. Check: f₁ has one × node, one + node, and three leaves.
  2. Evaluate each candidate at the inputs. For f₁: f₁(0) = 1, f₁(1) = 3, f₁(2) = 5. For f₂: f₂(0) = 1, f₂(1) = 2, f₂(2) = 5. Check: f₁(2) = 2 × 2 + 1 = 5.
  3. Score the error. Subtract each prediction from the target and square the result. For f₁ every difference is 0, so the sum is 0. For f₂ the differences are 0, 1 and 0, so the sum is 1. Check: 0² + 1² + 0² = 1.
  4. Add a penalty for size. Count the nodes in each tree. f₁ has 5 nodes (×, 2, x, +, 1) and f₂ also has 5 (^, x, 2, +, 1), so the size penalty ties here and the error decides. Combine the two numbers into one score, so a longer formula must fit noticeably better to win. Check: a formula with more nodes and equal error always scores higher, so it loses.
  5. Change the candidates and repeat. Make small edits to the trees: swap an operator, replace a leaf with x, or cut a subtree and graft another in. Keep the best ones and try again. Here f₁ wins at step 3 and no search is needed. For harder data the same loop runs for thousands of rounds.

The search in step 5 is the hard part. The space of trees grows very fast with the number of nodes, so the method uses an evolutionary search, a genetic algorithm: it keeps a population of trees, breeds them by swapping subtrees, and mutates them at random.

flowchart LR
  A[Population of expression trees] --> B[Evaluate fit and size]
  B --> C[Select the better trees]
  C --> D[Cross over and mutate subtrees]
  D --> A

The math

Definition: Symbolic regression

Given data (x₁, y₁), …, (xₙ, yₙ), find an expression f from a fixed set of allowed building blocks that makes the error small and the expression short. Writing size(f) for the number of nodes in its tree and λ for a weight that sets how much size counts:

min⁡f  ∑i=1n(yi−f(xi))2  +  λ size(f)\min_{f} \; \sum_{i=1}^{n} \bigl(y_i - f(x_i)\bigr)^2 \;+\; \lambda \,\mathrm{size}(f)

Here the sum is the squared error over all data points (the same squared error used in Mean Squared Error), and λ is a number chosen by the user. A large λ favours short formulas; a small λ favours formulas that fit the data closely.

Step 2 in symbols

For the example, the predictions of f₁ at x = 0, 1, 2 are f₁(x) = 2x + 1, giving 1, 3, 5. For f₂(x) = x² + 1 the predictions are 1, 2, 5.

Step 3 in symbols

The squared error of f₂ is

(1−1)2+(3−2)2+(5−5)2=0+1+0=1.(1-1)^2 + (3-2)^2 + (5-5)^2 = 0 + 1 + 0 = 1.

For f₁ it is 0, so f₁ is preferred at equal size and λ > 0 only adds a cost to whichever tree is bigger.

Step 4 in symbols

The total score is error + λ · size. Both trees have size 5, so with λ = 0.1 the score of f₁ is 0 + 0.1 × 5 = 0.5, and the score of f₂ is 1 + 0.1 × 5 = 1.5. The error decides, as in Step 3.

Worked examples

Example
1. Tiny: choose between two lines

Data: (0, 1), (1, 3), (2, 5). Candidates: g(x) = 2x + 1 and h(x) = 3x + 1.

  1. Evaluate g at 0, 1, 2: 1, 3, 5. These match the data exactly.
  2. Evaluate h at 0, 1, 2: 1, 4, 7.
  3. Compute the squared errors for h: (1−1)² + (3−4)² + (5−7)² = 0 + 1 + 4 = 5.
  4. Compare: g has error 0 and h has error 5. Both have the same size, so g wins.

Interpretation: when two formulas have the same number of nodes, the one with the smaller error is the better choice.

Example
2. Standard: a parabola with a linear term

Data generated by y = x² + x at x = 1, 2, 3, giving y = 2, 6, 12. Candidates: A(x) = x² + x, B(x) = 2x², C(x) = x².

  1. Evaluate A: A(1) = 2, A(2) = 6, A(3) = 12. These match the data.
  2. Evaluate B: B(1) = 2, B(2) = 8, B(3) = 18.
  3. Evaluate C: C(1) = 1, C(2) = 4, C(3) = 9.
  4. Squared error of B: (2−2)² + (6−8)² + (12−18)² = 0 + 4 + 36 = 40.
  5. Squared error of C: (2−1)² + (6−4)² + (12−9)² = 1 + 4 + 9 = 14.
  6. Squared error of A: 0.
  7. A is the only candidate with zero error, so the search keeps A.

Interpretation: the true law is recovered exactly, and the two wrong guesses fail in different ways. B has the right shape but the wrong coefficient, while C is missing the linear term.

The plot shows the three candidates. A passes through all three points.

00.511.522.533.5405101520xy
  • A: x² + x
  • B: 2x²
  • C: x²
Only A passes through all three measured points. B has the right shape but the wrong scale, and C is missing the linear term.
Example
3. Realistic: a pendulum's period

A simple pendulum of length L swinging with a small angle has period T. The physics says T = 2π √(L / g), with g ≈ 9.8 m/s². Suppose a lab records:

L (m)T measured (s)
0.251.00
1.002.01
2.253.01

Candidate P: T = 2π √(L / 9.8). Candidate Q: T = 2L.

  1. Evaluate P at L = 0.25: √(0.25 / 9.8) = √0.02551 ≈ 0.1597, then 2π × 0.1597 ≈ 1.00. Error about 0.00.
  2. Evaluate P at L = 1.00: √(1 / 9.8) ≈ 0.3194, times 2π ≈ 2.01. Error about 0.00.
  3. Evaluate P at L = 2.25: √(2.25 / 9.8) ≈ 0.4792, times 2π ≈ 3.01. Error about 0.00.
  4. Evaluate Q at L = 0.25: 2 × 0.25 = 0.5. Error 1.00 − 0.5 = 0.5.
  5. Evaluate Q at L = 2.25: 2 × 2.25 = 4.5. Error 4.5 − 3.01 = 1.49.
  6. Q has a large error at both ends, so P is the far better formula.

Interpretation: a symbolic search that keeps P has found the square-root law and the constant 9.8 in one pass. Its result can be checked: the period grows with the square root of the length, which matches the known physics.

Try it: for candidate Q at L = 1.00, the prediction is 2 × 1.00 = 2.00 and the measured period is 2.01. What is the squared error for this point, and how does it compare with the squared error for Q at L = 0.25?

Answer: the squared error at L = 1.00 is (2.01 − 2.00)² = 0.0001, which is tiny. At L = 0.25 it is 0.5² = 0.25. So Q fits well near L = 1 and badly at the other two points, which is why a search that tests the whole table rejects it.

Common mistakes and tradeoffs

Mistake 1: "Symbolic regression is just polynomial fitting." It is tempting to think the method only tunes the coefficients of a polynomial. In fact the building blocks can include sin, exp, division and any other operator the user allows, and the shape of the tree is itself searched, because the search changes which operators appear and where. A polynomial is one special family that the search can reach.

Mistake 2: "The lowest error formula is the right law." It is tempting to think that the formula with the smallest error is the answer. In fact a long formula can fit noise in the measurements, which is Overfitting. This is why the size penalty exists, and why Cross-Validation on held-out data is used to check that a formula predicts new points, not only the ones it was fitted on. Model Selection makes the same argument for any family of models.

Mistake 3: "The search always finds the true formula." It is tempting to think that a long enough search reaches the correct expression. In fact the search is stochastic, so two runs can return different formulas that fit about equally well. The result should be treated as a candidate to test on new experiments, not as a proof.

Mistake 4: "A formula that fits explains the cause." A symbolic formula describes a relationship in the data. It does not show that one variable causes another, and a formula can match the data through a hidden third variable.

When to use it. Use symbolic regression when the goal is a short, readable relation, the variables are few, and the data are clean enough that a small formula can fit them. Check the result against units and known limits before trusting it.

When not to use it. When the data are high-dimensional, noisy, or the goal is only accurate prediction, a flexible model such as a neural network is usually easier to train and gives better predictions. The search cost also grows quickly with the number of variables.

Alternatives. A fixed-shape model such as Lasso Regression fits a linear formula with many candidate terms and sets most coefficients to zero, which is faster and more stable, but it can only choose among the terms it was given. Sparse identification methods such as SINDy take the same idea further for differential equations by starting from a fixed library of candidate terms.

History

In the early 1990s John Koza showed that populations of programs, stored as trees, could be bred with the same selection and crossover operations used in genetic algorithms. He called this genetic programming. Its main early use was to evolve small programs and expressions that solved test problems, and it gave the field a way to search over formulas rather than over fixed-length strings of numbers.

In 2009 Michael Schmidt and Hod Lipson applied symbolic search to measured motion data and showed that it could recover known physical relations, which made the approach a practical tool for science rather than only a benchmark. Through the 2010s the method was used in materials science, chemistry and engineering, and it remained slow for large problems.

Sparse identification approaches appeared around 2016, with SINDy by Brunton, Proctor and Kutz. They gave an alternative that works from a fixed library of candidate terms and selects the few that matter, which is often faster for dynamical systems. The two approaches are now used side by side.

Recap

  • A symbolic regression result is a formula, written as an expression tree, rather than a fixed shape with tuned numbers.
  • The search scores each candidate by its squared error on the data plus a penalty on its size.
  • Candidates are bred and mutated by a genetic algorithm, which needs no gradient of the formula.
  • A low error alone is not enough: the formula must also be short, and it should predict data it was not fitted on.
  • Read next: Program Synthesis applies the same kind of search to finding programs, and SINDy uses a fixed library of terms to discover governing equations.

Check yourself

QuestionRecall: what does a symbolic regression result look like, compared with the result of ordinary linear regression?Show answer

Ordinary linear regression fixes the shape of the formula (for example a straight line) and fits only its coefficients. Symbolic regression searches over the shape as well, so the output is a complete expression, such as 2x + 1 or x² + x, that can be read and checked.

QuestionCompute: a candidate formula predicts 4 at input x = 3, and the measured output there is 6. What is its squared error at this point? If the candidate has a size penalty with λ = 0.5 and size 4, what is its total score for this single point?Show answer

The error is 6 − 4 = 2, and the squared error is 2² = 4. The penalty is λ × size = 0.5 × 4 = 2. The total score is 4 + 2 = 6.

QuestionWhy: a search returns a formula with 25 nodes that fits every training point exactly. Why might a formula with 5 nodes and a small error on the same data be the better choice?Show answer

The 25-node formula may have fitted the noise in the measurements, which is overfitting, so it can give poor predictions on new points. The 5-node formula is more likely to capture the real relationship, it is easier to check against theory, and the size penalty in the score is built to prefer it when its error is nearly as small.

QuestionApplied: you want to predict the heat output of a battery from its current and temperature, and you need a formula that a controller engineer can check against the physics. Would you choose symbolic regression or a neural network, and what would you check before trusting the result?Show answer

Symbolic regression is the better fit when the formula must be read and checked, and there are only two inputs. Before trusting it, check that the units balance, that the formula behaves sensibly at extreme inputs, and that its error on held-out data (from Cross-Validation) is close to its error on the training data. A neural network may predict better but cannot be checked this way.

Sources