Exemplo n.º 1
0
    def test_model_rmh_posterior_with_stop_and_resume(self):
        posterior_num_runs = 100
        posterior_num_traces_each_run = 20
        posterior_num_traces_correct = posterior_num_traces_each_run * posterior_num_runs
        true_posterior = Normal(7.25, math.sqrt(1/1.2))
        posterior_mean_correct = float(true_posterior.mean)
        posterior_stddev_correct = float(true_posterior.stddev)
        prior_mean_correct = 1.
        prior_stddev_correct = math.sqrt(5)

        posteriors = []
        initial_trace = None
        for i in range(posterior_num_runs):
            posterior = self._model.posterior_traces(num_traces=posterior_num_traces_each_run, inference_engine=InferenceEngine.RANDOM_WALK_METROPOLIS_HASTINGS, observe={'obs0': 8, 'obs1': 9}, initial_trace=initial_trace)
            initial_trace = posterior[-1]
            posteriors.append(posterior)
        posterior = Empirical.combine(posteriors).map(lambda trace: trace.result)
        posterior_num_traces = posterior.length
        posterior_mean = float(posterior.mean)
        posterior_mean_unweighted = float(posterior.unweighted().mean)
        posterior_stddev = float(posterior.stddev)
        posterior_stddev_unweighted = float(posterior.unweighted().stddev)
        kl_divergence = float(pyprob.distributions.Distribution.kl_divergence(true_posterior, Normal(posterior.mean, posterior.stddev)))

        util.eval_print('posterior_num_runs', 'posterior_num_traces_each_run', 'posterior_num_traces', 'posterior_num_traces_correct', 'prior_mean_correct', 'posterior_mean_unweighted', 'posterior_mean', 'posterior_mean_correct', 'prior_stddev_correct', 'posterior_stddev_unweighted', 'posterior_stddev', 'posterior_stddev_correct', 'kl_divergence')

        self.assertEqual(posterior_num_traces, posterior_num_traces_correct)
        self.assertAlmostEqual(posterior_mean, posterior_mean_correct, places=0)
        self.assertAlmostEqual(posterior_stddev, posterior_stddev_correct, places=0)
        self.assertLess(kl_divergence, 0.25)
Exemplo n.º 2
0
    def test_dist_empirical_combine_non_uniform_weights_use_initial(self):
        samples1 = 10000
        samples2 = 1000
        observation = [8, 9]
        posterior_mean_correct = 7.25
        posterior_stddev_correct = math.sqrt(1/1.2)

        posterior1 = self._model_gum.posterior_distribution(samples1, observation=observation)
        posterior1_mean = float(posterior1.mean)
        posterior1_mean_unweighted = float(posterior1.unweighted().mean)
        posterior1_stddev = float(posterior1.stddev)
        posterior1_stddev_unweighted = float(posterior1.unweighted().stddev)
        kl_divergence1 = float(util.kl_divergence_normal(Normal(posterior_mean_correct, posterior_stddev_correct), Normal(posterior1.mean, posterior1_stddev)))

        posterior2 = self._model_gum.posterior_distribution(samples2, observation=observation)
        posterior2_mean = float(posterior2.mean)
        posterior2_mean_unweighted = float(posterior2.unweighted().mean)
        posterior2_stddev = float(posterior2.stddev)
        posterior2_stddev_unweighted = float(posterior2.unweighted().stddev)
        kl_divergence2 = float(util.kl_divergence_normal(Normal(posterior_mean_correct, posterior_stddev_correct), Normal(posterior2.mean, posterior2_stddev)))

        posterior = Empirical.combine([posterior1, posterior2], use_initial_values_and_weights=True)
        posterior_mean = float(posterior.mean)
        posterior_mean_unweighted = float(posterior.unweighted().mean)
        posterior_stddev = float(posterior.stddev)
        posterior_stddev_unweighted = float(posterior.unweighted().stddev)
        kl_divergence = float(util.kl_divergence_normal(Normal(posterior_mean_correct, posterior_stddev_correct), Normal(posterior.mean, posterior_stddev)))

        util.debug('samples1', 'posterior1_mean_unweighted', 'posterior1_mean', 'posterior1_stddev_unweighted', 'posterior1_stddev', 'kl_divergence1', 'samples2', 'posterior2_mean_unweighted', 'posterior2_mean', 'posterior2_stddev_unweighted', 'posterior2_stddev', 'kl_divergence2', 'posterior_mean_unweighted', 'posterior_mean', 'posterior_mean_correct', 'posterior_stddev_unweighted', 'posterior_stddev', 'posterior_stddev_correct', 'kl_divergence')

        self.assertAlmostEqual(posterior1_mean, posterior_mean_correct, places=0)
        self.assertAlmostEqual(posterior1_stddev, posterior_stddev_correct, places=0)
        self.assertLess(kl_divergence1, 0.25)
        self.assertAlmostEqual(posterior2_mean, posterior_mean_correct, places=0)
        self.assertAlmostEqual(posterior2_stddev, posterior_stddev_correct, places=0)
        self.assertLess(kl_divergence2, 0.25)
        self.assertAlmostEqual(posterior_mean, posterior_mean_correct, places=0)
        self.assertAlmostEqual(posterior_stddev, posterior_stddev_correct, places=0)
        self.assertLess(kl_divergence, 0.25)
