Logistic regression 2

?

Percentage Accurate: 79.4% → 89.7%
Time: 16.1s
Precision: binary64
Cost: 13252

?

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

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


\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 7 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.4%
Target99.9%
Herbie89.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 < 700

    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 700 < x

    1. Initial program 13.7%

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

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

      [Start]13.7%

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

      log1p-def [=>]13.7%

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

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

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

Alternatives

Alternative 1
Accuracy89.7%
Cost13252
\[\begin{array}{l} \mathbf{if}\;x \leq 700:\\ \;\;\;\;\mathsf{log1p}\left(e^{x}\right) - x \cdot y\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 0.5 + \log 2\right) - x \cdot y\\ \end{array} \]
Alternative 2
Accuracy89.2%
Cost7108
\[\begin{array}{l} \mathbf{if}\;x \leq -1.35:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\left(x \cdot 0.5 + \log 2\right) - x \cdot y\\ \end{array} \]
Alternative 3
Accuracy88.9%
Cost6984
\[\begin{array}{l} \mathbf{if}\;x \leq -55:\\ \;\;\;\;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 4
Accuracy89.2%
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq -1.35:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;\log 2 + x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 5
Accuracy77.6%
Cost6728
\[\begin{array}{l} \mathbf{if}\;x \leq -9.5 \cdot 10^{-36}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{elif}\;x \leq 1.05 \cdot 10^{-39}:\\ \;\;\;\;\log 2\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 6
Accuracy52.1%
Cost452
\[\begin{array}{l} \mathbf{if}\;x \leq -2 \cdot 10^{-310}:\\ \;\;\;\;x \cdot \left(-y\right)\\ \mathbf{else}:\\ \;\;\;\;x \cdot \left(0.5 - y\right)\\ \end{array} \]
Alternative 7
Accuracy49.9%
Cost256
\[x \cdot \left(-y\right) \]

Reproduce?

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