Repository: scipy/scipy
File: scipy/stats/_new_distributions.py (Python)

Code before:

    def _entropy_formula(self, **kwargs):
        return (1 + np.log(2*np.pi))/2

    def _logentropy_formula(self, **kwargs):
        return np.log1p(np.log(2*np.pi)) - np.log(2)

    def _median_formula(self, **kwargs):
        return 0

    def _mode_formula(self, **kwargs):
        return 0

    def _moment_raw_formula(self, order, **kwargs):
        raw_moments = {0: 1, 1: 0, 2: 1, 3: 0, 4: 3, 5: 0}
        return raw_moments.get(order, None)

    def _moment_central_formula(self, order, **kwargs):
        return self._moment_raw_formula(order, **kwargs)

    def _moment_standardized_formula(self, order, **kwargs):
        return self._moment_raw_formula(order, **kwargs)

    def _lmoment_formula(self, order, **kwargs):
        lscale = 1 / np.sqrt(np.pi)
        lkurtosis = 30*np.arctan(np.sqrt(2))/np.pi - 9
        lmoments = {1: 0, 2: lscale, 3: 0, 4: lkurtosis * lscale}
        return lmoments.get(order, None)

    def _sample_formula(self, full_shape, rng, **kwargs):
        return rng.normal(size=full_shape)[()]


class Logistic(ContinuousDistribution):
    r"""Standard logistic distribution.

    The probability density function of the standard logistic distribution is:

    .. math::

        f(x) = \frac{1}{\left( e^{x / 2} + e^{-x / 2} \right)^2}

    """
    _x_support = _RealInterval(endpoints=(-inf, inf))
    _variable = _x_param = _RealParameter('x', domain=_x_support, typical=(-9, 9))
    _parameterizations = ()

    _scale = np.pi / np.sqrt(3)

    def _logpdf_formula(self, x, **kwargs):
        y = -np.abs(x)

Review comment (refactor):
Since I've seen this twice now, maybe it's an idea to extract this as a constant? If you care about import time, then you could pre-computing it with wolfram alpha or something.

Code after:

    def _entropy_formula(self, **kwargs):
        return (1 + np.log(2*np.pi))/2

    def _logentropy_formula(self, **kwargs):
        return np.log1p(np.log(2*np.pi)) - np.log(2)

    def _median_formula(self, **kwargs):
        return 0

    def _mode_formula(self, **kwargs):
        return 0

    def _moment_raw_formula(self, order, **kwargs):
        raw_moments = {0: 1, 1: 0, 2: 1, 3: 0, 4: 3, 5: 0}
        return raw_moments.get(order, None)

    def _moment_central_formula(self, order, **kwargs):
        return self._moment_raw_formula(order, **kwargs)

    def _moment_standardized_formula(self, order, **kwargs):
        return self._moment_raw_formula(order, **kwargs)

    def _lmoment_formula(self, order, **kwargs):
        return super()._lmoment_formula(order, mu=0., sigma=1., **kwargs)

    def _sample_formula(self, full_shape, rng, **kwargs):
        return rng.normal(size=full_shape)[()]


class Logistic(ContinuousDistribution):
    r"""Standard logistic distribution.

    The probability density function of the standard logistic distribution is:

    .. math::

        f(x) = \frac{1}{\left( e^{x / 2} + e^{-x / 2} \right)^2}

    """
    _x_support = _RealInterval(endpoints=(-inf, inf))
    _variable = _x_param = _RealParameter('x', domain=_x_support, typical=(-9, 9))
    _parameterizations = ()

    _scale = np.pi / np.sqrt(3)

    def _logpdf_formula(self, x, **kwargs):
        y = -np.abs(x)
        return y - 2 * special.log1p(np.exp(y))

    def _pdf_formula(self, x, **kwargs):