What Is Symbolic Regression? A Guide with a Real Example

Updated

Symbolic regression is a machine learning method that finds a mathematical formula that fits your data. You give it a table of numbers, and it returns equations like y = 0.33*x1*sqrt(x2). It searches for both the structure of the formula (which variables, operations and functions appear in it) and its constants. Ordinary regression only does the second part: it fits the constants of a formula you chose in advance.

That difference is the whole point. With linear regression, you decide the model is a straight line, and the algorithm finds the slope. A neural network makes accurate predictions with thousands of parameters that nobody can read. Symbolic regression searches for the model itself, and what you get is an interpretable model: a formula you can read, check against theory, paste into a spreadsheet, and, when it has captured the real mechanism, trust outside the range of the data it was fitted on.

Key takeaways:
  • Symbolic regression searches the space of formulas, not only the constants of one formula.
  • It returns a list of formulas that trade accuracy for simplicity; the useful one is usually where the error stops falling fast.
  • It works best with a handful of input variables (up to a few dozen) and tens to tens of thousands of rows, when you want to understand a relationship or deploy a compact model.
  • It has produced published results in physics, astronomy, engineering, computer graphics and mathematics.

An example: finding Kepler's third law in 30 seconds

In 1619, Johannes Kepler published the relationship between a planet's distance from the Sun and the time it takes to go around it, after years of work on Tycho Brahe's observations. It makes a good test, because we know the answer. Here is the data for the eight planets and Pluto, from NASA's Planetary Fact Sheet, converted to astronomical units (Earth's distance from the Sun) and years:

PlanetDistance (AU)Period (years)
Mercury0.3870.241
Venus0.7230.615
Earth1.0001.000
Mars1.5241.881
Jupiter5.20411.857
Saturn9.57229.423
Uranus19.16583.747
Neptune30.181163.721
Pluto39.482247.936

We gave this table to TuringBot and let it search for 30 seconds on a laptop. Like most symbolic regression tools, it doesn't return a single formula: it returns the most accurate formula it found at each size, leaving out sizes that don't improve on a smaller formula. Size adds up the variables, constants and operations in a formula, with more complex functions counting more: pow counts 4, so pow(distance, 1.4989) has size 6. This is part of what it returned (constants rounded):

SizeRMS error (years)Formula for the period
184.260.05 (a constant)
315.95.64 * distance
510.80.167 * distance * distance
60.57pow(distance, 1.4989)
80.510.965 * pow(distance, 1.5088)
150.0900.00618 * (160.7 + sin(distance)) * distance * sqrt(distance)
350.0000000002A formula with a cosine and ten fitted constants
RMS error versus formula size for the Kepler data: the error falls from 10.8 to 0.57 years at size 6, the formula pow(distance, 1.4989), and only slowly after that, until formulas of size 35 and more fit the nine points almost exactly
Error against formula size for the run above. The knee at size 6 is Kepler's third law; the plunge at size 35 is overfitting.

Read it from the top. Each step up in size buys some accuracy, until size 6, where the error drops almost twentyfold, from 10.8 to 0.57 years. That formula says the period grows with the distance to the power 1.4989: Kepler's third law, period = distance3/2, to within 0.1% of the exponent. After that, the error keeps falling, but by small steps and with pieces that have no physical meaning, like round and sin(distance): the formulas are bending themselves around nine data points. By size 35, a formula with a cosine and ten fitted constants, more constants than there are data points, matches every planet to within a fraction of a second, and means nothing.

That is how symbolic regression is used in practice. You look at the trade-off between size and error, and the formula where the error stops falling fast is usually the one that captured the mechanism. The ones after it are fitting noise. With more than a few dozen rows, you confirm this with a test sample: hold back part of the data, and see which formulas still work on rows they never saw.

Units matter, too. Given the same data in millions of kilometers and days, TuringBot found the same law, 0.184 * pow(distance, 1.509), but it needed a constant to convert the units, which made the formula larger and harder to spot.

How symbolic regression works

A formula can be written as a tree: 0.5*x*sqrt(x) is a multiplication whose branches are a constant, a variable and a square root. A symbolic regression algorithm searches the space of all such trees built from the variables in your data and a set of building blocks (addition, multiplication, division, powers, exp, log, sin, and so on). That space grows explosively with the size of the formula, and finding the best formula in it has been proven to be an NP-hard problem (Virgolin and Pissis, 2022). So practical methods either search heuristically or limit the search to small formulas or to sums of terms from a fixed list, and a heuristic search that runs longer can find better formulas.

The search has two goals that pull in opposite directions: low error and small size. Instead of combining them into one score, most tools keep the best formula for each size, the list you saw above. In the language of optimization, this is the Pareto front: no formula on it can be made more accurate without also getting larger.

