def _ep_pairwise(<EOL>n_items, comparisons, alpha, match_moments, max_iter, initial_state):
<EOL>_ep_pairwise.iterations = <NUM_LIT:0><EOL>m = len(comparisons)<EOL>prior_inv = alpha * np.eye(n_items)<EOL>if initial_state is None:<EOL><INDENT>mean = np.zeros(n_items)<EOL>cov = (<NUM_LIT:1> / alpha) * np.eye(n_items)<EOL>tau = np.zeros(m)<EOL>nu = np.zeros(m)<EOL>prec = np.zeros((n_items, n_items))<EOL>xs = np.zeros(n_items)<EOL><DEDENT>else:<EOL><INDENT>tau, nu = initial_state<EOL>mean, cov, xs, prec = _init_ws(<EOL>n_items, comparisons, prior_inv, tau, nu)<EOL><DEDENT>for _ in range(max_iter):<EOL><INDENT>_ep_pairwise.iterations += <NUM_LIT:1><EOL>tau_old = np.array(tau, copy=True)<EOL>nu_old = np.array(nu, copy=True)<EOL>for i in nprand.permutation(m):<EOL><INDENT>a, b = comparisons[i]<EOL>f_var = cov[a,a] + cov[b,b] - <NUM_LIT:2> * cov[a,b]<EOL>f_mean = mean[a] - mean[b]<EOL>tau_tot = <NUM_LIT:1.0> / f_var<EOL>nu_tot = tau_tot * f_mean<EOL>tau_cav = tau_tot - tau[i]<EOL>nu_cav = nu_tot - nu[i]<EOL>cov_cav = <NUM_LIT:1.0> / tau_cav<EOL>mean_cav = cov_cav * nu_cav<EOL>logpart, dlogpart, d2logpart = match_moments(mean_cav, cov_cav)<EOL>tau[i] = -d2logpart / (<NUM_LIT:1> + d2logpart / tau_cav)<EOL>delta_tau = tau[i] - tau_old[i]<EOL>nu[i] = ((dlogpart - (nu_cav / tau_cav) * d2logpart)<EOL>/ (<NUM_LIT:1> + d2logpart / tau_cav))<EOL>delta_nu = nu[i] - nu_old[i]<EOL>prec[(a, a, b, b), (a, b, a, b)] += delta_tau * MAT_ONE_FLAT<EOL>xs[a] += delta_nu<EOL>xs[b] -= delta_nu<EOL>if abs(delta_tau) > <NUM_LIT:0>:<EOL><INDENT>phi = -<NUM_LIT:1.0> / ((<NUM_LIT:1.0> / delta_tau) + f_var) * MAT_ONE<EOL>upd_mat = cov.take([a, b], axis=<NUM_LIT:0>)<EOL>cov = cov + upd_mat.T.dot(phi).dot(upd_mat)<EOL><DEDENT>mean = cov.dot(xs)<EOL><DEDENT>cov = inv_posdef(prior_inv + prec)<EOL>mean = cov.dot(xs)<EOL>if _converged((tau, nu), (tau_old, nu_old)):<EOL><INDENT>return mean, cov<EOL><DEDENT><DEDENT>raise RuntimeError(<EOL>"<STR_LIT>".format(max_iter))<EOL>
Compute a distribution of model parameters using the EP algorithm.

    Raises
    ------
    RuntimeError
        If the algorithm does not converge after ``max_iter`` iterations.