Statistics.Distribution.Poisson:$clogProbability from math-functions-0.1.5.2

Percentage Accurate: 99.9% → 99.9%
Time: 7.6s
Alternatives: 6
Speedup: 1.0×

Specification

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

\\
\left(x \cdot \log y - z\right) - 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 6 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.9% accurate, 1.0× speedup?

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

\\
\left(x \cdot \log y - z\right) - y
\end{array}

Alternative 1: 99.9% accurate, 1.0× speedup?

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

\\
\left(x \cdot \log y - y\right) - z
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot \log y - z\right) - y \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot \log y - z\right) - y} \]
    2. lift--.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot \log y - z\right)} - y \]
    3. associate--l-N/A

      \[\leadsto \color{blue}{x \cdot \log y - \left(z + y\right)} \]
    4. +-commutativeN/A

      \[\leadsto x \cdot \log y - \color{blue}{\left(y + z\right)} \]
    5. associate--r+N/A

      \[\leadsto \color{blue}{\left(x \cdot \log y - y\right) - z} \]
    6. lower--.f64N/A

      \[\leadsto \color{blue}{\left(x \cdot \log y - y\right) - z} \]
    7. lower--.f6499.9

      \[\leadsto \color{blue}{\left(x \cdot \log y - y\right)} - z \]
    8. lift-*.f64N/A

      \[\leadsto \left(\color{blue}{x \cdot \log y} - y\right) - z \]
    9. *-commutativeN/A

      \[\leadsto \left(\color{blue}{\log y \cdot x} - y\right) - z \]
    10. lower-*.f6499.9

      \[\leadsto \left(\color{blue}{\log y \cdot x} - y\right) - z \]
  4. Applied rewrites99.9%

    \[\leadsto \color{blue}{\left(\log y \cdot x - y\right) - z} \]
  5. Final simplification99.9%

    \[\leadsto \left(x \cdot \log y - y\right) - z \]
  6. Add Preprocessing

Alternative 2: 85.4% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(-z\right) - y\\ \mathbf{if}\;z \leq -28000000000000:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;z \leq 5 \cdot 10^{+80}:\\ \;\;\;\;\mathsf{fma}\left(\log y, x, -y\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (x y z)
 :precision binary64
 (let* ((t_0 (- (- z) y)))
   (if (<= z -28000000000000.0)
     t_0
     (if (<= z 5e+80) (fma (log y) x (- y)) t_0))))
double code(double x, double y, double z) {
	double t_0 = -z - y;
	double tmp;
	if (z <= -28000000000000.0) {
		tmp = t_0;
	} else if (z <= 5e+80) {
		tmp = fma(log(y), x, -y);
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(x, y, z)
	t_0 = Float64(Float64(-z) - y)
	tmp = 0.0
	if (z <= -28000000000000.0)
		tmp = t_0;
	elseif (z <= 5e+80)
		tmp = fma(log(y), x, Float64(-y));
	else
		tmp = t_0;
	end
	return tmp
end
code[x_, y_, z_] := Block[{t$95$0 = N[((-z) - y), $MachinePrecision]}, If[LessEqual[z, -28000000000000.0], t$95$0, If[LessEqual[z, 5e+80], N[(N[Log[y], $MachinePrecision] * x + (-y)), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(-z\right) - y\\
\mathbf{if}\;z \leq -28000000000000:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;z \leq 5 \cdot 10^{+80}:\\
\;\;\;\;\mathsf{fma}\left(\log y, x, -y\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if z < -2.8e13 or 4.99999999999999961e80 < z

    1. Initial program 100.0%

      \[\left(x \cdot \log y - z\right) - y \]
    2. Add Preprocessing
    3. Taylor expanded in x around 0

      \[\leadsto \color{blue}{-1 \cdot z} - y \]
    4. Step-by-step derivation
      1. mul-1-negN/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(z\right)\right)} - y \]
      2. lower-neg.f6489.1

        \[\leadsto \color{blue}{\left(-z\right)} - y \]
    5. Applied rewrites89.1%

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

    if -2.8e13 < z < 4.99999999999999961e80

    1. Initial program 99.8%

      \[\left(x \cdot \log y - z\right) - y \]
    2. Add Preprocessing
    3. Taylor expanded in z around 0

      \[\leadsto \color{blue}{x \cdot \log y - y} \]
    4. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \color{blue}{x \cdot \log y + \left(\mathsf{neg}\left(y\right)\right)} \]
      2. *-commutativeN/A

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

        \[\leadsto \color{blue}{\mathsf{fma}\left(\log y, x, \mathsf{neg}\left(y\right)\right)} \]
      4. lower-log.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{\log y}, x, \mathsf{neg}\left(y\right)\right) \]
      5. lower-neg.f6491.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\log y, x, -y\right)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 3: 99.9% accurate, 1.0× speedup?

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

