Generate sample data from Gaussian mixture model [duplicate]

I am given the values for mean, co-variance, initial_weights for a mixture of Gaussian Models. Now how can I generate samples given those: In brief, I need a function like

X = GMMSamples(W, mu, sigma, d)


where W: weight vector, mu - mean vector, sigma - covariance vector, d - dimensions of samples How can I implement it in python ? I found scipy library that has GaussianMixture library. It basically takes input as sample values and calculate itself mean, co-variance. But for my case it is almost reverse. I am given mean, co-variance, and parameters mentioned above and I need to generate sample data values. Thank you.

• Note that asking for code is off topic here. Oct 31 '16 at 15:41

Sampling from mixture distribution is super simple, the algorithm is as follows:

1. Sample $I$ from categorical distribution parametrized by vector $\boldsymbol{w} = (w_1,\dots,w_d)$, such that $w_i \ge 0$ and $\sum_i w_i = 1$.
2. Sample $x$ from normal distribution parametrized by $\mu_I$ and $\sigma_I$.

This thread on StackOverflow describes how to sample from categorical distribution.

• Tim, can you please elaborate more ? Oct 31 '16 at 15:04
• What is unclear for you?
– Tim
Oct 31 '16 at 15:04
• Sample I from categorical distribution means what ? What is the role of value I ? Oct 31 '16 at 15:08
• @Shyamkkhadka the idea of a mixture is that you have $d$ components, each appearing with probability $w_i$, so $I$ is a way of saying "take $I$-th component with probability $w_i$", what follows from the definition of mixture distribution.