Functions from pyAgrum¶
Useful functions in pyAgrum¶
- pyAgrum.about()¶
about() for pyAgrum
- pyAgrum.getPosterior(model, evs, target)¶
Compute the posterior of a single target (variable) in a BN given evidence
getPosterior uses a VariableElimination inference. If more than one target is needed with the same set of evidence or if the same target is needed with more than one set of evidence, this function is not relevant since it creates a new inference engine every time it is called.
- Parameters
bn (pyAgrum.BayesNet or pyAgrum.MarkovNet) – The probabilistic Graphical Model
evs (dictionaryDict) – {name/id:val, name/id : [ val1, val2 ], …}
target (string or int) – variable name or id
- Return type
posterior (pyAgrum.Potential or other)
Quick specification of (randomly parameterized) graphical models¶
aGrUM/pyAgrum offers a compact syntax that allows to quickly specify prototypes of graphical models. These fastPrototype aGrUM’s methods have also been wrapped in functions of pyAgrum.
gum.fastBN("A->B<-C;B->D")
The type of the random variables can be specifiy with different syntaxes:
by default, a variable is a
pyAgrum.RangeVariable
using the default domain size (second argument of the functions).with
a[10]
, the variable is apyAgrum.RangeVariable
using 10 as domain size (from 0 to 9)with
a[3,7]
, the variable is apyAgrum.RangeVariable
using a domainSize from 3 to 7with
a[1,3.14,5,6.2]
, the variable is apyAgrum.DiscretizedVariable
using the given ticks (at least 3 values)with
a{top|middle|bottom}
, the variable is apyAgrum.LabelizedVariable
using the given labels (here : ‘top’, ‘middle’ and ‘bottom’).with
a{-1|5|0|3}
, the variable is apyAgrum.IntegerVariable
using the sorted given values.
Note
If the dot-like string contains such a specification more than once for a variable, the first specification will be used.
the CPTs are randomly generated.
- pyAgrum.fastBN(structure, domain_size=2)¶
- 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).
Examples
>>> import pyAgrum as gum >>> bn=gum.fastBN('A->B[1,3]<-C{yes|No}->D[2,4]<-E[1,2.5,3.9]',6)
- Parameters
structure (str) – the string containing the specification
domain_size (int) – the default domain size for variables
- Returns
the resulting bayesian network
- Return type
- pyAgrum.fastMN(structure, domain_size=2)¶
- Create a Markov network with a modified dot-like syntax which specifies:
the structure ‘a-b-c;b-d;c-e;’ where each chain ‘a-b-c’ specifies a factor,
the type of the variables with different syntax (cf documentation).
Examples
>>> import pyAgrum as gum >>> bn=gum.fastMN('A--B[1,3]--C{yes|No};C--D[2,4]--E[1,2.5,3.9]',6)
- Parameters
structure (str) – the string containing the specification
domain_size (int) – the default domain size for variables
- Returns
the resulting Markov network
- Return type
- pyAgrum.fastID(structure, domain_size=2)¶
- Create an Influence Diagram with a modified dot-like syntax which specifies:
the structure ‘a->b<-c;b->d;c<-e;’,
the type of the variables with different syntax (cf documentation),
a prefix for the type of node (chance/decision/utiliy nodes):
a
: a chance node named ‘a’ (by default)$a
: a utility node named ‘a’*a
: a decision node named ‘a’
Examples
>>> import pyAgrum as gum >>> bn=gum.fastID('A->B[1,3]<-*C{yes|No}->$D<-E[1,2.5,3.9]',6)
- Parameters
structure (str) – the string containing the specification
domain_size (int) – the default domain size for variables
- Returns
the resulting Influence Diagram
- Return type
Input/Output for Bayesian networks¶
- pyAgrum.availableBNExts()¶
Give the list of all formats known by pyAgrum to save a Bayesian network.
- Returns
a string which lists all suffixes for supported BN file formats.
- pyAgrum.loadBN(filename, listeners=None, verbose=False, **opts)¶
load a BN from a file with optional listeners and arguments
- Parameters
filename – the name of the input file
listeners – list of functions to execute
verbose – whether to print or not warning messages
system – (for O3PRM) name of the system to flatten in a BN
classpath – (for O3PRM) list of folders containing classes
- Returns
a BN from a file using one of the availableBNExts() suffixes.
Listeners could be added in order to monitor its loading.
Examples
>>> import pyAgrum as gum >>> >>> # creating listeners >>> def foo_listener(progress): >>> if progress==200: >>> print(' BN loaded ') >>> return >>> elif progress==100: >>> car='%' >>> elif progress%10==0: >>> car='#' >>> else: >>> car='.' >>> print(car,end='',flush=True) >>> >>> def bar_listener(progress): >>> if progress==50: >>> print('50%') >>> >>> # loadBN with list of listeners >>> gum.loadBN('./bn.bif',listeners=[foo_listener,bar_listener]) >>> # .........#.........#.........#.........#..50% >>> # .......#.........#.........#.........#.........#.........% | bn loaded
- pyAgrum.saveBN(bn, filename)¶
save a BN into a file using the format corresponding to one of the availableWriteBNExts() suffixes.
- Parameters
bn(gum.BayesNet) – the BN to save
filename(str) – the name of the output file
Input/Output for Markov networks¶
- pyAgrum.availableMNExts()¶
Give the list of all formats known by pyAgrum to save a Markov network.
- Returns
a string which lists all suffixes for supported MN file formats.
- pyAgrum.loadMN(filename, listeners=None, verbose=False)¶
load a MN from a file with optional listeners and arguments
- Parameters
filename – the name of the input file
listeners – list of functions to execute
verbose – whether to print or not warning messages
- Returns
a MN from a file using one of the availableMNExts() suffixes.
Listeners could be added in order to monitor its loading.
Examples
>>> import pyAgrum as gum >>> >>> # creating listeners >>> def foo_listener(progress): >>> if progress==200: >>> print(' BN loaded ') >>> return >>> elif progress==100: >>> car='%' >>> elif progress%10==0: >>> car='#' >>> else: >>> car='.' >>> print(car,end='',flush=True) >>> >>> def bar_listener(progress): >>> if progress==50: >>> print('50%') >>> >>> # loadBN with list of listeners >>> gum.loadMN('./bn.uai',listeners=[foo_listener,bar_listener]) >>> # .........#.........#.........#.........#..50% >>> # .......#.........#.........#.........#.........#.........% | bn loaded
- pyAgrum.saveMN(mn, filename)¶
save a MN into a file using the format corresponding to one of the availableWriteMNExts() suffixes.
- Parameters
mn(gum.MarkovNet) – the MN to save
filename(str) – the name of the output file
Input for influence diagram¶
- pyAgrum.availableIDExts()¶
Give the list of all formats known by pyAgrum to save a influence diagram.
- Returns
a string which lists all suffixes for supported ID file formats.
- pyAgrum.loadID(filename)¶
read a gum.InfluenceDiagram from a ID file
- Parameters
filename – the name of the input file
- Returns
an InfluenceDiagram
- pyAgrum.saveID(infdiag, filename)¶
save an ID into a file using the format corresponding to one of the availableWriteIDExts() suffixes.
- Parameters
ID(gum.InfluenceDiagram) – the ID to save
filename(str) – the name of the output file