function [C] = multcov(n,p)
% MULTCOV Covariance of a multinomial distribution.
% [C] = MULTCOV(N,P) returns the covariance of a multinomial distribution
% with parameters N and P.
% The Coariance is:
% Covariance = -N Pi Pj; i,j = 1,2, ... ,k.
%
% Covariance = [ np1(1 - p1) -np1p2 -np1pk
% -np1p2 np2(1 - p2) -np2pk
% . . . .
% . . . .
% . . . .
% -np1pk -np2pk npk(1 - pk) ].
%
% Syntax: function [C] = multcov(n,p)
%
% Inputs:
% n - number of trials.
% p - vector of associated probabilities.
% Outputs:
% C - multinomial covariance matrix.
%
% Example from the Dr. Hossein Arsham Statistic Site (http://www.staff.vu.edu.au/
% sarath/Business-stats/opre504.htm). For a multinomial distribution function
% (http://www.staff.vu.edu.au/sarath/Business-stats/opre504.htm#rmultinomial).
% Consider two investment alternatives, Investment I and Investment II with the
% characteristics outlined in the following table:
%
% - Two Investments -
% Investment I Investment II
% Payoff Prob. Payoff Prob.
% 1 0.25 3 0.33
% 7 0.50 5 0.33
% 12 0.25 8 0.34
%
% For the investment I we are interested to get the multinomial covariance.
%
% Calling on Matlab the function:
% [C] = multcov(n,p)
%
% where n = 20 and p = [.25,.5,.25]
%
% Answer is:
%
% C =
% 1.7452 -1.5706 -0.1745
% -1.5706 1.7452 -0.1745
% -0.1745 -0.1745 0.3490
%
% Created by A. Trujillo-Ortiz, R. Hernandez-Walls and A. Castro-Perez
% Facultad de Ciencias Marinas
% Universidad Autonoma de Baja California
% Apdo. Postal 453
% Ensenada, Baja California
% Mexico.
% atrujo@uabc.mx
% Copyright (C) April 05, 2005.
%
% To cite this file, this would be an appropriate format:
% Trujillo-Ortiz, A., R. Hernandez-Walls and A. Castro-Perez. (2005). multcov:
% Covariance of a multinomial distribution. A MATLAB file. [WWW document]. URL
% http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=7375
%
% References:
%
% Arsham, H. Statistic Site http://www.staff.vu.edu.au/sarath/Business-stats/opre504.htm
% http://www.staff.vu.edu.au/sarath/Business-stats/opre504.htm#rmultinomial
%
if nargin < 2,
error('You need to input two arguments.');
return,
end;
k = length(p);
pp = sum(p);
if pp ~= 1,
error('The sum of the input probabilities must be equal 1.')
return,
end;
for i = 1:k,
for j = 1:k,
if (i==j);
C(i,j) = n*p(i)*(1.0-p(i));
else
C(i,j) = -n*p(i)*p(j);
end;
end;
end;
return,