## Abstract

Closed-form, interpretable mathematical models have been instrumental for advancing our understanding of the world; with the data revolution, we may now be in a position to uncover new such models for many systems from physics to the social sciences. However, to deal with increasing amounts of data, we need “machine scientists” that are able to extract these models automatically from data. Here, we introduce a Bayesian machine scientist, which establishes the plausibility of models using explicit approximations to the exact marginal posterior over models and establishes its prior expectations about models by learning from a large empirical corpus of mathematical expressions. It explores the space of models using Markov chain Monte Carlo. We show that this approach uncovers accurate models for synthetic and real data and provides out-of-sample predictions that are more accurate than those of existing approaches and of other nonparametric methods.

## INTRODUCTION

Since the scientific revolution, interpretable closed-form mathematical models have been instrumental for advancing our understanding of the world. Think, for example, of Newton’s law of gravitation and how it has enabled us to predict astronomical phenomena with great accuracy and, perhaps more importantly, to interpret seemingly unrelated physical phenomena. With the data revolution, we may now be in a position to uncover closed-form, interpretable mathematical models of natural and even socioeconomic systems that were previously not amenable to quantitative analysis.

To be able to do this, however, we need to develop algorithms for automatically identifying these models (*1*–*4*). Following Evans and Rzhetsky, here, we call these algorithms, which would assist human scientists, “machine scientists” (*2*).

Attempts to design machine scientists date back to, at least, the 1970s and have led to very successful approaches in recent years. One of these approaches is based on genetic programming (*5*, *6*). In this approach, closed-form mathematical expressions are represented as graphs, and, given a goodness-of-fit metric, populations of expressions are created and evolved in such a way that high-fitness expressions are selected for further exploration. Another successful approach is based on sparse regression (*7*–*11*). In this approach, closed-form mathematical models are assumed to be linear combinations of some (linear or nonlinear) “basis functions” of the independent variables, and sparse regression is used to select and weigh the relevant basis functions. This approach is particularly suited to learn differential equations (*8*–*11*), whose form often follows the assumption of linearity on relatively simple basis functions. For more general situations, sparse regression can be combined with genetic programs that automatically generate the basis functions, thus relaxing the need to know them a priori (*12*, *13*).

Despite this remarkable progress, machine scientists have stumbled upon two major challenges. First, algorithms must balance goodness of fit and model complexity, thus avoiding overfitting. In general, this issue is dealt with by defining model complexity heuristically and then applying model selection criteria to the models that lay on the fit-complexity Pareto front. Both the definition of complexity and the choice of model-selection criterion are, however, hard to generalize and systematize. Second, machine scientists should, in principle, explore an arbitrarily large space of closed-form mathematical models. This is typically addressed by using methods such as genetic programming, which have no guarantees of actually exploring the best models more frequently; or by restricting the search space, for example, to linear combinations of the basis functions in sparse regression approaches, thus leaving out potentially valid models.

Here, we propose a Bayesian approach to the definition of machine scientists. To address the fit-complexity trade-off, we obtain the posterior probability of each expression from basic probabilistic arguments and explicit approximations. The posterior, which leads to consistent model selection, naturally combines the goodness of fit and a prior over expressions that accounts for model complexity. To establish the prior over expressions, we compile a corpus of closed-form mathematical models from Wikipedia and then use a maximum entropy approach to formalize a prior that is statistically consistent with the corpus (*14*). To address the challenges related to the exploration of the space of closed-form mathematical expressions, we introduce a Markov chain Monte Carlo (MCMC) algorithm that samples from the posterior over expressions. We demonstrate that the Bayesian machine scientist successfully recovers the true generating model when fed with synthetic data, even in situations in which state-of-the-art machine scientists fail (*4*). We also demonstrate that the machine is able to uncover accurate, closed-form mathematical models for systems for which no closed-form model has agreed on. Last, we find that the machine scientist provides out-of-sample predictions that are more accurate than those of other machine scientists and of standard nonparametric machine learning approaches, such as Gaussian processes (*15*).

## RESULTS

### Bayesian formulation of the problem and expression plausibility

Let us first formalize the problem in probabilistic terms. Consider a property *y* that can be expressed as an unknown, closed-form mathematical function *y* = *F*(*x*, θ) of *K* variables *x* = {*x*_{1}, …, *x _{K}*} and

*L*parameters θ ∈

*R*[for example,

^{L}*y*= sin (θ

_{1}

*x*

_{1}) or

*y*= θ

_{1}

*x*

_{1}+ θ

_{2}

*x*

_{2}]. Given some data

*D*= {(

*y*

^{1},

*x*

^{1}), …, (

*y*,

^{N}*x*)}, and assuming that the measurements have some experimental error

^{N}*y*=

^{k}*F*(

*x*, θ) + ϵ

^{k}*, the Bayesian machine scientist assigns to each possible closed-form mathematical expression*

^{k}*f*a plausibility

_{i}*p*(

*f*∣

_{i}*D*) given by the marginal posterior

*are the parameters associated with expression*

_{i}*f*, the integral is over the space Θ

