Esempio n. 1
0
def _bql_logpdf(bdb, population_id, generator_id, targets, constraints):
    # P(T | C) = \sum_M P(T, M | C)
    # = \sum_M P(T | C, M) P(M | C)
    # = \sum_M P(T | C, M) P(M) P(C | M) / P(C)
    # = \sum_M P(T | C, M) P(M) P(C | M) / \sum_M' P(C, M')
    # = \sum_M P(T | C, M) P(M) P(C | M) / \sum_M' P(C | M') P(M')
    #
    # For a generator M, logpdf(M) computes P(T | C, M), and
    # loglikelihood(M) computes P(C | M).  For now, we weigh each
    # generator uniformly; eventually, we ought to allow the user to
    # specify a prior weight (XXX and update some kind of posterior
    # weight?).
    rowid, constraints = _retrieve_rowid_constraints(bdb, population_id,
                                                     constraints)

    def logpdf(generator_id, metamodel):
        return metamodel.logpdf_joint(bdb, generator_id, rowid, targets,
                                      constraints, None)

    def loglikelihood(generator_id, metamodel):
        if not constraints:
            return 0
        return metamodel.logpdf_joint(bdb, generator_id, rowid, constraints,
                                      [], None)

    generator_ids = _retrieve_generator_ids(bdb, population_id, generator_id)
    metamodels = [
        core.bayesdb_generator_metamodel(bdb, g) for g in generator_ids
    ]
    loglikelihoods = map(loglikelihood, generator_ids, metamodels)
    logpdfs = map(logpdf, generator_ids, metamodels)
    return logavgexp_weighted(loglikelihoods, logpdfs)
Esempio n. 2
0
def _bql_logpdf(bdb, population_id, generator_id, modelnos, targets,
        constraints):
    # P(T | C) = \sum_M P(T, M | C)
    # = \sum_M P(T | C, M) P(M | C)
    # = \sum_M P(T | C, M) P(M) P(C | M) / P(C)
    # = \sum_M P(T | C, M) P(M) P(C | M) / \sum_M' P(C, M')
    # = \sum_M P(T | C, M) P(M) P(C | M) / \sum_M' P(C | M') P(M')
    #
    # For a generator M, logpdf(M) computes P(T | C, M), and
    # loglikelihood(M) computes P(C | M).  For now, we weigh each
    # generator uniformly; eventually, we ought to allow the user to
    # specify a prior weight (XXX and update some kind of posterior
    # weight?).
    rowid, constraints = _retrieve_rowid_constraints(
        bdb, population_id, constraints)
    def logpdf(generator_id, backend):
        return backend.logpdf_joint(
            bdb, generator_id, modelnos, rowid, targets, constraints)
    def loglikelihood(generator_id, backend):
        if not constraints:
            return 0
        return backend.logpdf_joint(
            bdb, generator_id, modelnos, rowid, constraints, [])
    generator_ids = _retrieve_generator_ids(bdb, population_id, generator_id)
    backends = [
        core.bayesdb_generator_backend(bdb, g)
        for g in generator_ids
    ]
    loglikelihoods = map(loglikelihood, generator_ids, backends)
    logpdfs = map(logpdf, generator_ids, backends)
    return logavgexp_weighted(loglikelihoods, logpdfs)