{"id":17128290,"url":"https://github.com/elinorbgr/loopybayesnet","last_synced_at":"2025-04-13T07:05:08.871Z","repository":{"id":62442494,"uuid":"196241544","full_name":"elinorbgr/loopybayesnet","owner":"elinorbgr","description":"Rust implementation of the Loopy Belief propagation algorithm for inference in Bayesian Networks","archived":false,"fork":false,"pushed_at":"2022-03-13T17:52:09.000Z","size":18,"stargazers_count":34,"open_issues_count":3,"forks_count":1,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-02-05T14:02:11.275Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Rust","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/elinorbgr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-10T16:40:18.000Z","updated_at":"2025-01-30T21:21:15.000Z","dependencies_parsed_at":"2022-11-01T21:51:03.163Z","dependency_job_id":null,"html_url":"https://github.com/elinorbgr/loopybayesnet","commit_stats":null,"previous_names":["vberger/loopybayesnet"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elinorbgr%2Floopybayesnet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elinorbgr%2Floopybayesnet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elinorbgr%2Floopybayesnet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elinorbgr%2Floopybayesnet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elinorbgr","download_url":"https://codeload.github.com/elinorbgr/loopybayesnet/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240044422,"owners_count":19739183,"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":[],"created_at":"2024-10-14T19:06:41.778Z","updated_at":"2025-02-23T01:30:52.805Z","avatar_url":"https://github.com/elinorbgr.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Loopy Bayes Net\n\nAn implementation of the Loopy Belief Propagation algorithm for Bayesian Networks\n\n### Bayesian networks \u0026 Loopy belief propagation\n\nBayesian networks can be used to encode a set of causal or logical probabilistic dependency\nbetween events. They take the shape of directed acyclic graphs, each node being associated\nwith a probability table defining the probability of it taking each possible values depending\non the values of its parents. For further details, you can check\n[Wikipedia](https://en.wikipedia.org/wiki/Bayesian_network).\n\nThe Loopy Belief Propagation is an algorithm that computes an approximation of the marginal probability\ndistribution of each node of the network, conditionned by the value of a chosen set of \"observed\"\nvariables, for which the values are set beforehand.\n\nThis is an approximation, which behaves as if the parents of each node were conditionnaly\nindependent given the node. This is only true if the considered graph is actually a tree (there\nare no undirected loop), in which case the approximation is exact.\n\nA typical failure case of this algorithm is when some nodes have parents that are both strongly\ncorrelated and very random (which is notably the case for the `simple_net` example in this repository ;) ).\nThen, even if the algorithm converges (it is not always the case), it is likely to converge to a wrong\nvalue.\n\nOn the other hand, for networks where the observations almost certainly determine the value of the\nrest of the network (which is not rare in real-world problems), the Loopy Belief Propagation\nalgorithm provides a very good approximation (see [arXiv:1301.6725](https://arxiv.org/pdf/1301.6725.pdf)\nfor a study about it for example).\n\n### How to use this crate\n\nThis crate allows you, given a fully-specified Bayesian Network and a set of observations, to\niteratively run the Loopy Belief Propagation algorithm on it.\n\n```rust\nuse loopybayesnet::BayesNet;\nuse ndarray::{Array1, Array2};\n\nlet mut net = BayesNet::new();\n\n// First insert your modes to create your network\nlet node1 = net.insert_node_from_probabilities(\n    \u0026[], // This node does not have any parent,\n    Array1::from(vec![0.2, 0.3, 0.5]) // This node can take 3 values, and these are the\n                                      // associated prior probabilities\n);\nlet node2 = net.insert_node_from_probabilities(\n    \u0026[node1], // This node has node1 as a parent\n    Array2::from(vec![   // This node can take 2 values\n        [0.4, 0.3, 0.7], // these are the probabilities of value 0 depending on the value of node1\n        [0.6, 0.7, 0.3], // these are the probabilities of value 1 depending on the value of node1\n    ])\n);\n\n// Now that the net is set, we can run the algorithm\n\n// Reset the internal state which is kept, as the algorithm is iterative\nnet.reset_state();\n// Set as evidence that node2 = 0\nnet.set_evidence(\u0026[(node2, 0)]);\n\nfor _ in range 0..3 {\n    // Iterate over the steps of the algorithms until convergence.\n    // As a rule of thumb, the number of necessary iterations is often of the same order as the\n    // diameter of the graph, but it may be longer with graphs containing loops.\n    net.step();\n    // between each step we can observe the current state of the algorithm\n    let beliefs = net.beliefs();\n    // the algorithm works internally with log-probabilities for numerical stability\n    // the `.as_probabilities()` call converts them back to regular normalized probabilities.\n    println!(\"Marginals for node1: {:?\", beliefs[node1].as_probabilities());\n    println!(\"Marginals for node2: {:?\", beliefs[node2].as_probabilities());\n}\n```\n\n### Quality of the implementation\n\nThe implementation should be mostly correct, given the algorithm is quite clear to follow\nand `ndarray` handles the difficult stuff ;)\n\nI made no attempt to optimize the performance though, so it may run slowly on very large\ngraphs with many values and many parents per node.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felinorbgr%2Floopybayesnet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felinorbgr%2Floopybayesnet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felinorbgr%2Floopybayesnet/lists"}