_{i}*of possible values of these parameters,*

_{i}*Z*=

*p*(

*D*) does not depend on

*f*, and

_{i}*p*(

*f*) is the prior over expressions. The quantity

_{i}*f*, that is, the number of nats needed to jointly encode the data and the model with an optimal code (

_{i}*16*). Although in general the description length cannot be calculated exactly, it can be approximated in a number of ways (

*17*,

*18*); here, we take one of the simplest approximations

*B*(

*f*) is the Bayesian information criterion (BIC) of expression

_{i}*f*and can be readily calculated from the data (Supplementary text S1) (

_{i}*11*,

*17*). This is a first-order approximation to the description length and holds when the likelihood

*p*(

*D*∣

*f*, θ

_{i}*) is peaked around the maximum likelihood parameters*

_{i}*19*) or even calculate numerically the integral over the parameters. Beyond these potential limitations, Eqs. 1 to 3 naturally combine the goodness of fit of a model and its structural complexity, which is captured by the prior over expressions. In addition, in the limit of large datasets, we have ∣

*B*(

*f*)∣ ≫ ∣log

_{i}*p*(

*f*)∣; since

_{i}*B*(

*f*) is consistent, the machine scientist is also consistent, that is, in this limit it prefers the correct expression over any other expression with probability approaching 1.

_{i}### Sampling from the posterior distribution over expressions

The Bayesian machine scientist explores the space of closed-form mathematical expressions using MCMC. In particular, we introduce three move types that enable one to go from any closed-form expression to any other closed-form expression, thus enabling the machine scientist to explore, given enough time, the whole space of closed-form mathematical expressions (Materials and Methods) (Fig. 1). Regardless of the frequency of each move and other parameters of the Markov chain, MCMC samples expressions *f _{i}* from the stationary distribution

*p*(

*f*∣

_{i}*D*) (fig. S1). Although MCMC is slower than some alternative machine scientists that put emphasis on speed, such as evolutionary feature synthesis (EFS), the only dependency on the number of data points is in the estimation of the BIC of each model, and therefore its complexity scales as any other method using least squares to fit model parameters.

For model selection, the Bayesian machine scientist can use the most plausible expression from an MCMC run, that is, the maximum a posteriori (or minimum description length) expression. However, MCMC naturally samples over the whole space of models, thus generating arbitrarily long sequences of expressions; as we show below, this leads to a more complete characterization of the expression space and to higher out-of-sample prediction accuracy.

### Estimation of prior probabilities using a corpus of mathematical expressions

For the Bayesian machine scientist to be able to estimate the plausibility of a given expression, it needs to estimate the prior probabilities *p*(*f _{i}*). A common approach in model selection is to have no a priori preference for any given model over the others and assume that

*p*(

*f*) is the same for all models (

_{i}*17*,

*18*). In that case, and within our approximation for the description length, the most plausible model is simply the one with the lowest BIC. This is a consistent approach and generally reasonable when comparing a small number of simple models. However, it is inappropriate when considering a finite dataset and a very large (potentially infinite) space of mathematical expressions because one can always find a very complex model that fits the data arbitrarily well even with very few parameters. These unnecessarily complex models are likely to generalize very poorly in the same way that models with many parameters do—this structural overfitting is thus akin to traditional overfitting but arises from the large number of mathematical models considered rather than the large number of parameters (Supplementary text S4 and fig. S5). From this perspective, the prior over expressions acts as a model regularizer. It would also be possible to add other regularizers to expression trees, but, within our Bayesian framework, tree regularizers could still be cast as nonuniform priors, although they may be formally more complex and harder to interpret than the ones that we introduce below.

Given these considerations, the machine scientist needs to “learn” the a priori plausibility of models. Human scientists obtain this prior knowledge by studying science books and becoming familiar with the mathematical expressions that appear in them. We take a similar empirical approach to define the prior expectations of the machine scientist—we compiled a corpus of 4080 mathematical expressions that are included in Wikipedia entries (Materials and Methods)—and use these expressions to shape the prior expectations of the machine scientist, that is, to establish the statistical properties expected a priori for expressions (Fig. 2). To do this, we use an approach based on exponential random graphs (*20*–*23*). In particular, we choose a prior that generates expressions with the same average number of each type of operation [and their squares (*23*, *24*)] as in the empirical corpus and, given this constraint, satisfies the maximum entropy principle (*14*, *23*) (Materials and Methods; Supplementary text S2, fig. S2, and tables S3 and S4). As we show in Fig. 2, this prior generates mathematical expressions statistically consistent with the corpus. Note that the Bayesian machine scientist is not restricted to the 4080 expressions in the corpus, or even to arbitrary combinations of these expressions—all closed-form mathematical expressions are valid and can be visited by the MCMC; those that are statistically similar to the corpus are simply more plausible a priori. Note also that, as mentioned earlier, for large amounts of data, the prior washes out and the description length, as approximated in Eq. 3, is consistent regardless of the selection of prior.

### Validation of the Bayesian machine scientist with synthetic data

