%% Ex 1
P = [0.1 0.1 0; 0.2 0.3 0; 0 0 0.3];
x = [3 4 7]; y = [2 4 6];

pX = sum(P,2)'
pY = sum(P)

EX = x*pX'

VarX = ((x-EX).^2)*pX'

EY = y*pY'

VarY = ((y-EY).^2)*pY'

[X Y] = meshgrid(x,y); X = X'; Y = Y';

EXY = sum(sum(X.*Y.*P))

EXY3X1 = sum(sum( (X-Y.^3).*(X+1).*P ))

CovXY = EXY - EX*EY

%% Ex 2

x = [1	4	1	5	7	6	10	2	6	3	7	3	4];
y = [14.1338	32.3236	5.9754	35.785	52.4688	49.5751	77.6489	13.5761	49.7059	28.5717	51.8538	27.0028	27.4189];
n = length(x);

xbar = 1/n*sum(x);
ybar = 1/n*sum(y);
xybar = 1/n*x*y';
x2bar = 1/n*x*x';

a = (xybar-(xbar*ybar))/(x2bar - xbar^2)
b = ybar - a*xbar