In pyAgrum, Potentials represent multi-dimensionnal arrays with (discrete) random variables attached to each dimension. This mathematical object have tensorial operators w.r.t. to the variables attached.
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
a,b,c=[gum.LabelizedVariable(s,s,2) for s in "abc"]
p1=gum.Potential().add(a).add(b).fillWith([1,2,3,4]).normalize()
p2=gum.Potential().add(b).add(c).fillWith([4,5,2,3]).normalize()
gnb.flow.row(p1,p2,p1+p2,
captions=['p1','p2','p1+p2'])
|
| |
---|---|---|
0.1000 | 0.2000 | |
0.3000 | 0.4000 |
|
| |
---|---|---|
0.2857 | 0.3571 | |
0.1429 | 0.2143 |
|
| ||
---|---|---|---|
| 0.3857 | 0.6571 | |
0.2429 | 0.5143 | ||
| 0.4857 | 0.7571 | |
0.3429 | 0.6143 |
p3=p1+p2
gnb.showPotential(p3/p3.margSumOut(["b"]))
|
| ||
---|---|---|---|
| 0.3699 | 0.3208 | |
0.3908 | 0.3582 | ||
| 0.6301 | 0.6792 | |
0.6092 | 0.6418 |
p4=gum.Potential()+p3
gnb.flow.row(p3,p4,
captions=['p3','p4'])
|
| ||
---|---|---|---|
| 0.3857 | 0.6571 | |
0.2429 | 0.5143 | ||
| 0.4857 | 0.7571 | |
0.3429 | 0.6143 |
|
| ||
---|---|---|---|
| 1.3857 | 1.6571 | |
1.2429 | 1.5143 | ||
| 1.4857 | 1.7571 | |
1.3429 | 1.6143 |
bn=gum.fastBN("a->c;b->c",3)
bn
In such a small bayes net, we can directly manipulate $P(a,b,c)$. For instance : $$P(b|c)=\frac{\sum_{a} P(a,b,c)}{\sum_{a,b} P(a,b,c)}$$
pABC=bn.cpt("a")*bn.cpt("b")*bn.cpt("c")
pBgivenC=(pABC.margSumOut(["a"])/pABC.margSumOut(["a","b"]))
pBgivenC.putFirst("b") # in order to have b horizontally in the table
|
|
| |
---|---|---|---|
0.6233 | 0.1885 | 0.1881 | |
0.7019 | 0.1733 | 0.1248 | |
0.5082 | 0.3618 | 0.1301 |
Let's compute the joint probability $P(A,B)$ from $P(A,B,C)$
pAC=pABC.margSumOut(["b"])
print("pAC really is a probability : it sums to {}".format(pAC.sum()))
pAC
pAC really is a probability : it sums to 1.0
|
|
| |
---|---|---|---|
0.2214 | 0.0697 | 0.0729 | |
0.2349 | 0.1508 | 0.0829 | |
0.0417 | 0.0558 | 0.0699 |
pAC.margSumOut(["c"])
|
|
|
---|---|---|
0.4979 | 0.2763 | 0.2257 |
It is easy to compute $p(A, C=1)$
pAC.extract({"c":1})
|
|
|
---|---|---|
0.2349 | 0.1508 | 0.0829 |
Moreover, we know that $P(C=1)=\sum_A P(A,C=1)$
pAC.extract({"c":1}).sum()
0.46857414425142496
Now we can compute $p(A|C=1)=\frac{P(A,C=1)}{p(C=1)}$
pAC.extract({"c":1}).normalize()
|
|
|
---|---|---|
0.5012 | 0.3218 | 0.1770 |
$P(A|C)$ is represented by a matrix that verifies $p(A|C)=\frac{P(A,C)}{P(C}$
pAgivenC=(pAC/pAC.margSumIn("c")).putFirst("a")
# putFirst("a") : to correctly show a cpt, the first variable have to bethe conditionned one
gnb.flow.row(pAgivenC,pAgivenC.extract({'c':1}),
captions=["$P(A|C)$","$P(A|C=1)$"])
|
|
| |
---|---|---|---|
0.6081 | 0.1915 | 0.2003 | |
0.5012 | 0.3218 | 0.1770 | |
0.2492 | 0.3335 | 0.4172 |
|
|
|
---|---|---|
0.5012 | 0.3218 | 0.1770 |
A likelihood can also be found in this matrix.
pAgivenC.extract({'a':2})
|
|
|
---|---|---|
0.2003 | 0.1770 | 0.4172 |
A likelihood does not have to sum to 1. It is not relevant to normalize it.
pAgivenC.margSumIn(["a"])
|
|
|
---|---|---|
1.3586 | 0.8468 | 0.7946 |
%matplotlib inline
from pylab import *
import matplotlib.pyplot as plt
import numpy as np
p1=gum.Potential().add(a)
x = np.linspace(0, 1, 100)
plt.plot(x,[p1.fillWith([p,1-p]).entropy() for p in x])
plt.show()
t=gum.LabelizedVariable('t','t',3)
p1=gum.Potential().add(t)
def entrop(bc):
"""
bc is a list [a,b,c] close to a distribution
(normalized just to be sure)
"""
return p1.fillWith(bc).normalize().entropy()
import matplotlib.tri as tri
corners = np.array([[0, 0], [1, 0], [0.5, 0.75**0.5]])
triangle = tri.Triangulation(corners[:, 0], corners[:, 1])
# Mid-points of triangle sides opposite of each corner
midpoints = [(corners[(i + 1) % 3] + corners[(i + 2) % 3]) / 2.0 \
for i in range(3)]
def xy2bc(xy, tol=1.e-3):
"""
From 2D Cartesian coordinates to barycentric.
"""
s = [(corners[i] - midpoints[i]).dot(xy - midpoints[i]) / 0.75 \
for i in range(3)]
return np.clip(s, tol, 1.0 - tol)
def draw_entropy(nlevels=200, subdiv=6, **kwargs):
import math
refiner = tri.UniformTriRefiner(triangle)
trimesh = refiner.refine_triangulation(subdiv=subdiv)
pvals = [entrop(xy2bc(xy)) for xy in zip(trimesh.x, trimesh.y)]
plt.tricontourf(trimesh, pvals, nlevels, **kwargs)
plt.axis('equal')
plt.xlim(0, 1)
plt.ylim(0, 0.75**0.5)
plt.axis('off')
draw_entropy()
plt.show()