function [shrinkCov, alpha]=shrinkLedoit(SampleCov,X)
[N, T]=size(X);
X=X-mean(X,2);

%SampleCov=(1/T).*(X*X');

prior=eye(N)*trace(SampleCov)/N;

d2=trace((SampleCov-prior)*(SampleCov-prior'));
for t=1:T
    summand(t)=trace((X(:,t)*X(:,t)'-SampleCov)*(X(:,t)*X(:,t)'-SampleCov)');
end
b2bar=T^(-2)*sum(summand);

b2=min(b2bar,d2);
alpha=b2/d2;
shrinkCov = (alpha)*prior+(1-alpha)*SampleCov;