function [a11,kappa_p,ResidualVarInv]=my_IV_Identification(Sigma,Cov,k);
% Solve a11 = Sigma_{11}-(Sigma_{12}-a11a11' kappa') (Sigma_{22}-kappa a11a11'kappa')^{-1}(Sigma_{12}-a11a11' kappa')'

m=length(Sigma);
kappa_p=(Cov(1:k,1:k))\Cov(1:k,k+1:m);
% kappa is size (m-k) x k
% if ~exist('guess')
%     a11_sq=Sigma(1:k,1:k);
% else
%     a11_sq=guess*guess';
% end;

% myTol=1e-14;
% maxIter=1e4;

%ResidualVar=Sigma-Sigma*[kappa_p; -eye(m-k)] *inv([kappa_p', -eye(m-k)]*Sigma*[kappa_p; -eye(m-k)]) * [kappa_p', -eye(m-k)] * Sigma;
% TempMatrix=([kappa_p', -eye(m-k)]*Sigma*[kappa_p; -eye(m-k)])\([kappa_p', -eye(m-k)] * Sigma);
% ResidualVarOld=Sigma-(Sigma*[kappa_p; -eye(m-k)] )*TempMatrix;
% try
%     a11old=chol(ResidualVarOld(1:k,1:k))';
% catch
%     try
%         a11=chol(ResidualVarOld(1:k,1:k)+eye(k)*1e-10)';
%     catch
%         a11=real(sqrtm(ResidualVarOld(1:k,1:k)));   
%     end;
%     display(norm(a11*a11'-ResidualVarOld(1:k,1:k)));
% end;

% ResidualVar=[eye(k); kappa_p']'*(Sigma\[eye(k); kappa_p']);
ResidualVarInv=inv([eye(k); kappa_p']'*(Sigma\[eye(k); kappa_p']));
try
    a11=chol(ResidualVarInv, 'lower');
%     La11=inv( chol(ResidualVar, 'upper') )
%     a11*a11'-ResidualVarInv
%     a11=inv(chol(ResidualVar,'lower')');
% 
% a11*a11'-ResidualVarInv
%     a11=inv( chol(ResidualVar, 'lower') )
%     a11*a11'-ResidualVarInv
%     a11=inv(chol(ResidualVar,'lower')');
catch
    try
        a11=chol(ResidualVarInv(1:k,1:k)+eye(k)*1e-10)';
    catch
        a11=real(sqrtm(ResidualVarInv(1:k,1:k)));   
    end;
    display(norm(a11*a11'-ResidualVarInv(1:k,1:k)));
end;
