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

vX=gum.LabelizedVariable("X","X",0).addLabel("No").addLabel("Yes")

print(vX.domain())
## X<No,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
## <pyAgrum.pyAgrum.LabelizedVariable;...>
print(va)
## a<0,1,2,foo>

va.chgLabel(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>

vb.labels()
## ('A', 'B', 'C')

vb.isLabel('E')
## False

vb.label(2)
## 'B'
  • 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(2).addTick(3).addTick(3.1415) #doctest: +ELLIPSIS
## <pyAgrum.pyAgrum.DiscretizedVariable;...>
print(vX)
## X<[1;2[,[2;3[,[3;3.1415]>

vX.isTick(4)
## False

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

# where is the real value 2.5 ?
vX.index('2.5')
## 1
  • Range variables

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

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

vX.maxVal()
## 10

vX.belongs(1)
## False

# where is the value 5 ?
vX.index('5')
## 1

vi.labels()
## ('4', '5', '6', '7', '8', '9', '10')