def test_dist_uniform(self): dist_sample_shape_correct = [1] dist_means_correct = [0.5] dist_stddevs_correct = [0.288675] dist_lows_correct = [0] dist_highs_correct = [1] dist_log_probs_correct = [0] dist = Uniform(dist_lows_correct, dist_highs_correct) dist_sample_shape = list(dist.sample().size()) dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)]) dist_lows = util.to_numpy(dist.low) dist_highs = util.to_numpy(dist.high) 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_lows', 'dist_lows_correct', 'dist_highs', 'dist_highs_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_lows, dist_lows_correct, atol=0.1)) self.assertTrue(np.allclose(dist_highs, dist_highs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_log_probs, dist_log_probs_correct, atol=0.1))
def test_dist_uniform_batched(self): dist_sample_shape_correct = [4, 1] dist_means_correct = [[0.5], [7.5], [0.5], [0.5]] dist_stddevs_correct = [[0.288675], [1.44338], [0.288675], [0.288675]] dist_lows_correct = [[0], [5], [0], [0]] dist_highs_correct = [[1], [10], [1], [1]] dist_values = [[0.5], [7.5], [0], [1]] dist_log_probs_correct = [[0], [-1.60944], [float('-inf')], [float('-inf')]] dist = Uniform(dist_lows_correct, dist_highs_correct) dist_sample_shape = list(dist.sample().size()) dist_empirical = Empirical([dist.sample() for i in range(empirical_samples)]) dist_lows = util.to_numpy(dist.low) dist_highs = util.to_numpy(dist.high) 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_values)) util.debug('dist_sample_shape', 'dist_sample_shape_correct', 'dist_lows', 'dist_lows_correct', 'dist_highs', 'dist_highs_correct', 'dist_means', 'dist_means_empirical', 'dist_means_correct', 'dist_stddevs', 'dist_stddevs_empirical', 'dist_stddevs_correct', 'dist_values', '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_lows, dist_lows_correct, atol=0.1)) self.assertTrue(np.allclose(dist_highs, dist_highs_correct, atol=0.1)) self.assertTrue(np.allclose(dist_log_probs, dist_log_probs_correct, atol=0.1))
def marsaglia(self, mean, stddev): uniform = Uniform(-1, 1) s = 1 while float(s) >= 1: x = pyprob.sample(uniform)[0] y = pyprob.sample(uniform)[0] s = x * x + y * y return mean + stddev * (x * torch.sqrt(-2 * torch.log(s) / s))
def rejection_sampling(self): u = pyprob.sample(Uniform(0, 1), control=False) if u > 0.5: while True: x = pyprob.sample(Normal(self.prior_mean, self.prior_stddev * 4), replace=True) u2 = pyprob.sample(Uniform(0, 1), control=False) if x < 0 and u2 < 0.25 * torch.exp( Normal(self.prior_mean, self.prior_stddev).log_prob(x) - Normal(self.prior_mean, self.prior_stddev * 4).log_prob(x)): return x else: while True: x = pyprob.sample(Normal(self.prior_mean, self.prior_stddev), replace=True) if x >= 0: return x
def marsaglia(self, mean, stddev): uniform = Uniform(-1, 1) s = 1 while float(s) >= 1: pyprob.rs_start() x = pyprob.sample(uniform) y = pyprob.sample(uniform) s = x * x + y * y pyprob.rs_end() return mean + stddev * (x * torch.sqrt(-2 * torch.log(s) / s))
def forward(self): uniform = Uniform(-1, 1) for i in range(2): x = pyprob.sample(uniform) y = pyprob.sample(uniform) s = x * x + y * y likelihood = Normal(s, 0.1) pyprob.observe(likelihood, name='obs0') pyprob.observe(likelihood, name='obs1') return s
def forward(self, observation=[]): uniform = Uniform(0, 1) ret = pyprob.sample(uniform) ret = pyprob.sample(uniform) ret = pyprob.sample(uniform, control=False) ret = pyprob.sample(uniform, control=False) ret = pyprob.sample(uniform, control=False) pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) return ret
def forward(self): uniform = Uniform(0, 1) val = pyprob.sample(uniform) val = pyprob.sample(uniform) val = pyprob.sample(uniform, control=False) val = pyprob.sample(uniform, control=False) val = pyprob.sample(uniform, control=False) pyprob.tag(value=val, name='val') pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) pyprob.observe(uniform, 0.5) return val
def marsaglia(self, mean, stddev): uniform = Uniform(-1, 1) s = 1 i = 0 while True: x = pyprob.sample(uniform, replace=self.replace) y = pyprob.sample(uniform, replace=self.replace) s = x * x + y * y i += 1 if float(s) < 1: pyprob.tag(x, name='x_accepted') pyprob.tag(y, name='y_accepted') pyprob.tag(s, name='s_accepted') break else: pyprob.tag(x, name='x_rejected') pyprob.tag(y, name='y_rejected') pyprob.tag(s, name='s_rejected') pyprob.tag(i, name='iterations') return mean + stddev * (x * torch.sqrt(-2 * torch.log(s) / s))
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))