function y = makeSPD(A)
%
%
Ahat= A;
p = 1;
k = 0;
while p ~= 0
    [R,p] = chol(Ahat);
    k = k + 1;
    if p ~= 0
        % Ahat failed the chol test. It must have been just a hair off,
        % due to floating point trash, so it is simplest now just to
        % tweak by adding a tiny multiple of an identity matrix.
        %     mineig = min(eig(Ahat));
        %     Ahat = Ahat + (-mineig*k.^2 + eps(mineig))*eye(size(A));
        Ahat= Ahat + (2.2e-15*eye(size(Ahat)));
    end
end

y = Ahat;