% JSIMPLE file jsimple.m Calculates basic quantities for joint simple rv % Version of 5/25/95 % The joint probabilities are arranged as on the plane % (the top row corresponds to the largest value of Y) P = input('Enter JOINT PROBABILITIES (as on the plane) '); X = input('Enter row matrix of VALUES of X '); Y = input('Enter row matrix of VALUES of Y '); disp(' ') PX = sum(P); % marginal distribution for X PY = fliplr(sum(P')); % marginal distribution for Y XDBN = [X; PX]'; YDBN = [Y; PY]'; PT = idbn(PX,PY); D = total(abs(P - PT)); % test for difference if D > 1e-8 % to prevent roundoff error masking zero disp('{X,Y} is NOT independent') else disp('{X,Y} is independent') end disp(' ') [t,u] = meshgrid(X,fliplr(Y)); EX = total(t.*P) % E[X] EY = total(u.*P) % E[Y] EX2 = total((t.^2).*P) % E[X^2] EY2 = total((u.^2).*P) % E[Y^2] EXY = total(t.*u.*P) % E[XY] VX = EX2 - EX^2 % Var[X] VY = EY2 - EY^2 % Var[Y] cv = EXY - EX*EY; % Cov[X,Y] = E[XY] - E[X]E[Y] if abs(cv) > 1e-9 % to prevent roundoff error masking zero CV = cv else CV = 0 end a = CV/VX % regression line of Y on X is b = EY - a*EX % u = at + b R = CV/sqrt(VX*VY); % correlation coefficient rho disp(['The regression line of Y on X is: u = ',num2str(a),'t + ',num2str(b),]) disp(['The correlation coefficient is: rho = ',num2str(R),]) disp(' ') eYx = sum(u.*P)./PX; EYX = [X;eYx]'; disp('Marginal dbns are in X, PX, Y, PY; to view, call XDBN, YDBN') disp('E[Y|X = x] is in eYx; to view, call for EYX') disp('Use array operations on matrices X, Y, PX, PY, t, u, and P')