I have `N` i.i.d. observations of `p` categorical variables `X_1, ..., X_p`, where variable `X_j` takes `r_j` possible levels. The observations are stored as an `N x p` integer matrix `D` in which `D_{i, j} in {1, ..., r_j}`. I want to infer the directed acyclic graph (DAG) structure of a Bayesian network over these `p` variables together with the conditional probability tables.

My model is:

`G  ~  Uniform( DAGs on p nodes )`, restricted to graphs in which the in-degree of every node is at most `k_max`,

and for each node `j = 1, ..., p` with parent set `Pa_j` under `G` (`Pa_j` is a subset of `{1, ..., p} \ {j}`):

`theta_{j, pi, *} | G  ~  Dirichlet( alpha / (r_j q_j), ..., alpha / (r_j q_j) )`,
                                  for each parent configuration `pi`,
`X_{i, j} | Pa_j(i), theta, G  ~  Categorical( theta_{j, Pa_j(i), 1}, ..., theta_{j, Pa_j(i), r_j} )`,
                                  for `i = 1, ..., N`,

where `q_j = prod_{i in Pa_j} r_i` is the number of distinct configurations of the parents of `X_j` under `G` (with `q_j = 1` when `Pa_j` is empty), and `alpha > 0` is the equivalent sample size hyperparameter.

Here:
`N` is the sample size,
`p` is the number of variables,
`r_j` is the cardinality (number of levels) of variable `j`,
`D` is the `N x p` categorical data matrix with 1-indexed integer entries,
`Pa_j` is a subset of `{1, ..., p} \ {j}` (the parent set of `j` under `G`),
`q_j = prod_{i in Pa_j} r_i` is the parent-configuration count for `X_j` (`q_j = 1` if `Pa_j` is empty),
`theta_{j, pi, k}` is the conditional probability `P(X_j = k | parents in configuration pi)` under `G`,
`G` is the latent DAG,
`alpha > 0` is the Dirichlet equivalent sample size,
`k_max` is the maximum allowed in-degree per node.

Parameterization notes:
In `Dirichlet(a_1, ..., a_K)`, all `a_k > 0` and the density is on the `K`-simplex (probability vectors of length `K`).
In `Categorical(p_1, ..., p_K)`, `p_k >= 0` and `sum_k p_k = 1`.

Defaults: `alpha = 1`, `k_max = 3`.
