def _calc(self, y, w):
if self.discrete:<EOL><INDENT>self.lclass_ids = weights.lag_categorical(w, self.class_ids,<EOL>ties="<STR_LIT>")<EOL><DEDENT>else:<EOL><INDENT>ly = weights.lag_spatial(w, y)<EOL>self.lclass_ids, self.lag_cutoffs, self.m = self._maybe_classify(<EOL>ly, self.m, self.lag_cutoffs)<EOL>self.lclasses = np.arange(self.m)<EOL><DEDENT>T = np.zeros((self.m, self.k, self.k))<EOL>n, t = y.shape<EOL>for t1 in range(t - <NUM_LIT:1>):<EOL><INDENT>t2 = t1 + <NUM_LIT:1><EOL>for i in range(n):<EOL><INDENT>T[self.lclass_ids[i, t1], self.class_ids[i, t1],<EOL>self.class_ids[i, t2]] += <NUM_LIT:1><EOL><DEDENT><DEDENT>P = np.zeros_like(T)<EOL>for i, mat in enumerate(T):<EOL><INDENT>row_sum = mat.sum(axis=<NUM_LIT:1>)<EOL>row_sum = row_sum + (row_sum == <NUM_LIT:0>)<EOL>p_i = np.matrix(np.diag(<NUM_LIT:1.> / row_sum) * np.matrix(mat))<EOL>P[i] = p_i<EOL><DEDENT>return T, P<EOL>
Helper to estimate spatial lag conditioned Markov transition
        probability matrices based on maximum likelihood techniques.