how can I generate a Bernoulli block mixture model in matlab? I am trying to write the code of a Bernoulli block mixture model in matlab, but am facing an error every time I run the function. In particular, I'm having a problem with how to relate the distribution parameter $\alpha$ to the latent variables $Z$ and $W$.

In other words, \mathbf{Z} follows a multinomial distribution of parameter $\pi$, such that $\dim(\pi)=(1, g)$, where $g$ is the number of clusters of rows and $\sum \pi=1$. 

$W$ follows a multinomial distribution of parameter $\rho$ such that $\dim(\rho)=(1,m)$, where $m$ is the number of column clusters and $\sum \rho=1$.

$\mathbf{Z}$ is a matrix of $\dim(N,g)$, and $\mathbf{W}$ is a matrix of $\dim(d,m)$, where $N$ is the number of observations, $p$ is the number of variables, $g$ is the number of row clusters, and $m$ is the number of column clusters.

$X$ follows a bernoulli distribution of parameter $\alpha_{ZW}$, where the notation $\alpha_{ZW}$ denotes the values of $\alpha$ depends on $\mathbf{Z}$ and $\mathbf{W}$, and $\alpha$ is a $\dim(g,m)$ matrix. Please find attached a graphical representation of the model.

How do I write the code of $\alpha_{ZW}$ in order to get generate the model?
First, $x_{ij}$ is not a bernoulli random variable. In the text, $x_{ij}$ is described as real valued. Also, the dimensions of $z$ and $w$ are incorrect.

The text indicates that we're drawing a matrix of data call it $X (n\times d)$ such that $X_{i,j}=x_{ij}$.

A particular entry has distribution $x_{ij} \sim f(\alpha_{z_i, w_j})$. Where $\alpha$ is some given matrix of parameters. The maximum row index of $\alpha$ is $g$. The maximum column index of $\alpha$ is $m$. So $\alpha$ is $g \times m$.

All that remains is to pin down the indices $z$ and $w$. These are vectors rather than matrices since they have a categorical distribution, for example $z_i$ should be an integer between $1$ and $g$.

Putting this together we need to:


Draw $z$, a vector of length $n$, by $z_i \sim \text{Cat}(\pi), i=1,...,n$
Draw $w$, a vector of length $d$, by $w_j \sim \text{Cat}(\rho), i=1,...,d$
Draw $X$, an $(n\times d)$ matrix, by $x_{ij} \sim f(\alpha_{z_i, w_j})$