Test.__init__(self) num_samples = 1000 print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist(np.array([samples1, samples2]), "Gaussian (mean=0, stddev=1)", numbins=20, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/gaussian.pdf")
Test.__init__(self) num_samples = 1000 print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist(np.array([samples1, samples2]), "Uniform (low=0, high=1)", numbins=10, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/uniform.pdf")
Test.__init__(self) num_samples = 1000 print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist(np.array([samples1, samples2]), "Exponential (lambda=0.5)", numbins=20, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/exponential.pdf")
] def sample(self): return self.coin_weight def condition(self): return self.observed_data == self.sampled_data num_samples = 1000 print "Running metropolis hastings..." before = datetime.datetime.now() query = Test() prior_samples = [np.random.uniform(0, 1) for sample in xrange(num_samples)] samples = query.run(num_samples, 100) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tTime: %s seconds" % secs print truehist(np.array([prior_samples, samples]), "Beliefs about Coin Weight", numbins=20, labels=["prior to observing data", "conditioned on observed data"], path="../../../graphs/test4.pdf")
num_samples = 1000 print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist(np.array([samples1, samples2]), "Gaussian (mean=0, stddev=1)", numbins=20, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/gaussian.pdf")
print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10.0 ** 6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10 ** 6) / 10.0 ** 6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist( np.array([samples1, samples2]), "Gamma (k=2, theta=2)", numbins=20, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/gamma.pdf", )
Test.__init__(self) num_samples = 1000 print "Running rejection query..." before = datetime.datetime.now() query1 = TestRejectionQuery() samples1 = [query1.run() for x in xrange(num_samples)] after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples1) print "\tTime: %s seconds" % secs print "Running metropolis hastings..." before = datetime.datetime.now() query2 = TestMetropolisHastings() samples2 = query2.run(num_samples, 10) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tResult: %s" % np.mean(samples2) print "\tTime: %s seconds" % secs truehist(np.array([samples1, samples2]), "Gamma (k=2, theta=2)", numbins=20, labels=["RejectionQuery", "MetropolisHastings"], path="../../../graphs/gamma.pdf")
def sample(self): return self.coin_weight def condition(self): return self.observed_data == self.sampled_data num_samples = 1000 print "Running metropolis hastings..." before = datetime.datetime.now() query = Test() prior_samples = [np.random.uniform(0, 1) for sample in xrange(num_samples)] samples = query.run(num_samples, 100) after = datetime.datetime.now() td = after - before secs = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10.0**6 secs = np.round(secs, decimals=2) print "\tTime: %s seconds" % secs print truehist(np.array([prior_samples, samples]), "Beliefs about Coin Weight", numbins=20, labels=["prior to observing data", "conditioned on observed data"], path="../../../graphs/test4.pdf")