Logistic function

?

Percentage Accurate: 99.8% → 99.9%
Time: 24.6s
Precision: binary32
Cost: 9760

?

\[0 \leq s \land s \leq 1.0651631\]
\[\frac{1}{1 + e^{\frac{-x}{s}}} \]
\[e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)} \]
(FPCore (x s) :precision binary32 (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))
(FPCore (x s) :precision binary32 (exp (- (log1p (exp (/ (- x) s))))))
float code(float x, float s) {
	return 1.0f / (1.0f + expf((-x / s)));
}
float code(float x, float s) {
	return expf(-log1pf(expf((-x / s))));
}
function code(x, s)
	return Float32(Float32(1.0) / Float32(Float32(1.0) + exp(Float32(Float32(-x) / s))))
end
function code(x, s)
	return exp(Float32(-log1p(exp(Float32(Float32(-x) / s)))))
end
\frac{1}{1 + e^{\frac{-x}{s}}}
e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)}

Local Percentage Accuracy vs ?

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 13 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Initial program 99.7%

    \[\frac{1}{1 + e^{\frac{-x}{s}}} \]
  2. Applied egg-rr99.7%

    \[\leadsto \frac{1}{1 + \color{blue}{{\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}}} \]
    Step-by-step derivation

    [Start]99.7%

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

    div-inv [=>]99.6%

    \[ \frac{1}{1 + e^{\color{blue}{\left(-x\right) \cdot \frac{1}{s}}}} \]

    exp-prod [=>]77.1%

    \[ \frac{1}{1 + \color{blue}{{\left(e^{-x}\right)}^{\left(\frac{1}{s}\right)}}} \]

    neg-mul-1 [=>]77.1%

    \[ \frac{1}{1 + {\left(e^{\color{blue}{-1 \cdot x}}\right)}^{\left(\frac{1}{s}\right)}} \]

    exp-prod [=>]77.1%

    \[ \frac{1}{1 + {\color{blue}{\left({\left(e^{-1}\right)}^{x}\right)}}^{\left(\frac{1}{s}\right)}} \]

    pow-pow [=>]99.7%

    \[ \frac{1}{1 + \color{blue}{{\left(e^{-1}\right)}^{\left(x \cdot \frac{1}{s}\right)}}} \]

    div-inv [<=]99.7%

    \[ \frac{1}{1 + {\left(e^{-1}\right)}^{\color{blue}{\left(\frac{x}{s}\right)}}} \]
  3. Applied egg-rr99.8%

    \[\leadsto \color{blue}{e^{-\mathsf{log1p}\left({\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}\right)}} \]
    Step-by-step derivation

    [Start]99.7%

    \[ \frac{1}{1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}} \]

    add-exp-log [=>]99.7%

    \[ \color{blue}{e^{\log \left(\frac{1}{1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}}\right)}} \]

    log-rec [=>]99.7%

    \[ e^{\color{blue}{-\log \left(1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}\right)}} \]

    log1p-udef [<=]99.8%

    \[ e^{-\color{blue}{\mathsf{log1p}\left({\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}\right)}} \]
  4. Simplified99.8%

    \[\leadsto \color{blue}{e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)}} \]
    Step-by-step derivation

    [Start]99.8%

    \[ e^{-\mathsf{log1p}\left({\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}\right)} \]

    exp-prod [<=]99.8%

    \[ e^{-\mathsf{log1p}\left(\color{blue}{e^{-1 \cdot \frac{x}{s}}}\right)} \]

    neg-mul-1 [<=]99.8%

    \[ e^{-\mathsf{log1p}\left(e^{\color{blue}{-\frac{x}{s}}}\right)} \]

    distribute-neg-frac [=>]99.8%

    \[ e^{-\mathsf{log1p}\left(e^{\color{blue}{\frac{-x}{s}}}\right)} \]
  5. Final simplification99.8%

    \[\leadsto e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)} \]

Alternatives

Alternative 1
Accuracy99.9%
Cost9760
\[e^{-\mathsf{log1p}\left(e^{\frac{-x}{s}}\right)} \]
Alternative 2
Accuracy99.8%
Cost6656
\[\frac{1}{1 + {\left(e^{-1}\right)}^{\left(\frac{x}{s}\right)}} \]
Alternative 3
Accuracy99.8%
Cost3552
\[\frac{1}{1 + e^{2 \cdot \frac{-0.5}{\frac{s}{x}}}} \]
Alternative 4
Accuracy99.8%
Cost3456
\[\frac{1}{e^{\frac{-x}{s}} + 1} \]
Alternative 5
Accuracy62.5%
Cost548
\[\begin{array}{l} \mathbf{if}\;x \leq -5.000000136226006 \cdot 10^{-28}:\\ \;\;\;\;\frac{1}{2 + \left(x \cdot \left(x \cdot \frac{1}{s \cdot s}\right)\right) \cdot 0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 6
Accuracy60.7%
Cost484
\[\begin{array}{l} \mathbf{if}\;x \leq -1.000000046701102 \cdot 10^{-34}:\\ \;\;\;\;\frac{1}{2 + x \cdot \left(\frac{x}{s} \cdot \frac{0.5}{s}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 7
Accuracy62.2%
Cost484
\[\begin{array}{l} \mathbf{if}\;x \leq -6.000000050617872 \cdot 10^{-34}:\\ \;\;\;\;\frac{1}{2 + 0.5 \cdot \left(x \cdot \frac{x}{s \cdot s}\right)}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 8
Accuracy54.0%
Cost356
\[\begin{array}{l} \mathbf{if}\;x \leq -4.9999998413276127 \cdot 10^{-20}:\\ \;\;\;\;2 \cdot \left(\frac{s}{x} \cdot \frac{s}{x}\right)\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 9
Accuracy59.0%
Cost356
\[\begin{array}{l} \mathbf{if}\;x \leq -3.999999999279835 \cdot 10^{-23}:\\ \;\;\;\;2 \cdot \frac{s \cdot s}{x \cdot x}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 10
Accuracy44.7%
Cost292
\[\begin{array}{l} \mathbf{if}\;x \leq 2.0000000102211272 \cdot 10^{-38}:\\ \;\;\;\;\frac{1}{2 - \frac{x}{s}}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 11
Accuracy43.6%
Cost260
\[\begin{array}{l} \mathbf{if}\;x \leq -4.9999998413276127 \cdot 10^{-20}:\\ \;\;\;\;\frac{1}{\frac{-x}{s}}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 12
Accuracy42.4%
Cost196
\[\begin{array}{l} \mathbf{if}\;x \leq -4.9999998413276127 \cdot 10^{-20}:\\ \;\;\;\;\frac{-s}{x}\\ \mathbf{else}:\\ \;\;\;\;0.5\\ \end{array} \]
Alternative 13
Accuracy25.2%
Cost32
\[0.5 \]

Reproduce?

herbie shell --seed 2023256 
(FPCore (x s)
  :name "Logistic function"
  :precision binary32
  :pre (and (<= 0.0 s) (<= s 1.0651631))
  (/ 1.0 (+ 1.0 (exp (/ (- x) s)))))