Random Variables
aGrUM/pyAgrum is currently dedicated for discrete probability distributions.
There are 5 types of discrete random variables in aGrUM/pyAgrum: pyagrum.LabelizedVariable, pyagrum.DiscretizedVariable, pyagrum.IntegerVariable, pyagrum.RangeVariable and pyagrum.NumericalDiscreteVariable. The 5 types are mainly provided in order to ease modelization. Derived from DiscreteVariable, they share a common API. They essentially differ by the means to create, name and access to their modalities.
The function pyagrum.fastVariable() allows to easily create variables of any types with the fast syntax.
Common API for Random Discrete Variables
- class pyagrum.DiscreteVariable(*args, **kwargs)
DiscreteVariable is the (abstract) base class for discrete random variables.
Methods
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
returns whether x belongs to the variable domain (if the variable is a |
|
For numerical discrete variables (all except |
|
For numerical discrete variables (all except |
|
Draw a random float value from the x-th interval of the variable (only for |
|
Returns the integer domain of the variable (if the variable is a |
|
Returns whether the variable is empirical (if the variable is a |
|
returns whether s is a label of the variable |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a tick for the variable |
|
Returns whether x is a value for the variable |
|
returns the maximum value of the variable domain (if the variable is a |
|
returns the minimum value of the variable domain (if the variable is a |
|
Returns the numerical domain of the variable (if the variable is a |
|
Returns the index of the label |
|
set the description of the variable. |
|
sets the name of the variable. |
|
Returns the x-th tick of the variable (if the variable is a |
|
Returns the ticks of the variable |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Concrete classes for Random Discrete Variables
LabelizedVariable
- class pyagrum.LabelizedVariable(*args)
LabelizedVariable is a discrete random variable with a customizable sequence of labels.
- LabelizedVariable(aName, aDesc=’’, nbrLabel=2) -> LabelizedVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the (optional) description of the variable
nbrLabel (int) – the number of labels to create (2 by default)
- LabelizedVariable(aName, aDesc=’’, labels) -> LabelizedVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the (optional) description of the variable
labels (list of str) – the labels to create
- LabelizedVariable(aLDRV) -> LabelizedVariable
- Parameters:
aLDRV (
pyagrum.LabelizedVariable) – Thepyagrum.LabelizedVariablethat will be copied
- Raises:
pyagrum.InvalidArgument – if the name is empty or has an ASCII control character (a new line, a tabulation…)
pyagrum.InvalidArgument – if a label has an ASCII control character (a new line, a tabulation…); an empty label is accepted
Example
import pyagrum as gum
# creating a variable with 3 labels: '0', '1' and '2'
va = gum.LabelizedVariable("a", "a labelized variable", 3)
print(va)
# a:Labelized({0|1|2})
print(va.addLabel("foo"))
# a:Labelized({0|1|2|foo})
va.changeLabel(1, "bar")
print(va)
# a:Labelized({0|bar|2|foo})
vb = gum.LabelizedVariable("b", "b", 0).addLabel("A").addLabel("B").addLabel("C")
print(vb)
# b:Labelized({A|B|C})
vb.labels()
# ('A', 'B', 'C')
vb.isLabel("E")
# False
vb.label(2)
# 'C'
vc = gum.LabelizedVariable("c", "c", ["one", "two", "three"])
print(vc)
# c:Labelized({one|two|three})
Methods
Add a label with a new index (we assume that we will NEVER remove a label). |
|
Change the label at the specified index |
|
For numerical discrete variables (all except |
|
Erase all the labels from the variable. |
|
Indicates whether the variable already has the label passed in argument |
|
Return the position (index) of a label in the variable’s domain. |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Methods inherited from DiscreteVariable
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
returns whether x belongs to the variable domain (if the variable is a |
|
For numerical discrete variables (all except |
|
Draw a random float value from the x-th interval of the variable (only for |
|
Returns the integer domain of the variable (if the variable is a |
|
Returns whether the variable is empirical (if the variable is a |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a tick for the variable |
|
Returns whether x is a value for the variable |
|
returns the maximum value of the variable domain (if the variable is a |
|
returns the minimum value of the variable domain (if the variable is a |
|
Returns the numerical domain of the variable (if the variable is a |
|
set the description of the variable. |
|
sets the name of the variable. |
|
Returns the x-th tick of the variable (if the variable is a |
|
Returns the ticks of the variable |
|
DiscretizedVariable
- class pyagrum.DiscretizedVariable(*args)
DiscretizedVariable is a discrete random variable with a set of
ticksdefining intervals.- DiscretizedVariable(aName, aDesc) -> DiscretizedVariable
Parameters:
aName (
str) – the name of the variableaDesc (
str) – the description of the variable
- DiscretizedVariable(aName, aDesc, ticks[, is_empirical]) -> DiscretizedVariable
Parameters:
aName (
str) – the name of the variableaDesc (
str) – the description of the variableticks (
list[float]) – the list of ticks (limits of the intervals, each tick at most once, otherwisepyagrum.DuplicateElement)is_empirical (
bool) – if False (default), a value out of the bounds raises an error; positional only (not a keyword argument)
- DiscretizedVariable(aDDRV) -> DiscretizedVariable
Parameters:
aDDRV (
pyagrum.DiscretizedVariable) – thepyagrum.DiscretizedVariablethat will be copied
- Raises:
pyagrum.InvalidArgument – if the name is empty or has an ASCII control character (a new line, a tabulation…)
Example
import pyagrum as gum
vX = gum.DiscretizedVariable("X", "X has been discretized").addTick(1).addTick(2).addTick(3).addTick(3.1415)
print(vX)
# X:Discretized(<[1;2[,[2;3[,[3;3.1415]>)
vX.isTick(4)
# False
vX.labels()
# ('[1;2[', '[2;3[', '[3;3.1415]')
vX.index("2.5") # where is the real value 2.5 ?
# 1
vY = gum.DiscretizedVariable("Y", "Y", [0, 0.5, 1], True) # empirical: values out of bounds are accepted
print(vY)
# Y:Discretized(<(0;0.5[,[0.5;1)>)
vY.index("7")
# 1
Methods
For numerical discrete variables (all except |
|
Allow to draw a value in the i-th interval of the discretized variable.1 |
|
erase all the Ticks |
|
Check whether the variable uses relaxed (empirical) tick bounds. |
|
Enable or disable empirical (relaxed bounds) mode for the variable. |
|
Indicate the index of the Tick |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Methods inherited from DiscreteVariable
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
returns whether x belongs to the variable domain (if the variable is a |
|
For numerical discrete variables (all except |
|
Returns the integer domain of the variable (if the variable is a |
|
returns whether s is a label of the variable |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a value for the variable |
|
returns the maximum value of the variable domain (if the variable is a |
|
returns the minimum value of the variable domain (if the variable is a |
|
Returns the numerical domain of the variable (if the variable is a |
|
Returns the index of the label |
|
set the description of the variable. |
|
sets the name of the variable. |
|
IntegerVariable
- class pyagrum.IntegerVariable(*args)
IntegerVariable is a discrete random variable with a customizable sequence of int.
- IntegerVariable(aName, aDesc=’’, values=None) -> IntegerVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the (optional) description of the variable
values (list of int) – the values to create (each value at most once, otherwise
pyagrum.DuplicateElement)
- IntegerVariable(aIDRV) -> IntegerVariable
- Parameters:
aIDRV (
pyagrum.IntegerVariable) – Thepyagrum.IntegerVariablethat will be copied
- Raises:
pyagrum.InvalidArgument – if the name is empty or has an ASCII control character (a new line, a tabulation…)
Example
import pyagrum as gum
# creating a variable with 3 values: 1, 34, 142
va = gum.IntegerVariable("a", "an integer variable", [1, 34, 142])
print(va)
# a:Integer({1|34|142})
print(va.addValue(25))
# a:Integer({1|25|34|142})
va.changeValue(34, 43)
print(va)
# a:Integer({1|25|43|142})
vb = gum.IntegerVariable("b", "b").addValue(34).addValue(142).addValue(1)
print(vb)
# b:Integer({1|34|142})
vb.labels()
# ('1', '34', '142')
Methods
Add a value to the list of values for the variable. |
|
For numerical discrete variables (all except |
|
For numerical discrete variables (all except |
|
Remove all the domain. |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Methods inherited from DiscreteVariable
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
returns whether x belongs to the variable domain (if the variable is a |
|
Draw a random float value from the x-th interval of the variable (only for |
|
Returns whether the variable is empirical (if the variable is a |
|
returns whether s is a label of the variable |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a tick for the variable |
|
returns the maximum value of the variable domain (if the variable is a |
|
returns the minimum value of the variable domain (if the variable is a |
|
Returns the numerical domain of the variable (if the variable is a |
|
Returns the index of the label |
|
set the description of the variable. |
|
sets the name of the variable. |
|
Returns the x-th tick of the variable (if the variable is a |
|
Returns the ticks of the variable |
|
RangeVariable
- class pyagrum.RangeVariable(*args)
RangeVariable represents a variable with a range of integers as domain.
- RangeVariable(aName, aDesc,minVal, maxVal) -> RangeVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the description of the variable
minVal (int) – the minimal integer of the interval
maxVal (int) – the maximal integer of the interval
- RangeVariable(aName, aDesc=’’) -> RangeVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the description of the variable
By default
minVal=0andmaxVal=1- RangeVariable(aRV) -> RangeVariable
- Parameters:
aDV (RangeVariable) – the
pyagrum.RangeVariablethat will be copied
- Raises:
pyagrum.InvalidArgument – if the name is empty or has an ASCII control character (a new line, a tabulation…)
Example
import pyagrum as gum
vI = gum.RangeVariable("I", "I in [4,10]", 4, 10)
print(vI)
# I:Range([4,10])
vI.maxVal()
# 10
vI.belongs(1)
# False
vI.index("5") # where is the value 5 ?
# 1
vI.labels()
# ('4', '5', '6', '7', '8', '9', '10')
Methods
For numerical discrete variables (all except |
|
Set a new value of the upper bound |
|
Set a new value of the lower bound |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Methods inherited from DiscreteVariable
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
For numerical discrete variables (all except |
|
Draw a random float value from the x-th interval of the variable (only for |
|
Returns the integer domain of the variable (if the variable is a |
|
Returns whether the variable is empirical (if the variable is a |
|
returns whether s is a label of the variable |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a tick for the variable |
|
Returns whether x is a value for the variable |
|
Returns the numerical domain of the variable (if the variable is a |
|
Returns the index of the label |
|
set the description of the variable. |
|
sets the name of the variable. |
|
Returns the x-th tick of the variable (if the variable is a |
|
Returns the ticks of the variable |
|
NumericalDiscreteVariable
- class pyagrum.NumericalDiscreteVariable(*args)
NumericalDiscreteVariable is a discrete random variable with a customizable sequence of float.
- NumericalDiscreteVariable(aName, aDesc=’’, values=None) -> NumericalDiscreteVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the (optional) description of the variable
values (list of float) – the values to create equivalent to fast syntax
{v1|v2|v3|...|vn}(each value at most once, otherwisepyagrum.DuplicateElement)
- NumericalDiscreteVariable(aName, aDesc=’’, first, last, nbr) -> NumericalDiscreteVariable
- Parameters:
aName (str) – the name of the variable
aDesc (str) – the (optional) description of the variable
first (float) – specify a list of floats from
firsttolastinnbrsteps.last (float) –
nbr (int) – equivalent to fast syntax
{first:last:nbr}
- NumericalDiscreteVariable(aNDRV) -> NumericalDiscreteVariable
- Parameters:
aNDRV (
pyagrum.NumericalDiscreteVariable) – Thepyagrum.NumericalDiscreteVariablethat will be copied
- Raises:
pyagrum.InvalidArgument – if the name is empty or has an ASCII control character (a new line, a tabulation…)
Example
import pyagrum as gum
# creating a variable with 3 values: 1.5, 3.14, 1.42
va = gum.NumericalDiscreteVariable("a", "a numerical variable", [1.5, 3.14, 1.42])
print(va)
# a:NumericalDiscrete({1.42|1.5|3.14})
print(va.addValue(2.01))
# a:NumericalDiscrete({1.42|1.5|2.01|3.14})
va.changeValue(3.14, 3.1415)
print(va)
# a:NumericalDiscrete({1.42|1.5|2.01|3.1415})
vb = gum.NumericalDiscreteVariable("b", "b").addValue(3.14).addValue(1.42).addValue(1.5)
print(vb)
# b:NumericalDiscrete({1.42|1.5|3.14})
vb.labels()
# ('1.42', '1.5', '3.14')
vc = gum.NumericalDiscreteVariable("c", "c", 1.2, 3.8, 5) # 5 values from 1.2 to 3.8
print(vc)
# c:NumericalDiscrete({1.2|1.85|2.5|3.15|3.8})
Methods
Add a value to the list of values for the variable. |
|
For numerical discrete variables (all except |
|
For numerical discrete variables (all except |
|
Remove all the domain. |
|
Return the variable description in fast syntax (e.g. |
|
returns the type of variable |
Methods inherited from DiscreteVariable
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
Returns a copy of the variable as a |
|
returns whether x belongs to the variable domain (if the variable is a |
|
Draw a random float value from the x-th interval of the variable (only for |
|
Returns the integer domain of the variable (if the variable is a |
|
Returns whether the variable is empirical (if the variable is a |
|
returns whether s is a label of the variable |
|
Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable. |
|
Returns whether x is a tick for the variable |
|
returns the maximum value of the variable domain (if the variable is a |
|
returns the minimum value of the variable domain (if the variable is a |
|
Returns the index of the label |
|
set the description of the variable. |
|
sets the name of the variable. |
|
Returns the x-th tick of the variable (if the variable is a |
|
Returns the ticks of the variable |
|