in this video i want to talk about the deviants the deviance is a generalization of the residuals in ordinary least squares so you can think that in regular regression we use the residuals whereas in generalized linear regression in glm we use the deviance and so the deviants kind of replace some of the functions that ordinary residuals have in regular linear regression and so i want to start with the first quantity which is the unit deviance and in order to do that let's first remember how does the exponential family pdf look like okay so every pdf
looks like this and what i want to focus on is this quantity over here and i'm going to denote this quantity by t y and mu now you might ask why mu i mean the parameter here is theta but theta is dependent on mu theta is a monotone function of mu so this is equal now let's analyze this function a bit let's say what happens if we differentiate it with regards to mu and here i will use the chain rule okay and we've seen in a previous video that derivative of mu with regards to theta
is what is called the the variation function so this quantity over here and here it's just one over this v of mu and this quantity over here we said this is just mu this is just the mean okay so if we take this then we get that y minus mu and if we want to find the stationary point we have to equate this to zero and so we find that the stationary point is where y is equal to mu and if we were to differentiate this again it can be shown that this point is actually
a maximum so this quantity this value over here is the most when y is equal to mu and it goes down when y is less than mu and so now this can help us um define the unit deviance okay so we'll define the human deviant as twice t y where instead of mu we put y again meaning we calculate this function at its maximum minus t y of mu and note that this quantity since this is the maximum is always greater or equal than zero yeah so the unit deviance is equal to zero when we
put instead of mu y and it grows when mu goes away from y so in that sense it's also close to the notion of residual it's close to the notion of distance from of y from the mean of distribution at that point so this is in fact some sort of a distance measure between y and mu and let's calculate two examples of this let's say for the poisson so we've seen already that we can write the pdf of a poisson as where our t y mu the quantity that i showed you before is this thing
over here so if we want to calculate the unit deviance for poisson distribution it will be 2 times y lan now instead of mu the mean of a poisson distribution is the lambda so we put y minus and if we add all the terms together we get and this is the unit deviance of the poisson distribution and another example let's look at a normal so again we saw in a previous video that we can write the pdf of a normal distribution in the form of the exponential family as e where this thing over here can
just be thought of as the c function of y and the dispersion parameter which here is the sigma so again our quantity is this thing over here and we can calculate the unit deviance and here you can see that this looks exactly like regular residual in regular linear regression but this is only the case for the normal distribution and i encourage you to go back to the previous examples of uh the exponential family and calculate their unit deviance by yourself and so as i mentioned the unit deviance is a measure of the discrepancy between y
and u but it is not exactly a distance because it's not exactly symmetric and here is a graph that i took from a book about general linear regressions and r which i'll link in the description and you can see that for the normal case the unit deviance is symmetric so it is more or less a distance but for other distributions like poisson gamma and bernoulli you can see that the unit deviance is not symmetric now going back to our exponential family um general form then we can actually write this in terms of the deviance we
can write this as and notice in here we actually added a term i mean this thing over here is like the second term in the this thing over here it's like the second term in the unit deviance but the first term we kind of added it so we have to subtract it back but that's okay we can just uh this second term can just be swallowed by let's say a new c function yes c wave which again will only depend on y and the dispersion parameter and what we can do is that we can approximate
this function this function over here uh using the saturn saddle point approximation i mean we can approximate this c function which might not have a closed form but we can approximate it using saddle point approximation or also called laplace's method and we can approximate this function and what we will get is that this is equal more or less to this is the deviance over here and this is the variation function uh evaluated at mu equal y and why is this important well if we use this approximation of the pdf of the exponential family then what
we get and this is important what we get is that the unit deviance divided by the dispersion function is distributes chi squared with 1 degrees of freedom and this also means that the expectation of the deviance the unit deviance is this dispersion function now this is true under some milled assumptions and there are some guidelines as to when this approximation is more accurate and when it is not but i don't want to get into this right now but this is in general what i wanted to say about the unit deviance so the unit deviance is
kind of an approximation of the distance between uh the point y and its mean for every point in our regression in our general regression now to move from the unit deviance to the total deviance what we need to do is just sum over all of the unit deviant so total deviance is the sum over all of our y's the unit deviance between y i and its corresponding mean mu i and sometimes if we have certain observations that have more weight then this can also be the sum and the total deviance divided by the dispersion function
this distributes chi-square with n degrees of freedom and this quantity is also sometimes called the scaled total deviance and it can be shown that the scale total deviance is actually equal to two times the log likelihood of the saturated model meaning that we replace mu with y minus the non-saturated model and this is easy to show because what is the log likelihood of the general form of the exponential family well it's just this t quantity plus this c function and i'm sorry this is the sum of this over all observations and if we take now
this quantity this is l and if we take replace mu with y for this thing and then we subtract it we just get the scaled deviance and now the final thing i want to talk about in this video are the null and residual deviance and these are just the total deviants only we calculate them on our estimated coefficient so in the null deviance what we do is we calculate a model where we only have an intercept so the link function g of mu has a linear predictor with only an intercept yeah and then we calculate
the total deviance of the y and the fitted values for the mu and this will be our null deviance and the residual deviance will be the full model so let's say we have some model and it has an intercept but also some slope of some explanatory variable then again we will fit the model to the data and then we will get the estimated values for these muse and the total deviance of these estimated values will be the residual deviance yeah and this can be thought of kind of like the residual sum of square and actually
it is equal to the residual sum of square in the normal case but it's not in other distributions and so what we get from this is now that we can have uh we can have some measure of the goodness of fit of our model to the data and we can have it in general i mean how much in general our model is good to describing the data or we can also have it for specific coefficients a specific x okay how much is that x meaningful and significant and does it lower the residual the total residual
deviance by a lot and if so it means that the coefficient is significant and that this explanatory variable is needed in our model and i just want to show you an example in r well i generated data that looks like this okay and i did it using a poisson distribution and so i'm fitting now a general linear model uh through this poisson distribution and what the and this is the linear predictor we have mu equal 2.3 times x so the link function is just the identity function there is no intercept and the slope is 2.3
and we can see that indeed the um the estimation is quite close we get an intercept of 0.49 and a slope of 2.18 and look what else r tells us this tells us that the null deviance is 312 and this is what would be the total residual deviance if we fitted a model with only intercept and also it tells us the residual deviance which is what is the total deviance using our estimated muse and we can see that this quantity is 79 and it's on 74 degrees of freedom this quantity again distributes chi square so
um a chi-square with 74 degrees of freedom this quantity is very very uh probable so it seems that our model is doing a very good job and so the goodness of fit is very very good okay that's all i wanted to tell you about in this video i hope you enjoyed and see you in the next one