*fast* syntax
Quick specification of discrete random variables
Section titled “Quick specification of discrete random variables”aGrUM/pyAgrum offers a so-called fast syntax that allows to quickly and compactly specify prototypes of graphical models.
The type of the (discrete) random variables can be specifiy with different syntaxes:
- by default, a variable is a
pyagrum.RangeVariableusing the default domain size (second argument of the functions).- with
a[10], the variable is apyagrum.RangeVariableusing 10 as domain size (from 0 to 9)- with
a[3,7], the variable is apyagrum.RangeVariableusing the integer values from 3 to 7.- with
a[1,3.14,5,6.2], the variable is apyagrum.DiscretizedVariableusing the given ticks (at least 3 values).- with
a[1:6.15:9], the variable is apyagrum.DiscretizedVariableusing the ticks from 1 to 6.15 in 9 steps (the domain size is 9).- with
a+[1,3.14,5,6.2], the variable is apyagrum.DiscretizedVariableusing the given ticks (at least 3 values), empirical.- with
a+[1:6.15:9], the variable is apyagrum.DiscretizedVariableusing the ticks from 1 to 6.15 in 9 steps (the domain size is 9), empirical.- with
a{top|middle|bottom}, the variable is apyagrum.LabelizedVariableusing the given labels (here : top, middle and bottom).- with
a{-1|5|0|3}, the variable is apyagrum.IntegerVariableusing the sorted given values.- with
a{-0.5|5.01|0|3.1415}, the variable is apyagrum.NumericalDiscreteVariableusing the sorted given values.- with
a{-0.5:3.1415:5}, the variable is apyagrum.NumericalDiscreteVariableusing the values from -0.5 to 3.1415 in 5 steps (the domain size is 5).
pyagrum.fastVariable(*args)
Section titled “pyagrum.fastVariable(*args)”Use fast syntax to add a variable in the BayesNet.
- Raises: pyagrum.NotAllowed –
- Parameters:
- fast_description (str) – string following fast syntax description
- default_nbrmod (int) – nbr of modality if fast_description does not indicate it. default_nbrmod=1 is the way to create a variable with only one value (for instance for reward in influence diagram).
- Return type:
DiscreteVariable
Examples
>>> print(pyagrum.fastVariable('A{On|Off|Defun}'))A:Labelized({On|Off|Defun})>>> print(pyagrum.fastVariable('A{3.14|0|1.15}'))A:NumericalDiscrete({0|1.15|3.14})>>> print(pyagrum.fastVariable('A{1.2:5.2:5}}'))A:NumericalDiscrete({1.2|2.2|3.2|4.2|5.2})>>> print(pyagrum.fastVariable('A{1|3|9}'))A:Integer({1|3|9})>>> print(pyagrum.fastVariable('A[4,6]'))A:Range([4,6])>>> print(pyagrum.fastVariable('A[5]'))A:Range([0,4])>>> print(pyagrum.fastVariable('A[4,6,10]'))A:Discretized(<[4;6[,[6;10]>)>>> print(pyagrum.fastVariable('A[1:6:5]'))A:Discretized(<[1;2[,[2;3[,[3;4[,[4;5[,[5;6]>)Quick specification of (randomly parameterized) graphical models
Section titled “Quick specification of (randomly parameterized) graphical models”These fastPrototype aGrUM’s methods have also been wrapped in functions of pyagrum.
pyagrum.fastBN("A[10]->B<-C{top|middle|bottom};B->D")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=‘[2]’)
Section titled “pyagrum.fastBN(structure, domain=‘[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=pyagrum.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 (int or str) – the default domain size (int) or domain specification (str) for variables (default is “[2]”
- Returns: the resulting bayesian network
- Return type: pyagrum.BayesNet
pyagrum.fastMRF(structure, domain=‘[2]’)
Section titled “pyagrum.fastMRF(structure, domain=‘[2]’)”Create a Markov random field 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=pyagrum.fastMRF('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 (int or str) – the default domain size (int) or domain specification (str) for variables (default is “[2]”
- Returns: the resulting Markov random field
- Return type: pyagrum.MarkovRandomField
pyagrum.fastID(structure, domain=‘[2]’)
Section titled “pyagrum.fastID(structure, domain=‘[2]’)”Create an Influence Diagram with a modified dot-like syntax which specifies: : - the structure and the type of the variables following fast syntax,
- 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=pyagrum.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 (int or str) – the default domain size (int) or domain specification (str) for variables (default is “[2]”
- Returns: the resulting Influence Diagram
- Return type: pyagrum.InfluenceDiagram