Solved – Array of samples from multivariate gaussian distribution Python

I am trying to build in Python the scatter plot in part 2 of Elements of Statistical Learning. First it is said to generate

10 means mk
from a bivariate Gaussian distribution N((1,0)T,I) and labeled this class
BLUE. Similarly, 10 more were drawn from N((0,1)T,I) and labeled class
ORANGE.

I draw one such mean from bivariate gaussian using

from numpy.random import multivariate_normal as mvnorm
cov = [[1, 0], [0, 1]]
mean_or = [1,0]
mean_bl = [0,1]
m_or = [0]*10
m_bl = [0]*10
for i in range(10):
m_or[i] = list(mvnorm(mean_or,cov))
m_bl[i] = list(mvnorm(mean_bl,cov))

Is there a more elegant way to generate a vector of 10 means in python, a one-liner replacing the for-loop here ?

Side question : what difference between drawing samples from $N((1,0)T,I)$ and $N((0,1)T,I)$ ?

As far as I can tell you are drawing samples from that distribution rather than estimates of the mean. I'm not sure if this is what you want to be doing. If you just want to draw samples a simple way would be

from scipy.stats import multivariate_normal import numpy as np  n_samps_to_draw = 10  mvn(mean=[0,1],cov=np.eye(2)).rvs(n_samps_to_draw) 

alternatively, you could just go

n_samps_to_draw = 10 m_or = np.random.multivariate_normal([0,1],np.eye(2),n_samps_to_draw) m_bl = np.random.multivariate_normal([1,0],np.eye(2),n_samps_to_draw) 

if you wanted to sample 10 measurements of the mean, you could just run

from scipy.stats import multivariate_normal import numpy as np n_samples_to_est_mean = 500 n_mean_ests = 10  [np.mean(mvn(mean=[0,1],cov=np.eye(2)).rvs(n_samples_to_est_mean),axis=0) for _ in range(n_mean_ests)] 

or again with just numpy

import numpy as np n_samples_to_est_mean = 500 n_mean_ests = 10 [np.mean(np.random.multivariate_normal([0,1],np.eye(2), n_samples_to_est_mean),axis=0) for _ in range(n_mean_ests)] 

Similar Posts:

Rate this post

Leave a Comment

Solved – Array of samples from multivariate gaussian distribution Python

I am trying to build in Python the scatter plot in part 2 of Elements of Statistical Learning. First it is said to generate

10 means mk
from a bivariate Gaussian distribution N((1,0)T,I) and labeled this class
BLUE. Similarly, 10 more were drawn from N((0,1)T,I) and labeled class
ORANGE.

I draw one such mean from bivariate gaussian using

from numpy.random import multivariate_normal as mvnorm
cov = [[1, 0], [0, 1]]
mean_or = [1,0]
mean_bl = [0,1]
m_or = [0]*10
m_bl = [0]*10
for i in range(10):
m_or[i] = list(mvnorm(mean_or,cov))
m_bl[i] = list(mvnorm(mean_bl,cov))

Is there a more elegant way to generate a vector of 10 means in python, a one-liner replacing the for-loop here ?

Side question : what difference between drawing samples from $N((1,0)T,I)$ and $N((0,1)T,I)$ ?

Best Answer

As far as I can tell you are drawing samples from that distribution rather than estimates of the mean. I'm not sure if this is what you want to be doing. If you just want to draw samples a simple way would be

from scipy.stats import multivariate_normal import numpy as np  n_samps_to_draw = 10  mvn(mean=[0,1],cov=np.eye(2)).rvs(n_samps_to_draw) 

alternatively, you could just go

n_samps_to_draw = 10 m_or = np.random.multivariate_normal([0,1],np.eye(2),n_samps_to_draw) m_bl = np.random.multivariate_normal([1,0],np.eye(2),n_samps_to_draw) 

if you wanted to sample 10 measurements of the mean, you could just run

from scipy.stats import multivariate_normal import numpy as np n_samples_to_est_mean = 500 n_mean_ests = 10  [np.mean(mvn(mean=[0,1],cov=np.eye(2)).rvs(n_samples_to_est_mean),axis=0) for _ in range(n_mean_ests)] 

or again with just numpy

import numpy as np n_samples_to_est_mean = 500 n_mean_ests = 10 [np.mean(np.random.multivariate_normal([0,1],np.eye(2), n_samples_to_est_mean),axis=0) for _ in range(n_mean_ests)] 

Similar Posts:

Rate this post

Leave a Comment