def bql_row_column_predictive_probability(bdb, population_id, generator_id, rowid, colno): value = core.bayesdb_population_cell_value(bdb, population_id, rowid, colno) if value is None: return None # Retrieve all other values in the row. row_values = core.bayesdb_population_row_values(bdb, population_id, rowid) variable_numbers = core.bayesdb_variable_numbers(bdb, population_id, None) # Build the constraints and query from rowid, using a fresh rowid. fresh_rowid = core.bayesdb_population_fresh_row_id(bdb, population_id) query = [(colno, value)] constraints = [(col, value) for (col, value) in zip(variable_numbers, row_values) if (value is not None) and (col != colno)] def generator_predprob(generator_id): metamodel = core.bayesdb_generator_metamodel(bdb, generator_id) return metamodel.logpdf_joint(bdb, generator_id, fresh_rowid, query, constraints, None) generator_ids = _retrieve_generator_ids(bdb, population_id, generator_id) predprobs = map(generator_predprob, generator_ids) r = logmeanexp(predprobs) return ieee_exp(r)
def test_t1_simulate(colnos, constraints, numpredictions): if len(colnos) == 0: # No need to try this or confirm it fails gracefully -- # nothing should be trying it anyway, and bayeslite_simulate # is not exposed to users of the bayeslite API. return with analyzed_bayesdb_population(t1(), 1, 1) \ as (bdb, population_id, generator_id): if constraints is not None: rowid = 1 # XXX Avoid hard-coding this. # Can't use t1_rows[0][i] because not all t1-based tables # use the same column indexing -- some use a subset of the # columns. # # XXX Automatically test the correct exception. constraints = [ (i, core.bayesdb_population_cell_value(bdb, population_id, rowid, i)) for i in constraints if i not in colnos ] bqlfn.bayesdb_simulate(bdb, population_id, None, None, constraints, colnos, numpredictions=numpredictions)
def bql_row_column_predictive_probability(bdb, population_id, generator_id, rowid, colno): value = core.bayesdb_population_cell_value( bdb, population_id, rowid, colno) if value is None: return None def generator_predprob(generator_id): metamodel = core.bayesdb_generator_metamodel(bdb, generator_id) return metamodel.logpdf_joint( bdb, generator_id, [(rowid, colno, value)], [], None) generator_ids = [generator_id] if generator_id is not None else \ core.bayesdb_population_generators(bdb, population_id) predprobs = map(generator_predprob, generator_ids) r = logmeanexp(predprobs) return ieee_exp(r)
def test_t1_column_value_probability(colno, rowid): with analyzed_bayesdb_population(t1(), 1, 1) \ as (bdb, population_id, generator_id): if rowid == 0: rowid = bayesdb_maxrowid(bdb, population_id) value = core.bayesdb_population_cell_value(bdb, population_id, rowid, colno) bqlfn.bql_column_value_probability(bdb, population_id, None, None, colno, value) table_name = core.bayesdb_population_table(bdb, population_id) var = core.bayesdb_variable_name(bdb, population_id, None, colno) qt = bql_quote_name(table_name) qv = bql_quote_name(var) sql = ''' select bql_column_value_probability(?, NULL, NULL, ?, (select %s from %s where rowid = ?)) ''' % (qv, qt) bdb.sql_execute(sql, (population_id, colno, rowid)).fetchall()
def test_t1_column_value_probability(colno, rowid): with analyzed_bayesdb_population(t1(), 1, 1) \ as (bdb, population_id, generator_id): if rowid == 0: rowid = bayesdb_maxrowid(bdb, population_id) value = core.bayesdb_population_cell_value( bdb, population_id, rowid, colno) bqlfn.bql_column_value_probability( bdb, population_id, None, None, colno, value) table_name = core.bayesdb_population_table(bdb, population_id) var = core.bayesdb_variable_name(bdb, population_id, None, colno) qt = bql_quote_name(table_name) qv = bql_quote_name(var) sql = ''' select bql_column_value_probability(?, NULL, NULL, ?, (select %s from %s where rowid = ?)) ''' % (qv, qt) bdb.sql_execute(sql, (population_id, colno, rowid)).fetchall()
def test_t1_simulate(colnos, constraints, numpredictions): if len(colnos) == 0: # No need to try this or confirm it fails gracefully -- # nothing should be trying it anyway, and bayeslite_simulate # is not exposed to users of the bayeslite API. return with analyzed_bayesdb_population(t1(), 1, 1) \ as (bdb, population_id, generator_id): if constraints is not None: rowid = 1 # XXX Avoid hard-coding this. # Can't use t1_rows[0][i] because not all t1-based tables # use the same column indexing -- some use a subset of the # columns. # # XXX Automatically test the correct exception. constraints = [ (i, core.bayesdb_population_cell_value( bdb, population_id, rowid, i)) for i in constraints if i not in colnos ] bqlfn.bayesdb_simulate(bdb, population_id, None, None, constraints, colnos, numpredictions=numpredictions)
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]
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]