python sampling from different distributions with different probability
<p>I am trying to implement a fucntion which returns 100 samples from three different multivariate gaussian distributions.</p>

<p>numpy provides a way to sample from a sinle multivariate gaussian. But I could not find a way to sample from three different multivariate with different sampling probability.</p>

<p>My requirement is to sample with probability $[0.7, 0.2, 0.1]$ from three multivariate gaussians with mean and covariances as given below</p>

<pre><code>G_1  mean = [1,1] cov =[ [ 5, 1] [1,5]]
G_2  mean = [0,0] cov =[ [ 5, 1] [1,5]]
G_3  mean = [-1,-1] cov =[ [ 5, 1] [1,5]]
</code></pre>

<p>Any idea ?</p>

<p>Say you create an array of your generators:</p>

<pre><code>generators = [
    np.random.multivariate_normal([1, 1], [[5, 1], [1, 5]]),             
    np.random.multivariate_normal([0, 0], [[5, 1], [1, 5]]), 
    np.random.multivariate_normal([-1, -1], [[5, 1], [1, 5]])]
</code></pre>

<p>Now you can create a weighted random of generator indices, since <a href="http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.random.choice.html" rel="nofollow"><code>np.random.choice</code></a> supports weighted sampling:</p>

<pre><code>draw = np.random.choice([0, 1, 2], 100, p=[0.7, 0.2, 0.1])
</code></pre>

<p>(<code>draw</code> is a length-100 array of entries, each from <em>{0, 1, 2}</em> with probability <em>0.7, 0.2, 0.1</em>, respectively.)</p>

<p>Now just generate the samples:</p>

<pre><code>[generators[i] for i in draw]
</code></pre>

python numpy probability sampling