Equation Samples¶
Here, we use an equation sampler to sample random equations and then use them as AutoRA experiment runners. This can be used in a closed loop to benchmark or train novel AutoRA Experimentalists or AutoRA Theorists.
First install the packages via pip:
!pip install "autora-synthetic-abstract-equation"
!pip install "equation-sampler"
Collecting autora-synthetic-abstract-sympy-equation Downloading autora_synthetic_abstract_sympy_equation-0.0.1-py3-none-any.whl (7.4 kB) Collecting autora-core (from autora-synthetic-abstract-sympy-equation) Downloading autora_core-3.3.0-py3-none-any.whl (28 kB) Collecting autora-synthetic (from autora-synthetic-abstract-sympy-equation) Downloading autora_synthetic-1.0.3-py3-none-any.whl (13 kB) Requirement already satisfied: scikit-learn in /usr/local/lib/python3.10/dist-packages (from autora-synthetic-abstract-sympy-equation) (1.2.2) Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from autora-synthetic-abstract-sympy-equation) (1.11.1) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from autora-core->autora-synthetic-abstract-sympy-equation) (1.22.4) Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from autora-core->autora-synthetic-abstract-sympy-equation) (3.7.1) Requirement already satisfied: pandas in /usr/local/lib/python3.10/dist-packages (from autora-core->autora-synthetic-abstract-sympy-equation) (1.5.3) Requirement already satisfied: scipy>=1.3.2 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->autora-synthetic-abstract-sympy-equation) (1.10.1) Requirement already satisfied: joblib>=1.1.1 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->autora-synthetic-abstract-sympy-equation) (1.3.1) Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.10/dist-packages (from scikit-learn->autora-synthetic-abstract-sympy-equation) (3.2.0) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->autora-synthetic-abstract-sympy-equation) (1.3.0) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (1.1.0) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (0.11.0) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (4.41.1) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (1.4.4) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (23.1) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (9.4.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (3.1.0) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (2.8.2) Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.10/dist-packages (from pandas->autora-core->autora-synthetic-abstract-sympy-equation) (2022.7.1) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib->autora-core->autora-synthetic-abstract-sympy-equation) (1.16.0) Installing collected packages: autora-core, autora-synthetic, autora-synthetic-abstract-sympy-equation Successfully installed autora-core-3.3.0 autora-synthetic-1.0.3 autora-synthetic-abstract-sympy-equation-0.0.1 Collecting equation-sampler Downloading equation_sampler-0.0.4-py3-none-any.whl (11 kB) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from equation-sampler) (1.22.4) Requirement already satisfied: sympy in /usr/local/lib/python3.10/dist-packages (from equation-sampler) (1.11.1) Requirement already satisfied: mpmath>=0.19 in /usr/local/lib/python3.10/dist-packages (from sympy->equation-sampler) (1.3.0) Installing collected packages: equation-sampler Successfully installed equation-sampler-0.0.4
Then import the sample_equations, to_sympy
function to your project
from equation_sampler import sample_equations, to_sympy
Let's generate a sample equation using the Equation Sampler library. Please note that the Equation Sampler offers various settings for sampling, including priors. In this example, we will focus on using basic functionality. For more detailed information and options, you can refer to the Equation Sampler's documentation.
# First, we declare the operator and function space
functions = [ "sin", "cos","tan","exp","log","sqrt","abs"]
operators = ["+","-","*","/","^"]
# We also use a seed to get reproducable results:
import numpy as np
np.random.seed(42)
# then we can sample an equation:
equations = sample_equations(num_samples=4, max_depth=4, max_num_variables=2,
max_num_constants=1,
function_space=functions,
operation_space=operators, verbose=False)
# Use a helper function to transform the equations from the sample_equation into sympy equations
sympy_equations_and_values = to_sympy(equations, functions, operators)
0 equations generated all equations generated
Let's look at the equations:
sympy_equations_and_values
There are added values, but we are only interested in the equations themselves:
sympy_equations = sympy_equations_and_values[0]
sympy_equations
[Abs(x_2/x_1), tan(exp(sin(x_1))), c_1*cos(x_1), x_1*x_2]
And then we can use the expression to create an autora experiment
from autora.experiment_runner.synthetic.abstract.equation import equation_experiment
# Let's pick the first one
expr = sympy_equations[0]
experiment = equation_experiment(expr)
Let's try some values for the experiment.
# First, import numpy
import numpy as np
# Declare a test input:
test_input = np.array([[1, 2],[3, 4],[5, 6],[7, 8],[9, 10]])
# Run the experiment with this input:
experiment.experiment_runner(test_input)
x_1 | x_2 | observations | |
---|---|---|---|
0 | 1.000305 | 1.998960 | 1.998351 |
1 | 3.000750 | 4.000941 | 1.333313 |
2 | 4.998049 | 5.998698 | 1.200208 |
3 | 7.000128 | 7.999684 | 1.142791 |
4 | 8.999983 | 9.999147 | 1.111018 |
To use the plotting we have to define a domain.
# Import from autora
from autora.variable import IV
# Describe the variables
variable_x_1 = IV(name="x_1", allowed_values=np.linspace(-10, 10, 100), value_range=(-10, 10))
variable_x_2 = IV(name="x_2", allowed_values=np.linspace(-10, 10, 100), value_range=(-10, 10))
# Reinitialize the experiment:
experiment = equation_experiment(expr, [variable_x_1, variable_x_2])
Now we can plot the ground truth
experiment.plotter()
/usr/local/lib/python3.10/dist-packages/autora/experiment_runner/synthetic/abstract/sympy_equation/__init__.py:148: RuntimeWarning: Unnamed data is used. Arguments will be sorted alphabetically. Consider using a Pandas DataFrame with named columns for better clarity and ease of use. warnings.warn(
<Figure size 640x480 with 0 Axes>