?

Average Error: 0.6 → 0.5
Time: 10.9s
Precision: binary64
Cost: 19456

?

\[\log \left(1 + e^{x}\right) - x \cdot y \]
\[\mathsf{fma}\left(-y, x, \mathsf{log1p}\left(e^{x}\right)\right) \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
(FPCore (x y) :precision binary64 (fma (- y) x (log1p (exp x))))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
double code(double x, double y) {
	return fma(-y, x, log1p(exp(x)));
}
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function code(x, y)
	return fma(Float64(-y), x, log1p(exp(x)))
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := N[((-y) * x + N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\log \left(1 + e^{x}\right) - x \cdot y
\mathsf{fma}\left(-y, x, \mathsf{log1p}\left(e^{x}\right)\right)

Error?

Target

Original0.6
Target0.1
Herbie0.5
\[\begin{array}{l} \mathbf{if}\;x \leq 0:\\ \;\;\;\;\log \left(1 + e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\log \left(1 + e^{-x}\right) - \left(-x\right) \cdot \left(1 - y\right)\\ \end{array} \]

Derivation?

  1. Initial program 0.6

    \[\log \left(1 + e^{x}\right) - x \cdot y \]
  2. Simplified0.5

    \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
    Proof

    [Start]0.6

    \[ \log \left(1 + e^{x}\right) - x \cdot y \]

    log1p-def [=>]0.5

    \[ \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
  3. Applied egg-rr13.3

    \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{log1p}\left(e^{x}\right) - x \cdot y\right)\right)} \]
  4. Applied egg-rr0.5

    \[\leadsto \color{blue}{x \cdot \left(-y\right) + \left(\mathsf{log1p}\left(e^{x}\right) + \mathsf{fma}\left(-y, x, x \cdot y\right)\right)} \]
  5. Taylor expanded in x around inf 0.6

    \[\leadsto \color{blue}{\left(-2 \cdot y + y\right) \cdot x + \log \left(1 + e^{x}\right)} \]
  6. Simplified0.5

    \[\leadsto \color{blue}{\mathsf{fma}\left(-y, x, \mathsf{log1p}\left(e^{x}\right)\right)} \]
    Proof

    [Start]0.6

    \[ \left(-2 \cdot y + y\right) \cdot x + \log \left(1 + e^{x}\right) \]

    distribute-lft1-in [=>]0.6

    \[ \color{blue}{\left(\left(-2 + 1\right) \cdot y\right)} \cdot x + \log \left(1 + e^{x}\right) \]

    metadata-eval [=>]0.6

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

    log1p-def [=>]0.5

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

    fma-def [=>]0.5

    \[ \color{blue}{\mathsf{fma}\left(-1 \cdot y, x, \mathsf{log1p}\left(e^{x}\right)\right)} \]

    mul-1-neg [=>]0.5

    \[ \mathsf{fma}\left(\color{blue}{-y}, x, \mathsf{log1p}\left(e^{x}\right)\right) \]
  7. Final simplification0.5

    \[\leadsto \mathsf{fma}\left(-y, x, \mathsf{log1p}\left(e^{x}\right)\right) \]

Alternatives

Alternative 1
Error0.5
Cost13120
\[\mathsf{log1p}\left(e^{x}\right) - y \cdot x \]
Alternative 2
Error12.2
Cost6984
\[\begin{array}{l} \mathbf{if}\;x \leq -0.00195:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq 2.1 \cdot 10^{-24}:\\ \;\;\;\;\log 2 + x \cdot 0.5\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 3
Error0.8
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq -13000000:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right) + \log 2\\ \end{array} \]
Alternative 4
Error1.0
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq -13000000:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 - y \cdot x\\ \end{array} \]
Alternative 5
Error12.1
Cost6728
\[\begin{array}{l} \mathbf{if}\;x \leq -1.5 \cdot 10^{-24}:\\ \;\;\;\;y \cdot \left(-x\right)\\ \mathbf{elif}\;x \leq 1.3 \cdot 10^{-25}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 6
Error33.8
Cost256
\[y \cdot \left(-x\right) \]
Alternative 7
Error61.7
Cost192
\[x \cdot 0.5 \]

Error

Reproduce?

herbie shell --seed 2023039 
(FPCore (x y)
  :name "Logistic regression 2"
  :precision binary64

  :herbie-target
  (if (<= x 0.0) (- (log (+ 1.0 (exp x))) (* x y)) (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y))))

  (- (log (+ 1.0 (exp x))) (* x y)))