function E = iw_sig11 (v,S,z)
%
%
% PURPOSE: returns random draws from the Inverted Wishart with dof "v"
% and scale matrix S (k by k, pos. def.), with the (1,1) element of each 
% draw constrained to the value "z", following the procedure outlined
% in Nobile, 2000
%
%
%
% written by:
% Klaus Moeltner
% University of Nevada, Reno

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% swap rows and columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% k=size(S,1);
% if z<0
%     E=-1*ones(k,k);
% else

%swap rows
int1=S;
int1(1,:)=S(end,:);
int1(end,:)=S(1,:);
% swap columns
int2=int1;
int2(:,1)=int1(:,end);
int2(:,end)=int1(:,1);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compose drawn matrix
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

k=length(int2);
L=chol(int2)';%careful - remembert that chol in Matlab returns
% an upper triangular, but we want a lower!!
chivec = diag(sqrt(chi2rnd(v-(0:k-1))));
chivec(k,k)=(sqrt(z)*L(k,k))^(-1);
A=randn(k); % k by k vec of independent 0/1 normal draws
A=A-diag(diag(A)); % zero out diagonal
A=A+chivec; %put chi draws on diagonal
A=tril(A); % get lower triangular

E=inv(L*A*A'*L');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Re-swap rows and columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%swap columns
int1=E;
int1(:,1)=E(:,end);
int1(:,end)=E(:,1);
% swap rows
int2=int1;
int2(1,:)=int1(end,:);
int2(end,:)=int1(1,:);

E=int2;
% end

