def compute_JSD(mu_P, Sigma_P, mu_Q, Sigma_Q, N_MC_samples): # Jensen-Shannon distance (JSD) out_JSD = np.empty_like(mu_P) for index in range(mu_P.size): RV_p = MVN(mean=mu_P[index], cov=Sigma_P[index, index]) RV_q = MVN(mean=mu_Q[index], cov=Sigma_Q[index, index]) x = RV_p.rvs(N_MC_samples) p_x = RV_p.pdf(x) q_x = RV_q.pdf(x) m_x = (p_x + q_x) / 2. y = RV_q.rvs(N_MC_samples) p_y = RV_p.pdf(y) q_y = RV_q.pdf(y) m_y = (p_y + q_y) / 2. dKL_pm = np.log(p_x / m_x).mean() dKL_qm = np.log(q_y / m_y).mean() out_JSD[index] = 0.5 * (dKL_pm + dKL_qm) return out_JSD
def hastings_sampler(MN_OBJ, iter=10000): from scipy.stats import multivariate_normal as MN samples = np.zeros((iter, 2)) d_old = np.random.rand(2) mus = [10, 10] sig = [[1, 0.5], [0.5, 1]] for i in range(iter): delta = np.random.multivariate_normal(mus, sig) MN_PRO = MN(mus, sig) d_new = d_old + delta u = np.random.rand() samples[i, :] = d_old if u <= (MN_OBJ.pdf(d_new) * MN_PRO.pdf(-1 * delta)) / ( MN_OBJ.pdf(d_old) * MN_PRO.pdf(delta)): samples[i, :] = d_new d_old = d_new """ x = p_x_given_y(y, mus, sigmas) y = p_y_given_x(x, mus, sigmas) samples[i, :] = [x, y] """ return samples