def test_dist_poisson_multivariate_batched(self): dist_sample_shape_correct = [2, 3] dist_means_correct = [[1, 2, 15], [100, 200, 300]] dist_stddevs_correct = [[math.sqrt(1), math.sqrt(2), math.sqrt(15)], [math.sqrt(100), math.sqrt(200), math.sqrt(300)]] dist_rates_correct = [[1, 2, 15], [100, 200, 300]] dist_log_probs_correct = [[sum([-1, -1.30685, -2.27852])], [sum([-3.22236, -3.56851, -3.77110])]] dist = Poisson(dist_rates_correct) dist_sample_shape = list(dist.sample().size()) dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)]) dist_rates = util.to_numpy(dist.rate) dist_means = util.to_numpy(dist.mean) dist_means_empirical = util.to_numpy(dist_empirical.mean) dist_stddevs = util.to_numpy(dist.stddev) dist_stddevs_empirical = util.to_numpy(dist_empirical.stddev) dist_log_probs = util.to_numpy(dist.log_prob(dist_means_correct)) util.debug('dist_sample_shape', 'dist_sample_shape_correct', 'dist_rates', 'dist_rates_correct', 'dist_means', 'dist_means_empirical', 'dist_means_correct', 'dist_stddevs', 'dist_stddevs_empirical', 'dist_stddevs_correct', 'dist_log_probs', 'dist_log_probs_correct') self.assertEqual(dist_sample_shape, dist_sample_shape_correct) self.assertTrue(np.allclose(dist_means, dist_means_correct, atol=0.1)) self.assertTrue(np.allclose(dist_means_empirical, dist_means_correct, atol=0.25)) self.assertTrue(np.allclose(dist_stddevs, dist_stddevs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_stddevs_empirical, dist_stddevs_correct, atol=0.25)) self.assertTrue(np.allclose(dist_rates, dist_rates_correct, atol=0.1)) self.assertTrue(np.allclose(dist_log_probs, dist_log_probs_correct, atol=0.1))
def test_dist_poisson_multivariate_from_flat_params(self): dist_sample_shape_correct = [1, 3] dist_means_correct = [[1, 2, 15]] dist_stddevs_correct = [[math.sqrt(1), math.sqrt(2), math.sqrt(15)]] dist_rates_correct = [[1, 2, 15]] dist_log_probs_correct = [sum([-1, -1.30685, -2.27852])] dist = Poisson(dist_rates_correct[0]) dist_sample_shape = list(dist.sample().size()) dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)]) dist_rates = util.to_numpy(dist.rate) dist_means = util.to_numpy(dist.mean) dist_means_empirical = util.to_numpy(dist_empirical.mean) dist_stddevs = util.to_numpy(dist.stddev) dist_stddevs_empirical = util.to_numpy(dist_empirical.stddev) dist_log_probs = util.to_numpy(dist.log_prob(dist_means_correct)) util.debug('dist_sample_shape', 'dist_sample_shape_correct', 'dist_rates', 'dist_rates_correct', 'dist_means', 'dist_means_empirical', 'dist_means_correct', 'dist_stddevs', 'dist_stddevs_empirical', 'dist_stddevs_correct', 'dist_log_probs', 'dist_log_probs_correct') self.assertEqual(dist_sample_shape, dist_sample_shape_correct) self.assertTrue(np.allclose(dist_means, dist_means_correct, atol=0.1)) self.assertTrue(np.allclose(dist_means_empirical, dist_means_correct, atol=0.1)) self.assertTrue(np.allclose(dist_stddevs, dist_stddevs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_stddevs_empirical, dist_stddevs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_rates, dist_rates_correct, atol=0.1)) self.assertTrue(np.allclose(dist_log_probs, dist_log_probs_correct, atol=0.1))
def test_dist_poisson_batched(self): dist_sample_shape_correct = [2, 1] dist_means_correct = [[4], [100]] dist_stddevs_correct = [[math.sqrt(4)], [math.sqrt(100)]] dist_rates_correct = [[4], [100]] dist_log_probs_correct = [[-1.63288], [-3.22236]] dist = Poisson(dist_rates_correct) dist_sample_shape = list(dist.sample().size()) dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)]) dist_rates = util.to_numpy(dist.rate) dist_means = util.to_numpy(dist.mean) dist_means_empirical = util.to_numpy(dist_empirical.mean) dist_stddevs = util.to_numpy(dist.stddev) dist_stddevs_empirical = util.to_numpy(dist_empirical.stddev) dist_log_probs = util.to_numpy(dist.log_prob(dist_means_correct)) util.debug('dist_sample_shape', 'dist_sample_shape_correct', 'dist_rates', 'dist_rates_correct', 'dist_means', 'dist_means_empirical', 'dist_means_correct', 'dist_stddevs', 'dist_stddevs_empirical', 'dist_stddevs_correct', 'dist_log_probs', 'dist_log_probs_correct') self.assertEqual(dist_sample_shape, dist_sample_shape_correct) self.assertTrue(np.allclose(dist_means, dist_means_correct, atol=0.1)) self.assertTrue(np.allclose(dist_means_empirical, dist_means_correct, atol=0.1)) self.assertTrue(np.allclose(dist_stddevs, dist_stddevs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_stddevs_empirical, dist_stddevs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_rates, dist_rates_correct, atol=0.1)) self.assertTrue(np.allclose(dist_log_probs, dist_log_probs_correct, atol=0.1))
def forward(self): count_prior = Poisson(4) r = pyprob.sample(count_prior) if 4 < float(r): l = 6 else: l = 1 + self.fibonacci(3 * int(r)) + pyprob.sample(count_prior) pyprob.observe(Poisson(l), name='obs') return r
def true_posterior(self, observe=6): count_prior = Poisson(4) vals = [] log_weights = [] for r in range(40): for s in range(40): if 4 < float(r): l = 6 else: f = self.fibonacci(3 * r) l = 1 + f + count_prior.sample() vals.append(r) log_weights.append(Poisson(l).log_prob(observe) + count_prior.log_prob(r) + count_prior.log_prob(s)) return Empirical(vals, log_weights)
def test_distributions_remote(self): num_samples = 4000 prior_normal_mean_correct = Normal(1.75, 0.5).mean prior_uniform_mean_correct = Uniform(1.2, 2.5).mean prior_categorical_mean_correct = 1. # Categorical([0.1, 0.5, 0.4]) prior_poisson_mean_correct = Poisson(4.0).mean prior_bernoulli_mean_correct = Bernoulli(0.2).mean prior_beta_mean_correct = Beta(1.2, 2.5).mean prior_exponential_mean_correct = Exponential(2.2).mean prior_gamma_mean_correct = Gamma(0.5, 1.2).mean prior_log_normal_mean_correct = LogNormal(0.5, 0.2).mean prior_binomial_mean_correct = Binomial(10, 0.72).mean prior_weibull_mean_correct = Weibull(1.1, 0.6).mean prior = self._model.prior(num_samples) prior_normal = prior.map( lambda trace: trace.named_variables['normal'].value) prior_uniform = prior.map( lambda trace: trace.named_variables['uniform'].value) prior_categorical = prior.map( lambda trace: trace.named_variables['categorical'].value) prior_poisson = prior.map( lambda trace: trace.named_variables['poisson'].value) prior_bernoulli = prior.map( lambda trace: trace.named_variables['bernoulli'].value) prior_beta = prior.map( lambda trace: trace.named_variables['beta'].value) prior_exponential = prior.map( lambda trace: trace.named_variables['exponential'].value) prior_gamma = prior.map( lambda trace: trace.named_variables['gamma'].value) prior_log_normal = prior.map( lambda trace: trace.named_variables['log_normal'].value) prior_binomial = prior.map( lambda trace: trace.named_variables['binomial'].value) prior_weibull = prior.map( lambda trace: trace.named_variables['weibull'].value) prior_normal_mean = util.to_numpy(prior_normal.mean) prior_uniform_mean = util.to_numpy(prior_uniform.mean) prior_categorical_mean = util.to_numpy(int(prior_categorical.mean)) prior_poisson_mean = util.to_numpy(prior_poisson.mean) prior_bernoulli_mean = util.to_numpy(prior_bernoulli.mean) prior_beta_mean = util.to_numpy(prior_beta.mean) prior_exponential_mean = util.to_numpy(prior_exponential.mean) prior_gamma_mean = util.to_numpy(prior_gamma.mean) prior_log_normal_mean = util.to_numpy(prior_log_normal.mean) prior_binomial_mean = util.to_numpy(prior_binomial.mean) prior_weibull_mean = util.to_numpy(prior_weibull.mean) util.eval_print('num_samples', 'prior_normal_mean', 'prior_normal_mean_correct', 'prior_uniform_mean', 'prior_uniform_mean_correct', 'prior_categorical_mean', 'prior_categorical_mean_correct', 'prior_poisson_mean', 'prior_poisson_mean_correct', 'prior_bernoulli_mean', 'prior_bernoulli_mean_correct', 'prior_beta_mean', 'prior_beta_mean_correct', 'prior_exponential_mean', 'prior_exponential_mean_correct', 'prior_gamma_mean', 'prior_gamma_mean_correct', 'prior_log_normal_mean', 'prior_log_normal_mean_correct', 'prior_binomial_mean', 'prior_binomial_mean_correct', 'prior_weibull_mean', 'prior_weibull_mean_correct') self.assertTrue( np.allclose(prior_normal_mean, prior_normal_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_uniform_mean, prior_uniform_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_categorical_mean, prior_categorical_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_poisson_mean, prior_poisson_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_bernoulli_mean, prior_bernoulli_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_beta_mean, prior_beta_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_exponential_mean, prior_exponential_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_gamma_mean, prior_gamma_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_log_normal_mean, prior_log_normal_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_binomial_mean, prior_binomial_mean_correct, atol=0.1)) self.assertTrue( np.allclose(prior_weibull_mean, prior_weibull_mean_correct, atol=0.1))