In this example we will transform a gaussian density function to a density function that is uncorrelated in the new variables. The transformed density function will plotted as a twodimensional space curve that represents a locus of constant density points or a contour plot. We begin with a gaussian density function as a function of two random variables X and Y. For simplicity we assumed that E[X] and E[Y] = zero. We use the symbol f for the exponent of the gaussian density function and fg for the gaussian density function syms x y rho sx sy f=-1/2/(1-rho^2)*(x^2/sx^2-2*rho*x*y/sx/sy+y^2/sy^2) pretty(f) f =
-1/2/(1-rho^2)*(x^2/sx^2-2*rho*x*y/sx/sy+y^2/sy^2)
2 2
x rho x y y
--- - 2 ------- + ---
2 sx sy 2
sx sy
- 1/2 ---------------------
2
1 - rho fxy=1/(2*pi*sx*sy)/(1-rho^2)^(1/2)*exp(f); pretty(fxy)
2 2
x rho x y y
--- - 2 ------- + ---
2 sx sy 2
sx sy
exp(- 1/2 ---------------------)
2
1 - rho
1/2 --------------------------------
2 1/2
pi sx sy (1 - rho )
The variables X and Y are to be transformed to Z and W by syms z w
syms th eqz=x*cos(th)+y*sin(th)-z eqw=-x*sin(th)+y*cos(th)-w eqz = x*cos(th)+y*sin(th)-z eqw =
-x*sin(th)+y*cos(th)-w
The inverse transformation can be easily found to be
[xs ys]=solve(eqz,eqw) xs =
-w*sin(th)+cos(th)*z ys = cos(th)*w+z*sin(th)
The Jacobian can be evaluated using Matlab as jac=jacobian([xs;ys],[z w]) jac =
[ cos(th), -sin(th)]
[ sin(th), cos(th)] djac=det(jac) djac = cos(th)^2+sin(th)^2
The trig manipulations are performed in Maple directly by first entering the expression as a maple variable and then using combine to perform the trig reduction maple('djac:=cos(th)^2+sin(th)^2'); maple('combine','djac,trig') ans =
1
Using Equation 4.5-10, we have after substitution for x and y in the exponent, expanding the resultant expression and collecting on the subexpression z we have.
fzw=subs(f,{x y},{xs ys}); fzw1=expand(fzw); pretty(collect(collect(fzw1,w),z))
/ 2 2 \ /
| sin(th) cos(th) rho sin(th) cos(th)| 2 |
|- 1/2 -------------- - 1/2 -------------- + -------------------| z + |
| 2 2 2 2 2 | |
\ (1 - rho ) sy (1 - rho ) sx (1 - rho ) sx sy / \
2 2
\
rho sin(th) sin(th) cos(th) sin(th) cos(th) rho cos(th)
|
- ---------------- - --------------- + --------------- + --------------
--|
2 2 2 2 2 2
|
(1 - rho ) sx sy (1 - rho ) sy (1 - rho ) sx (1 - rho ) sx sy/
/ 2 2 \
| rho sin(th) cos(th) cos(th) sin(th) |
2
w z + |- ------------------- - 1/2 -------------- - 1/2 --------------| w
| 2 2 2 2 2|
\ (1 - rho ) sx sy (1 - rho ) sy (1 - rho ) sx / collecting expressions A, B and C are defined as
A=(-1/2/(1-rho^2)/sy^2*sin(th)^2-1/2/(1-rho^2)/sx^2*cos(th)^2+
1/(1-rho^2)*rho/sx/sy*sin(th)*cos(th));
B=(-1/(1-rho^2)*rho/sx/sy*sin(th)^2-1/(1rho^2)/sy^2*sin(th)*cos(th)+ 1/(1-rho^2)/sx^2*sin(th)*cos(th)+1/(1rho^2)*rho/sx/sy*cos(th)^2);
C=(-1/(1-rho^2)*rho/sx/sy*sin(th)*cos(th)-1/2/(1rho^2)/sy^2*cos(th)^2-1/2/(1-rho^2)/sx^2*sin(th)^2);
'A=' pretty(A)
'B=' pretty(B)
'C=' pretty(C) ans =
A=
2 2
sin(th) cos(th) rho sin(th) cos(th)
- 1/2 -------------- - 1/2 -------------- + -------------------
2 2 2 2 2
(1 - rho ) sy (1 - rho ) sx (1 - rho ) sx sy ans =
B=
2 2
rho sin(th) sin(th) cos(th) sin(th) cos(th) rho cos(th)
- ---------------- - --------------- + --------------- + ---------------
-
2 2 2 2 2 2
(1 - rho ) sx sy (1 - rho ) sy (1 - rho ) sx (1 - rho ) sx sy ans =
C=
2 2
rho sin(th) cos(th) cos(th) sin(th)
- ------------------- - 1/2 -------------- - 1/2 --------------
2 2 2 2 2
(1 - rho ) sx sy (1 - rho ) sy (1 - rho ) sx
Using the trigonometric identities, the expression for B becomes
BE=maple('combine','(-1*rho/sx/sy*sin(th)^2-1/sy^2*sin(th)*cos(th)
+1/sx^2*sin(th)*cos(th) +1*rho/sx/sy*cos(th)^2),`trig`')
BE =
1/2*(2*rho*sx*sy*cos(2*th)-sin(2*th)*sx^2+sin(2*th)*sy^2)/sx^2/sy^2
Reentering the expression so it can be used by Matlab to be able to solve for
we have.
BE=(rho*sx*sy*cos(2*th)-
1/2*sin(2*th)*sx^2+1/2*sin(2*th)*sy^2)/sx^2/sy^2; pretty(BE)
2 2
rho sx sy cos(2 th) - 1/2 sin(2 th) sx + 1/2 sin(2 th) sy
-----------------------------------------------------------
2 2
sx sy
We desire to have B = 0 which is the correlation term of the exponent of the gaussian in z and w to be equal to zero. We can solve for
to make the numerator of B = 0 or th0=solve(BE,th) th0 =
1/2*atan(2*rho*sx*sy/(sx^2-sy^2))
This results can be interpreted: when we rotate the coordinate axes x, y by
to form the z, w axes the gaussian function expressed in the new coordinates is uncorrelated. The new expression for the gaussian density function in z and w coordinates is syms sz sw rho fzw=subs(f,{x y sx sy rho},{z w sz sw 0}); pretty(fzw)
2 2
z w
- 1/2 --- - 1/2 ---
2 2
sz sw
Where
x and
w can be determined from A and C in terms of sz = sqrt(simplify(1/A/(1-rho^2)/-2)); pretty(sz)
x,
y and
as
/ 2 2 \1/2
| sx sy |
|- ----------------------------------------------------------------|
| 2 2 2 2 2 |
\ -sx + sx cos(th) - cos(th) sy + 2 rho sin(th) cos(th) sy sx/ sw = sqrt(simplify(1/C/(1-rho^2)/-2)); pretty(sw)
/ 2 2 \1/2
| sx sy |
|---------------------------------------------------------------|
| 2 2 2 2 2|
\2 rho sin(th) cos(th) sy sx + sx cos(th) + sy - cos(th) sy /
The maximum excursions the ellipse in the x and y axes are determined by differentiating the equation for the ellipse with respect to x and then y respectively and setting the result to zero.
Differentiation with respect to x, setting the result to zero gives the curve for maximum excursion in the x direction. and solving for y we have
solve(diff(f,x),y) ans = x/sx/rho*sy
Similarly for the x extension we have solve(diff(f,y),x) ans = y/sy/rho*sx
The expressions derived above can be used to plot the curves of constant probability density. Let use assume the following values for the gaussian parameters
sx = 1.2 sy = 1 rho = 0.4
The lines in the XY plane where the ZW coordinates produce an uncorrelated curve along the new axes are th1=subs(th0,sx,1.2); th1=subs(th1,sy,1); thr=180/pi*subs(th1,rho,0.4) thr =
32.6882
In the ZW plane the curve of constant probability density is wz=solve(fzw+1,w) wz =
[ (-z^2+2*sz^2)^(1/2)*sw/sz]
[ -(-z^2+2*sz^2)^(1/2)*sw/sz]
Where szr=double(subs(sz,{sx sy, th rho},{1.2 1 thr*pi/180 0.4})) swr=double(subs(sw,{sx sy, th rho},{1.2 1 thr*pi/180 0.4})) szr =
1.4426 swr =
0.9076
We will plot the ellipse contour where the value of the ellipse is set to unity. Solving for w where it yields real values when -zmax < z <zmax we have zmax=sqrt(2)*szr; zp=-zmax:.02:zmax; wzp=(-zp.^2+2*szr^2).^(1/2)*swr/szr;
The values of z are selected and the corresponding values of w determined using this function. The
ZW values are then transformed to the XY plane. The equations for the axes rotation are given below thr=thr*pi/180; xp=cos(thr)*zp-wzp*sin(thr); yp=sin(thr)*zp+wzp*cos(thr);
The lines where the maximum excursions occur or the zero slope lines are given with the rotation to the z, w axes by the angle
yc=zp*tan(thr); ycw=zp*tan(thr+pi/2); rcw=1/1.2/0.4*zp; rc=zp*1/1.2*0.4; plot(xp,yp,-xp,-yp,zp,yc,zp,ycw,zp,rc,zp,rcw) axis([-2.5 2.5 -2.5 2.5])
2.5
2
1.5
1
0.5
0
-0.5
-1
-1.5
-2
-2.5
-2.5
-2 -1.5
-1 -0.5
0 0.5
1 1.5
2 2.5
The same curve can be found using the contour plotting capability of Matlab. A copy of the gaussian function is repeated below
fp=-1/2/(1-rho^2)*(x.^2/sx^2-2*rho*x.*y/sx/sy+y.^2/sy^2); gau=1/(2*pi*sx*sy)/(1-rho^2)^(1/2)*exp(fp);
pretty(gau)
2 2
x rho x y y
--- - 2 ------- + ---
2 sx sy 2
sx sy
exp(- 1/2 ---------------------)
2
1 - rho
1/2 --------------------------------
2 1/2
pi sx sy (1 - rho )
This has been functionalized in function gauss_2D.
. function y=gauss_2D(sx,sy,rho,x,y)
% gaussian probability density function in 2 dim
% sx sy rho are std deviations and correlation
% and x y are the input variables fp=-1/2/(1-rho^2)*(x.^2/sx^2-2*rho/sx/sy*x.*y+y.^2/sy^2); y=1/(2*pi*sx*sy)/(1-rho^2)^(1/2)*exp(fp);
[xpd,ypd]=meshgrid(-3:.1:3);
MP=Gauss_2d(1.2, 1, .4, xpd, ypd); and the contour plot is drawn contour(xpd,ypd,MP)
3
2
1
0
-1
-2
-3
-3 -2 -1 0 1 2 3
The outer contour corresponds approximately to the 2
ellipse drawn above. A three dimensional gaussian is drawn for reference below. mesh(xpd,ypd,MP)