Having addressed the methodological challenges in the definition of the Bayesian machine scientist, we next demonstrate the ways in which it can be used and illustrate how one can get insights by using it. First, we test that, when fed with synthetic data, the machine scientist recovers the models that truly generated the data. We start by selecting an arbitrary expression and setting its parameters to values uniformly selected from [−2, 2]. The selected expression (Fig. 3) is *F*(*x*_{1},*x*_{2}; θ_{1},θ_{2}) = *x*_{1}(θ_{1} + *x*_{2}) cos(*x*_{1})/[θ_{2} log(θ_{2})], with θ_{1} = −1.19 and θ_{2} = 0.29. Note that this expression is not one of the expressions present in our empirical corpus.

We then feed the machine scientist with 400 noisy data points generated using this expression. The Bayesian machine scientist finds the correct model and assigns to it the maximum plausibility or, equivalently, the minimum description length (Fig. 3). To evaluate to which extent it is remarkable that the Bayesian machine scientist finds the correct model in this situation, we attempt the same task with state-of-the-art machine scientists (*4*), including two methods based on genetic programing [Eureqa (*5*) and ϵ-lexicase selection (EPLEX) (*6*)] and a method that combines genetic programming with sparse regression (Materials and Methods) [EFS; (*13*)]. We find that, from the same dataset, none of these methods are able to recover the correct model and that they tend to structurally overfit the data (Supplementary text S3).

The ability of the Bayesian machine scientist (and of any other method) to identify the correct model requires a minimum amount of data points. We find that, for this synthetic dataset, and even though the other approaches do not identify the correct expression with 400 points, the Bayesian machine recovers the true model with as few as 100 points (Supplementary text S3 and fig. S3).

Next, we investigate whether the machine is able to recover the differential equations that govern the Rössler system (*25*), and what is the effect of increasing observational noise in the equation discovery process. Since we are mostly interested in the effect of increasing noise in the target variable, we assume that the only measurement error is in the derivatives, although in some real situations derivatives may need to be estimated numerically from noisy measurements of the variables. In those situations, it may be necessary to use advanced techniques to estimate the derivatives (*26*). Under the conditions of our experiment, we find that the machine is able to recover the correct differential equations when the derivatives are measured with moderate noise (Fig. 4). When the measurement noise is high, the true expressions are still regarded as very plausible, but the machine identifies as the most plausible ones expressions that are “regularized” versions of the exact models. In these regularized models, small terms are disregarded; in all cases, the most plausible models are almost indistinguishable from the true ones (fig. S4). Thus, as expected, the machine scientist automatically adjusts the complexity of the models to the quality of the data (Supplementary text S4). Again, these results stand in contrast to those of alternative machine scientists [with the exception of pure sparse regression methods particularly suited to reverse-engineer differential equations (*8*–*11*), which would be able to recover the true expressions, at least in the case with low noise]. Even for the simplest case in this experiment (inference of

Last, we test the behavior of the machine scientist when presented with data that are generated from a model that does not have a closed-form mathematical expression in terms of the basic functions that it uses. In particular, we generate synthetic data using Bessel functions *J*_{α}(*x*) with α ∈ {0, 1, 2, 3, 4}. We find that the machine scientist is able to identify closed-form expressions that are as accurate as high-order Taylor expansions of the exact functions. We also find that, when the synthetic data are noisy and the machine scientist is restricted to choose among low-order series expansions, it consecutively chooses first-order, second-order, or third-order expansions, as the range of observed data increases and as the noise decreases, as one would expect (Supplementary text S5 and fig. S6).

### Use of the machine scientist on small datasets and on the Nikuradse dataset

Next, we turn to the analysis of real datasets. First, we analyze how the machine scientist provides insights into problems for which there are scarce and noisy data. We focus on three datasets that have been the subject of recent analyses and for which models have been proposed: a dataset on funding success in different European countries (*27*); a dataset on cell-to-cell stresses (*28*); and a dataset on stocks of salmon in the Fraser River in British Columbia, Canada (*29*).

For each of these three datasets, we compare existing models to models identified by the machine scientist in terms of their plausibility *p*(*f _{i}*∣

*D*), their BIC

*B*(

*f*), and their cross-validation error (Supplementary text S6 and tables S5 to S7). In all cases, the machine scientist identifies at least one model that is better than existing models in a Pareto sense, namely, better in at least one of the three performance metrics without being worse in any of the others.

_{i}Last, we show in more detail how the machine scientist can help us to solve major open scientific problems. For this, we focus on the classical experiment of turbulent friction in rough pipes performed in the early 1930s by Johann Nikuradse (*30*–*33*). In his experiments, Nikuradse measured the turbulent friction λ as a function of the roughness *x _{D}* of the pipe and the Reynolds number

*x*

_{R}. We take the original Nikuradse dataset and feed it to the machine scientist to study possible analytical expressions for the turbulent friction. A typical MCMC run uncovers numerous expressions that fit all observed data remarkably well (Fig. 5, A and B). We compare these expressions to the best expressions uncovered by other machine scientists (Materials and Methods): Eureqa (

*5*), EFS (

*13*), and EPLEX (

*6*) (see Supplementary text S7 and fig. S10 for EPLEX). The expressions uncovered by the Bayesian machine scientist fit the Nikuradse dataset better than those uncovered by Eureqa, which, in turn, is significantly better than all other benchmark methods.

The Bayesian machine scientist does not find any candidate expression that is overwhelmingly more plausible than all the others; rather, it uncovers a collection of similarly plausible models. This has two important implications. First, it points toward the need to revisit our tendency to look for single “best models” from data. Second, it suggests that, when using the machine scientist to make predictions, we should average over the whole ensemble of plausible models (*34*, *35*). In particular, the posterior predictive distribution for a point *y ^{k}* can be approximated as

*f*’s parameters, δ(

_{i}*x*) is the Dirac delta function, and the sum runs over all possible expressions

*f*. Note that estimating the posterior predictive distribution in this way makes interpretation of the predictions harder—even if each model

_{i}*f*is interpretable, averaging leads to a noninterpretable effective model. However, it is important to point out that this is the most comprehensive approach possible, in the sense that, even if one is certain that the data were generated with a single, unknown model

_{i}*F*, the best predictive distribution comes from taking all models into consideration, each weighted by its plausibility

*p*(

*f*∣

_{i}*D*) (

*34*).

In practice, the average over models can be computed using the MCMC sample collected by the machine scientist, and we can use the median of the posterior distribution *p*(*y ^{k}*∣

*D*;

*x*) to make predictions for

^{k}*y*that minimize the mean absolute predictive error. By considering, among all sampled models, the one that most resembles this median prediction, we obtain a single closed-form mathematical model that is optimally predictive—we call this model the median predictive model. To test the predictive power of the median predictive model in the Nikuradse dataset, we compare it to the alternative machine scientists (Eureqa, EPLEX, and EFS), as well as to a standard nonparametric Bayesian approach, Gaussian processes (

^{k}*15*). In particular, we test the ability of all approaches to generalize to data never seen before (Fig. 5, C and D). We find that the predictions of the Bayesian machine scientist are significantly more accurate than those of all alternative approaches.

The median predictive model, being a function of the roughness only for large Reynolds numbers, also predicts the expected limiting scaling for the turbulent friction (Fig. 5, E and F), which is remarkable considering that (i) most of the observed data correspond to a regime with different physics (*31*) and (ii) many of the sampled models do not scale correctly (fig. S9). Last, to fully exploit the potential of the machine scientist to obtain interpretable models of turbulent friction, we use it in combination with the known physics of the problem. In 1933, Prandtl suggested that the function *30*, *33*). We use the machine scientist to obtain the most plausible form for such universal function and get

