A simple Python implementation of Bayesian Networks (BN). Not the most efficient implementation, but it's simple and it works.
- Add nodes
- Add edges
- Check colliders
- Check unblocked paths
- Check d-separation
- Check conditional independence
- Check Markov blanket
- Check Markov equivalence
Clone the repository and navigate to the project directory.
git clone https://github.com/jiemingyou/bayesian-networks-python.git
cd bayesian-networks-pythonRun the main script:
python3 src/network.pyHere's an example of how to assess the conditional independence between
from src.network import BayesianNetwork
# Initiate the network
bn = BayesianNetwork()
# Add the nodes
bn.add_node("A")
bn.add_node("B")
bn.add_node("C")
bn.add_node("D")
bn.add_node("E")
bn.add_node("F")
# Add the directed edges
bn.add_edge("A", "C")
bn.add_edge("A", "D")
bn.add_edge("A", "F")
bn.add_edge("B", "C")
bn.add_edge("B", "E")
bn.add_edge("C", "D")
bn.add_edge("D", "E")
bn.add_edge("F", "E")
# Calculate the d-separation
bn.is_d_separated("C", "E", ["B", "D"])
# Returns: (False , {unblocked set})