1
$\begingroup$

I am trying to generate a plot of points randomly sampled from a 2D elliptical distribution. I want to control the length and orientation of the ellipse this random sample creates. It seems like modifying covariance is the way to do this.

Here's what I've got:

mean = [0, 0]
cov = np.array([[1, 0],
               [0 ,1]])
points = points = np.random.multivariate_normal(mean, cov, size=10000)
sns.scatterplot(x=points[:,0], y=points[:,1], size=0.01, label='x ~ Normal')

which yields a spherical, normal distribution.

My own research: From the research I've done so far (https://en.wikipedia.org/wiki/Covariance_matrix), the covariance matrix has on its diagonal the variance of each individual element.

Each non-diagonal entry contains a value that describes how as one variable (x) varies from the sample mean (mu_x), how far does the other variable (y) stray from it's sample mean(mu_y).

So in my mind, it's the non-diagonal entries that dictate the length and orientation of my ellipse. But I always get an warning when I make up my own covariance matrix:

RuntimeWarning: covariance is not positive-semidefinite.

Then I tried covariance values (non-diagonal) that are < 1, and the error goes away. I see nothing in the formula that would imply that covariance should be < 1:

 σ(x,y)=E[(x−E(x))(y−E(y))]

However, I read a proof here that the covariance matrix is always positive semidefinite: https://stats.stackexchange.com/a/53105/275052, so that makes sense. I just don't know how to guarantee that a random covariance matrix I choose meets this requirement.

At the end of all this random information is this question:

How do I control the shape of my ellipse while keeping the matrix positive semidefinite?

$\endgroup$
1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.