## DISCUSSION

In the age of data, there is a pressing need to design algorithms capable of helping in the scientific process, from assisting in the proof of theorems to parsing scientific texts. Discovering closed-form mathematical models was one of the earliest tasks attempted, and yet, despite much progress in the area, two related difficulties have arisen repeatedly—deciding what is the correct balance between goodness of fit and model complexity and searching systematically through the space of models.

These are fundamental difficulties that stem from the nature of the problem that we aim to solve, and, therefore, no approach can possibly avoid them completely—one cannot establish an assumption-free measure of model complexity that serves all purposes or make the search space small without risking to leave out valid models or even the true generating model.

However, the Bayesian approach that we have introduced here forces us to be explicit and transparent about all the necessary assumptions and approximations to deal with these challenges. With regard to the fit-complexity trade-off, model complexity enters through our prior expectations about expressions. The use of an empirical corpus is somewhat arbitrary, and one may need to tune the corpus to the problem at hand; but it is reasonable to expect that, lacking any data, the machine scientist should propose models that statistically resemble existing phenomenological models in the literature. With our maximum entropy approach, we guarantee that the prior distribution is the most agnostic distribution satisfying this constraint. Besides, our approach makes it clear that, given enough data, prior expectations become irrelevant, and the machine scientist will consistently prefer the true model with probability approaching 1. With regard to the search through the space of models, MCMC guarantees that, asymptotically (that is, given enough data and time), the Bayesian machine scientist samples from the posterior distribution and, because it is consistent, visits the true model with the highest frequency.

Despite these advantages, our approach is not, of course, exempt of difficulties, the most pressing of which we have already pointed out. First, despite the virtues of MCMC for sampling the expression space, it may be necessary to develop more efficient approaches for very large datasets, without losing the guarantees provided by MCMC. Evidence for the need for these approaches would come from the observation that different MCMC runs on the same dataset result in different description length distributions, which would indicate that the stationary posterior is not being sampled correctly. Alternatives to sampling the full posterior using MCMC may range from Bayesian inference methods such as variational approximations to mathematical optimization methods, if suitable representations of the problem can be found and we are content with obtaining a single model and some bounds to its optimality. Second, it may be desirable to develop and compare other approaches to setting priors for expressions, perhaps based on noninformative priors or symmetry considerations, or empirical approaches different from the one that we have adopted here. A situation in which this need would be obvious is when the proposed models clearly violate some basic features of the desired solution, such as some particular limiting behavior or dimensional consistency. If such additional information is available, then it would be appropriate to formalize it in the prior. Last, in some situations involving small datasets and leading to broad likelihoods in parameter space, it may be necessary to use approximations to the description length that are more accurate than those based on the BIC, for example, the generalized BIC (*18*). However, this problem would be more difficult to diagnose in a practical situation because, even for a single dataset, the BIC can be a good approximation for some models but not for others. In any case, situations in which the BIC is a poor approximation are likely to involve small or very noisy datasets, situations in which the importance of the BIC is relatively small compared to that of the prior and simple models will be sampled anyway.

