# Learning Bayesian Networks with the bnlearn R Package

bnlearn lets users pair any structure learner with any statistical test or score

Marco Scutari · arXiv · 2009-8-26

- **Problem** Bayesian network structure learning usually ties each algorithm to one statistical criterion. Users cannot pick the combination that suits their data, because the search and the test are fixed together.
- **Method** bnlearn is an R package that separates the search procedure from the statistic used to judge candidate networks. It offers constraint-based and score-based learners, and users pair any learner with any test or score.
- **Results** On the learning.test data, three other constraint-based learners matched the Grow-Shrink network exactly.

## Why learning the graph is hard

A Bayesian network is a directed acyclic graph whose nodes are random variables and whose arcs stand for probabilistic dependencies. Recovering that graph from data is hard because the number of candidate structures climbs quickly as variables are added. Exhaustive search only works for the smallest data sets, so every algorithm has to prune the space in some way.

Practical methods also split the job: they learn the graph first and estimate each local distribution afterwards, which avoids modelling the full joint distribution.

The literature has mostly studied two cases, discrete and continuous data. Discrete data use multinomial distributions stored as contingency tables, and this is by far the most common setting. Continuous data are usually modelled as multivariate normal, which gives Gaussian Bayesian networks with linear constraints between nodes. Other distributional assumptions exist, but they bring more complex algorithms or limits, such as a mixed-variable approach that forbids a continuous parent of a discrete node.

## Separating the algorithm from the criterion

The key idea is to decouple the search procedure from the statistic used to judge a candidate network. In a reference implementation the algorithm is usually fixed to one criterion, so users can't pick the combination that suits their data. bnlearn offers both constraint-based and score-based learners and lets the user pair a learner with a test or score.

Constraint-based algorithms read the structure off conditional independence tests, then build a graph that satisfies the resulting d-separation statements. Score-based algorithms instead give each candidate network a score and run a heuristic search to maximize it. The reason to keep the two apart is that the best combination depends on the data at hand.

## How constraint-based learning runs

The Inductive Causation algorithm underlies the constraint-based family and runs in three steps. First it learns the skeleton, the undirected graph underlying the network, restricting each search to the Markov blanket of a node. Then it orients the arcs of every v-structure, a converging connection \\( X\_j \\rightarrow X\_i \\leftarrow X\_k \\).

Last, it orients the remaining arcs so that the graph stays acyclic.

Grow-Shrink learns each Markov blanket with the simplest detection method in this family. IAMB adds nodes by forward selection and then removes false positives, while inter.iamb interleaves forward stepwise selection to avoid them during detection. fast.iamb uses speculative stepwise forward selection to cut the number of tests. mmpc selects neighbours by maximizing the minimum association with any subset chosen earlier, and its arcs stay undirected.

Each constraint-based algorithm comes in three versions. The optimized default uses backtracking, which roughly halves the number of independence tests. The unoptimized version follows the original description closely, which helps compare learners and tests against each other. The parallel version needs a cluster built with makeCluster from snow and passed in through the cluster argument.

## Tests and scores behind the learners

For discrete data every test is a function of the observed frequencies \\( n\_{ijk} \\) and of the conditioning configurations. Mutual information sums the frequencies against their expected counts on a log scale, as \\\[ MI(X,Y \\mid Z)=\\sum\_{i,j,k}\\frac{n\_{ijk}}{n}\\,\\log\\frac{n\_{ijk}\\,n\_{++k}}{n\_{i+k}\\,n\_{+jk}} \\\] Mutual information is the test the Grow-Shrink run on learning.test uses.

Pearson's X² uses expected counts built from the marginal totals, \\( m\_{ijk}=\\frac{n\_{i+k}\\,n\_{+jk}}{n\_{++k}} \\). Both mutual information and X² come with an asymptotic χ² version and a Monte Carlo permutation version. The fast mutual information is set to zero unless there are at least five data per parameter. The experimental aict test instead compares the mutual information with a threshold built from the number of levels and the sample size.

For continuous data the tests depend on the partial correlation coefficients \\( \\rho\_{XY \\mid Z} \\). The linear correlation test has a Student's t version and a Monte Carlo version. Fisher's Z transforms the correlation, the same transformation used by TETRAD and the pcalg package. The continuous mutual information is \\( MI\_g=-\\tfrac{1}{2}\\log(1-\\rho^2\_{XY \\mid Z}) \\).

The discrete scores are the likelihood, the log-likelihood, AIC, BIC, bde and K2. bde is score equivalent, but K2 is not. The only continuous score is bge, a Gaussian posterior density that follows a Wishart distribution.

## Encoding prior knowledge as arcs

Prior knowledge enters through the blacklist and whitelist arguments, which force arcs to be absent or present. An arc whitelisted in both directions stays in the graph, but its orientation is left to the learner. An arc blacklisted in both directions, along with its undirected version, is never present. Whitelisting one direction fixes that direction and rules out the reverse, while blacklisting one direction removes only that direction.

## Worked example on learning.test

learning.test holds 5000 observations of six discrete variables, A to E with three levels each and F with two. Grow-Shrink returns five arcs, one undirected and four directed, after 43 tests. iamb, fast.iamb and inter.iamb each compare equal to that network.

Hill-climbing with the AIC score returns a fully directed network after 40 tests. It doesn't compare equal to the Grow-Shrink network, because the arc between A and B points one way in one and is undirected in the other. The two orientations of A and B can't be told apart by score, since score equivalence holds for every score except K2.

Blacklisting one direction forces the other, and the resulting network compares equal to the hill-climbing network. Under bde both orientations scored -24002.36, so the score alone can't choose between them.

## ALARM network: learner accuracy and cost

