-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcovCor.asv
48 lines (38 loc) · 1.24 KB
/
covCor.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function [sigma,shrinkage]=covCor(x,shrink)
% function sigma=covcorr(x)
% x (t*n): t iid observations on n random variables
% sigma (n*n): invertible covariance matrix estimator
%
% Shrinks towards constant correlation matrix
% if shrink is specified then this const. is used for shrinkage
% The notation follows Ledoit and Wolf (2004)
% This version: 06/2009
% de-mean returns
[t,n]=size(x);
meanx=mean(x);
x=x-meanx(ones(t,1),:);
% compute sample covariance matrix
sample=(1/t).*(x'*x);
% compute prior
var=diag(sample);
sqrtvar=sqrt(var);
rho=(sum(sum(sample./(sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))')))-n)/(n*(n-1));
prior=rho*sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))';
prior(logical(eye(n)))=var;
if (nargin < 2 || shrink == -1) % compute shrinkage parameters
c=norm(sample-prior,'fro')^2;
y=x.^2;
p=1/t*sum(sum(y'*y))-sum(sum(sample.^2));
rdiag=1/t*(sum(sum(y.^2)))-sum(var.^2);
v=((x.^3)'*x)/t-(var(:,ones(1,n)).*sample);
v(logical(eye(n)))=zeros(n,1);
roff=sum(sum(v.*(sqrtvar(:,ones(n,1))'./sqrtvar(:,ones(n,1)))));
r=rdiag+rho*roff;
% compute shrinkage constant
k=(p-r)/c;
shrinkage=max(0,min(1,k/t));
else % use specified number
shrinkage = shrink;
end
% compute the estimator
sigma=shrinkage*prior+(1-shrinkage)*sample;