Giter Site home page Giter Site logo

ebay / bayesian-belief-networks Goto Github PK

View Code? Open in Web Editor NEW
1.1K 90.0 258.0 1.74 MB

Pythonic Bayesian Belief Network Package, supporting creation of and exact inference on Bayesian Belief Networks specified as pure python functions.

License: Other

bayesian-belief-networks's Introduction

Pythonic Bayesian Belief Network Framework
------------------------------------------

Allows creation of Bayesian Belief Networks
and other Graphical Models with pure Python
functions. Where tractable exact inference
is used. Currently four different inference
methods are supported with more to come.

Graphical Models Supported
--------------------------

- Bayesian Belief Networks with discrete variables
- Gaussian Bayesian Networks with continous variables having gaussian distributions


Inference Engines
-----------------

- Message Passing and the Junction Tree Algorithm
- The Sum Product Algorithm
- MCMC Sampling for approximate inference
- Exact Propagation in Gaussian Bayesian Networks


Other Features
--------------

- Automated conversion to Junction Trees
- Inference of Graph Structure from Mass Functions
- Automatic conversion to Factor Graphs
- Seemless storage of samples for future use
- Exact inference on cyclic graphs
- Export of graphs to GraphViz (dot language) format
- Discrete and Continuous Variables (with some limitations)
- Minimal dependancies on non-standard library modules.

Please see the short tutorial in the docs/tutorial directory
for a short introduction on how to build a Bayesian Belief Network.
There are also many examples in the examples directory.


Installation
------------

$ python setup.py install
$ pip install -r requirements.txt

Building The Tutorial

$ pip install sphinx
$ cd docs/tutorial
$ make clean
$ make html

Unit Tests:

To run the tests in a development environment:

$ PYTHONPATH=. py.test bayesian/test


Resources
=========

http://www.fil.ion.ucl.ac.uk/spm/course/slides10-vancouver/08_Bayes.pdf
http://www.ee.columbia.edu/~vittorio/Lecture12.pdf
http://www.csse.monash.edu.au/bai/book/BAI_Chapter2.pdf
http://www.comm.utoronto.ca/frank/papers/KFL01.pdf
http://www.snn.ru.nl/~bertk/ (Many real-world examples listed)
http://www.cs.ubc.ca/~murphyk/Bayes/Charniak_91.pdf
http://www.sciencedirect.com/science/article/pii/S0888613X96000692
http://arxiv.org/pdf/1301.7394v1.pdf

Junction Tree Algorithm:
http://www.inf.ed.ac.uk/teaching/courses/pmr/docs/jta_ex.pdf
http://ttic.uchicago.edu/~altun/Teaching/CS359/junc_tree.pdf
http://eniac.cs.qc.cuny.edu/andrew/gcml/lecture10.pdf
http://leo.ugr.es/pgm2012/proceedings/eproceedings/evers_a_framework.pdf

Guassian Bayesian Networks:
http://www.cs.ubc.ca/~murphyk/Teaching/CS532c_Fall04/Lectures/lec17x4.pdf
http://webdocs.cs.ualberta.ca/~greiner/C-651/SLIDES/MB08_GaussianNetworks.pdf
http://people.cs.aau.dk/~uk/papers/castillo-kjaerulff-03.pdf

bayesian-belief-networks's People

Contributors

arne-cl avatar grayj avatar kyle-lee avatar michaeljoseph avatar nnewey avatar thakadu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bayesian-belief-networks's Issues

Sumproduct algorithm returning marginal probabilities of 1

