Rotate covariance matrix
Question:
I am generating 3D gaussian point clouds. I’m using the scipy.stats.multivariate.normal() function, which takes a mean value and a covariance matrix as arguments. It can then provide random samples using the rvs() method.
Next I want to perform a rotation of the cloud in 3D, but rather than rotate each point I would like to rotate the random variable parameters, and then regenerate the point cloud.
I’m really struggling to figure this out. After an rotation, the axes of variance will no longer align with the coordinate system. So I guess what what I want is to express variance along three arbitrary orthogonal axes.
Thank you for any help.
Final edit: Thank you, I got what I needed. Below is an example
cov = np.array([
[ 3.89801357, 0.38668784, 1.47657614],
[ 0.38668784, 0.87396495, 1.43575688],
[ 1.47657614, 1.43575688, 15.09192414]])
rotation_matrix = np.array([
[ 2.22044605e-16, 0.00000000e+00, 1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[-1.00000000e+00, 0.00000000e+00, 2.22044605e-16]]) # 90 degrees around y axis
new_cov = rotation_matrix @ cov @ rotation_matrix.T # based on Warren and Paul's comments
rv = scipy.stats.multivariate_normal(mean=mean,cov=new_cov)
If you get an error
ValueError: the input matrix must be positive semidefinite
This page I found useful
Answers:
I edited the question with the answer, but again it is
new_cov = rotation_matrix @ cov @ rotation_matrix.T
I am generating 3D gaussian point clouds. I’m using the scipy.stats.multivariate.normal() function, which takes a mean value and a covariance matrix as arguments. It can then provide random samples using the rvs() method.
Next I want to perform a rotation of the cloud in 3D, but rather than rotate each point I would like to rotate the random variable parameters, and then regenerate the point cloud.
I’m really struggling to figure this out. After an rotation, the axes of variance will no longer align with the coordinate system. So I guess what what I want is to express variance along three arbitrary orthogonal axes.
Thank you for any help.
Final edit: Thank you, I got what I needed. Below is an example
cov = np.array([
[ 3.89801357, 0.38668784, 1.47657614],
[ 0.38668784, 0.87396495, 1.43575688],
[ 1.47657614, 1.43575688, 15.09192414]])
rotation_matrix = np.array([
[ 2.22044605e-16, 0.00000000e+00, 1.00000000e+00],
[ 0.00000000e+00, 1.00000000e+00, 0.00000000e+00],
[-1.00000000e+00, 0.00000000e+00, 2.22044605e-16]]) # 90 degrees around y axis
new_cov = rotation_matrix @ cov @ rotation_matrix.T # based on Warren and Paul's comments
rv = scipy.stats.multivariate_normal(mean=mean,cov=new_cov)
If you get an error
ValueError: the input matrix must be positive semidefinite
This page I found useful
I edited the question with the answer, but again it is
new_cov = rotation_matrix @ cov @ rotation_matrix.T