示例#1
0
    def testReproducibility(self):
        def target_log_prob_fn(event):
            return tfd.Normal(loc=0., scale=1.).log_prob(event)

        tf.set_random_seed(4)
        xs = no_u_turn_sampler.kernel(target_log_prob_fn=target_log_prob_fn,
                                      current_state=[0.],
                                      step_size=[0.3],
                                      seed=3)
        tf.set_random_seed(4)
        ys = no_u_turn_sampler.kernel(target_log_prob_fn=target_log_prob_fn,
                                      current_state=[0.],
                                      step_size=[0.3],
                                      seed=3)
        for x, y in zip(xs, ys):
            self.assertAllEqual(x, y)
示例#2
0
    def testMultivariateNormalNd(self, event_size, num_samples):
        def target_log_prob_fn(event):
            return tfd.MultivariateNormalFullCovariance(
                loc=tf.zeros(event_size),
                covariance_matrix=tf.eye(event_size)).log_prob(event)

        state = tf.zeros(event_size)
        samples = []
        for seed in range(num_samples):
            [state], _, _ = no_u_turn_sampler.kernel(
                target_log_prob_fn=target_log_prob_fn,
                current_state=[state],
                step_size=[0.3],
                seed=seed)
            npstate = state.numpy()
            samples.append([npstate[0], npstate[1]])

        samples = np.array(samples)
        plt.scatter(samples[:, 0], samples[:, 1])
        savefig("projection_chain_{}d_normal_{}_steps.png".format(
            event_size, num_samples))
        plt.close()

        target_samples = tfd.MultivariateNormalFullCovariance(
            loc=tf.zeros(event_size),
            covariance_matrix=tf.eye(event_size)).sample(num_samples,
                                                         seed=4).numpy()
        plt.scatter(target_samples[:, 0], target_samples[:, 1])
        savefig("projection_independent_{}d_normal_{}_samples.png".format(
            event_size, num_samples))
        plt.close()
示例#3
0
    def testSkewedMultivariateNormal2d(self):
        def target_log_prob_fn(event):
            return tfd.MultivariateNormalFullCovariance(
                loc=tf.zeros(2),
                covariance_matrix=tf.diag([1., 10.])).log_prob(event)

        rng = np.random.RandomState(seed=7)
        states = tf.cast(rng.normal(scale=[1.0, 10.0], size=[10, 2]),
                         tf.float32)
        plt.scatter(states[:, 0], states[:, 1])
        savefig("skewed_start_positions_2d.png")
        plt.close()

        samples = []
        for seed, state in enumerate(states):
            [state], _, _ = no_u_turn_sampler.kernel(
                target_log_prob_fn=target_log_prob_fn,
                current_state=[state],
                step_size=[0.3],
                seed=seed)
            samples.append(state)

        samples = tf.stack(samples).numpy()
        plt.scatter(samples[:, 0], samples[:, 1])
        savefig("one_step_skewed_posterior_conservation_2d.png")
        plt.close()
        plot_with_expectation(
            samples[:, 0],
            dist=scipy.stats.norm(0, 1),
            suffix="one_step_skewed_posterior_conservation_2d_dim_0.png")
        plot_with_expectation(
            samples[:, 1],
            dist=scipy.stats.norm(0, 10),
            suffix="one_step_skewed_posterior_conservation_2d_dim_1.png")
示例#4
0
    def testLogitBeta(self):
        def target_log_prob_fn(event):
            return tfd.TransformedDistribution(
                distribution=tfd.Beta(concentration0=1.0, concentration1=3.0),
                bijector=tfb.Invert(tfb.Sigmoid())).log_prob(event)

        states = tfd.TransformedDistribution(
            distribution=tfd.Beta(concentration0=1.0, concentration1=3.0),
            bijector=tfb.Invert(tfb.Sigmoid())).sample(10, seed=7)
        plt.hist(states.numpy(), bins=30)
        savefig("logit_beta_start_positions.png")
        plt.close()

        samples = []
        for seed, state in enumerate(states):
            [state], _, _ = no_u_turn_sampler.kernel(
                target_log_prob_fn=target_log_prob_fn,
                current_state=[state],
                step_size=[0.3],
                seed=seed)
            samples.append(state)

        samples = np.array(samples)
        plt.hist(samples, bins=30)
        savefig("one_step_logit_beta_posterior_conservation.png")
        plt.close()

        _ = scipy.stats.ks_2samp(samples.flatten(), states.numpy().flatten())
示例#5
0
    def testOneStepFromOrigin(self):
        def target_log_prob_fn(event):
            return tfd.Normal(loc=0., scale=1.).log_prob(event)

        samples = []
        for seed in range(10):
            [state], _, _ = no_u_turn_sampler.kernel(
                target_log_prob_fn=target_log_prob_fn,
                current_state=[0.],
                step_size=[0.3],
                seed=seed)
            samples.append(state)

        samples = np.array(samples)
        plt.hist(samples, bins=30)
        savefig("one_step_from_origin.png")
        plt.close()
示例#6
0
  def testNormal(self):
    def target_log_prob_fn(event):
      return tfd.Normal(loc=0., scale=1.).log_prob(event)

    rng = np.random.RandomState(seed=7)
    states = tf.cast(rng.normal(size=10), dtype=tf.float32)
    tf.set_random_seed(2)
    samples = []
    for seed, state in enumerate(states):
      [state], _, _ = no_u_turn_sampler.kernel(
          target_log_prob_fn=target_log_prob_fn,
          current_state=[state],
          step_size=[0.3],
          seed=seed)
      samples.append(state)

    samples = np.array(samples)
    plot_with_expectation(samples,
                          dist=scipy.stats.norm(0, 1),
                          suffix="one_step_posterior_conservation_normal.png")