The main families of search methods are:

  • Genetic programming, popularized by John Koza's 1992 book and used by most symbolic regression tools. A population of formulas evolves: the better ones are combined and mutated, and the worse ones discarded.
  • Simulated annealing, used by TuringBot. Formulas are changed one modification at a time, and worse changes are sometimes accepted, more often early in the search, so it doesn't get stuck on the first decent formula it finds.
  • Physics-inspired decomposition, as in AI Feynman (Udrescu and Tegmark, 2020), which uses units, symmetries and separability to split a problem into smaller ones before searching.
  • Neural network-guided search, where a neural network proposes candidate formulas. In deep symbolic regression (Petersen et al., 2021), the network is trained by reinforcement learning on the dataset at hand; transformer models (for example, Kamienny et al., 2022) are instead pretrained on millions of synthetic equations and output a formula in one pass, which is then refined.
  • Sparse regression, as in SINDy (Brunton, Proctor and Kutz, 2016). It builds a long list of candidate terms (x, x², xy, sin x, …) and uses linear regression with a penalty that keeps only a few of them. It works well for finding differential equations, but it can only find formulas that are weighted sums of the terms in the list.

In most modern tools, the numerical constants of each candidate formula are fitted by a separate numerical optimization inside the structural search.

Symbolic regression vs. other kinds of regression

Linear or curve fittingSymbolic regressionNeural networks, gradient boosting
You chooseThe formulaThe building blocksThe architecture
It findsThe constantsThe formula and its constantsThousands to billions of parameters
ResultA formula you already knew the shape ofA formula you can readA model you can run, but not read
Outside the data's rangeGood if the formula is rightGood if it captured the mechanismUsually poor
Best withA known modelFew variables, an unknown relationshipMany variables, lots of data

When to use it, and when not to

Symbolic regression is a good fit when:

  • You want to understand a relationship, not only predict it: an empirical law, a design equation, a correlation for a report.
  • You need a compact model that runs anywhere: in a spreadsheet, on a microcontroller, as one line of code in a simulation.
  • You want a fast approximation of an expensive computation, like a finite element model or a numerical simulation.
  • Your data has a handful of input variables, up to a few dozen, and from tens to tens of thousands of rows.

It is the wrong tool when:

  • The inputs are images, audio, text, or thousands of features. That is what neural networks are for.
  • You only care about prediction accuracy on a large, complex dataset. Gradient boosting will usually be more accurate, and a formula that matches it may not exist.
  • The data is small and noisy. No method can find a reliable formula in it, and symbolic regression will find one that looks convincing anyway, so a test sample is essential.

What people have found with it

The best-known early demonstration is Schmidt and Lipson (2009), whose algorithm recovered Hamiltonians, Lagrangians and conservation laws from motion-tracking data of oscillators and a chaotic double pendulum, with no prior knowledge of physics.

In published research, the formula is often the result itself. A few examples from papers that used TuringBot:

  • Computer graphics. Rendering realistic fur at a distance needs a term for how light fades through the fibers. A simple exponential did not fit, so the authors derived Ad = (1.216 + 0.331·N·ρ)−0.623 − 0.038 with symbolic regression, where N is the number of fibers merged into one and ρ how densely they fill it. (ACM Transactions on Graphics, 2022)
  • Astrophysics. New color–magnitude calibrations for blue horizontal-branch stars, from SDSS and Gaia data. Calibrated on stars in globular clusters with known distances, they estimate the stars' absolute magnitudes, and from them their distances; symbolic regression showed that the (u−r) color worked better than the traditional (g−r), with no need for metallicity. (The Astrophysical Journal, 2022)
  • Mathematics. The perimeter of an ellipse has no exact closed-form formula. The authors used symbolic regression with integer coefficients to find new approximations, benchmarked against a historical collection of them. (Algorithms, 2024)
  • Wind energy. A stall delay correction model for predicting wind turbine blade performance, checked on a second rotor. (Wind, 2022)
  • Explainable AI. A neural network fitted to Tycho Brahe's observations of Mars, turned into formulas with symbolic regression, recovering Kepler's first and second laws and Newton's law of gravitation. (arXiv, 2021)
  • Civil engineering. Equations for how fiber-reinforced rubberized concrete strengthens under high strain rates. (Construction and Building Materials, 2022)

There are more in the list of publications that used TuringBot.

