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

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

belongs()

returns whether x belongs to the variable domain (if the variable is a pyagrum.RangeVariable)

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

description()

domain()

domainSize()

draw()

Draw a random float value from the x-th interval of the variable (only for pyagrum.DiscretizedVariable).

empty()

index()

integerDomain()

Returns the integer domain of the variable (if the variable is a pyagrum.IntegerVariable)

isEmpirical()

Returns whether the variable is empirical (if the variable is a pyagrum.DiscretizedVariable).

isLabel()

returns whether s is a label of the variable

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isTick()

Returns whether x is a tick for the variable

isValue()

Returns whether x is a value for the variable

label()

labels()

maxVal()

returns the maximum value of the variable domain (if the variable is a pyagrum.RangeVariable)

minVal()

returns the minimum value of the variable domain (if the variable is a pyagrum.RangeVariable)

name()

numerical()

numericalDomain()

Returns the numerical domain of the variable (if the variable is a pyagrum.NumericalDiscreteVariable)

posLabel()

Returns the index of the label s in the variable’s domain (if the variable is a pyagrum.LabelizedVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

stype()

tick()

Returns the x-th tick of the variable (if the variable is a pyagrum.DiscretizedVariable)

ticks()

Returns the ticks of the variable

toFast()

Return the variable description in fast syntax (e.g. name{val1|val2|...}, name[min,max]…).

toStringWithDescription()

varType()

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:
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

addLabel()

Add a label with a new index (we assume that we will NEVER remove a label).

changeLabel()

Change the label at the specified index

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

domain()

domainSize()

eraseLabels()

Erase all the labels from the variable.

index()

isLabel()

Indicates whether the variable already has the label passed in argument

label()

numerical()

posLabel()

Return the position (index) of a label in the variable’s domain.

stype()

toFast()

Return the variable description in fast syntax (e.g. name{val1|val2|...}).

varType()

returns the type of variable

Methods inherited from DiscreteVariable

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

belongs()

returns whether x belongs to the variable domain (if the variable is a pyagrum.RangeVariable)

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

description()

draw()

Draw a random float value from the x-th interval of the variable (only for pyagrum.DiscretizedVariable).

empty()

integerDomain()

Returns the integer domain of the variable (if the variable is a pyagrum.IntegerVariable)

isEmpirical()

Returns whether the variable is empirical (if the variable is a pyagrum.DiscretizedVariable).

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isTick()

Returns whether x is a tick for the variable

isValue()

Returns whether x is a value for the variable

labels()

maxVal()

returns the maximum value of the variable domain (if the variable is a pyagrum.RangeVariable)

minVal()

returns the minimum value of the variable domain (if the variable is a pyagrum.RangeVariable)

name()

numericalDomain()

Returns the numerical domain of the variable (if the variable is a pyagrum.NumericalDiscreteVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

tick()

Returns the x-th tick of the variable (if the variable is a pyagrum.DiscretizedVariable)

ticks()

Returns the ticks of the variable

toStringWithDescription()

DiscretizedVariable

class pyagrum.DiscretizedVariable(*args)

DiscretizedVariable is a discrete random variable with a set of ticks defining intervals.

DiscretizedVariable(aName, aDesc) -> DiscretizedVariable

Parameters:

  • aName (str) – the name of the variable

  • aDesc (str) – the description of the variable

DiscretizedVariable(aName, aDesc, ticks[, is_empirical]) -> DiscretizedVariable

Parameters:

  • aName (str) – the name of the variable

  • aDesc (str) – the description of the variable

  • ticks (list[float]) – the list of ticks (limits of the intervals, each tick at most once, otherwise pyagrum.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:

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

addTick()

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

domain()

domainSize()

draw()

Allow to draw a value in the i-th interval of the discretized variable.1

eraseTicks()

erase all the Ticks

index()

isEmpirical()

Check whether the variable uses relaxed (empirical) tick bounds.

isTick()

label()

numerical()

setEmpirical()

Enable or disable empirical (relaxed bounds) mode for the variable.

stype()

tick()

Indicate the index of the Tick

ticks()

toFast()

Return the variable description in fast syntax (e.g. name[t1,t2,t3], name+[t1,t2,t3] if empirical).

varType()

returns the type of variable

Methods inherited from DiscreteVariable

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

belongs()

returns whether x belongs to the variable domain (if the variable is a pyagrum.RangeVariable)

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

description()

empty()

integerDomain()

Returns the integer domain of the variable (if the variable is a pyagrum.IntegerVariable)

isLabel()

returns whether s is a label of the variable

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isValue()

Returns whether x is a value for the variable

labels()

maxVal()

returns the maximum value of the variable domain (if the variable is a pyagrum.RangeVariable)

minVal()

returns the minimum value of the variable domain (if the variable is a pyagrum.RangeVariable)

name()

numericalDomain()

Returns the numerical domain of the variable (if the variable is a pyagrum.NumericalDiscreteVariable)

posLabel()

Returns the index of the label s in the variable’s domain (if the variable is a pyagrum.LabelizedVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

toStringWithDescription()

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:
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

addValue()

Add a value to the list of values for the variable.

changeValue()

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

domain()

domainSize()

eraseValue()

eraseValues()

Remove all the domain.

index()

integerDomain()

isValue()

label()

numerical()

stype()

toFast()

Return the variable description in fast syntax (e.g. name{v1|v2|...}).

varType()

returns the type of variable

Methods inherited from DiscreteVariable

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

belongs()

returns whether x belongs to the variable domain (if the variable is a pyagrum.RangeVariable)

description()

draw()

Draw a random float value from the x-th interval of the variable (only for pyagrum.DiscretizedVariable).

empty()

isEmpirical()

Returns whether the variable is empirical (if the variable is a pyagrum.DiscretizedVariable).

isLabel()

returns whether s is a label of the variable

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isTick()

Returns whether x is a tick for the variable

labels()

maxVal()

returns the maximum value of the variable domain (if the variable is a pyagrum.RangeVariable)

minVal()

returns the minimum value of the variable domain (if the variable is a pyagrum.RangeVariable)

name()

numericalDomain()

Returns the numerical domain of the variable (if the variable is a pyagrum.NumericalDiscreteVariable)

posLabel()

Returns the index of the label s in the variable’s domain (if the variable is a pyagrum.LabelizedVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

tick()

Returns the x-th tick of the variable (if the variable is a pyagrum.DiscretizedVariable)

ticks()

Returns the ticks of the variable

toStringWithDescription()

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=0 and maxVal=1

RangeVariable(aRV) -> RangeVariable
Parameters:
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

belongs()

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

domain()

domainSize()

index()

label()

maxVal()

minVal()

numerical()

setMaxVal()

Set a new value of the upper bound

setMinVal()

Set a new value of the lower bound

stype()

toFast()

Return the variable description in fast syntax (e.g. name[n] or name[min,max]).

varType()

returns the type of variable

Methods inherited from DiscreteVariable

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

description()

draw()

Draw a random float value from the x-th interval of the variable (only for pyagrum.DiscretizedVariable).

empty()

integerDomain()

Returns the integer domain of the variable (if the variable is a pyagrum.IntegerVariable)

isEmpirical()

Returns whether the variable is empirical (if the variable is a pyagrum.DiscretizedVariable).

isLabel()

returns whether s is a label of the variable

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isTick()

Returns whether x is a tick for the variable

isValue()

Returns whether x is a value for the variable

labels()

name()

numericalDomain()

Returns the numerical domain of the variable (if the variable is a pyagrum.NumericalDiscreteVariable)

posLabel()

Returns the index of the label s in the variable’s domain (if the variable is a pyagrum.LabelizedVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

tick()

Returns the x-th tick of the variable (if the variable is a pyagrum.DiscretizedVariable)

ticks()

Returns the ticks of the variable

toStringWithDescription()

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, otherwise pyagrum.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 first to last in nbr steps.

  • last (float) –

  • nbr (int) – equivalent to fast syntax {first:last:nbr}

NumericalDiscreteVariable(aNDRV) -> NumericalDiscreteVariable
Parameters:
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

addValue()

Add a value to the list of values for the variable.

changeValue()

closestIndex()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the index of the closest value to a given float value in the variable’s domain.

closestLabel()

For numerical discrete variables (all except pyagrum.LabelizedVariable), this method returns the label of the closest value to a given float value in the variable’s domain.

domain()

domainSize()

eraseValue()

eraseValues()

Remove all the domain.

index()

isValue()

label()

numerical()

numericalDomain()

stype()

toFast()

Return the variable description in fast syntax (e.g. name{v1|v2|...}).

varType()

returns the type of variable

Methods inherited from DiscreteVariable

asDiscretizedVar()

Returns a copy of the variable as a pyagrum.DiscretizedVariable.

asIntegerVar()

Returns a copy of the variable as a pyagrum.IntegerVariable.

asLabelizedVar()

Returns a copy of the variable as a pyagrum.LabelizedVariable.

asNumericalDiscreteVar()

Returns a copy of the variable as a pyagrum.NumericalDiscreteVariable.

asRangeVar()

Returns a copy of the variable as a pyagrum.RangeVariable.

belongs()

returns whether x belongs to the variable domain (if the variable is a pyagrum.RangeVariable)

description()

draw()

Draw a random float value from the x-th interval of the variable (only for pyagrum.DiscretizedVariable).

empty()

integerDomain()

Returns the integer domain of the variable (if the variable is a pyagrum.IntegerVariable)

isEmpirical()

Returns whether the variable is empirical (if the variable is a pyagrum.DiscretizedVariable).

isLabel()

returns whether s is a label of the variable

isNumerical()

Returns True if the variable has a meaningful numerical representation, i.e. it is not a LabelizedVariable.

isTick()

Returns whether x is a tick for the variable

labels()

maxVal()

returns the maximum value of the variable domain (if the variable is a pyagrum.RangeVariable)

minVal()

returns the minimum value of the variable domain (if the variable is a pyagrum.RangeVariable)

name()

posLabel()

Returns the index of the label s in the variable’s domain (if the variable is a pyagrum.LabelizedVariable)

setDescription()

set the description of the variable.

setName()

sets the name of the variable.

tick()

Returns the x-th tick of the variable (if the variable is a pyagrum.DiscretizedVariable)

ticks()

Returns the ticks of the variable

toStringWithDescription()