These challenges notwithstanding, our results suggest that, in practice, the Bayesian machine scientist is able to uncover models that are good in terms of both describing the observed data and predicting new data. Our approach can also be used in other contexts, for example when a phenomenological model has been proposed from data, and we aim to find whether there are other models that are more plausible or, at least, similarly plausible. Or, given two conflicting theories for the same process, the Bayesian machine scientist could be used to establish which one is more plausible and to what extent, and whether there exist other reasonable models. From a broader perspective, our approach sets the basis for further developing theories of model discoverability: Is it always possible to identify the correct model given the data? And if not, then under which conditions is the true model discoverable? In addition, our approach opens the door to addressing fundamental questions related to the limits of predictability for mathematical models: To what extent can a system be accurately predicted? Are closed-form, interpretable models as expressive as machine learning models such as deep neural networks? And if so, why? These are all important questions whose answer may lead to significant new insights about the scientific process and about our capacity to understand and describe the world using mathematics.

## MATERIALS AND METHODS

### MCMC moves for mathematical expression sampling

To perform a systematic exploration of the space of closed-form mathematical expressions, we represented closed-form mathematical expressions as expression trees. In these trees, internal nodes represent operations (for example, sum or exponential) and leaves represent variables or parameters (Fig. 1). To avoid problems with improper priors, we limited the size of expressions trees to 50 nodes. Although, in principle, one could make this number arbitrarily large, all parameter values and results in the paper are obtained using this value.

We classified the nodes of the expression tree based on the number of offspring that they have. Leaves (variables and parameters) have no offspring, whereas operations can have one offspring (operations that take only one argument, like the exponential function) or two offspring (operations that take two arguments, such as the sum). In table S1, we list all the operations that the machine scientist uses for building models.

An elementary tree (ET) is an expression tree (thus, a mathematical expression) that contains at most one operation. For the implementation of the Markov chain, we used a variety of moves (described in detail below) that operate by adding, removing, replacing, or modifying ETs. We call those ETs whose operation has *k* offspring *k*-ET. For example, *x* + *a* is a 2-ET (because the sum has two offspring: *x* and *a*), sin *x* is a 1-ET (because the sine function has a single offspring: *x*), and *x* is a 0-ET. Expressions such as sin(*x* + *a*) are not ETs because they contain more than one operation.

We designed an MCMC algorithm to sample expressions from the posterior distribution *p*(*f _{i}*∣

*D*), which gives the plausibility of an expression given the observed data. This distribution is given by Eq. 3. We used three types of move to update mathematical expressions (Fig. 1):

1) Node replacement. We replaced a node in the expression tree (selected uniformly at random among all nodes in the tree) by a randomly selected node, with the only restriction that the replacement must have the same number of offspring as the original node (for example, a “+” node can be replaced by a “*” node but not by an “exp” node). The offspring branches remain unchanged.

2) Root addition (RA). We added a new root to the expression tree. The new root can be any operation. If the operation takes one offspring, then the old expression tree becomes the offspring of the new root; otherwise, the old expression tree becomes the leftmost offspring of the operation, with the other offspring being randomly chosen 0-ETs (that is, variables or parameters). To be more precise, at the beginning of a sampling process, all possible replacement roots are enumerated (all operations with all possible 0-ET offspring in all positions other than the leftmost branch, which is left empty); when a RA is attempted, the new root is chosen uniformly among this list of candidates. The reverse move, root removal (RR), consists of removing the root of the expression tree and all its offspring except for the leftmost branch, which becomes the new expression tree. RR is only possible when all branches except the leftmost one are 0-ETs.

3) Elementary tree replacement (ETR). We replaced a randomly selected ET in the complete expression tree by another randomly selected ET. At the beginning of a sampling process, all possible ETs are enumerated (all operations with all possible 0-ET offspring). In each move, an ET (chosen uniformly at random among all ETs in the expression tree) is replaced by an ET chosen uniformly at random among all possible ETs.

The ETR move introduces small variations to expression trees and is therefore the major source of expression variation. By contrast, node replacement moves often introduce major changes in expressions (for example, replacing a sum by a product often alters a model very significantly) and are therefore not very efficient. However, they are useful in that they represent long jumps in the space of models. Last, root replacement is the only move that can make trees grow/shrink at the top and is useful to add/remove terms to models that are already reasonable. Without root replacement, adding an additive term to an expression tree would require disassembling the whole tree and assembling it again with the additional term from the beginning.

