コード例 #1
0
def test_crosscat_constraints():
    class FakeEngine(crosscat.LocalEngine.LocalEngine):
        def predictive_probability_multistate(self, M_c, X_L_list, X_D_list, Y,
                                              Q):
            self._last_Y = Y
            sup = super(FakeEngine, self)
            return sup.simple_predictive_probability_multistate(
                M_c=M_c, X_L_list=X_L_list, X_D_list=X_D_list, Y=Y, Q=Q)

        def simple_predictive_sample(self, seed, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).simple_predictive_sample(seed=seed,
                                                                    M_c=M_c,
                                                                    X_L=X_L,
                                                                    X_D=X_D,
                                                                    Y=Y,
                                                                    Q=Q,
                                                                    n=n)

        def impute_and_confidence(self, seed, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).impute_and_confidence(seed=seed,
                                                                 M_c=M_c,
                                                                 X_L=X_L,
                                                                 X_D=X_D,
                                                                 Y=Y,
                                                                 Q=Q,
                                                                 n=n)

    engine = FakeEngine(seed=0)
    mm = CrosscatMetamodel(engine)
    with bayesdb(metamodel=mm) as bdb:
        t1_schema(bdb)
        t1_data(bdb)
        bdb.execute('''
            CREATE GENERATOR t1_cc FOR t1 USING crosscat(
                label CATEGORICAL,
                age NUMERICAL,
                weight NUMERICAL
            )
        ''')
        gid = core.bayesdb_get_generator(bdb, 't1_cc')
        assert core.bayesdb_generator_column_number(bdb, gid, 'label') == 1
        assert core.bayesdb_generator_column_number(bdb, gid, 'age') == 2
        assert core.bayesdb_generator_column_number(bdb, gid, 'weight') == 3
        from bayeslite.metamodels.crosscat import crosscat_cc_colno
        assert crosscat_cc_colno(bdb, gid, 1) == 0
        assert crosscat_cc_colno(bdb, gid, 2) == 1
        assert crosscat_cc_colno(bdb, gid, 3) == 2
        bdb.execute('INITIALIZE 1 MODEL FOR t1_cc')
        bdb.execute('ANALYZE t1_cc FOR 1 ITERATION WAIT')
        bdb.execute('ESTIMATE PROBABILITY OF age = 8 GIVEN (weight = 16)'
                    ' BY t1_cc').next()
        assert engine._last_Y == [(28, 2, 16)]
        bdb.execute("SELECT age FROM t1 WHERE label = 'baz'").next()
        bdb.execute("INFER age FROM t1_cc WHERE label = 'baz'").next()
        assert engine._last_Y == [(3, 0, 1), (3, 2, 32)]
        bdb.execute('SIMULATE weight FROM t1_cc GIVEN age = 8 LIMIT 1').next()
        assert engine._last_Y == [(28, 1, 8)]
