# Generating Bayesian Belief Networks¶

Let’s generate some Bayesian Belief Networks (BBNs). The algorithms are taken from Random Generation of Bayesian Networks [IC02]. There are two types of BBNs you may generate.

singly-connected

multi-connected

A singly-connected BBN is one, where ignoring the direction of the edges, there is at most one path between any two nodes.
A multi-connected BBN is one that is `not`

singly-connected.

## Singly-Connected¶

The key method to use here is `generate_singly_bbn`

.

```
1import numpy as np
2
3from pybbn.generator.bbngenerator import generate_singly_bbn, convert_for_exact_inference, convert_for_drawing
4
5# very important to set the seed for reproducible results
6np.random.seed(37)
7
8# this method generates the graph, g, and probabilities, p
9# note we are generating a singly-connected graph
10g, p = generate_singly_bbn(5, max_iter=5)
11
12# you have to convert g and p to a BBN
13bbn = convert_for_exact_inference(g, p)
14
15# you can convert the BBN to a nx graph for visualization
16nx_graph = convert_for_drawing(bbn)
```

## Multi-Connected¶

The key method to use here is `generate_multi_bbn`

.

```
1import numpy as np
2
3from pybbn.generator.bbngenerator import generate_multi_bbn, convert_for_exact_inference, convert_for_drawing
4
5# very important to set the seed for reproducible results
6np.random.seed(37)
7
8# this method generates the graph, g, and probabilities, p
9# note we are generating a multi-connected graph
10g, p = generate_multi_bbn(5, max_iter=5)
11
12# you have to convert g and p to a BBN
13bbn = convert_for_exact_inference(g, p)
14
15# you can convert the BBN to a nx graph for visualization
16nx_graph = convert_for_drawing(bbn)
```

## Direct Generation¶

In the case where you do `NOT`

need a reference to the BBN objects, use the API’s convenience method to generate and serialize the BBN directly to file.

```
1import numpy as np
2
3from pybbn.generator.bbngenerator import generate_bbn_to_file
4
5# set the seed for reproducibility
6np.random.seed(37)
7
8# generate a singly-connected BBN
9generate_bbn_to_file(n=10, file_path='singly-bbn.csv', bbn_type='singly', max_alpha=10)
10
11# generate a multi-connected BBN
12generate_bbn_to_file(n=10, file_path='multi-bbn.csv', bbn_type='multi', max_alpha=10)
```

Here’s the output for `singly-bbn.csv`

.

```
10,0,state0,state1,|,0.5495149877004699,0.4504850122995299
21,1,state0,state1,|,0.35835359558290997,0.64164640441709,0.8660444980250707,0.13395550197492936
32,2,state0,state1,|,0.5828348518985648,0.4171651481014352,0.6352808281847757,0.3647191718152243
43,3,state0,state1,|,0.43155247482552955,0.5684475251744704,0.05744110250902426,0.9425588974909757,0.44585399607259946,0.5541460039274007,0.286749915005319,0.713250084994681
54,4,state0,state1,|,0.3190576398549361,0.6809423601450639,0.011424133320075755,0.9885758666799241
65,5,state0,state1,|,0.48207371043602226,0.5179262895639779,0.07147107402394111,0.9285289259760588
76,6,state0,state1,|,0.2076134466833406,0.7923865533166594,0.44542849473036455,0.5545715052696354
87,7,state0,state1,|,0.757560101942848,0.242439898057152
98,8,state0,state1,|,0.1906328058926942,0.8093671941073058,0.2814000588799281,0.7185999411200719
109,9,state0,state1,|,0.7854793106243432,0.2145206893756569,0.12392098364527641,0.8760790163547235
110,1,directed
121,2,directed
132,3,directed
143,4,directed
153,8,directed
165,6,directed
175,3,directed
187,5,directed
198,9,directed
```

Here’s the output for `multi-bbn.csv`

.

```
10,0,state0,state1,|,0.680874572938313,0.319125427061687
21,1,state0,state1,|,0.7617263477727293,0.23827365222727065,0.3117227721913154,0.6882772278086846
32,2,state0,state1,|,0.12614472921860395,0.8738552707813961,0.7070911105993563,0.29290888940064375
43,3,state0,state1,|,0.4055587320025024,0.5944412679974977,0.9624106996627307,0.037589300337269156
54,4,state0,state1,|,0.31986562609614827,0.6801343739038517,0.022365118374575416,0.9776348816254246
65,5,state0,state1,|,0.77366174354673,0.2263382564532701,0.8579513677510221,0.1420486322489778,0.3183725110598738,0.6816274889401261,0.04262514631905535,0.9573748536809447
76,6,state0,state1,|,0.05830032685169777,0.9416996731483022,0.5840685338695271,0.41593146613047294,0.7078930065265004,0.29210699347349944,0.490562272424676,0.509437727575324
87,7,state0,state1,|,0.7569425298012309,0.243057470198769,0.6536654079476188,0.3463345920523811,0.6299885487124776,0.3700114512875224,0.4929042112083024,0.5070957887916976
98,8,state0,state1,|,0.3295640257593744,0.6704359742406256,0.9098731919901998,0.09012680800980029
109,9,state0,state1,|,0.7804943261233692,0.21950567387663072,0.43963638923803844,0.5603636107619615,0.03168532379450399,0.968314676205496,0.7189237718440259,0.28107622815597405,0.356320337335263,0.643679662664737,0.8089559692517324,0.19104403074826756,0.520364955519572,0.47963504448042804,0.3989706528653481,0.601029347134652
110,1,directed
120,9,directed
130,5,directed
141,2,directed
152,3,directed
163,4,directed
174,5,directed
184,6,directed
194,7,directed
205,6,directed
216,7,directed
226,9,directed
237,8,directed
248,9,directed
```