A directed graphical model (Bayes net) over discrete variables defines the joint distribution as a product of one conditional probability table (CPT) per node:
Implement bn_query(parents, cpts, query, evidence) that returns the exact posterior by enumerating every joint assignment:
parents: dict mapping each variable name to the list of its parent names (in a fixed order).cpts: dict mapping each variable name to a numpy array. For a variable with parents the array has shape and cpts[v][j1, ..., jm, k] . A root node has shape . States are integers ; the cardinality of a variable is the last dimension of its CPT.query: one variable name; evidence: dict {name: state} (possibly empty, never containing query).Return a 1-D numpy array of length that sums to one. You may assume the evidence has positive probability.
Example: in the classic water-sprinkler network, and .