Logistic regression 2

?

Percentage Accurate: 79.3% → 99.7%
Time: 10.1s
Precision: binary64
Cost: 26436

?

\[\log \left(1 + e^{x}\right) - x \cdot y \]
\[\begin{array}{l} \mathbf{if}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 10^{+248}:\\ \;\;\;\;\mathsf{log1p}\left(e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(x - x \cdot y\right) + \log 2\\ \end{array} \]
(FPCore (x y) :precision binary64 (- (log (+ 1.0 (exp x))) (* x y)))
(FPCore (x y)
 :precision binary64
 (if (<= (- (log (+ 1.0 (exp x))) (* x y)) 1e+248)
   (- (log1p (exp x)) (* x y))
   (+ (- x (* x 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 ((log((1.0 + exp(x))) - (x * y)) <= 1e+248) {
		tmp = log1p(exp(x)) - (x * y);
	} else {
		tmp = (x - (x * y)) + log(2.0);
	}
	return tmp;
}
public static double code(double x, double y) {
	return Math.log((1.0 + Math.exp(x))) - (x * y);
}
public static double code(double x, double y) {
	double tmp;
	if ((Math.log((1.0 + Math.exp(x))) - (x * y)) <= 1e+248) {
		tmp = Math.log1p(Math.exp(x)) - (x * y);
	} else {
		tmp = (x - (x * y)) + Math.log(2.0);
	}
	return tmp;
}
def code(x, y):
	return math.log((1.0 + math.exp(x))) - (x * y)
def code(x, y):
	tmp = 0
	if (math.log((1.0 + math.exp(x))) - (x * y)) <= 1e+248:
		tmp = math.log1p(math.exp(x)) - (x * y)
	else:
		tmp = (x - (x * y)) + math.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 (Float64(log(Float64(1.0 + exp(x))) - Float64(x * y)) <= 1e+248)
		tmp = Float64(log1p(exp(x)) - Float64(x * y));
	else
		tmp = Float64(Float64(x - Float64(x * 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[N[(N[Log[N[(1.0 + N[Exp[x], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], 1e+248], N[(N[Log[1 + N[Exp[x], $MachinePrecision]], $MachinePrecision] - N[(x * y), $MachinePrecision]), $MachinePrecision], N[(N[(x - N[(x * y), $MachinePrecision]), $MachinePrecision] + N[Log[2.0], $MachinePrecision]), $MachinePrecision]]
\log \left(1 + e^{x}\right) - x \cdot y
\begin{array}{l}
\mathbf{if}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 10^{+248}:\\
\;\;\;\;\mathsf{log1p}\left(e^{x}\right) - x \cdot y\\

\mathbf{else}:\\
\;\;\;\;\left(x - x \cdot y\right) + \log 2\\


\end{array}

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 8 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

Target

Original79.3%
Target99.9%
Herbie99.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 (-.f64 (log.f64 (+.f64 1 (exp.f64 x))) (*.f64 x y)) < 1.00000000000000005e248

    1. Initial program 100.0%

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

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

      [Start]100.0%

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

      log1p-def [=>]100.0%

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

    if 1.00000000000000005e248 < (-.f64 (log.f64 (+.f64 1 (exp.f64 x))) (*.f64 x y))

    1. Initial program 43.7%

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

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

      [Start]43.7%

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

      log1p-def [=>]43.7%

      \[ \color{blue}{\mathsf{log1p}\left(e^{x}\right)} - x \cdot y \]
    3. Taylor expanded in x around 0 67.8%

      \[\leadsto \color{blue}{\left(0.5 - y\right) \cdot x + \log 2} \]
    4. Applied egg-rr10.1%

      \[\leadsto \color{blue}{\left({\left(x \cdot \left(0.5 - y\right)\right)}^{2} - {\log 2}^{2}\right) \cdot \frac{1}{x \cdot \left(0.5 - y\right) - \log 2}} \]
      Step-by-step derivation

      [Start]67.8%

      \[ \left(0.5 - y\right) \cdot x + \log 2 \]

      flip-+ [=>]10.1%

      \[ \color{blue}{\frac{\left(\left(0.5 - y\right) \cdot x\right) \cdot \left(\left(0.5 - y\right) \cdot x\right) - \log 2 \cdot \log 2}{\left(0.5 - y\right) \cdot x - \log 2}} \]

      div-inv [=>]10.1%

      \[ \color{blue}{\left(\left(\left(0.5 - y\right) \cdot x\right) \cdot \left(\left(0.5 - y\right) \cdot x\right) - \log 2 \cdot \log 2\right) \cdot \frac{1}{\left(0.5 - y\right) \cdot x - \log 2}} \]

      pow2 [=>]10.1%

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

      *-commutative [=>]10.1%

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

      pow2 [=>]10.1%

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

      *-commutative [=>]10.1%

      \[ \left({\left(x \cdot \left(0.5 - y\right)\right)}^{2} - {\log 2}^{2}\right) \cdot \frac{1}{\color{blue}{x \cdot \left(0.5 - y\right)} - \log 2} \]
    5. Taylor expanded in y around inf 67.8%

      \[\leadsto \color{blue}{\left(\log 2 + \left(x + -1 \cdot \left(y \cdot x\right)\right)\right) - 0.5 \cdot x} \]
    6. Simplified67.8%

      \[\leadsto \color{blue}{\left(x - y \cdot x\right) + \left(\log 2 + x \cdot -0.5\right)} \]
      Step-by-step derivation

      [Start]67.8%

      \[ \left(\log 2 + \left(x + -1 \cdot \left(y \cdot x\right)\right)\right) - 0.5 \cdot x \]

      +-commutative [=>]67.8%

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

      associate--l+ [=>]67.8%

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

      *-commutative [<=]67.8%

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

      neg-mul-1 [<=]67.8%

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

      unsub-neg [=>]67.8%

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

      *-commutative [=>]67.8%

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

      sub-neg [=>]67.8%

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

      *-commutative [=>]67.8%

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

      distribute-rgt-neg-in [=>]67.8%

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

      metadata-eval [=>]67.8%

      \[ \left(x - y \cdot x\right) + \left(\log 2 + x \cdot \color{blue}{-0.5}\right) \]
    7. Taylor expanded in x around 0 98.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 10^{+248}:\\ \;\;\;\;\mathsf{log1p}\left(e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(x - x \cdot y\right) + \log 2\\ \end{array} \]

Alternatives

Alternative 1
Accuracy99.7%
Cost26436
\[\begin{array}{l} \mathbf{if}\;\log \left(1 + e^{x}\right) - x \cdot y \leq 10^{+248}:\\ \;\;\;\;\mathsf{log1p}\left(e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(x - x \cdot y\right) + \log 2\\ \end{array} \]
Alternative 2
Accuracy88.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;x \leq -58:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 1.4:\\ \;\;\;\;\log 2 - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 3
Accuracy89.2%
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq -2.05:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 4
Accuracy99.1%
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq -2.05:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x - x \cdot y\right) + \log 2\\ \end{array} \]
Alternative 5
Accuracy76.4%
Cost6728
\[\begin{array}{l} \mathbf{if}\;x \leq -1.02 \cdot 10^{-39}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 2.2 \cdot 10^{-47}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 6
Accuracy52.4%
Cost452
\[\begin{array}{l} \mathbf{if}\;x \leq -1 \cdot 10^{-310}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 7
Accuracy50.2%
Cost256
\[x \cdot \left(-y\right) \]
Alternative 8
Accuracy5.4%
Cost192
\[x \cdot 0.5 \]

Reproduce?

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