Skip to content

Instantly share code, notes, and snippets.

@MilesCranmer
Last active October 17, 2020 04:35
Show Gist options
  • Select an option

  • Save MilesCranmer/ee64614f3e8d9fcd6368e75e2fd86cc9 to your computer and use it in GitHub Desktop.

Select an option

Save MilesCranmer/ee64614f3e8d9fcd6368e75e2fd86cc9 to your computer and use it in GitHub Desktop.
Analytic Approximation of log(1+erf(x)) for x in [-10, -5]. Uses PySR: https://pysr.readthedocs.io/, mpmath: http://mpmath.org/
import numpy as np
from mpmath import mp, mpmathify
from pysr import *
#Set precision to 200 decimal places:
mp.dps = 200
x = np.linspace(-10, -5, num=300)
#High precision calculation:
y = np.array([float(mp.log(1+mp.erf(mpmathify(xi)))) for xi in x])
# Symbolic regression
equations = pysr(x[:, None], y,
procs=40, # Change procs=40 to the number of cores on your system.
variable_names=['x'], #optional; for printing
unary_operators=["exp", "logm", "sqrtm", "neg"],
binary_operators=["plus", "sub", "mult", "div", 'pow'],
# Fix max complexity of some operator arguments, for readability:
constraints={'exp': 7, 'logm': 7, 'sqrtm': 7, 'pow': (7, 3)},
useFrequency=True, maxsize=40, niterations=10000 #hyperparams
)
# This will print several analytic continuations of log(1+erf(x)) to small x.
# Hit <ctl-c> after you are satisfied.
equations = get_hof()
print(best(equations.iloc[-1:])) #Sorted by MSE, so select last row, and pretty-print it
#-x**2 - 0.982152327897448*log(Abs(x)) - 0.6179907
print(best_tex(equations.iloc[-1:]))
# - x^{2} - 0.982152327897448 \log{\left(\left|{x}\right| \right)} - 0.6179907
f = best_callable(equations.iloc[-1:])
print(f(-6))
# -38.378
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment