# Deviance residual for binomial regression

The deviance residual for binomial regression has the form $$sign(y_i- m_i\hat{p}_i)d_i$$, where $$m_i\hat{p}_i$$ is the fitted value, $$d_i$$ is the contribution of observation i to the deviance of the model fitted. I am having some troubles deriving the form of $$d_i$$. As shown below, I could not obtain the coefficient for the 2nd log term to be $$m_i-y_i$$. Could anyone show me where I made mistakes? Thanks in advance!

The form given in my lecture notes:

My attempt of derivation:

As mentioned by Thomas, the natural parameter $$\theta_i$$ = logit($$\mu_i$$) instead. The correct derivation:

Your version breaks down if $$y_i=m_i$$ but $$\hat p_i\neq 1$$, giving $$m_i\log 0=-\infty$$ for the second term and no rescue from the first term, which stays finite. The version in your notes is ok if you define $$x\log x$$ at $$x=0$$ by the limit from above.
Even before that, though, you've taken the parameter to be $$\log \mu$$ and it's actually $$\mathrm{logit} \mu$$ (assuming you're using the canonical logit link, which matches the formula in your lecture notes).