Exemplo n.º 3
0
    def test_dist_empirical_combine_uniform_weights(self):
        dist1_mean_correct = 1
        dist1_stddev_correct = 3
        dist2_mean_correct = 5
        dist2_stddev_correct = 2
        dist3_mean_correct = -2.5
        dist3_stddev_correct = 1.2
        dist_combined_mean_correct = 1.16667
        dist_combined_stddev_correct = 3.76858

        dist1 = Normal(dist1_mean_correct, dist1_stddev_correct)
        dist1_empirical = Empirical([dist1.sample() for i in range(empirical_samples)])
        dist1_empirical_mean = float(dist1_empirical.mean)
        dist1_empirical_stddev = float(dist1_empirical.stddev)
        dist2 = Normal(dist2_mean_correct, dist2_stddev_correct)
        dist2_empirical = Empirical([dist2.sample() for i in range(empirical_samples)])
        dist2_empirical_mean = float(dist2_empirical.mean)
        dist2_empirical_stddev = float(dist2_empirical.stddev)
        dist3 = Normal(dist3_mean_correct, dist3_stddev_correct)
        dist3_empirical = Empirical([dist3.sample() for i in range(empirical_samples)])
        dist3_empirical_mean = float(dist3_empirical.mean)
        dist3_empirical_stddev = float(dist3_empirical.stddev)
        dist_combined_empirical = Empirical.combine([dist1_empirical, dist2_empirical, dist3_empirical])
        dist_combined_empirical_mean = float(dist_combined_empirical.mean)
        dist_combined_empirical_stddev = float(dist_combined_empirical.stddev)

        util.debug('dist1_empirical_mean', 'dist1_empirical_stddev', 'dist1_mean_correct', 'dist1_stddev_correct', 'dist2_empirical_mean', 'dist2_empirical_stddev', 'dist2_mean_correct', 'dist2_stddev_correct', 'dist3_empirical_mean', 'dist3_empirical_stddev', 'dist3_mean_correct', 'dist3_stddev_correct', 'dist_combined_empirical_mean', 'dist_combined_empirical_stddev', 'dist_combined_mean_correct', 'dist_combined_stddev_correct')

        self.assertAlmostEqual(dist1_empirical_mean, dist1_mean_correct, places=1)
        self.assertAlmostEqual(dist1_empirical_stddev, dist1_stddev_correct, places=1)
        self.assertAlmostEqual(dist2_empirical_mean, dist2_mean_correct, places=1)
        self.assertAlmostEqual(dist2_empirical_stddev, dist2_stddev_correct, places=1)
        self.assertAlmostEqual(dist3_empirical_mean, dist3_mean_correct, places=1)
        self.assertAlmostEqual(dist3_empirical_stddev, dist3_stddev_correct, places=1)
        self.assertAlmostEqual(dist_combined_empirical_mean, dist_combined_mean_correct, places=1)
        self.assertAlmostEqual(dist_combined_empirical_stddev, dist_combined_stddev_correct, places=1)