finitecov covxy=finitecov(SX,SY,PXY)
function covxy=finitecov(SX,SY,PXY); %Usage:cxy=finitecov(SX,SY,PXY)
%returns the covariance of
%finite random variables X and Y
%given by grids SX,SY,and PXY
ex=finiteexp(SX,PXY);
ey=finiteexp(SY,PXY);
R=finiteexp(SX.*SY,PXY);
covxy=R-ex*ey;Input:Grids SX,SY and probability grid PXY describing the?nite random
variables X and Y.
Output:covxy,the covariance of X and Y.
finiteexp ex=finiteexp(sx,px)
function ex=finiteexp(sx,px);
%Usage:ex=finiteexp(sx,px)
%returns the expected value E[X]
%of finite random variable X described %by samples sx and probabilities px
ex=sum((sx(:)).*(px(:)));Input:Probability vector px,vector of samples sx describing random
variable X.
Output:ex,the expected value E[X].
finitepmf y=finitepmf(sx,p,x)
function pmf=finitepmf(sx,px,x)
%finite random variable X:
%vector sx of sample space
%elements{sx(1),sx(2),...}
%vector px of probabilities
%px(i)=P[X=sx(i)]
%Output is the vector
%pmf:pmf(i)=P[X=x(i)]
pmf=zeros(size(x(:)));
for i=1:length(x)
pmf(i)=sum(px(find(sx==x(i)))); end Input:sx is the range of a?nite random variable X,px is the corresponding
probability assignment,x is a vector
of possible sample values Output:y is a vector with y(i)= P[X=x(i)].
finiterv x=finiterv(sx,p,m)
function x=finiterv(s,p,m) %returns m samples
%of finite(s,p)rv
%s=s(:);p=p(:);
r=rand(m,1);
cdf=cumsum(p);
x=s(1+count(cdf,r));Input:sx is the range of a?nite random variable X,p is the corresponding probability assignment,m is
positive integer
Output:x is a vector of m sample values y(i)=
F X(x(i)).