Exemple #1
0
if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler

    from LOTlib.MCMCSummary import *

    for h in break_ctrlc(
            Print(PosteriorTrace(MHSampler(make_hypothesis(),
                                           make_data(100))))):
        pass

    # pt = PosteriorTrace(plot_every=1000, window=False)
    #
    # for h in break_ctrlc(pt(MHSampler(make_hypothesis(), make_data(100)))):
    #     print h
Exemple #2
0
                probs=[v.posterior_score for v in population],
                log=True)

            try:
                kid = mutate(crossover(mom, dad))
            except (ProposalFailedException, NodeSamplingException):
                continue

            kid.compute_posterior(data)
            yield kid

            nextpopulation.append(kid)

            # # if MH_acceptance(population[i].posterior_score, kid.posterior_score, 0.0):
            # if kid.posterior_score > population[i].posterior_score:
            #     population[i] = kid
            #     yield kid
        population = nextpopulation


if __name__ == "__main__":
    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Miscellaneous import qq
    data = make_data(400)

    for h in break_ctrlc(
            genetic_algorithm(make_hypothesis, data, mutate_lot,
                              crossover_lot)):
        print h.posterior_score, h.get_knower_pattern(), qq(h)
Exemple #3
0
if __name__ == "__main__":

    from optparse import OptionParser

    parser = OptionParser()
    parser.add_option("--steps", dest="STEPS", type="int", default=1000, help="Number of samples to run")
    parser.add_option("--data", dest="DATA", type="int", default=300, help="Amount of data")
    (options, args) = parser.parse_args()

    from time import time

    from LOTlib.Examples.Number.Model import make_data

    data = make_data(options.DATA)

    from Model import LuaHypothesis, BASE

    start = time()
    for _ in xrange(options.STEPS):
        h = LuaHypothesis(base=BASE)
        h.compute_posterior(data)
    print "# Lua time: %s" % (time() - start)

    from LOTlib.Examples.Number.Model import NumberExpression
    from LOTlib.Examples.Number.Model import grammar as number_grammar

    start = time()
    for _ in xrange(options.STEPS):
        h = NumberExpression(grammar=number_grammar)
        h.compute_posterior(data)
    print "# Python time: %s" % (time() - start)
                    print "# Current: ", round(cur,3), self.current_sample
                    print "# Proposal:", round(prop,3), self.proposal
                    print ""
                
                # if MH_acceptance(cur, prop, fb, acceptance_temperature=self.acceptance_temperature): # this was the old form
                if MH_acceptance(cur, prop, fb, p=p, acceptance_temperature=self.acceptance_temperature):
                    self.current_sample = self.proposal
                    self.was_accepted = True
                    self.acceptance_count += 1
                else:
                    self.was_accepted = False

                self.proposal_count += 1

            self.samples_yielded += 1
            return self.current_sample

if __name__ == "__main__":

    # Just an example
    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_data, NumberExpression, grammar

    data = make_data(300)
    h0 = NumberExpression(grammar)
    sampler = MHSamplerShortcut(h0, data, steps=100000)
    for h in break_ctrlc(sampler):
        print h.posterior_score, h.prior, h.likelihood, h.compute_likelihood(data), h


Exemple #5
0
                # if MH_acceptance(cur, prop, fb, acceptance_temperature=self.acceptance_temperature): # this was the old form
                if MH_acceptance(
                        cur,
                        prop,
                        fb,
                        acceptance_temperature=self.acceptance_temperature):
                    self.current_sample = self.proposal
                    self.was_accepted = True
                    self.acceptance_count += 1
                else:
                    self.was_accepted = False

                self.proposal_count += 1

            self.samples_yielded += 1
            return self.current_sample


if __name__ == "__main__":

    # Just an example
    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_data, NumberExpression, grammar

    data = make_data(300)
    h0 = NumberExpression(grammar)
    sampler = MHSampler(h0, data, steps=100000)
    for h in break_ctrlc(sampler):
        print h.posterior_score, h.prior, h.likelihood, h.compute_likelihood(
            data), h
Exemple #6
0
    def __enter__(self):
        for a in self.outputs:
            a.__enter__()

    def __exit__(self, t, value, traceback):
        """ I defaultly call all of my children's exits """
        for a in self.outputs:
            a.__exit__(t, value, traceback)
        return False


if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler
    from LOTlib.SampleStream import *

    sampler = break_ctrlc(MHSampler(make_hypothesis(), make_data()))

    for h in SampleStream(sampler) >> Tee(Skip(2) >> Unique() >> Print(), PosteriorTrace()) >> Z():
        pass







Exemple #7
0
    parser.add_option("--steps",
                      dest="STEPS",
                      type="int",
                      default=1000,
                      help="Number of samples to run")
    parser.add_option("--data",
                      dest="DATA",
                      type="int",
                      default=300,
                      help="Amount of data")
    (options, args) = parser.parse_args()

    from time import time

    from LOTlib.Examples.Number.Model import make_data
    data = make_data(options.DATA)

    from Model import LuaHypothesis, BASE

    start = time()
    for _ in xrange(options.STEPS):
        h = LuaHypothesis(base=BASE)
        h.compute_posterior(data)
    print "# Lua time: %s" % (time() - start)

    from LOTlib.Examples.Number.Model import NumberExpression
    from LOTlib.Examples.Number.Model import grammar as number_grammar

    start = time()
    for _ in xrange(options.STEPS):
        h = NumberExpression(grammar=number_grammar)
Exemple #8
0
            mom = weighted_sample(population, probs=[v.posterior_score for v in population], log=True)
            dad = weighted_sample(population, probs=[v.posterior_score for v in population], log=True)

            try:
                kid = mutate(crossover(mom, dad))
            except (ProposalFailedException, NodeSamplingException):
                continue

            kid.compute_posterior(data)
            yield kid

            nextpopulation.append(kid)

            # # if MH_acceptance(population[i].posterior_score, kid.posterior_score, 0.0):
            # if kid.posterior_score > population[i].posterior_score:
            #     population[i] = kid
            #     yield kid
        population = nextpopulation

if __name__ == "__main__":
    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Miscellaneous import qq
    data = make_data(400)

    for h in break_ctrlc(genetic_algorithm(make_hypothesis, data, mutate_lot, crossover_lot)):
        print h.posterior_score, h.get_knower_pattern(), qq(h)



Exemple #9
0
if __name__ == "__main__":

    from LOTlib import break_ctrlc
    from LOTlib.Examples.Number.Model import make_hypothesis, make_data
    from LOTlib.Inference.Samplers.MetropolisHastings import MHSampler

    from LOTlib.MCMCSummary import *

    for h in break_ctrlc(Print(PosteriorTrace(MHSampler(make_hypothesis(), make_data(100))))):
        pass

    # pt = PosteriorTrace(plot_every=1000, window=False)
    #
    # for h in break_ctrlc(pt(MHSampler(make_hypothesis(), make_data(100)))):
    #     print h