Logistic regression 2

Percentage Accurate: 99.2% → 99.1%
Time: 10.1s
Alternatives: 9
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \log \left(1 + e^{x}\right) - x \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
	return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y):
	return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function tmp = code(x, y)
	tmp = log((1.0 + exp(x))) - (x * y);
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

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.

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
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.

Initial Program: 99.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \log \left(1 + e^{x}\right) - x \cdot y \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
double code(double x, double y) {
	return log((1.0 + exp(x))) - (x * y);
}
real(8) function code(x, y)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    code = log((1.0d0 + exp(x))) - (x * y)
end function
public static double code(double x, double y) {
	return Math.log((1.0 + Math.exp(x))) - (x * y);
}
def code(x, y):
	return math.log((1.0 + math.exp(x))) - (x * y)
function code(x, y)
	return Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
end
function tmp = code(x, y)
	tmp = log((1.0 + exp(x))) - (x * y);
end
code[x_, y_] := N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

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

Alternative 1: 99.1% accurate, 1.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq -450000:\\ \;\;\;\;-x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.125, 0.5\right) - y, \log 2\right)\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (if (<= x -450000.0) (- (* x y)) (fma x (- (fma x 0.125 0.5) y) (log 2.0))))
double code(double x, double y) {
	double tmp;
	if (x <= -450000.0) {
		tmp = -(x * y);
	} else {
		tmp = fma(x, (fma(x, 0.125, 0.5) - y), log(2.0));
	}
	return tmp;
}
function code(x, y)
	tmp = 0.0
	if (x <= -450000.0)
		tmp = Float64(-Float64(x * y));
	else
		tmp = fma(x, Float64(fma(x, 0.125, 0.5) - y), log(2.0));
	end
	return tmp
end
code[x_, y_] := If[LessEqual[x, -450000.0], (-N[(x * y), $MachinePrecision]), N[(x * N[(N[(x * 0.125 + 0.5), $MachinePrecision] - y), $MachinePrecision] + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq -450000:\\
\;\;\;\;-x \cdot y\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < -4.5e5

    1. Initial program 100.0%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot y\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot y\right)} \]
      2. distribute-rgt-neg-inN/A

        \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(y\right)\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(y\right)\right)} \]
      4. lower-neg.f64100.0

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    5. Applied rewrites100.0%

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

    if -4.5e5 < x

    1. Initial program 98.8%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\log 2 + x \cdot \left(\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot \left(\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y\right) + \log 2} \]
      2. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y, \log 2\right)} \]
      3. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y}, \log 2\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(\frac{1}{8} \cdot x + \frac{1}{2}\right)} - y, \log 2\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot \frac{1}{8}} + \frac{1}{2}\right) - y, \log 2\right) \]
      6. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, \frac{1}{8}, \frac{1}{2}\right)} - y, \log 2\right) \]
      7. lower-log.f6499.5

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.125, 0.5\right) - y, \color{blue}{\log 2}\right) \]
    5. Applied rewrites99.5%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.125, 0.5\right) - y, \log 2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification99.6%

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

Alternative 2: 97.0% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \log \left(1 + e^{x}\right) - x \cdot y\\ t_1 := -x \cdot y\\ \mathbf{if}\;t\_0 \leq 4 \cdot 10^{-27}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;t\_0 \leq 1:\\ \;\;\;\;\mathsf{fma}\left(x, 0.5, \log 2\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (- (log (+ 1.0 (exp x))) (* x y))) (t_1 (- (* x y))))
   (if (<= t_0 4e-27) t_1 (if (<= t_0 1.0) (fma x 0.5 (log 2.0)) t_1))))
double code(double x, double y) {
	double t_0 = log((1.0 + exp(x))) - (x * y);
	double t_1 = -(x * y);
	double tmp;
	if (t_0 <= 4e-27) {
		tmp = t_1;
	} else if (t_0 <= 1.0) {
		tmp = fma(x, 0.5, log(2.0));
	} else {
		tmp = t_1;
	}
	return tmp;
}
function code(x, y)
	t_0 = Float64(log(Float64(1.0 + exp(x))) - Float64(x * y))
	t_1 = Float64(-Float64(x * y))
	tmp = 0.0
	if (t_0 <= 4e-27)
		tmp = t_1;
	elseif (t_0 <= 1.0)
		tmp = fma(x, 0.5, log(2.0));
	else
		tmp = t_1;
	end
	return tmp
