Tensor.expectedValue
- Tensor.expectedValue(*args)
Calculate the mathematical expected value of a (joint) random variable using the given function as an argument.
- Parameters:
func (
function(dict)->float) – A function that takes a single argument, representing the value of a python representation of apyagrum.Instantiation(as a dictionary), and returns a float.
Warning
The
pyagrum.Tensoris assumed to contain a joint distribution.- Returns:
The mathematical expected value of the random variable calculated using the given function as an argument.
- Return type:
float
Example
import math
import pyagrum as gum
x = gum.fastVariable("x{1|2|4}")
p = gum.Tensor().add(x).fillWith([0.2, 0.5, 0.3])
p.expectedValue(lambda d: x.numerical(d["x"])) # E[x]
# 2.4
round(p.expectedValue(lambda d: -math.log2(p[d])), 4) # the entropy of p (in bits)
# 1.4855
Class