예제 #1
0
    def test_dir_tight(self):
        theta = DirichletParameterNode(
            'theta',
            Factor(['X', 'ZDummy'], {
                'X': ['same', 'diff'],
                'ZDummy': ['dummy']
            }, {
                ('same', 'dummy'): 1,
                ('diff', 'dummy'): 1,
            },
                   logarithmetic=False))

        X = DiscreteVariableNode('X', ['same', 'diff'], logarithmetic=False)
        D = DiscreteVariableNode('ZDummy', ['dummy'], logarithmetic=False)
        f = DirichletFactorNode('f')

        X.observed({('same', ): 0.8, ('diff', ): 0.2})

        f.connect(theta)
        f.connect(X, parent=False)
        f.connect(D)

        X.message_to(f)
        D.message_to(f)
        f.update()
        f.message_to(theta)

        theta.message_to(f)

        X.observed({('same', ): 0.5, ('diff', ): 0.7})
        X.message_to(f)
        f.update()
        f.message_to(theta)
예제 #2
0
파일: test_node.py 프로젝트: UFAL-DSG/alex
    def test_dir_tight(self):
        theta = DirichletParameterNode('theta', Factor(
            ['X', 'ZDummy'],
            {
                'X': ['same', 'diff'],
                'ZDummy': ['dummy']
            },
            {
                ('same', 'dummy'): 1,
                ('diff', 'dummy'): 1,
            },
            logarithmetic=False
        ))

        X = DiscreteVariableNode('X', ['same', 'diff'], logarithmetic=False)
        D = DiscreteVariableNode('ZDummy', ['dummy'], logarithmetic=False)
        f = DirichletFactorNode('f')

        X.observed({('same',): 0.8, ('diff',): 0.2})

        f.connect(theta)
        f.connect(X, parent=False)
        f.connect(D)

        X.message_to(f)
        D.message_to(f)
        f.update()
        f.message_to(theta)

        theta.message_to(f)

        X.observed({('same',): 0.5, ('diff',): 0.7})
        X.message_to(f)
        f.update()
        f.message_to(theta)
예제 #3
0
    def test_parameter(self):
        alpha = DirichletParameterNode('theta', Factor(
            ['X0', 'X1'],
            {
                'X0': ['x0_0', 'x0_1'],
                'X1': ['x1_0', 'x1_1', 'x1_2'],
            },
            {
                ('x0_0', 'x1_0'): 1,
                ('x0_0', 'x1_1'): 8,
                ('x0_0', 'x1_2'): 1,
                ('x0_1', 'x1_0'): 1,
                ('x0_1', 'x1_1'): 2,
                ('x0_1', 'x1_2'): 1,
            }
        ))

        factor = DirichletFactorNode('factor')
        x0 = DiscreteVariableNode('X0', ['x0_0', 'x0_1'])
        x1 = DiscreteVariableNode('X1', ['x1_0', 'x1_1', 'x1_2'])

        x0.observed({('x0_0',): 1})
        x1.observed({('x1_0',): 0.7, ('x1_1',): 0.2, ('x1_2',): 0.1})

        factor.connect(alpha)
        factor.connect(x0, parent=False)
        factor.connect(x1, parent=True)

        x0.message_to(factor)
        x1.message_to(factor)

        factor.update()

        factor.message_to(x0)
        factor.message_to(x1)

        x0.update()

        factor.message_to(alpha)
        print alpha.alpha

        alpha.message_to(factor)

        factor.update()
        factor.message_to(alpha)
        print alpha.alpha