A1=importdata('data.txt');
n1=unique(date1)
n=length(n1)
p=length(return1)/n
yy=reshape(return1, [n, p])
[n,p]=size(yy)
meanx=mean(yy)
yyy=zeros(168,450);
for i=1:168
for j=1:450
yyy(i,j)=yy(i,j);
end
end
i=0;
while (i<=47.5)
x=yy(i+1:i+120,:);
n=size(x,1);
p=size(x,2);
meanx=mean(x);
x=x-meanx(ones(n,1),:);
xmkt=mean(x')';
sample=cov([x xmkt])*(n-1)/n;
covmkt=sample(1:p,p+1);
varmkt=sample(p+1,p+1);
sample(:,p+1)=[];
sample(p+1,:)=[];
prior=covmkt*covmkt'./varmkt;
prior(logical(eye(p)))=diag(sample);
if (nargin < 2 | shrink == -1)
c=norm(sample-prior,'fro')^2;
y=x.^2;
pr=1/n*sum(sum(y'*y))-sum(sum(sample.^2));
rdiag=1/n*sum(sum(y.^2))-sum(diag(sample).^2);
z=x.*xmkt(:,ones(1,p));
v1=1/n*y'*z-covmkt(:,ones(1,p)).*sample;
roff1=sum(sum(v1.*covmkt(:,ones(1,p))'))/varmkt...
-sum(diag(v1).*covmkt)/varmkt;
v3=1/n*z'*z-varmkt*sample;
roff3=sum(sum(v3.*(covmkt*covmkt')))/varmkt^2 ...
-sum(diag(v3).*covmkt.^2)/varmkt^2;
roff=2*roff1-roff3;
r=rdiag+roff;
k=(pr-r)/c;
shrinkage=max(0,min(1,k/n));
else
shrinkage = shrink;
end
sigma=shrinkage*prior+(1-shrinkage)*sample;
insigma=inv(sigma);
meanp=(meanx)';
Rmax=mean(meanp);
M=48;
u=ones(p,1);
a=zeros(2,2);
a(1,1)=u'*insigma*u;
a(1,2)=meanp'*insigma*u;
a(2,1)=a(1,2)
a(2,2)=meanp'*insigma*meanp;
d=a(1,1)*a(2,2)-a(1,2)*a(1,2);
f=(insigma*(a(2,2)*u-a(1,2)*meanp))/d;
g=(insigma*(-a(1,2)*u+a(1,1)*meanp))/d;
r=Rmax;
w=zeros(450,M);
sigmap=zeros(M,1);
for m=1:M
w(:,m)=f+r(m)*g;
sigmap(m)=sqrt(w(:,m)*sigma*w(:,m));
end
s= sqrt(a(1,1)*((r - a(1,2)/a(1,1))^2)/d + 1/a(1,1));
minrisk=sqrt(1/a(1,1));
minreturn=a(1,2)/a(1,1);
wminp=f +(a(1,2)/a(1,1))*g;
sharpe= Rmax/s;
tanrisk=sqrt(a(2,2))/a(1,2);
tanreturn=a(2,2)/a(1,2);
wtanp=f+(a(2,2)/a(1,2))*g;
i=i+1
end