Implement the Inference-by-Enumeration algorithm in Python

Implement the inference_by_enumeration function for a generic probabilistic query of the form π‘ƒ(π—βˆ£π„)P(X∣E). Note that this version of the Inference-by-Enumeration algorithm computes the probabilistic query for all possible assignments to the evidence variables, not only for one specific assignment (cf. slide deck: Probabilistic Models – Part 2: Fundamental Concepts and Notation, p. 40). The function must return one object:

  • The answer to the probabilistic query, which is a np.ndarray with the same number of dimensions and the same variable order as the FJDT, but not the same size: The dimensions of non-query and non-evidence variables (𝐙Z) must be converted to singleton dimensions, i.e., dimensions of size one.

For example, if we have a full joint distribution table of three binary variables (shape 2Γ—2Γ—22Γ—2Γ—2) and we ask for the distribution of the first variable given the second variable, the resulting conditional distribution table would be of shape 2Γ—2Γ—12Γ—2Γ—1.

Hint: Remember to solve this without a for loop. Set the keepdims parameter of NumPy’s sum method to True to not discard the reduced dimensions. Keeping these empty dimensions simplifies broadcasting operations to a no-brainer.

CODE:

def inference_by_enumeration(FJDT: np.ndarray, query_variable_indices: tuple, evidence_variable_indices: tuple=tuple()) -> np.ndarray:

”’

Computes the answer to a probabilistic query exactly from the full joint distribution table.

:param table: The full joint distribution table as a np.ndarray.

:param query_variable_indices: A tuple containing the indices of the query variables in the FJDT.

:param evidence_variable_indices: A tuple containing the indices of the evidence variables in the FJDT.

:returns: The answer to the probabilistic query; a `np.ndarray`.

”’

assert type(FJDT) == np.ndarray, “FJDT must be a np.ndarray”

assert type(query_variable_indices) == tuple, “query_variable_indices must be a tuple”

assert type(evidence_variable_indices) == tuple, “evidence_variable_indices must be a tuple”

# compute the set of non-query and non-evidence variables, Z

query_variables = query_variable_indices + evidence_variable_indices

Z = tuple(set(range(FJDT.ndim)).difference(query_variables))

# YOUR CODE HERE

raise NotImplementedError()

× How can I help you?