Types of variables

Posted on lun. 17 avril 2017 in pyAgrum

There are 3 types of discrete random variables in aGrUM/pyAgrum : LabelizedVariable, DiscretizedVariable and RangeVariable. The 3 types are mainly provided in order to ease the modelisation. Derived from DiscreteVariable, they share a common API

>>> print(vX.domain())
<No,Yes>

>>> vX.labels()
('No', 'Yes')

>>> vX.isLabel('Maybe')
False

>>> vX.label(1)
'Yes'

They essentially differ by the means to create, name and access to their modalities.

  • Labelized variables

gum.LabelizedVariable represents a variable with a list of labels that can be managed.

# creating a variable with 3 labels : '0', '1' and '2'
>>> va=gum.LabelizedVariable("a","a labelized variable  'a'",3)
>>> print(va)
a<0,1,2>

>>> va.addLabel("foo") #doctest: +ELLIPSIS
(gum::LabelizedVariable@...) a<0,1,2,foo>

>>> print(va)
a<0,1,2,foo>

>>> va.changeLabel(1,"bar")
>>> print(va)
a<0,bar,2,foo>

>>> vb=gum.LabelizedVariable("b","b",0).addLabel('A').addLabel('B').addLabel('C')
>>> print(vb)
b<A,B,C>

# faster (and newer)
>>> vc=gum.LabelizedVariable("c","c",['X','Y','Z'])
>>> print(vc)
c<X,Y,Z>
  • Discretized variables

gum.DiscretizedVariable represents a variable with a list of ticks that discretize the variable domain.

>>> vX=gum.DiscretizedVariable("X","X has been discretized")
>>> vX.addTick(1).addTick(3.1415).addTick(2).addTick(3) #doctest: +ELLIPSIS
(gum::DiscretizedVariable<double>@...) X<[1;2[,[2;3[,[3;3.1415]>
>>> print(vX)
X<[1;2[,[2;3[,[3;3.1415]>

>>> vX.isTick(4)
False

>>> vX.ticks()
(1.0, 2.0, 3.0, 3.1415)

>>> vX.labels()
('[1;2[', '[2;3[', '[3;3.1415]')

# In which class  is the real value 2.5 ?
>>> vX.index('2.5')
1

#faster (and newer) creation for Discretized
>>> vY=gum.DiscretizedVariable("X","X has been discretized",[1,2,3.14,3])
>>> print(vY)
X<[1;2[,[2;3[,[3;3.14]>
  • Range variables

gum.RangeVariable represents a variable with a range of integers as domain.

>>> vI=gum.RangeVariable("I","I in [4,10]",4,10)
>>> print(vI)
I[4-10]

>>> vI.maxVal()
10

>>> vI.belongs(1)
False

# which class for value 5 ?
>>> vI.index('5')
1

>>> vI.labels()
('4', '5', '6', '7', '8', '9', '10')