Beispiel #1
0
        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

hist(np.array([samples1, samples2]), "Poisson Distribution (lambda=4)",
     labels=["RejectionQuery",
             "MetropolisHastings"],
     path="../../../graphs/poisson.pdf")
Beispiel #2
0

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, 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 "\tResult: %s" % np.mean(samples2)
print "\tTime:   %s seconds" % secs

hist(np.array([samples1, samples2]),
     "Probability of Breast Cancer\nGiven a Positive Mammogram",
     labels=["RejectionQuery", "MetropolisHastings"],
     path="../../../graphs/test2.pdf")
Beispiel #3
0
        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

hist(np.array([samples1, samples2]), "Flip (weight=0.5)",
     labels=["RejectionQuery",
             "MetropolisHastings"],
     path="../../../graphs/flip.pdf")
Beispiel #4
0
        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

hist(np.array([samples1, samples2]), "Binomial Distribution (n=20, p=0.8)",
     labels=["RejectionQuery",
             "MetropolisHastings"],
     path="../../../graphs/binomial.pdf")
Beispiel #5
0
    def __init__(self):
        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

hist(np.array([samples1, samples2]), "Sample Integer (low=0, high=10)",
     labels=["RejectionQuery", "MetropolisHastings"],
     path="../../../graphs/sample_integer.pdf")
Beispiel #6
0
            or (self.other and flip(0.01))
        )

        self.chest_pain = (self.lung_cancer and flip(0.4)) or (self.TB and flip(0.5)) or (self.other and flip(0.01))

        self.shortness_of_breath = (
            (self.lung_cancer and flip(0.4)) or (self.TB and flip(0.5)) or (self.other and flip(0.01))
        )

    def sample(self):
        return self.lung_cancer, self.TB

    def condition(self):
        return self.cough and self.fever and self.chest_pain and self.shortness_of_breath


num_samples = 1000

print "Running metropolis hastings..."
before = datetime.datetime.now()
query = Test()
samples = [str(samp) for samp in 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

hist(samples, "Joint Inferences for\nLung Cancer and TB", path="../../../graphs/test3.pdf")
Beispiel #7
0
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, 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 "\tResult: %s" % np.mean(samples2)
print "\tTime:   %s seconds" % secs


hist(np.array([samples1, samples2]), "Probability of Breast Cancer\nGiven a Positive Mammogram",
     labels=["RejectionQuery",
             "MetropolisHastings"],
     path="../../../graphs/test2.pdf")
Beispiel #8
0

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

hist(np.array([samples1, samples2]),
     "Flip (weight=0.5)",
     labels=["RejectionQuery", "MetropolisHastings"],
     path="../../../graphs/flip.pdf")
Beispiel #9
0
        return self.D >= 2

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, 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 "\tResult: %s" % np.mean(samples2)
print "\tTime:   %s seconds" % secs

hist(np.array([samples1, samples2]), "Probability of A Given that D>=2",
     labels=["RejectionQuery",
             "MetropolisHastings"],
     path="../../../graphs/test1.pdf")
Beispiel #10
0
                          (self.TB and flip(0.5)) or \
                          (self.other and flip(0.01))

        self.shortness_of_breath = (self.lung_cancer and flip(0.4)) or \
                                   (self.TB and flip(0.5)) or \
                                   (self.other and flip(0.01))

    def sample(self):
        return self.lung_cancer, self.TB

    def condition(self):
        return self.cough and self.fever and self.chest_pain and self.shortness_of_breath


num_samples = 1000

print "Running metropolis hastings..."
before = datetime.datetime.now()
query = Test()
samples = [str(samp) for samp in 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

hist(samples,
     "Joint Inferences for\nLung Cancer and TB",
     path="../../../graphs/test3.pdf")