Exemplo n.º 1
0
def generate_unique_trees(grammar, start='START', N=1000):
    """
            Yield a bunch of unique trees, produced from the grammar
    """
    for _ in break_ctrlc(range(N)):
        t = grammar.generate(start)
        yield t
Exemplo n.º 2
0
class MyHypothesis(GaussianLikelihood, LOTHypothesis):
    def __init__(self, **kwargs):
        LOTHypothesis.__init__(self, grammar=grammar, **kwargs)


# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Main
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

if __name__ == "__main__":
    from LOTlib3 import break_ctrlc
    from LOTlib3.Miscellaneous import qq
    from LOTlib3.TopN import TopN
    from LOTlib3.Samplers.MetropolisHastings import MetropolisHastingsSampler

    h0 = MyHypothesis()
    data = make_data()
    top = TopN(N=10)
    thin = 100

    for i, h in enumerate(break_ctrlc(MetropolisHastingsSampler(h0, data))):

        top << h

        if i % thin == 0:
            print("#", i, h.posterior_score, h.prior, h.likelihood, qq(h))

    for h in top:
        print(h.posterior_score, h.prior, h.likelihood, qq(h))
Exemplo n.º 3
0
class PriorDirichletDistribution(DirichletDistribution):
    """ propose from the prior (useful for debugging)
    """
    def propose(self):
        if len(self.value) == 1:
            return PriorDirichletDistribution(
                value=self.value,
                alpha=self.value), 0.0  # handle singleton rules

        ret = PriorDirichletDistribution(value=numpy.random.dirichlet(
            self.alpha),
                                         alpha=self.alpha)

        fb = dirichlet.logpdf(ret.value, self.alpha) - dirichlet.logpdf(
            self.value, self.alpha)

        return ret, fb


class BetaDistribution(DirichletDistribution):
    def __init__(self, a=1, b=1, **kwargs):
        DirichletDistribution.__init__(self, alpha=[a, b], **kwargs)


if __name__ == "__main__":
    from LOTlib3.Inference.Samplers.MetropolisHastings import MHSampler

    h0 = BetaDistribution(1, 2)
    for h in break_ctrlc(MHSampler(h0, [])):
        print(h)