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

Code before:

        if lmoment is None and 'quadrature_icdf' in methods and (
                self._overrides('_icdf_formula') or self._overrides('_iccdf_formula')):
            lmoment = self._lmoment_integrate_icdf(order, **params)

        if lmoment is None and 'order_statistics' in methods:
            lmoment = self._lmoment_from_order_statistics(order, **params)

        if lmoment is None and 'quadrature_icdf' in methods:
            lmoment = self._lmoment_integrate_icdf(order, **params)

        if lmoment is not None and self.cache_policy != _NO_CACHE:
            self._lmoment_cache[order] = lmoment

        return lmoment

    def _lmoment_formula(self, order, **params):
        return None

    def _lmoment_general(self, order, **params):
        return self.mean() if order == 1 else None

    def _lmoment_from_order_statistics(self, order, **params):
        k = np.arange(order)
        k = xpx.atleast_nd(k, ndim=self._ndim + 1).T
        E = order_statistic(self, r=order-k, n=order).mean()
        bc = special.binom(order-1, k)
        return np.sum((-1)**k * bc * E, axis=0) / order

    def _lmoment_integrate_icdf(self, order, **params):
        def integrand(p, **params):
            x = self._icdf_dispatch(p, **params)
            P = special.eval_sh_legendre(order - 1, p)
            return x * P
        return self._quadrature(integrand, limits=(0., 1.), params=params)

    ### Convenience

    def plot(self, x='x', y=None, *, t=None, ax=None):
        r"""Plot a function of the distribution.

        Convenience function for quick visualization of the distribution
        underlying the random variable.

        Parameters
        ----------
        x, y : str, optional
            String indicating the quantities to be used as the abscissa and
            ordinate (horizontal and vertical coordinates), respectively.
            Defaults are ``'x'`` (the domain of the random variable) and either
            ``'pdf'`` (the probability density function) (continuous) or

Review comment (performance):
If I remember correctly, these coefficients coincide with the inverse of the lower pascal matrix. Perhaps that could speed things up a bit here.

Code after:
                self._overrides('_icdf_formula') or self._overrides('_iccdf_formula')):
            lmoment = self._lmoment_integrate_icdf(order, **params)

        if lmoment is None and 'order_statistics' in methods:
            lmoment = self._lmoment_from_order_statistics(order, **params)

        if lmoment is None and 'quadrature_icdf' in methods:
            lmoment = self._lmoment_integrate_icdf(order, **params)

        if lmoment is not None and self.cache_policy != _NO_CACHE:
            self._lmoment_cache[order] = lmoment

        return lmoment

    def _lmoment_formula(self, order, **params):
        return None

    def _lmoment_general(self, order, **params):
        return self.mean() if order == 1 else None

    def _lmoment_from_order_statistics(self, order, **params):
        k = np.arange(order)
        k = xpx.atleast_nd(k, ndim=self._ndim + 1).T
        E = order_statistic(self, r=order-k, n=order).mean()
        bc = special.binom(order-1, k)
        return np.sum((-1)**k * bc * E, axis=0) / order

    def _lmoment_integrate_icdf(self, order, **params):
        def integrand(p, **params):
            x = self._icdf_dispatch(p, **params)
            P = special.eval_sh_legendre(order - 1, p)
            return x * P
        return self._quadrature(integrand, limits=(0., 1.), params=params)

    ### Convenience

    def plot(self, x='x', y=None, *, t=None, ax=None):
        r"""Plot a function of the distribution.

        Convenience function for quick visualization of the distribution
        underlying the random variable.

        Parameters
        ----------
        x, y : str, optional
            String indicating the quantities to be used as the abscissa and
            ordinate (horizontal and vertical coordinates), respectively.
            Defaults are ``'x'`` (the domain of the random variable) and either
            ``'pdf'`` (the probability density function) (continuous) or
            ``'pdf'`` (the probability density function) (discrete).
            Valid values are: