Short description of the encountered problem
I am getting an error when using a MultinomialDistribution
to encode a conditional probability distribution (CPD) within the joint probability distribution of a Bayesian Network while it works completely fine as a standalone distribution. A bug?
A simple Bayesian Network taken from Daphne Koller's book on PGM
As a minimal example, I am trying to encode the first three nodes of the Bayesian Network that is given on page 53 in Probabilistic Graphical Models (2009), by Daphne Koller and Nir Friedman:
Encoding the joint Probability Distribution P(I,D,G)
I am encoding the Bayesian Network given by the nodes Difficulty (easy,hard) of the class taken by a student, Intelligence (low,high) of the student and Grade (g1=A, g2=B, g3=C) achieved in the class as follows:
distI = BernoulliDistribution[ 0.3 ]; (* probability of high intelligence *)
distD = BernoulliDistribution[ 0.4 ]; (* probability of hard class *)
grade = Function[ { g1, g2, g3 },
{ g1, g2, g3 }.Range[3]
]; (* map the vector of grades onto the set {1,2,3} *)
(* conditional distribution of the grade *)
cpdG = Function[ { i, d },
With[
{
n = 1,
p = Piecewise[
{
{ { 0.3, 0.4, 0.3 }, i == 0 && d == 0 },
{ { 0.05, 0.25, 0.7 }, i == 0 && d == 1 },
{ { 0.9, 0.08, 0.02 }, i == 1 && d == 0 },
{ { 0.5, 0.3, 0.2 }, i == 1 && d == 1 }
}
]
},
TransformedDistribution[
grade @@ {g1,g2,g3},
{g1,g2,g3} \[Distributed] MultinomialDistribution[ n, p ]
]
]
];
(* the joint distribution P(I,D,G) as given by the chain rule *)
distB = ProbabilityDistribution[
PDF[ distI, i] PDF[ distD, d] PDF[ cpdG[ i, d ], g ],
{i, 0, 1, 1},
{d, 0, 1, 1},
{g, 1, 3, 1}
]
But this will give the follwing error message (repeatedly):
Note, that the conditional distribution nicely works on its own:
PDF[ cpdG[1, 1], 2 ]
0.3
What is the problem here?
Update
The MultinomialDistribution
can be replaced with the EmpiricalDistribution
, which avoids some problems:
(* conditional distribution of the grade *)
cpdG = Function[ { i, d },
With[
{
p = Piecewise[
{
{ { 0.3, 0.4, 0.3 }, i == 0 && d == 0 },
{ { 0.05, 0.25, 0.7 }, i == 0 && d == 1 },
{ { 0.9, 0.08, 0.02 }, i == 1 && d == 0 },
{ { 0.5, 0.3, 0.2 }, i == 1 && d == 1 }
}
]
},
EmpiricalDistribution[ p -> Range[3] ]
]
];
Then we do probabilistic inference using Probability
:
Probability[ g == 1 \[Conditioned] i == 1, {i,d,g} \[Distributed] distB ]
0.74
But to my disappointment this will fail for the full Bayesian Network -- but that should probably go into a separate question...
BernoulliDisztribution
is apparently misspelled. $\endgroup$