コード例 #2
0
def test_crosscat_constraints():
    class FakeEngine(crosscat.LocalEngine.LocalEngine):
        def predictive_probability_multistate(self, M_c, X_L_list,
                X_D_list, Y, Q):
            self._last_Y = Y
            sup = super(FakeEngine, self)
            return sup.simple_predictive_probability_multistate(M_c=M_c,
                X_L_list=X_L_list, X_D_list=X_D_list, Y=Y, Q=Q)
        def simple_predictive_sample(self, seed, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).simple_predictive_sample(seed=seed,
                M_c=M_c, X_L=X_L, X_D=X_D, Y=Y, Q=Q, n=n)
        def impute_and_confidence(self, seed, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).impute_and_confidence(seed=seed,
                M_c=M_c, X_L=X_L, X_D=X_D, Y=Y, Q=Q, n=n)
    engine = FakeEngine(seed=0)
    mm = CrosscatMetamodel(engine)
    with bayesdb(metamodel=mm) as bdb:
        t1_schema(bdb)
        t1_data(bdb)
        bdb.execute('''
            CREATE POPULATION p1 FOR t1 (
                id IGNORE;
                label CATEGORICAL;
                age NUMERICAL;
                weight NUMERICAL
            )
        ''')
        bdb.execute('''
            CREATE GENERATOR p1_cc FOR p1 USING crosscat(
                label CATEGORICAL,
                age NUMERICAL,
                weight NUMERICAL
            )
        ''')
        pid = core.bayesdb_get_population(bdb, 'p1')
        assert core.bayesdb_variable_number(bdb, pid, None, 'label') == 1
        assert core.bayesdb_variable_number(bdb, pid, None, 'age') == 2
        assert core.bayesdb_variable_number(bdb, pid, None, 'weight') == 3
        gid = core.bayesdb_get_generator(bdb, pid, 'p1_cc')
        from bayeslite.metamodels.crosscat import crosscat_cc_colno
        assert crosscat_cc_colno(bdb, gid, 1) == 0
        assert crosscat_cc_colno(bdb, gid, 2) == 1
        assert crosscat_cc_colno(bdb, gid, 3) == 2
        bdb.execute('INITIALIZE 1 MODEL FOR p1_cc')
        bdb.execute('ANALYZE p1_cc FOR 1 ITERATION WAIT')
        bdb.execute('ESTIMATE PROBABILITY DENSITY OF age = 8'
            ' GIVEN (weight = 16)'
            ' BY p1').next()
        assert engine._last_Y == [(28, 2, 16)]
        bdb.execute("SELECT age FROM t1 WHERE label = 'baz'").next()
        bdb.execute("INFER age FROM p1 WHERE label = 'baz'").next()
        assert engine._last_Y == [(3, 0, 1), (3, 2, 32)]
        bdb.execute('SIMULATE weight FROM p1 GIVEN age = 8 LIMIT 1').next()
        assert engine._last_Y == [(28, 1, 8)]
        # Simulate with an unknown nominal value should throw an error.
        with pytest.raises(bayeslite.BQLError):
            bdb.execute('SIMULATE weight FROM p1 GIVEN label = \'q\' LIMIT 1;')
コード例 #3
0
ファイル: test_core.py プロジェクト: madeleineth/bayeslite
def test_crosscat_constraints():
    class FakeEngine(crosscat.LocalEngine.LocalEngine):
        def predictive_probability_multistate(self, M_c, X_L_list, X_D_list, Y, Q):
            self._last_Y = Y
            sup = super(FakeEngine, self)
            return sup.simple_predictive_probability_multistate(M_c=M_c, X_L_list=X_L_list, X_D_list=X_D_list, Y=Y, Q=Q)

        def simple_predictive_sample(self, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).simple_predictive_sample(M_c=M_c, X_L=X_L, X_D=X_D, Y=Y, Q=Q, n=n)

        def impute_and_confidence(self, M_c, X_L, X_D, Y, Q, n):
            self._last_Y = Y
            return super(FakeEngine, self).impute_and_confidence(M_c=M_c, X_L=X_L, X_D=X_D, Y=Y, Q=Q, n=n)

    engine = FakeEngine(seed=0)
    mm = CrosscatMetamodel(engine)
    with bayesdb(metamodel=mm) as bdb:
        t1_schema(bdb)
        t1_data(bdb)
        bdb.execute(
            """
            CREATE GENERATOR t1_cc FOR t1 USING crosscat(
                label CATEGORICAL,
                age NUMERICAL,
                weight NUMERICAL
            )
        """
        )
        gid = core.bayesdb_get_generator(bdb, "t1_cc")
        assert core.bayesdb_generator_column_number(bdb, gid, "label") == 1
        assert core.bayesdb_generator_column_number(bdb, gid, "age") == 2
        assert core.bayesdb_generator_column_number(bdb, gid, "weight") == 3
        from bayeslite.metamodels.crosscat import crosscat_cc_colno

        assert crosscat_cc_colno(bdb, gid, 1) == 0
        assert crosscat_cc_colno(bdb, gid, 2) == 1
        assert crosscat_cc_colno(bdb, gid, 3) == 2
        bdb.execute("INITIALIZE 1 MODEL FOR t1_cc")
        bdb.execute("ANALYZE t1_cc FOR 1 ITERATION WAIT")
        bdb.execute("ESTIMATE PROBABILITY OF age = 8 GIVEN (weight = 16)" " BY t1_cc").next()
        assert engine._last_Y == [(28, 2, 16)]
        bdb.execute("SELECT age FROM t1 WHERE label = 'baz'").next()
        bdb.execute("INFER age FROM t1_cc WHERE label = 'baz'").next()
        assert engine._last_Y == [(3, 0, 1), (3, 2, 32)]
        bdb.execute("SIMULATE weight FROM t1_cc GIVEN age = 8 LIMIT 1").next()
        assert engine._last_Y == [(28, 1, 8)]