% MS-A050x % Example of a discrete joint distribution of two variables. % (MATLAB/Octave code) % % X = #persons in household % Y = #rooms in household % P(X=x, Y=y) = proportion of such households x = 1:6; y = (1:6)'; pxy = [ 0.126 0.013 0.002 0.001 0.000 0.000 0.196 0.086 0.012 0.005 0.001 0.000 0.073 0.097 0.034 0.019 0.005 0.001 0.038 0.079 0.031 0.030 0.010 0.003 0.015 0.041 0.017 0.021 0.009 0.002 0.004 0.012 0.006 0.007 0.003 0.001 ]; % Marginal distribution of X, by taking column sums px = sum(pxy, 1) % Marginal distribution of Y, by taking row sums py = sum(pxy, 2) % Row of joint distribution, corresponding to Y=1 rooms and Y=6 rooms pxrow1 = pxy(1,:) pxrow6 = pxy(6,:) % Conditional distributions, when Y=1 rooms and when Y=6 rooms px_cond_y1 = pxrow1 / sum(pxrow1) px_cond_y6 = pxrow6 / sum(pxrow6) % Color picture of the bivariate distribution. % Dark = large value. imagesc(pxy); colormap(1-gray); colorbar; set(gca,'ydir','normal'); title('Finnish households') xlabel('X = #persons') ylabel('Y = #rooms')