'EM step for regime transition probabilities'

def _em_regime_transition(self, result):

    tmp = result.smoothed_joint_probabilities
    for i in range((tmp.ndim - 3)):
        tmp = np.sum(tmp, (-2))
    smoothed_joint_probabilities = tmp
    k_transition = len(self.parameters[(0, 'regime_transition')])
    regime_transition = np.zeros((self.k_regimes, k_transition))
    for i in range(self.k_regimes):
        for j in range((self.k_regimes - 1)):
            regime_transition[(i, j)] = (np.sum(smoothed_joint_probabilities[(j, i)]) / np.sum(result.smoothed_marginal_probabilities[i]))
        delta = (np.sum(regime_transition[i]) - 1)
        if (delta > 0):
            warnings.warn('Invalid    regime    transition    probabilities    estimated    in    EM    iteration;    probabilities    have    been    re-scaled    to    continue    estimation.', EstimationWarning)
            regime_transition[i] /= ((1 + delta) + 1e-06)
    return regime_transition