### MCMC acceptance rules

At each MCMC step, we attempt one of the three moves described above and accept or reject the proposed move according to Metropolis’ rule (*36*)*g*(*f*_{f} ∣ *f*_{i}) is the distribution of movement proposal *f*_{i} → *f*_{f} (where f and i stand for final and initial, respectively). This rule ensures that the stationary distribution is *p*(*f*_{i}∣*D*)*Z* being a normalizing constant and ℒ(*f _{i}*) being the description length, as defined and approximated in the main text

For all samples in the paper, we attempt a root replacement move 5% of the time, a node replacement move 45% of the time, and an ETR 50% of the time. These rates are arbitrary and should not affect the achievement or the equilibrium distribution but do have an effect on how fast the Markov chain converges to the equilibrium distribution.

In what follows, we describe the specific form of the acceptance (Eq. 5) rule for each movement.

1) Node replacement (NR). Since the node to change is chosen uniformly at random and so is the new node, the move is symmetric, that is, the probability of attempting a move and its reverse are the same *g*(*f*_{f}∣*f*_{i}) = *g*(*f*_{i}∣*f*_{f}). Therefore, the acceptance probability in this case is given simply by the difference in the description lengths of the expression trees

2) RA and RR. In this case, the proposal distribution for the RA and RR movements is not symmetric. The RR move is deterministic in that it always affects the existing root of the expression tree. By contrast, the RA move involves selecting uniformly at random among all possible *N*_{root} roots that can be added (which are enumerated once at the beginning of the sampling process, as described above). Therefore, if *p*_{RR} is the probability of selecting this type of move, then *g*(RR) = *p*_{RR} and *g*(RA) = *p*_{RR}/*N*_{root} so that *g*(RR)/*g*(RA) = *N*_{root}, which gives the following acceptance rules*N*_{root} is the number of possible roots among which we choose in the RA move.

3) ETR. The ETR move is the most involved in terms of defining the move proposal probabilities necessary to define the acceptance rule. First, we need to specify exactly how we choose the attempted move. We start by selecting the orders *o*_{i} and *o*_{f} of the existing (initial, i) and replacement (final, f) ETs. For example, we may choose to replace a 0-ET (*o*_{i} = 0, a constant or a variable) by a 2-ET (*o*_{f} = 2, e.g., a sum or a product). In this initial choice, we need to take into consideration the number *n*_{if} of options that we have to choose *o*_{i} and *o*_{f} (for example, if a tree has reached the maximum allowed size *o*_{f} ≤ *o*_{i} necessarily). Once the orders *o*_{i} and *o*_{f} are selected, we need to account for (i) all the possible ETs of order *o*_{i} in the initial tree Ω_{i} that we can choose to replace and (ii) all the possible ETs of order *o*_{f} that we can choose to replace the ET in the initial tree, *s*_{f}. If *p*_{ETR} is the probability with which we choose this move, then taking into account the previous considerations *g*(*o*_{f}∣*o*_{i}) = *p*_{ETR} × 1/*n*_{if} × 1/Ω_{i} × 1/*s*_{f}. Conversely, *g*(*o*_{i}∣*o*_{f}) = *p*_{ETR} × 1/*n*_{fi} × 1/Ω_{f} × 1/*s*_{i}, so that the acceptance rule is

To validate that these moves and these acceptance rules lead to sampling from the equilibrium distribution *p*(*f*_{i}∣*D*), we use the same example as in Fig. 1. We generate data as in Fig. 1 and sample expressions using MCMC, limiting the expressions to use only “+” and “sin” operations, a single variable *x*, a single parameter *a*, and a maximum of seven nodes. In fig. S1, we show that, as expected, the equilibrium distribution is *p*(*f _{i}*∣

*D*).

### Avoidance of expression duplicates in MCMC

The mapping of expressions to expression trees is not one to one because several expression trees can represent the same expression (for example, the expression *x* + *a* can be encoded in an expression tree with *x* or with *a* on the left branch). To avoid overcounting expressions, we internally reduce all expression trees to a “canonical form” using the Python module Sympy (*37*). Then, if an expression tree that is visited for the first time reduces to an expression that is equivalent to that of a previously visited expression tree, then the current tree is forbidden (assigned infinite description length) and never visited again, so that only one representative of each expression remains.

### Parallel tempering MCMC

The search space for the MCMC is extremely rugged, with some neighboring expressions having very different description lengths. This makes the sampling process problematic and prone to getting trapped in local minima. To partly alleviate this problem, we used parallel tempering (*38*). In traditional parallel tempering (typically used in physics for spin glasses and other disordered systems), several replicas of the sampling process are kept at logarithmically spaced and increasing temperatures—at high temperatures, the sampling easily escapes local minima, whereas at low temperatures, the sampling explores configurations that are physically more meaningful. From time to time, samples at consecutive temperatures are switched with a rule that guarantees detailed balance at all temperatures. Therefore, the lowest temperature sample is always equilibrated at the desired temperature, but the configuration space is explored more efficiently because of the high temperature samples exploring larger portions of the space.

For our purposes here, we introduce a computational temperature *T* and sample from the distributions

With this definition, *p*(*f*_{i},*T* = 1) = *p*(*f*_{i}∣*D*) is the equilibrium distribution from which we aim to sample, and *p*(*f*_{i},*T* = ∞) = *p*(*f*_{i}) is the prior distribution, independent of the data.

The results of the manuscript were obtained with 40 different temperatures defined as *T _{k}* = 1.05

*, with*

^{k}*k*= {0,1, …,39}. From all these, we take expressions only from the

*T*

_{0}= 1 sample (Fig. 3). In addition, for the Nikuradse dataset, we used four restarts of the parallel tempering MCMC so that the sampling is even better equilibrated.

### Parsing of Wikipedia expressions for expression priors

We compiled all the mathematical expressions included in pages listed under Wikipedia’s “List of scientific equations named after people,” which we surmise often correspond to phenomenological models of the kind that we aim to identify (as opposed, for example, to derivations of mathematical identities, proofs of mathematical theorems, etc.).

In Wikipedia, expressions are encoded in such a way that some of them are ambiguous without the context provided by the text in the page, so fully automatic parsing of expressions is virtually impossible. For example, the expression *x ^{a}* +

*x*could represent the sum of the two components of a vector or the sum of two powers of

^{b}*x*. We designed an algorithm that parses Wikipedia expressions into Sympy (

*37*), using heuristics for the ambiguous cases (the full code for expression parsing is available at https://bitbucket.org/rguimera/machine-scientist/src/no_degeneracy/Process-Formulas/). We then selected three random subsets of 200 expressions out of the 4080 expressions that could be parsed and verified manually that the parsings were meaningful (accepting parsings that may be wrong once the context is known but that are otherwise mathematically correct). We refined the heuristics until we found that there were fewer than 5% of errors in the parsed expressions of all three subsets. In table S2, we show a small sample of the corpus.

### Prior definition

To define the prior distribution over mathematical expressions, we took advantage of the fact that mathematical expressions can be represented as graphs and used an approach based on exponential random graph models (*20*–*23*). As in exponential random graphs models, we aimed to generate mathematical expressions (graphs) with statistical properties similar to those in the corpus. Specifically, we aimed to generate mathematical expressions for which the average number of each operation per expression is the same as in the corpus (for example, in the corpus, the average number of sums per expression is 〈*n*_{+}〉 = 0.312). We also aimed to reproduce the average of the square of the number of each operation per expression (for example, the average of the square of the number of sums per expression, *23*, *24*). As in exponential random graph models, we selected the prior probability *p*(*f*_{i}) that generates expressions with these desired average properties and that, at the same time, is maximally uninformative and therefore consistent with the maximum entropy principle (*14*, *23*, *39*). This prior is given by* _{o}* and β

*are hyperparameters that we fitted so that expressions generated from the prior were consistent with the corpus (*

_{o}*22*,

*23*).

### Fitting of prior hyperparameters

To fit the α* _{o}* and β

*hyperparameters, we proceeded as follows:*

_{o}1) Obtain, from the empirical corpus of mathematical expressions, the average number 〈*n _{o}*〉

^{target}of each operation

*o*per expression and the average of the square

2) Set some initial values for the parameters.

3) Repeat until the change in parameters values is small enough:

(i) Generate, using the MCMC, a large number of expressions (typically 1 million to 10 million).

(ii) Measure 〈*n _{o}*〉

^{measured}and

(iii) Update the α* _{o}* parameters as follows

(iv) Update the β* _{o}* parameters as follows

*becomes negative, then its value is set to 0.*

_{o}(v) Repeat from (i).

This method is adapted from an exisiting method to fit the parameters of exponential random graphs (*22*). A typical evolution of the parameter values is shown in fig. S2, and the parameter values obtained are listed in tables S3 and S4 (Supplementary text S2).

### Benchmark machine scientists

We benchmarked the performance of the Bayesian machine scientist against three other machine scientists: Eureqa (*5*), EPLEX (*6*), and EFS (*13*). Eureqa uses a genetic algorithm to search the space of expressions (*5*). Eureqa requires that a complexity penalty is set for each operation type; we set these penalties to their default values and selected the same basic operations used by the Bayesian machine scientist. We also selected the default fitness function. We ran Eureqa for several weeks in each experiment, until the number of evaluated expressions is, at least, 10^{13}. Eureqa is available at www.nutonian.com/download/eureqa-desktop-download/.

EPLEX is another algorithm based on genetic programming and was the top performer in a recent systematic comparison of state of the art machine scientists (*4*). We ran EPLEX in the same conditions as in that study (implementation available at https://epistasislab.github.io/ellyn/); in particular, we obtained 10^{6} models with a population size of 1000 and 1000 generations. Moreover, we repeated this procedure 10 times and selected the realization with the best fitness among the 10 repetitions. However, in all our experiments, EPLEX gives results that are considerably worse than Eureqa’s, so we do not show its results in the main text (Supplementary text S7 and fig. S10).

Last, EFS is a method based on sparse regression that generates basis functions automatically using a genetic algorithm (*13*). Thus, this approach has the advantages of sparse regression, and, at the same time, it does not require a priori knowledge of the basis functions. EFS is an evolution of multiple regression genetic programming (*12*), which was also a top performer in (*4*), although somehow more inconsistent than EPLEX. EFS is fast and gives expressions within seconds. Thus, we were able to repeat the training process 100 times for each experiment and select the model with the best default fitness measure (mean squared error). We used the implementation of EFS available at http://flexgp.github.io/efs/.

### Gaussian process models

For the comparison of the out-of-sample predictions of Gaussian process models to those of the machine scientist, we proceeded as follows. We trained the Gaussian process using the scikit-learn implementation (*40*). We tested different kernels (including radial-basis function kernels, Matérn kernels, white kernels, rational quadratic kernels, exponential kernels, and linear and quadratic combinations of these), and we also tested the effects of using *D*/*r* or its logarithm as the feature. Among these, we selected the combination that gave the best results (an radial-basis function plus white kernel with logarithmic *D*/*r*)—all results reported in the main text correspond to this combination.

## SUPPLEMENTARY MATERIALS

Supplementary material for this article is available at http://advances.sciencemag.org/cgi/content/full/6/5/eaav6971/DC1

Supplementary Text S1. Calculation of the BIC

Supplementary Text S2. Prior parameter values

Supplementary Text S3. Validation and benchmarking on synthetic data

Supplementary Text S4. Rössler system

Supplementary Text S5. Recovery of Bessel functions

Supplementary Text S6. Results for small datasets

Supplementary Text S7. Nikuradse dataset

Table S1. Operations used by the machine scientist and their properties.

Table S2. Sample of the expressions in the empirical corpus.

Table S3. Values of the α prior parameters for different number of variables, *N _{v}*, and parameters

*N*.

_{p}Table S4. Values of the β prior parameters for different number of variables, *N _{v}*, and parameters

*N*.

_{p}Table S5. Models for the funding application success in different European countries as a function of socioeconomic indicators of the countries and of their ability to attract and retain scientific talent.

Table S6. Models for the cell-to-cell stresses as a function of the concentration of several cell adhesion proteins.

Table S7. Models for the (logarithm of the) Seymour stock of salmon in the Fraser River system in British Columbia, Canada as a function of several ecological indicators.

Fig. S1. Equilibrium distribution of the MCMC.

Fig. S2. Fitting of the parameters of the prior distribution for expressions.

Fig. S3. Ability of the Bayesian machine scientist to identify the correct model from synthetic data as a function the number of observed points.

Fig. S4. True and most plausible models of the Rössler system in high-noise scenarios.

Fig. S5. True and minimum BIC models of the Rössler system.

Fig. S6. Recovery of Bessel functions.

Fig. S7. Out-of-sample predictions for small datasets.

Fig. S8. Energy distribution for the Nikuradse dataset.

Fig. S9. Sampled models.

Fig. S10. EPLEX models and out-of-sample predictions for the Nikuradse dataset.

Movie S1. Video of the Bayesian machine scientist finding models for a synthetic dataset.

This is an open-access article distributed under the terms of the Creative Commons Attribution-NonCommercial license, which permits use, distribution, and reproduction in any medium, so long as the resultant use is **not** for commercial advantage and provided the original work is properly cited.

## REFERENCES AND NOTES

**Acknowledgments:**We thank E. G. Altmann, L. A. N. Amaral, A. Arenas, J. Bonet Avalos, and D. Shasha for helpful comments and suggestions. We thank I. Arnaldo for help with the EFS software. We thank A. Arenas for pointing us toward the Nikuradse dataset. We thank M. De Domenico and A. Arenas for sharing the financial success dataset. We thank E. Bazellières and X. Trepat for sharing the cell adhesion dataset.

**Funding:**This project has received funding from the Spanish Ministerio de Economia y Competitividad (FIS2015-71563-ERC, FIS2016-78904-C3-P-1, and DPI2016-75791-C2-1-P). F.A.M. acknowledges financial support by the Spanish MINECO grant PTQ-14-06718 (2016-2019) of the Torres Quevedo Programme.

**Author contributions:**R.G. conceived the research. R.G., I.R., A.A.-M., F.A.M., M.M., and M.S.-P. contributed methods, wrote code for the computational experiments, and carried out computational experiments. All authors designed the computational experiments and interpreted the results, and wrote and edited the manuscript.

**Competing interests:**The authors declare that they have no competing interests.

**Data and materials availability:**A Python implementation of the Bayesian machine scientist and all data needed to evaluate the conclusions in the paper are publicly available from Bitbucket at https://bitbucket.org/rguimera/machine-scientist. Additional data related to this paper may be requested from the authors.

- Copyright © 2020 The Authors, some rights reserved; exclusive licensee American Association for the Advancement of Science. No claim to original U.S. Government Works. Distributed under a Creative Commons Attribution NonCommercial License 4.0 (CC BY-NC).