Example #1
0
def bql_pdf_joint(bdb, generator_id, modelno, *args):
    metamodel = core.bayesdb_generator_metamodel(bdb, generator_id)
    # A nonexistent (`unobserved') row id.
    fake_row_id = core.bayesdb_generator_fresh_row_id(bdb, generator_id)
    i = 0
    targets = []
    while i < len(args):
        if args[i] == -1:
            i += 1
            break
        if i + 1 == len(args):
            raise ValueError('Missing logpdf target value: %r' % (args[i],))
        t_colno = args[i]
        t_value = args[i + 1]
        targets.append((fake_row_id, t_colno, t_value))
        i += 2
    constraints = []
    while i < len(args):
        if i + 1 == len(args):
            raise ValueError('Missing logpdf constraint value: %r' %
                (args[i],))
        c_colno = args[i]
        c_value = args[i + 1]
        constraints.append((fake_row_id, c_colno, c_value))
        i += 2
    logp = metamodel.logpdf_joint(bdb, generator_id, targets, constraints,
        modelno)
    return ieee_exp(logp)
Example #2
0
def bql_row_column_predictive_probability(
        bdb, population_id, generator_id, modelnos, rowid, targets,
        constraints):
    targets = json.loads(targets)
    constraints = json.loads(constraints)
    modelnos = _retrieve_modelnos(modelnos)
    # Build the constraints and query from rowid, using a fresh rowid.
    fresh_rowid = core.bayesdb_population_fresh_row_id(bdb, population_id)
    def retrieve_values(colnos):
        values = [
            core.bayesdb_population_cell_value(bdb, population_id, rowid, colno)
            for colno in colnos
        ]
        return [(c,v) for (c,v) in zip (colnos, values) if v is not None]
    cgpm_targets = retrieve_values(targets)
    # If all targets have NULL values, return None.
    if len(cgpm_targets) == 0:
        return None
    cgpm_constraints = retrieve_values(constraints)
    def generator_predprob(generator_id):
        backend = core.bayesdb_generator_backend(bdb, generator_id)
        return backend.logpdf_joint(
            bdb, generator_id, modelnos, fresh_rowid, cgpm_targets,
            cgpm_constraints)
    generator_ids = _retrieve_generator_ids(bdb, population_id, generator_id)
    predprobs = map(generator_predprob, generator_ids)
    r = logmeanexp(predprobs)
    return ieee_exp(r)
Example #3
0
def bql_pdf_joint(bdb, population_id, generator_id, modelnos, *args):
    modelnos = _retrieve_modelnos(modelnos)
    i = 0
    targets = []
    while i < len(args):
        if args[i] is None:
            i += 1
            break
        if i + 1 == len(args):
            raise ValueError('Missing logpdf target value: %r' % (args[i],))
        t_colno = args[i]
        t_value = args[i + 1]
        targets.append((t_colno, t_value))
        i += 2
    constraints = []
    while i < len(args):
        if i + 1 == len(args):
            raise ValueError('Missing logpdf constraint value: %r' %
                (args[i],))
        c_colno = args[i]
        c_value = args[i + 1]
        constraints.append((c_colno, c_value))
        i += 2
    logp = _bql_logpdf(bdb, population_id, generator_id, modelnos, targets,
        constraints)
    return ieee_exp(logp)
Example #4
0
def bql_row_column_predictive_probability(bdb, generator_id, modelno, rowid,
        colno):
    metamodel = core.bayesdb_generator_metamodel(bdb, generator_id)
    value = core.bayesdb_generator_cell_value(
        bdb, generator_id, rowid, colno)
    if value is None:
        return None
    r = metamodel.logpdf_joint(
        bdb, generator_id, [(rowid, colno, value)], [], modelno)
    return ieee_exp(r)
Example #5
0
def bql_column_value_probability(bdb, population_id, generator_id, colno,
        value, *constraint_args):
    # A nonexistent (`unobserved') row id.
    fake_row_id = core.bayesdb_population_fresh_row_id(bdb, population_id)
    constraints = []
    i = 0
    while i < len(constraint_args):
        if i + 1 == len(constraint_args):
            raise ValueError('Odd constraint arguments: %s' %
                (constraint_args,))
        constraint_colno = constraint_args[i]
        constraint_value = constraint_args[i + 1]
        constraints.append((fake_row_id, constraint_colno, constraint_value))
        i += 2
    targets = [(fake_row_id, colno, value)]
    logp = _bql_logpdf(bdb, population_id, generator_id, targets, constraints)
    return ieee_exp(logp)
