fastBN

pyagrum.fastBN(structure, domain='[2]', *, uniform=False)
Create a Bayesian network with a dot-like syntax which specifies:
  • the structure ‘a->b->c;b->d<-e;’,

  • the type of the variables with different syntax (cf documentation),

  • names possibly between single quotes ("'Visit Asia'->X").

The conditional probability tables are drawn at random, or uniform with uniform=True.

Parameters:
  • structure (str) – the string containing the specification

  • domain (int | str) – the default domain size (int) or domain specification (str) for variables (default is “[2]”

  • uniform (bool) – if True, every conditional probability table is uniform (no value is improbable, whatever the random generator), instead of random

Returns:

the resulting bayesian network

Return type:

pyagrum.BayesNet

Example

import pyagrum as gum

bn = gum.fastBN("A->B[3]<-C{yes|no};B->D[1,5]")
bn.variable("B").labels()
# ('0', '1', '2')
bn.variable("C").labels()
# ('yes', 'no')
bn.variable("D").labels()
# ('1', '2', '3', '4', '5')
sorted(bn.variable(i).name() for i in bn.parents("B"))
# ['A', 'C']

# the second argument is the default domain size of the variables without specification
bn = gum.fastBN("A->B[1,3]<-C{yes|No}->D<-E[1,2.5,3.9]", 6)
print(bn.variable("A"))
# A:Range([0,5])
print(bn.variable("E"))
# E:Discretized(<[1;2.5[,[2.5;3.9]>)

# the CPTs are random, or uniform with uniform=True
bn = gum.fastBN("A->B<-C", uniform=True)
bn.cpt("A").tolist()
# [0.5, 0.5]

# a name between single quotes may contain any character ('' for a quote)
bn = gum.fastBN("'Visit Asia'{yes|no}->'X-ray'[3]<-'l''eau'")
sorted(bn.names())
# ['Visit Asia', 'X-ray', "l'eau"]