function out = iwishpdf(W,v,S)
%
% implementation: pE=iwishpdf(Emean,v1,S1)
% PURPOSE: returns the density of the Inverted Wishart with dof "v"
% and scale matrix S (k by k, pos. def.)at W
% Follows Gelman's formula, i.e. E(W)=(1/(v-k-1))*S;
%
% written by:
% Klaus Moeltner
% University of Nevada, Reno
% 

k=length(S);

logexpterm=-.5*trace(S*inv(W));
logdetWterm=(-(v+k+1)/2)*log(det(W));
logdetSterm=(v/2)*log(det(S));
log2term=(v*k/2)*log(2);
logpiterm=(k*(k-1)/4)*log(pi);

tt=[1:1:k]';

sumgamln=sum(gammaln(((v+1)-tt)/2));

logpdf=logexpterm + logdetSterm + logdetWterm -(log2term + logpiterm +sumgamln);
out=exp(logpdf);




