Rolling window using by while loop
1 view (last 30 days)
Show older comments
Hello, I am currently working on the comparison of the constructed portfolios using out-of-sample variance criteria. I am going to use rolling window procedure for the comparison. First, I choose a window over which to perform estimation. Length of estimation window let say t is smaller than n, where n is the total number of returns (n=168). I use estimation window of t=120 data points which correspond to 10 years for monthly data. Second, using the return data over the estimation window, t, I compute various portfolios. I repeat this rolling window procedure for the next month and dropping the data for the earliest month. I continue this until the end of the dataset is reached. At the end of the this process, I will have generated n-t portfolio weight vectors (which should be 48 vectors in total) for each strategy. I am trying to do this with while loop in Matlab. There is a bug in the loop, it always gives the dimension error but I could not solve the problem. You can also reach my codes below. I would be very glad for your any help. Thank you
% initialization & downloading data
A1=importdata('data.txt');
n1=unique(date1)
n=length(n1)
p=length(return1)/n
yy=reshape(return1, [n, p]) % reshape as a matrix
[n,p]=size(yy)
meanx=mean(yy)
yyy=zeros(168,450); % convert cell to a matrix
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,:);
% de-mean returns
n=size(x,1);
p=size(x,2);
meanx=mean(x);
x=x-meanx(ones(n,1),:);
xmkt=mean(x')';
% compute sample covariance matrix
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,:)=[];
% compute prior (F)
prior=covmkt*covmkt'./varmkt;
prior(logical(eye(p)))=diag(sample);
if (nargin < 2 | shrink == -1) % compute shrinkage parameters
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;
% compute shrinkage constant (k/n)
k=(pr-r)/c;
shrinkage=max(0,min(1,k/n));
else % use specified number
shrinkage = shrink;
end
% compute the estimator
sigma=shrinkage*prior+(1-shrinkage)*sample;
% compute the inverse of the covariance matrix
insigma=inv(sigma);
% portfolio optimization
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
1 Comment
Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!