{"id":19845861,"url":"https://github.com/jluttine/junction-tree","last_synced_at":"2025-06-21T05:02:50.305Z","repository":{"id":20762347,"uuid":"73953656","full_name":"jluttine/junction-tree","owner":"jluttine","description":"The junction tree algorithm for (discrete) factor graphs","archived":false,"fork":false,"pushed_at":"2022-04-23T15:58:30.000Z","size":314,"stargazers_count":18,"open_issues_count":3,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-15T10:43:46.957Z","etag":null,"topics":["bayesian-networks","clique-potentials","factor-graphs","graphical-models","inference","junction-trees","marginalization"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jluttine.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-16T19:29:24.000Z","updated_at":"2025-04-11T08:15:02.000Z","dependencies_parsed_at":"2022-07-25T08:01:57.484Z","dependency_job_id":null,"html_url":"https://github.com/jluttine/junction-tree","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/jluttine/junction-tree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jluttine%2Fjunction-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jluttine%2Fjunction-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jluttine%2Fjunction-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jluttine%2Fjunction-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jluttine","download_url":"https://codeload.github.com/jluttine/junction-tree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jluttine%2Fjunction-tree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261059898,"owners_count":23103955,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bayesian-networks","clique-potentials","factor-graphs","graphical-models","inference","junction-trees","marginalization"],"created_at":"2024-11-12T13:09:31.949Z","updated_at":"2025-06-21T05:02:45.294Z","avatar_url":"https://github.com/jluttine.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# junction-tree\n\nImplementation of discrete factor graph inference utilizing the Junction Tree\nalgorithm\n\n## Requirements\n\n* Python3 (\u003e= 3.5.5), NumPy (\u003e= 1.13.3), SciPy (\u003e= 1.1), attrs (\u003e=17.4)\n\n## Factor graphs:\n\nA factor graph is given as a list of variables that indicate which variables are\nin the factor.\n\n```\n[vars1, ..., varsN]  # a list of N factors\n```\n\nThe index in the list can be used as an ID for the factor, that is, the first\nfactor in the list has ID 0 and the last factor has ID N-1.\n\nA companion list (of numpy arrays) of the same length as the factor list is\nprovided as a representation for the factor values\n\n```\n[values1, ..., valuesN]\n```\n\nAlso, the size of each of the M variables is given as a dictionary:\n\n```\n{\n    var1: size1,\n    ...\n    varM: sizeM\n}\n```\n\nHere, size is an integer representing the size of the variable. It is the same\nas the length of the corresponding axis in the numeric array.\n\n\n## Generic trees (recursive definition)\n\n```\n[index, vars, child_tree1, ..., child_treeN]\n```\n\n\n## Junction trees\n\n```\ntree structure (composed of node indices found in node list):\n[\n    index,\n    (\n        separator1_index,\n        child_tree1\n    ),\n    ...,\n    (\n        separatorN_index,\n        child_treeN\n    )\n]\nnode list (elements are list of variables which define node):\n[node0_vars, node1_vars,...,nodeN_vars]\n\nmaxcliques and separators are both types of nodes\n```\n\n## Potentials in (junction) trees\n\nA list of arrays. The node IDs in the tree graphs map to the arrays in this data\nstructure in order to get the numeric arrays in the execution phase. The numeric\narrays are not needed in the compilation phase.\n\n\n## Usage\n\n### Junction Tree construction\n\nStarting with the definition of a factor graph\n\n(Example taken from http://mensxmachina.org/files/software/demos/jtreedemo.html)\n\n```\nimport junctiontree.beliefpropagation as bp\nimport junctiontree.junctiontree as jt\nimport numpy as np\n\nvar_sizes = {\n                    \"cloudy\": 2,\n                    \"sprinkler\": 2,\n                    \"rain\": 2,\n                    \"wet_grass\": 2\n                }\n\nfactors = [\n            [\"cloudy\"],\n            [\"cloudy\", \"sprinkler\"],\n            [\"cloudy\", \"rain\"],\n            [\"rain\", \"sprinkler\", \"wet_grass\"]\n]\n\nvalues = [\n            np.array([0.5,0.5]),\n            np.array(\n                        [\n                            [0.5,0.5],\n                            [0.9,0.1]\n                        ]\n                    ),\n            np.array(\n                        [\n                            [0.8,0.2],\n                            [0.2,0.8]\n                        ]\n                    ),\n            np.array(\n                        [\n                            [\n                                [1,0],\n                                [0.1,0.9]\n                            ],\n                            [\n                                [0.1,0.9],\n                                [0.01,0.99]\n                            ]\n                        ]\n            )\n]\n\ntree = jt.create_junction_tree(factors, var_sizes)\n\n```\n\n\n### Global Propagation\n\nThe initial clique potentials are inconsistent. The potentials are made\nconsistent through global propagation on the junction tree\n\n```\nprop_values = tree.propagate(values)\n```\n\n### Observing Data\n\nAlternatively, clique potentials can be made consistent after observing data for\nthe variables in the junction tree\n\n```\n# Update the size of observed variable\ncond_sizes = var_sizes.copy()\ncond_sizes[\"wet_grass\"] = 1\ncond_tree = jt.create_junction_tree(factors, cond_sizes)\n\n# Then, also similarly the values:\ncond_values = values.copy()\n# remove axis corresponding to \"wet_grass\" == 0\ncond_values[3] = cond_values[3][:,:,1:2]\n\n# Perform global propagation using conditioned values\nprop_values =  tree.propagate(cond_values)\n```\n\n### Marginalization\n\nFrom a collection of consistent clique potentials, the marginal value of\nvariables of interest can be calculated\n\n```\n# Pr(sprinkler|wet_grass = 1)\nmarginal = np.sum(prop_values[1], axis=0)\n\n# The probabilities are unnormalized but we can calculate the normalized values:\nnorm_marginal = marginal/np.sum(marginal)\n```\n\n\n## References\n\n- S. M. Aji and R. J. McEliece, \"The generalized distributive law,\" in IEEE\n  Transactions on Information Theory, vol. 46, no. 2, pp. 325-343, Mar 2000.\n  doi: 10.1109/18.825794\n\n- Cecil Huang, Adnan Darwiche, Inference in belief networks: A procedural guide,\n  International Journal of Approximate Reasoning, Volume 15, Issue 3, 1996,\n  Pages 225-263, ISSN 0888-613X,\n  http://dx.doi.org/10.1016/S0888-613X(96)00069-2.\n\n- F. R. Kschischang, B. J. Frey and H. A. Loeliger, \"Factor graphs and the\n  sum-product algorithm,\" in IEEE Transactions on Information Theory, vol. 47,\n  no. 2, pp. 498-519, Feb 2001. doi: 10.1109/18.910572\n\n- Kjærulff, Uffe. 1997. Nested junction trees. In Proceedings of the Thirteenth\n  conference on Uncertainty in artificial intelligence (UAI’97). Morgan Kaufmann\n  Publishers Inc., San Francisco, CA, USA, 294–301.\n\n## Other Python Junction Tree Implementations\n\n- [symfer](https://mbsd.cs.ru.nl/symfer/index.html) - a tool suite, written\n  mostly in Python, for performing probabilistic inference\n\n- [bayesian-belief-networks](https://github.com/eBay/bayesian-belief-networks) -\n  a library for the creation of and exact inference on Bayesian Belief Networks\n  specified as pure Python functions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjluttine%2Fjunction-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjluttine%2Fjunction-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjluttine%2Fjunction-tree/lists"}