\\
\left(x \cdot \log y - z\right) - y
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot \log y - z\right) - y \]
  2. Add Preprocessing
  3. Add Preprocessing

Alternative 4: 66.2% accurate, 18.7× speedup?

\[\begin{array}{l} \\ \left(-z\right) - y \end{array} \]
(FPCore (x y z) :precision binary64 (- (- z) y))
double code(double x, double y, double z) {
	return -z - y;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = -z - y
end function
public static double code(double x, double y, double z) {
	return -z - y;
}
def code(x, y, z):
	return -z - y
function code(x, y, z)
	return Float64(Float64(-z) - y)
end
function tmp = code(x, y, z)
	tmp = -z - y;
end
code[x_, y_, z_] := N[((-z) - y), $MachinePrecision]
\begin{array}{l}

\\
\left(-z\right) - y
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot \log y - z\right) - y \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0

    \[\leadsto \color{blue}{-1 \cdot z} - y \]
  4. Step-by-step derivation
    1. mul-1-negN/A

      \[\leadsto \color{blue}{\left(\mathsf{neg}\left(z\right)\right)} - y \]
    2. lower-neg.f6466.8

      \[\leadsto \color{blue}{\left(-z\right)} - y \]
  5. Applied rewrites66.8%

    \[\leadsto \color{blue}{\left(-z\right)} - y \]
  6. Add Preprocessing

Alternative 5: 34.1% accurate, 37.3× speedup?

\[\begin{array}{l} \\ -y \end{array} \]
(FPCore (x y z) :precision binary64 (- y))
double code(double x, double y, double z) {
	return -y;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = -y
end function
public static double code(double x, double y, double z) {
	return -y;
}
def code(x, y, z):
	return -y
function code(x, y, z)
	return Float64(-y)
end
function tmp = code(x, y, z)
	tmp = -y;
end
code[x_, y_, z_] := (-y)
\begin{array}{l}

\\
-y
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot \log y - z\right) - y \]
  2. Add Preprocessing
  3. Taylor expanded in y around inf

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

      \[\leadsto \color{blue}{\mathsf{neg}\left(y\right)} \]
    2. lower-neg.f6431.6

      \[\leadsto \color{blue}{-y} \]
  5. Applied rewrites31.6%

    \[\leadsto \color{blue}{-y} \]
  6. Add Preprocessing

Alternative 6: 2.3% accurate, 112.0× speedup?

\[\begin{array}{l} \\ y \end{array} \]
(FPCore (x y z) :precision binary64 y)
double code(double x, double y, double z) {
	return y;
}
real(8) function code(x, y, z)
    real(8), intent (in) :: x
    real(8), intent (in) :: y
    real(8), intent (in) :: z
    code = y
end function
public static double code(double x, double y, double z) {
	return y;
}
def code(x, y, z):
	return y
function code(x, y, z)
	return y
end
function tmp = code(x, y, z)
	tmp = y;
end
code[x_, y_, z_] := y
\begin{array}{l}

\\
y
\end{array}
Derivation
  1. Initial program 99.9%

    \[\left(x \cdot \log y - z\right) - y \]
  2. Add Preprocessing
  3. Taylor expanded in y around inf

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

      \[\leadsto \color{blue}{\mathsf{neg}\left(y\right)} \]
    2. lower-neg.f6431.6

      \[\leadsto \color{blue}{-y} \]
  5. Applied rewrites31.6%

    \[\leadsto \color{blue}{-y} \]
  6. Step-by-step derivation
    1. Applied rewrites17.0%

      \[\leadsto \frac{0 - y \cdot y}{\color{blue}{0 + y}} \]
    2. Step-by-step derivation
      1. Applied rewrites2.6%

        \[\leadsto y \]
      2. Add Preprocessing

      Reproduce

      ?
      herbie shell --seed 2024296 
      (FPCore (x y z)
        :name "Statistics.Distribution.Poisson:$clogProbability from math-functions-0.1.5.2"
        :precision binary64
        (- (- (* x (log y)) z) y))