def resid_dev(self, endog, mu, scale=1.):
        r"""
        Negative Binomial Deviance Residual

        Parameters
        ----------
        endog : array-like
            `endog` is the response variable
        mu : array-like
            `mu` is the fitted value of the model
        scale : float, optional
            An optional argument to divide the residuals by scale. The default
            is 1.

        Returns
        --------
        resid_dev : array
            The array of deviance residuals

        Notes
        -----
        :math:`resid\_dev_i = sign(Y_i-\mu_i) * \sqrt{piecewise_i}`

        where :math:`piecewise_i` is defined as

        If :math:`Y_i = 0`:

        :math:`piecewise_i = 2 * \log(1 + \alpha * \mu_i)/ \alpha`

        If :math:`Y_i > 0`:

        :math:`piecewise_i = 2 * Y_i * \log(Y_i / \mu_i) - (2 / \alpha) *
        (1 + \alpha * Y_i) * \log((1 + \alpha * Y_i) / (1 + \alpha * \mu_i))`
        """
        iszero = np.equal(endog, 0)
        notzero = 1 - iszero
        endog_mu = self._clean(endog / mu)
        tmp = iszero * 2 * np.log(1 + self.alpha * mu) / self.alpha
        tmp += notzero * (2 * endog * np.log(endog_mu) - 2 / self.alpha *
                          (1 + self.alpha * endog) *
                          np.log((1 + self.alpha * endog) /
                                 (1 + self.alpha * mu)))
        return np.sign(endog - mu) * np.sqrt(tmp) / scale
dataset/ETHPy150Open statsmodels/statsmodels/statsmodels/genmod/families/family.py/NegativeBinomial.resid_dev/original