# Can anyone help me calculating the average covariance of return?

1 view (last 30 days)
Silvia Grasso on 27 Oct 2022
Commented: Silvia Grasso on 27 Oct 2022
• Hi everyone. I'm trying to convert this formula into MatLab code:
where :
Fcvc is a target matrix used for shrinking the sample covariance matrix of return towards their average variance and average covariance, in order to obtain a better estimator. The topic is fromt De Nard paper "Oops! I Shrunk the Sample Covariance Matrix Again: Blockbuster Meets Shrinkage"
I don't know how to compute the average covariance (average of off-diagonal entries of a simmetric matrix), can anyone help me?

William Rose on 27 Oct 2022
Edited: William Rose on 27 Oct 2022
[Edit: change the name of the initial matrix; it should not be Fcvc, it should be S. Add code to construct Fcvc using the which we calculate.]
You can just write a nested for loop (i,j) that does the calculation:
%Make a 4x4 covariance matrix
N=4; S=eye(N); %identity matrix
for i=1:N,
for j=i+1:N
S(i,j)=-1+2*rand(1,1);
S(j,i)=S(i,j);
end
end
S=10*S %multiply by some arbitray constant
S = 4×4
10.0000 -7.0228 -1.6309 6.1487 -7.0228 10.0000 -7.4054 5.1093 -1.6309 -7.4054 10.0000 0.3682 6.1487 5.1093 0.3682 10.0000
That looks like a reasonable covariance matrix: it is positive along the diagonal, and the absolute value of off-diagonal element are less than the diagoonal, and it is symmetric.
Now compute and :
siibar=sum(sum(S))/N^2;
sijbar=0;
for i=1:N
for j=i+1:N
sijbar=sijbar+S(i,j);
end
end
sijbar=sijbar/((N^2-N)/2);
fprintf('siibar=%.4f, sijbar=%.4f\n',siibar,sijbar)
siibar=1.9459, sijbar=-0.7388
Those values look reasonable.
Use siibar and sijbar to make Fcvc
Fcvc=sijbar*(ones(N)-eye(N))+siibar*eye(N)
Fcvc = 4×4
1.9459 -0.7388 -0.7388 -0.7388 -0.7388 1.9459 -0.7388 -0.7388 -0.7388 -0.7388 1.9459 -0.7388 -0.7388 -0.7388 -0.7388 1.9459
That looks good.
##### 2 CommentsShowHide 1 older comment
Silvia Grasso on 27 Oct 2022
I tried to do this:
meanvar=mean(diag(sample));
meancov=sum(sum(sample(~eye(p))))/p*(p-1); %
target=meanvar*eye(p)+meancov*(~eye(p));
Could have sense? @William Rose