end
code[x_, y_] := Block[{t$95$0 = N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = (-N[(x * y), $MachinePrecision])}, If[LessEqual[t$95$0, 4e-27], t$95$1, If[LessEqual[t$95$0, 1.0], N[(x * 0.5 + N[Log[2.0], $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \log \left(1 + e^{x}\right) - x \cdot y\\
t_1 := -x \cdot y\\
\mathbf{if}\;t\_0 \leq 4 \cdot 10^{-27}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;t\_0 \leq 1:\\
\;\;\;\;\mathsf{fma}\left(x, 0.5, \log 2\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 (log.f64 (+.f64 #s(literal 1 binary64) (exp.f64 x))) (*.f64 x y)) < 4.0000000000000002e-27 or 1 < (-.f64 (log.f64 (+.f64 #s(literal 1 binary64) (exp.f64 x))) (*.f64 x y))

    1. Initial program 98.4%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf

      \[\leadsto \color{blue}{-1 \cdot \left(x \cdot y\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\mathsf{neg}\left(x \cdot y\right)} \]
      2. distribute-rgt-neg-inN/A

        \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(y\right)\right)} \]
      3. lower-*.f64N/A

        \[\leadsto \color{blue}{x \cdot \left(\mathsf{neg}\left(y\right)\right)} \]
      4. lower-neg.f6497.5

        \[\leadsto x \cdot \color{blue}{\left(-y\right)} \]
    5. Applied rewrites97.5%

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

    if 4.0000000000000002e-27 < (-.f64 (log.f64 (+.f64 #s(literal 1 binary64) (exp.f64 x))) (*.f64 x y)) < 1

    1. Initial program 99.9%

      \[\log \left(1 + e^{x}\right) - x \cdot y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{\log 2 + x \cdot \left(\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y\right)} \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \color{blue}{x \cdot \left(\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y\right) + \log 2} \]
      2. lower-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(x, \left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y, \log 2\right)} \]
      3. lower--.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(\frac{1}{2} + \frac{1}{8} \cdot x\right) - y}, \log 2\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\left(\frac{1}{8} \cdot x + \frac{1}{2}\right)} - y, \log 2\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{fma}\left(x, \left(\color{blue}{x \cdot \frac{1}{8}} + \frac{1}{2}\right) - y, \log 2\right) \]
      6. lower-fma.f64N/A

        \[\leadsto \mathsf{fma}\left(x, \color{blue}{\mathsf{fma}\left(x, \frac{1}{8}, \frac{1}{2}\right)} - y, \log 2\right) \]
      7. lower-log.f6498.1

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.125, 0.5\right) - y, \color{blue}{\log 2}\right) \]
    5. Applied rewrites98.1%

      \[\leadsto \color{blue}{\mathsf{fma}\left(x, \mathsf{fma}\left(x, 0.125, 0.5\right) - y, \log 2\right)} \]
    6. Taylor expanded in y around 0

      \[\leadsto \mathsf{fma}\left(x, \frac{1}{2} + \color{blue}{\frac{1}{8} \cdot x}, \log 2\right) \]
    7. Step-by-step derivation
      1. Applied rewrites96.9%

        \[\leadsto \mathsf{fma}\left(x, \mathsf{fma}\left(x, \color{blue}{0.125}, 0.5\right), \log 2\right) \]
      2. Taylor expanded in x around 0

        \[\leadsto \mathsf{fma}\left(x, \frac{1}{2}, \log 2\right) \]
      3. Step-by-step derivation
        1. Applied rewrites96.5%

          \[\leadsto \mathsf{fma}\left(x, 0.5, \log 2\right) \]
      4. Recombined 2 regimes into one program.
      5. Final simplification97.0%

        \[\leadsto \begin{array}{l} \mathbf{if}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 4 \cdot 10^{-27}:\\ \;\;\;\;-x \cdot y\\ \mathbf{elif}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 1:\\ \;\;\;\;\mathsf{fma}\left(x, 0.5, \log 2\right)\\ \mathbf{else}:\\ \;\;\;\;-x \cdot y\\ \end{array} \]
      6. Add Preprocessing

      Developer Target 1: 99.9% accurate, 0.9× speedup?

      \[\begin{array}{l} \\ \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} \end{array} \]
      (FPCore (x y)
       :precision binary64
       (if (<= x 0.0)
         (- (log (+ 1.0 (exp x))) (* x y))
         (- (log (+ 1.0 (exp (- x)))) (* (- x) (- 1.0 y)))))
      double code(double x, double y) {
      	double tmp;
      	if (x <= 0.0) {
      		tmp = log((1.0 + exp(x))) - (x * y);
      	} else {
      		tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y));
      	}
      	return tmp;
      }
      
      real(8) function code(x, y)
          real(8), intent (in) :: x
          real(8), intent (in) :: y
          real(8) :: tmp
          if (x <= 0.0d0) then
              tmp = log((1.0d0 + exp(x))) - (x * y)
          else
              tmp = log((1.0d0 + exp(-x))) - (-x * (1.0d0 - y))
          end if
          code = tmp
      end function
      
      public static double code(double x, double y) {
      	double tmp;
      	if (x <= 0.0) {
      		tmp = Math.log((1.0 + Math.exp(x))) - (x * y);
      	} else {
      		tmp = Math.log((1.0 + Math.exp(-x))) - (-x * (1.0 - y));
      	}
      	return tmp;
      }
      
      def code(x, y):
      	tmp = 0
      	if x <= 0.0:
      		tmp = math.log((1.0 + math.exp(x))) - (x * y)
      	else:
      		tmp = math.log((1.0 + math.exp(-x))) - (-x * (1.0 - y))
      	return tmp
      
      function code(x, y)
      	tmp = 0.0
      	if (x <= 0.0)
      		tmp = Float64(log(Float64(1.0 + exp(x))) - Float64(x * y));
      	else
      		tmp = Float64(log(Float64(1.0 + exp(Float64(-x)))) - Float64(Float64(-x) * Float64(1.0 - y)));
      	end
      	return tmp
      end
      
      function tmp_2 = code(x, y)
      	tmp = 0.0;
      	if (x <= 0.0)
      		tmp = log((1.0 + exp(x))) - (x * y);
      	else
      		tmp = log((1.0 + exp(-x))) - (-x * (1.0 - y));
      	end
      	tmp_2 = tmp;
      end
      
      code[x_, y_] := If[LessEqual[x, 0.0], N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[Log[N[(1.0 + N[Exp[(-x)], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[((-x) * N[(1.0 - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
      
      \begin{array}{l}
      
      \\
      \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}
      \end{array}
      

      Reproduce

      ?
      herbie shell --seed 2024222 
      (FPCore (x y)
        :name "Logistic regression 2"
        :precision binary64
      
        :alt
        (! :herbie-platform default (if (<= x 0) (- (log (+ 1 (exp x))) (* x y)) (- (log (+ 1 (exp (- x)))) (* (- x) (- 1 y)))))
      
        (- (log (+ 1.0 (exp x))) (* x y)))