def _helmert_contrast(self, levels): n = len(levels) #http://www.ats.ucla.edu/stat/sas/webbooks/reg/chapter5/sasreg5.htm#HELMERT #contr = np.eye(n - 1) #int_range = np.arange(n - 1., 1, -1) #denom = np.repeat(int_range, np.arange(n - 2, 0, -1)) #contr[np.tril_indices(n - 1, -1)] = -1. / denom #http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm#HELMERT #contr = np.zeros((n - 1., n - 1)) #int_range = np.arange(n, 1, -1) #denom = np.repeat(int_range[:-1], np.arange(n - 2, 0, -1)) #contr[np.diag_indices(n - 1)] = (int_range - 1.) / int_range #contr[np.tril_indices(n - 1, -1)] = -1. / denom #contr = np.vstack((contr, -1./int_range)) #r-like contr = np.zeros((n, n - 1)) contr[1:][diag_indices(n - 1)] = np.arange(1, n) contr[triu_indices(n - 1)] = -1 return contr