コード例 #1
0
def main():
    """
    The main function
    """
    # create the network
    graph = BNet( 'Water Sprinkler Bayesian Network' )
    b,c = [graph.add_v( BVertex( nm, False, 1 ) ) for nm in 'b c'.split()]
    a = graph.add_v(BVertex('a',True,2))
    
    for start, end in [( a, b ), ( b, c )]:
        graph.add_e( DirEdge( len( graph.e ), start, end ) )
    
    print graph
    
    # finalize the bayesian network once all edges have been added   
    graph.init_distributions()
    
    # fill in the parameters
    a.distribution.set_parameters([0.7,0.3])
    b.distribution.set_parameters(mu=[2.0,0.0], sigma=[1.0,1.0])
    c.distribution.set_parameters(mu=2.0, sigma=1.0, wi=1.0)
    
    case = graph.sample(1)        #---TODO: correct this bug...
    print case
    
    # NOTE : for the moment only MCMCEngine can work for continuous networks
    engine = MCMCEngine(graph)
    
    res = engine.marginalise_all()
    
    for res in res.values(): 
        print res
コード例 #2
0
    def eval(self, evidences, node):
        """
        This use MCMC engine to compute the marginal on a given node
        based on the given evidence
        """
        #TODO: node could be a list if more than 1 node needs to be guessed
        ie = MCMCEngine(self._network)

        for e in evidences:
            ie.set_obs(e)
        return ie.marginalise(node).convert_to_cpt()
コード例 #3
0
def main():
    """
    Simply the main function. We watn to be clean
    """
    # create the network
    graph = BNet( 'Water Sprinkler Bayesian Network' )
    node_a, node_b, node_c = [graph.add_v(BVertex(nm, False, 1)) 
                              for nm in 'a b c'.split()]
    for start, end in [(node_a, node_b), (node_b, node_c)]:
        graph.add_e(DirEdge(len(graph.e), start, end ))
    print graph
    # finalize the bayesian network once all edges have been added   
    graph.init_distributions()
    # fill in the parameters
    node_a.distribution.set_parameters(mu=1.0, sigma=0.5)
    node_b.distribution.set_parameters(mu=2.0, sigma=1.0, wi=2.0)
    node_c.distribution.set_parameters(mu=2.0, sigma=1.0, wi=1.0)
    # NOTE : for the moment only MCMCEngine can work for continuous networks
    engine = MCMCEngine(graph)
    res = engine.marginalise_all()
    for res in res.values(): 
        print res,'\n'