Logistic function from Lakshay Garg

?

Percentage Accurate: 53.5% → 99.4%
Time: 6.6s
Precision: binary64
Cost: 7304

?

\[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
\[\begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -50:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \mathbf{elif}\;-2 \cdot x \leq 0.0004:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]
(FPCore (x y) :precision binary64 (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))
(FPCore (x y)
 :precision binary64
 (if (<= (* -2.0 x) -50.0)
   (+ (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) -1.0)
   (if (<= (* -2.0 x) 0.0004) (+ x (* -0.3333333333333333 (pow x 3.0))) -1.0)))
double code(double x, double y) {
	return (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
}
double code(double x, double y) {
	double tmp;
	if ((-2.0 * x) <= -50.0) {
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	} else if ((-2.0 * x) <= 0.0004) {
		tmp = x + (-0.3333333333333333 * pow(x, 3.0));
	} else {
		tmp = -1.0;
	}
	return tmp;
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) - 1.0d0
end function
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8) :: tmp
    if (((-2.0d0) * x) <= (-50.0d0)) then
        tmp = (2.0d0 / (1.0d0 + exp(((-2.0d0) * x)))) + (-1.0d0)
    else if (((-2.0d0) * x) <= 0.0004d0) then
        tmp = x + ((-0.3333333333333333d0) * (x ** 3.0d0))
    else
        tmp = -1.0d0
    end if
    code = tmp
end function
public static double code(double x, double y) {
	return (2.0 / (1.0 + Math.exp((-2.0 * x)))) - 1.0;
}
public static double code(double x, double y) {
	double tmp;
	if ((-2.0 * x) <= -50.0) {
		tmp = (2.0 / (1.0 + Math.exp((-2.0 * x)))) + -1.0;
	} else if ((-2.0 * x) <= 0.0004) {
		tmp = x + (-0.3333333333333333 * Math.pow(x, 3.0));
	} else {
		tmp = -1.0;
	}
	return tmp;
}
def code(x, y):
	return (2.0 / (1.0 + math.exp((-2.0 * x)))) - 1.0
def code(x, y):
	tmp = 0
	if (-2.0 * x) <= -50.0:
		tmp = (2.0 / (1.0 + math.exp((-2.0 * x)))) + -1.0
	elif (-2.0 * x) <= 0.0004:
		tmp = x + (-0.3333333333333333 * math.pow(x, 3.0))
	else:
		tmp = -1.0
	return tmp
function code(x, y)
	return Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) - 1.0)
end
function code(x, y)
	tmp = 0.0
	if (Float64(-2.0 * x) <= -50.0)
		tmp = Float64(Float64(2.0 / Float64(1.0 + exp(Float64(-2.0 * x)))) + -1.0);
	elseif (Float64(-2.0 * x) <= 0.0004)
		tmp = Float64(x + Float64(-0.3333333333333333 * (x ^ 3.0)));
	else
		tmp = -1.0;
	end
	return tmp
end
function tmp = code(x, y)
	tmp = (2.0 / (1.0 + exp((-2.0 * x)))) - 1.0;
end
function tmp_2 = code(x, y)
	tmp = 0.0;
	if ((-2.0 * x) <= -50.0)
		tmp = (2.0 / (1.0 + exp((-2.0 * x)))) + -1.0;
	elseif ((-2.0 * x) <= 0.0004)
		tmp = x + (-0.3333333333333333 * (x ^ 3.0));
	else
		tmp = -1.0;
	end
	tmp_2 = tmp;
end
code[x_, y_] := N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision]
code[x_, y_] := If[LessEqual[N[(-2.0 * x), $MachinePrecision], -50.0], N[(N[(2.0 / N[(1.0 + N[Exp[N[(-2.0 * x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], If[LessEqual[N[(-2.0 * x), $MachinePrecision], 0.0004], N[(x + N[(-0.3333333333333333 * N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0]]
\frac{2}{1 + e^{-2 \cdot x}} - 1
\begin{array}{l}
\mathbf{if}\;-2 \cdot x \leq -50:\\
\;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\

\mathbf{elif}\;-2 \cdot x \leq 0.0004:\\
\;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\

\mathbf{else}:\\
\;\;\;\;-1\\


\end{array}

Local Percentage Accuracy?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Herbie found 7 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each of Herbie's proposed alternatives. Up and to the right is better. Each dot represents an alternative program; the red square represents the initial program.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 3 regimes
  2. if (*.f64 -2 x) < -50

    1. Initial program 100.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]

    if -50 < (*.f64 -2 x) < 4.00000000000000019e-4

    1. Initial program 8.5%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Taylor expanded in x around 0 100.0%

      \[\leadsto \color{blue}{-0.3333333333333333 \cdot {x}^{3} + x} \]

    if 4.00000000000000019e-4 < (*.f64 -2 x)

    1. Initial program 100.0%

      \[\frac{2}{1 + e^{-2 \cdot x}} - 1 \]
    2. Taylor expanded in x around 0 97.2%

      \[\leadsto \frac{2}{\color{blue}{2 + -2 \cdot x}} - 1 \]
    3. Simplified97.2%

      \[\leadsto \frac{2}{\color{blue}{2 + x \cdot -2}} - 1 \]
      Step-by-step derivation

      [Start]97.2

      \[ \frac{2}{2 + -2 \cdot x} - 1 \]

      *-commutative [=>]97.2

      \[ \frac{2}{2 + \color{blue}{x \cdot -2}} - 1 \]
    4. Taylor expanded in x around inf 100.0%

      \[\leadsto \color{blue}{-1} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification100.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;-2 \cdot x \leq -50:\\ \;\;\;\;\frac{2}{1 + e^{-2 \cdot x}} + -1\\ \mathbf{elif}\;-2 \cdot x \leq 0.0004:\\ \;\;\;\;x + -0.3333333333333333 \cdot {x}^{3}\\ \mathbf{else}:\\ \;\;\;\;-1\\ \end{array} \]

Alternatives

Alternative 1
Accuracy79.5%
Cost708
\[\begin{array}{l} \mathbf{if}\;x \leq -0.65:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\left(x + x\right) \cdot \frac{1}{x + 2}\\ \end{array} \]
Alternative 2
Accuracy80.0%
Cost584
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2.5:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2 - \frac{4}{x}\\ \end{array} \]
Alternative 3
Accuracy79.4%
Cost580
\[\begin{array}{l} \mathbf{if}\;x \leq -0.65:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{2}{\frac{x + 2}{x}}\\ \end{array} \]
Alternative 4
Accuracy80.0%
Cost328
\[\begin{array}{l} \mathbf{if}\;x \leq -1:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2:\\ \;\;\;\;x\\ \mathbf{else}:\\ \;\;\;\;2\\ \end{array} \]
Alternative 5
Accuracy32.5%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq 1.1 \cdot 10^{-308}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;2\\ \end{array} \]
Alternative 6
Accuracy27.6%
Cost64
\[-1 \]

Reproduce?

herbie shell --seed 2023161 
(FPCore (x y)
  :name "Logistic function from Lakshay Garg"
  :precision binary64
  (- (/ 2.0 (+ 1.0 (exp (* -2.0 x)))) 1.0))