'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 sqrt(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{(2 * Y_i * \log(Y_i / \mu_i)
- (2 / \alpha) * (1 + \alpha * Y_i)
* \log((1 + \alpha * Y_i) / (1 + \alpha * \mu_i)))/scale}'

def resid_dev(self, endog, mu, scale=1.0):

    endog_mu = self._clean((endog / mu))
    tmp = self._clean(((1 + (self.alpha * endog)) / (1 + (self.alpha * mu))))
    return (np.sign((endog - mu)) * np.sqrt(((((2 * endog) * np.log(endog_mu)) - (((2 / self.alpha) * (1 + (self.alpha * endog))) * np.log(tmp))) / scale)))
