Rotate 2D Gaussian given parameters a, b and c

In summary: There is no simple answer, as this depends on the specific Gaussian function and the specific rotation matrix. However, a level curve can be used to visualize the variances of the distributions.
  • #1
schniefen
178
4
TL;DR Summary
Rotate mixtured Gaussian with variances unknown.
I have a Gaussian function of the form:

Python:
def f(x,y):
  a=some number
  b=...
  c=...
  return 3*np.exp(-a*(-0.5 + x)**2+b*(x-0.5)*(y-0.5)-c*(-0.5 + y)**2)
This is a Gaussian function symmetric around y=x, and I'd like to rotate it 45 degrees (counter)clockwise. Wikipedia gives an overdetermined system of equations for the variances of x and y respectively, but it looks cumbersome. Is there a simple way to do this?

Parameters a,b and c
 
Technology news on Phys.org
  • #2
The "best" way to view this is in linear algebra terms. The quadratic exponent in the Gaussian can be express via matrix representation of a quadratic form using (and defining) the covariance matrix.

[tex] f(x,y) = N \exp\left( -\frac{1}{2} (\mathbf{x}-\boldsymbol{\mu})^T K (\mathbf{x}-\boldsymbol{\mu})\right) [/tex]
where, with your numbers...
[tex] \mathbf{x}=\left(\begin{array}{c} x \\ y\end{array}\right),\quad \boldsymbol{\mu}=\left(\begin{array}{c} \mu_x \\ \mu_y\end{array}\right)=\left(\begin{array}{c} 0.5\\ 0.5\end{array}\right), \text{ and }\quad K=\left(\begin{array}{cc} \sigma^2_x & \sigma_{xy}\\ \sigma_{xy} & \sigma^2_y\end{array}\right)=\left(\begin{array}{cc} 2a & b\\ b & 2c\end{array}\right)[/tex]
The factors of 2 in the covariance matrix are due to your lack of a factored 1/2 in your code. It doesn't occur on the b terms because they will be added twice.

Then to rotate the distribution you rotate the covariance matrix (which is a rank 2 tensor) and the vector mean using a rotation matrix.

[tex] R_{45^\circ} = \frac{\sqrt{2}}{2}\left( \begin{array}{rr} 1 & -1\\ 1 & 1\end{array}\right),\quad \boldsymbol{\mu}' = R\boldsymbol{\mu}, \quad K' = RKR^T [/tex]
With your numbers...
[tex] \boldsymbol{\mu}'=\frac{\sqrt{2}}{2}\left( \begin{array}{rr} 1 & -1\\ 1 & 1\end{array}\right)\cdot\left(\begin{array}{c} 0.5\\ 0.5\end{array}\right)=\frac{\sqrt{2}}{2}\left(\begin{array}{c} 0.5-0.5\\ 0.5+0.5\end{array}\right)=\left(\begin{array}{c} 0 \\ \sqrt{2}/2 \end{array}\right)[/tex]
and
[tex] K' = \frac{2}{4}\left( \begin{array}{rr} 1 & -1\\ 1 & 1\end{array}\right)\left(\begin{array}{cc} 2a & b\\ b & 2c\end{array}\right)\left( \begin{array}{rr} 1 & 1\\ -1 & 1\end{array}\right)= \cdots =\left(\begin{array} ((a-b+c) & (a-c)\\(a-c) & (a+b+c)\end{array}\right)[/tex]

It's "simpler" this way but that usually comes with more sophisticated approach. So brush up on your matrix multiplication.

Now this was an active rotation of the distribution ccw 45degrees. For the passive version, where you are rotating the axes use the inverse rotation matrix. For rotations this is just the transpose.
Also you should check my work (both to understand better and to catch any errors.)

In general a 2x2 rotation matrix takes the form:
[tex]R_\theta = \left(\begin{array}{cc} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array}\right)[/tex]
Note that transposing has the same effect as reversing direction [itex]\theta \mapsto -\theta[/itex].
 
  • Like
Likes schniefen
  • #3
If one would like to keep the Gaussian centered at 0.5,0.5, it is really just plugging in the numbers 0.5,0.5 into ##\mu'## ? Thanks for the helpful repetion on linear algebra.
 
  • #4
Yes, basically just don't transform the means.

On a further note, you can dive deeper into this in principle component analysis. There you find a general linear transformation (not necessarily a rotation) which will make the covariance matrix diagional. This means you're resolving the distribution into independent components. In linear algebra terms you're finding the eigen-values and eigen-vectors. In probability terms you're determining the independent set of random variables.
 
  • Like
Likes schniefen
  • #5