Example #6
0
def bql_column_value_probability(bdb, population_id, generator_id, modelnos,
                                 colno, value, *constraint_args):
    modelnos = _retrieve_modelnos(modelnos)
    constraints = []
    i = 0
    while i < len(constraint_args):
        if i + 1 == len(constraint_args):
            raise ValueError('Odd constraint arguments: %s' %
                             (constraint_args, ))
        constraint_colno = constraint_args[i]
        constraint_value = constraint_args[i + 1]
        constraints.append((constraint_colno, constraint_value))
        i += 2
    targets = [(colno, value)]
    logp = _bql_logpdf(bdb, population_id, generator_id, modelnos, targets,
                       constraints)
    return ieee_exp(logp)
Example #7
0
def bql_column_value_probability(
        bdb, population_id, generator_id, modelnos, colno, value,
        *constraint_args):
    modelnos = _retrieve_modelnos(modelnos)
    constraints = []
    i = 0
    while i < len(constraint_args):
        if i + 1 == len(constraint_args):
            raise ValueError(
                'Odd constraint arguments: %s' % (constraint_args,))
        constraint_colno = constraint_args[i]
        constraint_value = constraint_args[i + 1]
        constraints.append((constraint_colno, constraint_value))
        i += 2
    targets = [(colno, value)]
    logp = _bql_logpdf(bdb, population_id, generator_id, modelnos, targets,
        constraints)
    return ieee_exp(logp)
Example #8
0
def bql_column_value_probability(bdb, generator_id, modelno, colno, value,
                                 *constraint_args):
    metamodel = core.bayesdb_generator_metamodel(bdb, generator_id)
    # A nonexistent (`unobserved') row id.
    fake_row_id = core.bayesdb_generator_fresh_row_id(bdb, generator_id)
    constraints = []
    i = 0
    while i < len(constraint_args):
        if i + 1 == len(constraint_args):
            raise ValueError('Odd constraint arguments: %s' %
                             (constraint_args, ))
        constraint_colno = constraint_args[i]
        constraint_value = constraint_args[i + 1]
        constraints.append((fake_row_id, constraint_colno, constraint_value))
        i += 2
    targets = [(fake_row_id, colno, value)]
    r = metamodel.logpdf_joint(bdb, generator_id, targets, constraints,
                               modelno)
    return ieee_exp(r)
Example #9
0
def bql_column_value_probability(bdb, generator_id, modelno, colno, value,
        *constraint_args):
    metamodel = core.bayesdb_generator_metamodel(bdb, generator_id)
    # A nonexistent (`unobserved') row id.
    fake_row_id = core.bayesdb_generator_fresh_row_id(bdb, generator_id)
    constraints = []
    i = 0
    while i < len(constraint_args):
        if i + 1 == len(constraint_args):
            raise ValueError('Odd constraint arguments: %s' %
                (constraint_args,))
        constraint_colno = constraint_args[i]
        constraint_value = constraint_args[i + 1]
        constraints.append((fake_row_id, constraint_colno, constraint_value))
        i += 2
    targets = [(fake_row_id, colno, value)]
    r = metamodel.logpdf_joint(
        bdb, generator_id, targets, constraints, modelno)
    return ieee_exp(r)
Example #10
0
def bql_pdf_joint(bdb, population_id, generator_id, *args):
    i = 0
    targets = []
    while i < len(args):
        if args[i] is None:
            i += 1
            break
        if i + 1 == len(args):
            raise ValueError('Missing logpdf target value: %r' % (args[i], ))
        t_colno = args[i]
        t_value = args[i + 1]
        targets.append((t_colno, t_value))
        i += 2
    constraints = []
    while i < len(args):
        if i + 1 == len(args):
            raise ValueError('Missing logpdf constraint value: %r' %
                             (args[i], ))
        c_colno = args[i]
        c_value = args[i + 1]
        constraints.append((c_colno, c_value))
        i += 2
    logp = _bql_logpdf(bdb, population_id, generator_id, targets, constraints)
    return ieee_exp(logp)