Average Error: 0.5 → 0.7
Time: 10.3s
Precision: binary64
Cost: 13252
\[\log \left(1 + e^{x}\right) - x \cdot y \]
\[\begin{array}{l} \mathbf{if}\;x \leq -17738.61967637253:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, 0.5 - y, \log 2\right)\\ \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
(FPCore (x y)
 :precision binary64
 (if (<= x -17738.61967637253) (* x (- y)) (fma x (- 0.5 y) (log 2.0))))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
double code(double x, double y) {
	double tmp;
	if (x <= -17738.61967637253) {
		tmp = x * -y;
	} else {
		tmp = fma(x, (0.5 - y), log(2.0));
	}
	return tmp;
}
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function code(x, y)
	tmp = 0.0
	if (x <= -17738.61967637253)
		tmp = Float64(x * Float64(-y));
	else
		tmp = fma(x, Float64(0.5 - y), log(2.0));
	end
	return tmp
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
code[x_, y_] := If[LessEqual[x, -17738.61967637253], N[(x * (-y)), $MachinePrecision], N[(x * N[(0.5 - y), $MachinePrecision] + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\log \left(1 + e^{x}\right) - x \cdot y
\begin{array}{l}
\mathbf{if}\;x \leq -17738.61967637253:\\
\;\;\;\;x \cdot \left(-y\right)\\

\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5 - y, \log 2\right)\\


\end{array}

Error

Target

Original0.5
Target0.0
Herbie0.7
\[\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. Split input into 2 regimes
  2. if x < -17738.619676372531

    1. Initial program 0

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
      Proof
      (-.f64 (log1p.f64 (exp.f64 x)) (*.f64 x y)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= log1p-def_binary64 (log.f64 (+.f64 1 (exp.f64 x)))) (*.f64 x y)): 3 points increase in error, 0 points decrease in error
    3. Taylor expanded in x around inf 0

      \[\leadsto \color{blue}{-1 \cdot \left(y \cdot x\right)} \]
    4. Simplified0

      \[\leadsto \color{blue}{x \cdot \left(-y\right)} \]
      Proof
      (*.f64 x (neg.f64 y)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= distribute-rgt-neg-in_binary64 (neg.f64 (*.f64 x y))): 0 points increase in error, 0 points decrease in error
      (neg.f64 (Rewrite<= *-commutative_binary64 (*.f64 y x))): 0 points increase in error, 0 points decrease in error
      (Rewrite<= mul-1-neg_binary64 (*.f64 -1 (*.f64 y x))): 0 points increase in error, 0 points decrease in error

    if -17738.619676372531 < x

    1. Initial program 0.7

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

      \[\leadsto \color{blue}{\mathsf{log1p}\left(e^{x}\right) - x \cdot y} \]
      Proof
      (-.f64 (log1p.f64 (exp.f64 x)) (*.f64 x y)): 0 points increase in error, 0 points decrease in error
      (-.f64 (Rewrite<= log1p-def_binary64 (log.f64 (+.f64 1 (exp.f64 x)))) (*.f64 x y)): 3 points increase in error, 0 points decrease in error
    3. Taylor expanded in x around 0 0.9

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
    4. Simplified0.9

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, 0.5 - y, \log 2\right)} \]
      Proof
      (fma.f64 x (-.f64 1/2 y) (log.f64 2)): 0 points increase in error, 0 points decrease in error
      (Rewrite<= fma-def_binary64 (+.f64 (*.f64 x (-.f64 1/2 y)) (log.f64 2))): 3 points increase in error, 0 points decrease in error
      (+.f64 (Rewrite<= *-commutative_binary64 (*.f64 (-.f64 1/2 y) x)) (log.f64 2)): 0 points increase in error, 0 points decrease in error
  3. Recombined 2 regimes into one program.
  4. Final simplification0.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -17738.61967637253:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, 0.5 - y, \log 2\right)\\ \end{array} \]

Alternatives

Alternative 1
Error0.5
Cost13120
\[\mathsf{log1p}\left(e^{x}\right) - x \cdot y \]
Alternative 2
Error0.7
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq -17738.61967637253:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 3
Error12.2
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq -2.0689133335891962 \cdot 10^{-16}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot 0.5\\ \end{array} \]
Alternative 4
Error1.0
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \leq -17738.61967637253:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 - x \cdot y\\ \end{array} \]
Alternative 5
Error12.7
Cost6728
\[\begin{array}{l} \mathbf{if}\;x \leq -2.0689133335891962 \cdot 10^{-16}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 7.3602523156783945 \cdot 10^{-87}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 6
Error34.3
Cost256
\[x \cdot \left(-y\right) \]
Alternative 7
Error61.8
Cost192
\[x \cdot 0.5 \]

Error

Reproduce

herbie shell --seed 2022317 
(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)))