Q:

How does backward difference encoding work for testing set? 

According to this website (http://www.statsmodels.org/dev/contrasts.htmlenter link description here), the definition of backward difference encoding is 'In backward difference coding, the mean of the dependent variable for a level is compared with the mean of the dependent variable for the prior level.  This type of coding may be useful for a nominal or an ordinal variable.'  
What I don't understand is if this encoding method relies on the dependent variable (same thing as output variable if I understand correctly), how can we perform backward difference encoding with the testing set when dependent variable is not given to the model ahead of time?  In the training set, values for the dependent variable is given but in testing set they are not given.  Can anybody advise? 

A:

This method is simply Impact Coding the categorial variables. 
If your categorical column has the categories {C1,C2,C3,...}, then impact coding is done as follows:
                         Impact(category = Ci) = E[y|Ci] - E[y]

During training, for each category, (Ci), it calculates the difference between the mean output (given that category, i.e. a posteriori) and the overall expected value of the dependent variable (a priori).  For more reference on impact coding, you can refer to this paper https://arxiv.org/abs/1611.09477v3 (Page10)
In the testing phase, to convert the categorial variables of the testing data to their impact codes, it uses the same expected value of 'y' as it used for the training data.  Since this is an expected value, it doesn't matter if the training data has more number of samples than the testing data (as long as the distribution of 'y' is similar in both the datasets).