def log_i0(x): """ Calculates the logarithm of the 0 order modified Bessel function of the first kind"" """ return at.switch( at.lt(x, 5), at.log1p(x**2.0 / 4.0 + x**4.0 / 64.0 + x**6.0 / 2304.0 + x**8.0 / 147456.0 + x**10.0 / 14745600.0 + x**12.0 / 2123366400.0), x - 0.5 * at.log(2.0 * np.pi * x) + at.log1p(1.0 / (8.0 * x) + 9.0 / (128.0 * x**2.0) + 225.0 / (3072.0 * x**3.0) + 11025.0 / (98304.0 * x**4.0)), )
def normal_lccdf(mu, sigma, x): z = (x - mu) / sigma return aet.switch( aet.gt(z, 1.0), aet.log(aet.erfcx(z / aet.sqrt(2.0)) / 2.0) - aet.sqr(z) / 2.0, aet.log1p(-aet.erfc(-z / aet.sqrt(2.0)) / 2.0), )
def normal_lcdf(mu, sigma, x): """Compute the log of the cumulative density function of the normal.""" z = (x - mu) / sigma return aet.switch( aet.lt(z, -1.0), aet.log(aet.erfcx(-z / aet.sqrt(2.0)) / 2.0) - aet.sqr(z) / 2.0, aet.log1p(-aet.erfc(z / aet.sqrt(2.0)) / 2.0), )
def log1mexp(x): r"""Return log(1 - exp(-x)). This function is numerically more stable than the naive approach. For details, see https://cran.r-project.org/web/packages/Rmpfr/vignettes/log1mexp-note.pdf References ---------- .. [Machler2012] Martin Mächler (2012). "Accurately computing `\log(1-\exp(- \mid a \mid))` Assessed by the Rmpfr package" """ return at.switch(at.lt(x, 0.6931471805599453), at.log(-at.expm1(-x)), at.log1p(-at.exp(-x)))
def logaddexp(a, b): diff = b - a return at.switch(diff > 0, b + at.log1p(at.exp(-diff)), a + at.log1p(at.exp(diff)))