% ProbCorrEx5.m
%
% By: Neil E. Cotter
%
% Script to compute correlation for several joint pdf's.
%

syms x y

% f(x,y) = 1/pi for x^2 + y^2 < 1  (a cylinder, radius 1, height 1/pi)

% fX(x) = 2*sqrt(1-x^2)/pi
% Check that area under fX(x) = 1.
int(2*sqrt(1-x^2)/pi,x,-1,1);

% Show E(X) = muX = 0.
int(x*2*sqrt(1-x^2)/pi,x,-1,1);

% Show Var(X) = 1/4.
int(x^2*2*sqrt(1-x^2)/pi,x,-1,1);

% Show Covar(XY) = 0.  Note that muX = muY = 0.
int(int(x*y/pi,y,-sqrt(1-x^2),sqrt(1-x^2)),x,-1,1);


% f(x,y) = 1/ln(2)^2 * 1/xy on 1<x<2 and 1<y<2

% Find fX(x) and fY(y)= 1/xln(2)
1/(x*log(2)^2)*int(1/y,y)

% Find muX = muY = 1/ln(2)
int(x/(x*log(2)),x,1,2)

% Covar(XY) = E((X-muX)(Y-muY)) = E(XY) - muX muY
%           = 1/ln(2)^2 - 1/ln(2) 1/ln(2) = 0
int(int(1/log(2)^2,x,1,2),y,1,2)


% f(x,y) = (x+y)/3 on 1<x<2 and 1<y<2

% Find fX(x) = 1/3*x+1/2 and fY(y) = 1/3*y+1/2
int((x+y)/3,y,1,2)

% Find muX = muY = 55/36
int(x*int((x+y)/3,y,1,2),1,2)

% Find E(XY) = 7/3
int(int(x*y*(x+y)/3,y,1,2),x,1,2)

% Find Covar(XY) = E(XY) - muX muY = 7/3 - 55/36 55/36 = -1/36^2
7/3 - 55/36 * 55/36