On a related note, consider the 2D Gaussian function below, for which a level curve has been sketched. Clearly it has a width and a length, and are either of these related to the variances? In 1D, the width is another name for the variance. In 2D, however, which is which? In this particular case I'm looking for the semi-minor axes of the ellipse.
Gaussian.png
 
  • #6
I don't know why, but somehow I don't get the Gaussian properly rotated. Consider the following code:

Python:
import numpy as np
import matplotlib.pyplot as plt
a=1.25
b=0   
c=10000
d=(a-b+c)/2
e=a-c
f=(a+b+c)/2
fig, ax = plt.subplots()
x,y=np.meshgrid(np.linspace(0,1,50),np.linspace(0,1,50))
z=3*np.exp(-a*(-0.5 + x)**2-b*(-0.5 + x)*(-0.5 + y)-c*(-0.5 + y)**2)
w=3*np.exp(-d*(-0.5 + x)**2-e*(-0.5 + x)*(-0.5 + y)-f*(-0.5 + y)**2) #rotated by 45 degrees
cs=ax.contour(x,y,z,levels=[0.8],colors='k',linestyles='dashed');
cs=ax.contour(x,y,w,levels=[0.8],colors='k',linestyles='dashed');
print(d,e,f)

Then I get 5000.625, -9998.75 , 5000.625, and the following plot:

download.png


Also, consider the integrals evaluated on WolframAlpha:

double integrate 3*exp(-1.25*(-0.5 + x)**2-0(x-0.5)(y-0.5)-10000(-0.5 + y)**2) from 0 to 1 - Wolfram|Alpha
double integrate 3*exp(-5000.625*(-0.5 + x)**2-9998.75(x-0.5)(y-0.5)-5000.625(-0.5 + y)**2) from 0 to 1 - Wolfram|Alpha
 
  • #7
How does one rotate the square ## 0 \leq x,y \leq 1## by 45 degrees, over which the Gaussian is defined? And what are the new bounds for ##x,y##?
 
  • #8
To your last question, That's easily enough done geometrically. The origin point remains fixed, the opposite diagional (1,1) will rotate to [itex](0,\sqrt{2})[/itex]

As to your difficulties with the rotated gaussian, I'll check my math. BRB.
 
  • Like
Likes schniefen
  • #9
Concerning my previous posts, I think the level curves are correct, it is just when one rotates the Gaussian, its central axis changes to be the diagonal of the square, and so more of the function fits into the square and the level curve changes correspondingly, right? However, the integral is still not correct, since the domain of integration for the rotated function should be the rotated square. What are the integration limits for that rotated square?
 
  • #10
Concerning the rotated quadratic. I confirmed the matrix multiplication. It is easier to just ignore the usual 1/2 in the gaussian and rotate the quadratic form as is:
[tex] a' = a-b+c, \quad b'/2 = a-c, c'=a+b+c[/tex]
So you're parameter transform looks good. Hmmm...
your c=10000 is huge that's a standard deviation of like 141.4 and you're "playing" in the unit square. Possibly both ellipses are off the grid but the pre-rotated one looks different due to truncation. Try c=1.

To integrate the rotated square you would normally just change coordinates, (effectively rotate back and integrate the original.) Or you can integrate over a suitable large standard square. You want the boundaries to be on the order of 10 standard deviations out from the mean. (I use 10 as a rule of thumb; it's easy to simply shift the decimal, but 6 should be sufficient.)
 

1. How do I calculate the rotation angle for a 2D Gaussian given parameters a, b and c?

The rotation angle can be calculated using the formula: θ = arctan(b/a). This will give you the angle in radians.

2. Can I rotate a 2D Gaussian without changing its parameters a, b and c?

Yes, you can rotate a 2D Gaussian without changing its parameters by using the rotation matrix. The formula for rotating a point (x, y) by an angle θ is: x' = x*cos(θ) - y*sin(θ) and y' = x*sin(θ) + y*cos(θ).

3. What is the significance of parameters a, b and c in a 2D Gaussian?

Parameter a is the height of the Gaussian peak, parameter b is the x-coordinate of the center of the peak, and parameter c is the standard deviation of the Gaussian. These parameters determine the shape and location of the 2D Gaussian distribution.

4. How does rotating a 2D Gaussian affect its shape and orientation?

Rotating a 2D Gaussian will change its orientation, but it will not change its shape. The peak will still be at the same height and the standard deviation will remain the same. However, the center of the peak will shift to a new location based on the rotation angle.

5. Can I rotate a non-Gaussian 2D distribution using the same method as a 2D Gaussian?

No, the rotation method for 2D Gaussians is specific to Gaussian distributions. Other distributions may require different methods for rotation.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
8
Views
796
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top