def testMixtureTargetLogProb(self): seed = test_util.test_seed() n = 4 mu = np.ones(n) * (1. / 2) w = 0.1 proposal = tfd.Sample(tfd.Normal(0., 10.), sample_shape=n) init_state = proposal.sample(5000, seed=seed) likelihood_dist = tfd.MixtureSameFamily( mixture_distribution=tfd.Categorical(probs=[w, 1. - w]), components_distribution=tfd.MultivariateNormalDiag( loc=np.asarray([mu, -mu]).astype(np.float32), scale_identity_multiplier=[.1, .2])) # Uniform prior init_log_prob = tf.zeros_like(proposal.log_prob(init_state)) [n_stage, final_state, _] = tfp.experimental.mcmc.sample_sequential_monte_carlo( lambda x: init_log_prob, likelihood_dist.log_prob, init_state, max_num_steps=50, parallel_iterations=1, seed=seed) self.assertTrue(self.evaluate(n_stage), 15) self.evaluate( st.kolmogorov_smirnov_distance_two_sample( final_state, likelihood_dist.sample(5000, seed=seed)))
def test_kolmogorov_smirnov_distance_two_sample(self, dtype): del dtype samples1 = np.array([[1, 1, 2, 2], [1, 2, 3, 4], [1, 3, 3, 3]]) samples2 = np.array([[1, 2, 2, 1, 1, 2], [4, 4, 3, 2, 1, 1], [2, 2, 2, 1, -4, 7]]) # Unlike empirical_cdfs, the samples Tensors must come in iid across the # leading dimension. obtained = self.evaluate( st.kolmogorov_smirnov_distance_two_sample( tf.transpose(a=samples1), tf.transpose(a=samples2))) self.assertAllClose([0.0, 1 / 12., 0.75 - 1 / 6.], obtained, atol=1e-7)