# Conditional sampling from a multivariate Gaussian Mixture

I am using scikit-learn to fit a gaussian mixture on a non-parametric multivariate distribution with three variables $\mathbf{X} = (X_1, X_2, X_3)$

I want to sample from that distribution given specific values for $X_1, X_2$, i.e. I want to sample from the conditional distribution of $X_3$ given $X_1$ and $X_2$. Similarly, I also want to sample values $X_1,X_2$ given $X_3$.

Is this possible with the GaussianMixture class? If not, how would I go about writing a function like conditional_sample(X,size) where X is a vector of fixed values?

One way to do this is to sample the cluster assignment (call it $$z$$) from the not-quite-a-full-posterior $$P(Z|X_1, X_2)$$ and then sample $$X_3$$ from $$P(X_3 | Z=z, X_1, X_2)$$. The second task involves just one cluster, and it's covered extensively elsewhere -- look for "multivariate Gaussian conditional distributions". The first task is the same as the usual prediction task for "which cluster is $$X$$ from", but you get the cluster probabilities ignoring $$X_3$$. This is how I would do it. Bayes' Rule says that if you have a prior $$P(Z)$$ on your cluster assignments, then
$$P(Z=z | X_{-k}) = \frac{P(X_{-k} | Z=z)P(Z=z)}{\sum_z P(X_{-k} | Z=z)P(Z=z)}$$
. If cluster $$z$$ has mean $$\mu$$ and covariance $$\Sigma$$ (paying attention to only variables 1 & 2), then you need to compute $$P(X_1, X_2 | Z=z) = \frac{1}{\sqrt{\det(2\pi\Sigma)}}\exp[\frac{-(x-\mu)^T\Sigma^{-1}(x-\mu)}{2}]$$.