The attached script 'ebay_alarm_problem.py' ( https://www.dropbox.com/s/ux5vznts6mn70qg/ebay_alarm_problem.py ) is created to analyse the ALARM Bayesian network (found here: http://www.bnlearn.com/bnrepository/#alarm ). As can be seen, when a bbn object is created using build_bbn, or when a factor_graph object is created and the inference method is set to 'sample', the marginals obtained by querying are reasonable. However, if the 'sumproduct' inference method is used on the factor_graph object, the query returns multiple marginal probabilities with a value of 1 for the same variable, on several variables. This is clearly incorrect. Since the ALARM network is not a polytree, could it be that the algorithm is failing because the software does not convert the network to a junction tree? The README file lists "Automated conversion to Junction Trees" under "Other Features", but there is no mention of this in the code.

Parent Nodes and Evidence

I noticed in all cases except for the Monty Hall Problem the parent nodes have only 2 bins, usually true and false. Is it possible to assign a non uniform prior distribution to a bin size of greater than 2 for a parent node? Also, can we assert evidence without absolute certainty?

Test not working

Ok i just noticed the last commit was some time ago.

[ erik@halberstadt:~/bayes_test/bayesian-belief-networks ] $ git show
commit 511856960e87e070748db5bac7d59ddd1bb5b1e0
Author: Neville Newey <[email protected]>
Date:   Sat Aug 9 10:59:26 2014 -0700

If anyone is still out there I tried running the unit tests and they failed in a way that looks important.

[ erik@halberstadt:~/bayes_test/bayesian-belief-networks ] $ PYTHONPATH=. py.test bayesian/test
========================================================= test session starts ==========================================================
platform darwin -- Python 2.7.10, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /Users/erik/bayes_test/bayesian-belief-networks, inifile: 
collected 98 items 

bayesian/test/test_bbn.py ...........F......
bayesian/test/test_examples.py ......
bayesian/test/test_factor_graph_verify.py .........
bayesian/test/test_gaussian.py ............
bayesian/test/test_gaussian_bayesian_network.py .....
bayesian/test/test_gbn_examples.py ..
bayesian/test/test_graph.py ..........................
bayesian/test/test_linear_algebra.py .......
bayesian/test/test_persistance.py .
bayesian/test/test_undirected_graph.py .
bayesian/test/examples/bbns/test_cancer.py ......
bayesian/test/examples/bbns/test_earthquake.py .
bayesian/test/examples/bbns/test_huang_darwiche.py .
bayesian/test/examples/bbns/test_monty_hall.py ..
bayesian/test/examples/factor_graphs/test_earthquake_fg.py .

=============================================================== FAILURES ===============================================================
_____________________________________________________ TestBBN.test_assign_clusters _____________________________________________________

self = <test_bbn.TestBBN instance at 0x1025a7878>, huang_darwiche_jt = <bayesian.bbn.JoinTree object at 0x1025b7390>
huang_darwiche_dag = <bayesian.bbn.BBN object at 0x102597d50>

    def test_assign_clusters(self, huang_darwiche_jt, huang_darwiche_dag):

        # NOTE: This test will fail sometimes as assign_clusters
        # is currently non-deterministic, we should fix this.

        bbn_nodes = dict([(node.name, node) for node in
                          huang_darwiche_dag.nodes])
        assignments = huang_darwiche_jt.assign_clusters(huang_darwiche_dag)
        jt_cliques = dict([(node.name, node) for node
                           in huang_darwiche_jt.clique_nodes])
        # Note that these assignments are slightly different
        # to the ones in H&D. In their paper they never
        # give a full list of assignments so we will use
        # these default deterministic assignments for the
        # test. These are assumed to be a valid assignment
        # as all other tests pass.
        assert [] == assignments[jt_cliques['Clique_ADE']]
        assert [bbn_nodes['f_f']] == assignments[jt_cliques['Clique_DEF']]
        assert [bbn_nodes['f_h']] == assignments[jt_cliques['Clique_EGH']]
>       assert [bbn_nodes['f_a'], bbn_nodes['f_c']] == \
            assignments[jt_cliques['Clique_ACE']]
E       assert [<BBNNode f_a...(['a', 'c'])>] == [<BBNNode f_c (['a', 'c'])>]
E         At index 0 diff: <BBNNode f_a (['a'])> != <BBNNode f_c (['a', 'c'])>
E         Left contains more items, first extra item: <BBNNode f_c (['a', 'c'])>
E         Use -v to get the full diff

bayesian/test/test_bbn.py:543: AssertionError
================================================= 1 failed, 97 passed in 0.78 seconds ==================================================

Dynamically create a Bayesian network

From the tutorial, I see that the network is created by defining a function for each random variable where the function outputs the conditional probability given its parents (the parent assignments are arguments to the function). At first glance, this does not seem to allow for one to create a Bayesian network dynamically (say, based on an input file). Is there a good way to dynamically create a Bayesian network?

Marginal Probability Querying

If we need to use the marginal probabilities from the "PrettyTable" output by g.q(), what is the easiest way to call on particular values? Such as, for node A and value 2, how would one reference the associated marginal? I see the query function but am having difficulty getting it to work.

Test can not find module....

When I try to use the unit test $ PYTHONPATH=. py.test bayesian/test then it show 3 error. All three errors are about not finding gaussian_bayesian_networks.river

ImportError while importing test module '/Users/dandapeng/AI557/assignment5/bayesian-belief-networks-master/bayesian/test/test_gbn_examples.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
bayesian/test/test_gbn_examples.py:3: in
from bayesian.examples.gaussian_bayesian_networks.river import (
E ImportError: No module named gaussian_bayesian_networks.river

Inference of Graph Structure from Mass Function

In the readme file under "other features" you write that we can do "Inference of Graph Structure from Mass Functions" using this package. I have been going through the examples and the code but wasn't able to find out how I would go about doing that.

Any help would be greatly appreciated:)

make html not working

When running in virtualenv the installation works, but when I get to the html build the following error happens.

commit 5118569

sphinx-build -b html -d _build/doctrees . _build/html
Running Sphinx v1.4.1
making output directory...
WARNING: sphinx.ext.pngmath has been deprecated. Please use sphinx.ext.imgmath instead.

Extension error:
Config value 'math_number_all' already present
make: *** [html] Error 1

Running the package on windows 7 & python 3.7.2

I have windows 7 operating system and i installed python 3.7.2 , But when i try to install the package (python setup.py install) i get a lot of ERRORS saying "syntax error missing parenthesis in call to print".
Do i have to go over all the codes and put parenthesis ? :(

Best Regards,
Omar

continuous joint distributions examples

To whom this may concern,

It would be wonderful to see a short tutorial in which some very simple continuous joint distributions are defined! Instead of the table, how is it represented etc.? Also, what if I want to calculate some Information Theoretic quantity (such as the Jensen Shannon Divergence between two Bayes Nets), how can I do that using Bayes Nets as defined in the way that you do using this software? For example, in order to calculate the Entropy, H, of a discrete random variable with k possible values, x1, ...xk, I would calculate:

H(x)=p(x1)_log(p(x1)) + p(x2)_log(p(x2))+...+p(xk)*log(p(xk))

or for some Joint Distribution in other packages I would use python code such as:

H = sum(p*log(p, 2) for p, x in JointObject.items())

for a continuous variable it would be an equivalent integral.

Any help would be greatly appreciated!

Johannes

Node functions arguments

When a node function as its arguments wrong or misspelled no exception is thrown, the program just takes forever to build the bbn and doesn't give any feedback to the user to what's going wrong.

P.S. It would be nice to allow the functions to have the argument "self" so they could be methods of classes.

More than marginals?

Is it possible to obtain various joint distributions instead of marginals? For example, in the Monty Hall example, I'd want something like: g.jq(guest_door='A') to display the joint distribution P(monty_door, prize_door | guest_door='A'). I realize this doesn't scale well when the number of left-over variables is large, and I'm okay if there isn't a magic one-liner function jq to prepare this information.

GraphViz Input

How does one send the stringio generated for GraphViz input to GraphViz to get the graph image? I downloaded Graphviz but am unsure of how to pass the stringio to Graphviz. Is PyGraphViz necessary?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.