Practical tips

  • Allow only building blocks that make sense for your problem. Defaults differ between tools (some start with arithmetic only; TuringBot enables most functions), and every function the data doesn't need is another way to fit noise, like the sin(distance) terms in the Kepler run. Add exp for growth and decay, sin and cos for periodic data.
  • Use a test sample whenever you have more than a few dozen rows, and judge formulas by their test error.
  • Pick the formula where the error stops falling fast, not the most accurate one at the bottom of the list.
  • Choose sensible units and scales. As the Kepler example shows, the same law can be a smaller formula in better units.
  • Choose the error metric for your goal. When the target spans orders of magnitude, like the periods above, relative error treats Mercury and Pluto equally; RMS error is dominated by the largest values.
  • Check the formula against what you know: its units, its sign, what happens as each variable goes to zero or grows.

Symbolic regression software and Python libraries

ToolWhat it isSearch method
TuringBotDesktop app for Windows, macOS and Linux, with a command line and a Python library. Free for up to 200 rows and 5 columns.Simulated annealing
PySROpen-source Python libraryEvolutionary (genetic programming)
gplearnOpen-source Python library with a scikit-learn interfaceGenetic programming
OperonOpen-source C++ libraryGenetic programming
AI FeynmanOpen-source research codePhysics-inspired decomposition and brute force
EureqaThe best-known commercial tool, which grew out of Schmidt and Lipson's 2009 work at Cornell. Its company, Nutonian, was acquired by DataRobot in 2017, and Eureqa is no longer sold on its ownGenetic programming

For an independent comparison of methods, see SRBench (La Cava et al., 2021), which benchmarked 14 symbolic regression methods and 7 other machine learning methods on 252 regression problems.

Frequently asked questions

Is symbolic regression machine learning?

Yes. It is supervised learning: it learns a model from examples of inputs and outputs. The difference is that the model is a formula.

How is it different from curve fitting?

Curve fitting finds the constants of a formula you choose, like a line or an exponential. Symbolic regression also chooses the formula.

How much data does it need?

It depends on how noisy the data is and how complex the relationship. Nine precise measurements were enough for Kepler's law. Noisy measurements need more rows and a test sample to tell real structure from noise.

Can it find exact laws of physics?

When the data is precise and the law can be built from the building blocks you allow, yes. The example above recovers Kepler's third law with an exponent of 1.4989 instead of 1.5, because the tabulated distances don't follow the law perfectly (Neptune's is almost 1% off what its period implies). Benchmarks like the Feynman equations in SRBench measure how often methods recover known physics formulas exactly.

Is symbolic regression deep learning?

No. Some methods use neural networks to guide the search, but the result is a formula, not a network.

Can I do symbolic regression in Python?

Yes: PySR and gplearn are Python libraries, and TuringBot has a Python library that runs searches from scripts and notebooks, and also provides a scikit-learn estimator. For a walkthrough, see symbolic regression in Python in 3 steps and our R example.

Try it yourself

Copy this into a file called planets.csv, open it in TuringBot (the free version handles it), and press Start search. Kepler's law should appear within seconds.

distance_au,period_years
0.3870,0.2409
0.7233,0.6152
1.0000,0.9998
1.5241,1.8809
5.2039,11.8574
9.5723,29.4232
19.1647,83.7467
30.1809,163.7208
39.4818,247.9357

References

  • Koza, J. R. (1992). Genetic Programming: On the Programming of Computers by Means of Natural Selection. MIT Press.
  • Schmidt, M., and Lipson, H. (2009). Distilling free-form natural laws from experimental data. Science, 324(5923), 81–85. doi:10.1126/science.1165893
  • Udrescu, S.-M., and Tegmark, M. (2020). AI Feynman: A physics-inspired method for symbolic regression. Science Advances, 6(16). doi:10.1126/sciadv.aay2631
  • Brunton, S. L., Proctor, J. L., and Kutz, J. N. (2016). Discovering governing equations from data by sparse identification of nonlinear dynamical systems. PNAS, 113(15), 3932–3937. doi:10.1073/pnas.1517384113
  • Petersen, B. K., et al. (2021). Deep symbolic regression: Recovering mathematical expressions from data via risk-seeking policy gradients. ICLR 2021. arXiv:1912.04871
  • Kamienny, P.-A., d'Ascoli, S., Lample, G., and Charton, F. (2022). End-to-end symbolic regression with transformers. NeurIPS 2022. arXiv:2204.10532
  • La Cava, W., et al. (2021). Contemporary symbolic regression methods and their relative performance. NeurIPS Datasets and Benchmarks. arXiv:2107.14351
  • Virgolin, M., and Pissis, S. P. (2022). Symbolic regression is NP-hard. Transactions on Machine Learning Research. arXiv:2207.01018
  • NASA Goddard Space Flight Center. Planetary Fact Sheet.

Find the formula behind your data

Try it on your own data. TuringBot runs on your machine and returns an explicit equation you can read, check and publish, not a black box.

Free version for Windows, macOS and Linux. No signup, no credit card.

Download for free