function testParameterEstimation
%% prepare sample sizes and true parameter values
k=4;
nX=5;
nY=7;
meX=2*rand(1,k)-1;
meY=2*rand(1,k)-1;
cov0=cov(2*rand(k+1,k));
%% calculate true discriminant function parameters
V=ones(1,k+1);
V(1,2:k+1)=(meY-meX)/cov0;
V(1,1)=V(1,2:k+1)*(meY+meX)'/2;
%disp(func(k,nX,nY,meX,meY,cov0,V));
%% Monte-Carlo
L=500;
res=ones(L,k+1);
for i=1:L
res(i,:)=func(k,nX,nY,meX,meY,cov0,V);
end;
probab=((1:L)-0.5)/L;
%% plot results
colors=['b.';'c.';'g.';'m.';'y.';'bo';'co';'go';'mo';'yo'];
for j=1:k+1 % number of estimated parameter
plot(sort(res(:,j)),probab',colors(j,:),'DisplayName',int2str(j));
hold on;
end;
P = tcdf(sort(res(:,j)),nX+nY-k-1);
plot(sort(res(:,j)),P,'r-','DisplayName','stud');
legend('show')
hold off;
function r=func(k,nX,nY,meX,meY,cov0,param0)
% generate X and Y, calculate main parameter estimation
r=ones(1,k+1);
X=mvnrnd(meX(ones(nX,1),:),cov0);
Y=mvnrnd(meY(ones(nY,1),:),cov0);
mX=mean(X);
mY=mean(Y);
covX=cov(X);
covY=cov(Y);
S=((nX-1)*covX+(nY-1)*covY)/(nX+nY-2);
% calculate discriminant function parameters estimations
Vc=ones(1,k+1);
Vc(1,2:k+1)=(mY-mX)/S;
Vc(1,1)=Vc(1,2:k+1)*(mY+mX)'/2;
% calculate covariance matrix
sigma=((nX+nY)^3+4*(nX+nY)*(nX+nY-1))/(nX*nY*(nX+nY-k-1));
sigma=sigma+(nX+nY-2)/(nX+nY-k-1)*(mY-mX)/S*(mY-mX)';
W=[ones(nX,1),X;ones(nY,1),Y];
C=sigma*inv(W'*W);
% calculate Student stasistics for discriminant function parameters estimations
std=sqrt(diag(C))';
r=(Vc-param0)./std;