The ALARM network has 37 discrete variables and 46 arcs, and the alarm data set holds 20000 samples drawn from it.

Grow-Shrink ran 1727 conditional independence tests, while the constraint-based learners all ran between 1727 and 3106 tests. Hill-climbing performed 2841 network score comparisons, a count that the authors compare with the tests of the other learners.

Hill-climbing learned 53 arcs, compared to 42 to 45 arcs for the constraint-based learners. The authors point out that this is about ten more arcs than the other learners. Execution took 13.54360 for Grow-Shrink and 72.38705 for hill-climbing on an Intel Core 2 Duo machine with 1GB of RAM.

## Monte Carlo X2 tests on ALARM

The probability structure of ALARM produces many sparse contingency tables. Grow-Shrink with the parametric X² test missed 12 arcs of the network. With the Monte Carlo version of X², run with B = 10000, it identified all but five arcs.

The authors attribute the gap to the sparse tables, where the asymptotic approximation is least reliable. This comparison rests on one sample of 20000 observations from ALARM.

## Marks data: continuous conditional independence

The marks data hold examination marks for 88 students in five subjects. Under multivariate normality the question reduces to partial correlations, and some of them are clearly not significant.

The linear correlation test of STAT and VECT given ALG gives cor = 0.0527 with p-value = 0.628. The Fisher's Z test gives 0.6289112, the Monte Carlo correlation test 0.6332 and the mi-g test 0.6209406, so the conclusion doesn't depend on the test.

All learners agree up to arc direction, and analysis and statistics are conditionally independent of mechanics and vectors given algebra.

## Limitations

mmpc returns only undirected arcs and makes no attempt to detect their orientation. K2 isn't score equivalent, so the two orientations of an arc need not score alike. Parametric tests miss more arcs on sparse tables, as the ALARM case shows.

The examples are small: learning.test with six variables, one ALARM sample of 20000, and a single continuous example, marks. Execution times come from one machine.

## Paper info

- **Authors**: Marco Scutari
- **Venue**: arXiv · Preprint
- **First published**: 2009-8-26
- **Added**: 2026-11-12
- **Updated**: 2010-7-10
- **Version**: v2
- **arXiv**: 0908.3817
- **Category**: stat.ML
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/0908.3817v2) · [PDF ↗](https://arxiv.org/pdf/0908.3817v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Marco Scutari (2009). Learning Bayesian Networks with the bnlearn R Package. arXiv:0908.3817v2. https://arxiv.org/abs/0908.3817v2

**BibTeX**

```
@misc{scutari2009learning,
  title = {{Learning Bayesian Networks with the bnlearn R Package}},
  author = {Marco Scutari},
  year = {2009},
  eprint = {0908.3817},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/0908.3817v2}
}
```

## Other paper commentary

- [4D Spatio-Temporal ConvNets: Minkowski Convolutional Neural Networks](https://refivu.com/en/papers/bb10cb3b-b1cc-4119-9de1-8503fdbd27ff-4d-spatio-temporal-convnets-minkowski-convolutional-neural.md)
- [Designing Network Design Spaces](https://refivu.com/en/papers/37f6e2ac-519b-4565-a6cb-673b7ed0265a-designing-network-design-spaces.md)
- [Deep metric learning using Triplet network](https://refivu.com/en/papers/f6dd7198-0b7a-4f6a-a88a-eb7bbe1ff7ee-deep-metric-learning-using-triplet-network.md)
- [Autoformer: Decomposition Transformers with Auto-Correlation for Long-Term Series Forecasting](https://refivu.com/en/papers/d52df827-0d8c-42c3-97e1-3063d2cc1256-autoformer-decomposition-transformers-with-auto-correlation.md)
- [DeepSurv: Personalized Treatment Recommender System Using A Cox Proportional Hazards Deep Neural Network](https://refivu.com/en/papers/e56bf111-97b6-4d99-b702-a3e324f609b2-deepsurv-personalized-treatment-recommender-system-using-a.md)
- [BinaryConnect: Training Deep Neural Networks with binary weights during propagations](https://refivu.com/en/papers/50c59bf6-7b51-443a-b99a-d2bbc087f3e9-binaryconnect-training-deep-neural-networks-with-binary.md)
- [A Survey on In-context Learning](https://refivu.com/en/papers/e3dcb216-0b54-4a23-8e0d-f077c493f505-a-survey-on-in-context-learning.md)
- [Probabilistic Latent Semantic Analysis](https://refivu.com/en/papers/2bb41f8f-b6cc-410c-9347-fd84aed3b2b4-probabilistic-latent-semantic-analysis.md)
- [Deep Learning for Sentiment Analysis : A Survey](https://refivu.com/en/papers/423bc94d-8ef5-42e2-a68f-87cb54c7636d-deep-learning-for-sentiment-analysis-a-survey.md)
- [Understanding Convolution for Semantic Segmentation](https://refivu.com/en/papers/c32b12c2-d4e5-4e10-b7c6-b643713776fc-understanding-convolution-for-semantic-segmentation.md)
- [InternVL3: Exploring Advanced Training and Test-Time Recipes for Open-Source Multimodal Models](https://refivu.com/en/papers/93e60b18-9322-49b4-85e3-af682c647d05-internvl3-exploring-advanced-training-and-test-time-recipes.md)
- [Applications of Deep Reinforcement Learning in Communications and Networking: A Survey](https://refivu.com/en/papers/0b4d9b08-577a-4c81-8c2b-a8a7244761e2-applications-of-deep-reinforcement-learning-in.md)

## Terms in this paper

- [mutual information](https://refivu.com/en/terms/31de24f17a5e-mutual-information)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/bdeeb8df-cd3a-419f-af61-f58819af852d-learning-bayesian-networks-with-the-bnlearn-r-package)
