% JAPPROX file japprox.m Basic quantities for ac pair {X,Y} % Version of 5/7/96 % Assumes tuappr has set X, Y, PX, PY, t, u, P 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); disp(['The regression line of Y on X is: u = ',num2str(a),'t + ',num2str(b),]) disp(['The correlation coefficient is: rho = ',num2str(R),]) disp(' ') eY = sum(u.*P)./sum(P); % eY(t) = E[Y|X = t] RL = a*X + b; plot(X,RL,X,eY,'-.') grid title('Regression line and Regression curve') xlabel('X values') ylabel('Y values') legend('Regression line','Regression curve') clear eY % To conserve memory clear RL disp('Calculate with X, Y, t, u, P, as in joint simple case')