Quantcast
Viewing all articles
Browse latest Browse all 130

Re: Ledoit Wolf shrinkage factor Portfolio optimization. Undefined function or variable 'shrink'.

"Hans-Philipp Otto" <otto@afs.biz> wrote in message <nhs1nc$rs7$1@newscl01ah.mathworks.com>...
> Dear all,
>
> currently I am trying to implement the Ledoit Wolf shrinkage factor Portfolio optimization.
> As of now I alway get the following error message "Undefined function or variable 'shrink'."
> I have tryied several srink.m files availabe online. However non works for my code.
> Could you please help me how to solve that issue. The code is attached.
> Many thanks in advance for your help.
>
> Best regards,
> Philipp
>
> % Ledoit-Wolf shrinkage factor portfolio optimization
> t = size(InSampleData,1);
> n = size(InSampleData,2);
> meanx = mean(InSampleData);
> x = InSampleData-meanx(ones(t,1),:);
> % compute sample covariance matrix
> sample=(1/t).*(x'*x);
> % compute prior
> var=diag(sample);
> sqrtvar=sqrt(var);
> rBar=(sum(sum(sample./(sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))')))-n)/(n*(n-1));
> prior=rBar*sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))';
> prior(logical(eye(n)))=var;
> % compute weight vector
> if (nargin < 2 || shrink == -1) % compute shrinkage parameters and constant
> % what we call pi-hat
> y=x.^2;
> phiMat=y'*y/t - 2*(x'*x).*sample/t + sample.^2;
> phi=sum(sum(phiMat));
> % what we call rho-hat
> term1=((x.^3)'*x)/t;
> help = x'*x/t;
> helpDiag=diag(help);
> term2=helpDiag(:,ones(n,1)).*sample;
> term3=help.*var(:,ones(n,1));
> term4=var(:,ones(n,1)).*sample;
> thetaMat=term1-term2-term3+term4;
> thetaMat(logical(eye(n)))=zeros(n,1);
> rho=sum(diag(phiMat))+rBar*sum(sum(((1./sqrtvar)*sqrtvar').*thetaMat));
> % what we call gamma-hat
> gamma=norm(sample-prior,'fro')^2;
> % compute shrinkage constant
> kappa=(phi-rho)/gamma;
> shrinkage=max(0,min(1,kappa/t));
> else % use specified constant
> shrinkage=shrink;

Dear all,

in the next step I have removed the following parts:
if (nargin < 2 || shrink == -1) % compute shrinkage parameters and constant
else % use specified constant
shrinkage=shrink;
So I have removed the specified constant. The weight vector looks alright.
Please could any help me what the did to the optimization. I have never used nargin before.
I think I have removed the constant but I am not sure.
However, I still have not figured out why MATLAB has a problem with "shrink == -1) " in the first place.
The code looks now as follows.
I would be very thankful for any input.

Best regards,
Philipp

% Ledoit-Wolf shrinkage factor portfolio optimization
t = size(InSampleData,1);
n = size(InSampleData,2);
meanx = mean(InSampleData);
x = InSampleData-meanx(ones(t,1),:);
% compute sample covariance matrix
sample=(1/t).*(x'*x);
% compute prior
var=diag(sample);
sqrtvar=sqrt(var);
rBar=(sum(sum(sample./(sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))')))-n)/(n*(n-1));
prior=rBar*sqrtvar(:,ones(n,1)).*sqrtvar(:,ones(n,1))';
prior(logical(eye(n)))=var;
% compute weight vector
% what we call pi-hat
y=x.^2;
phiMat=y'*y/t - 2*(x'*x).*sample/t + sample.^2;
phi=sum(sum(phiMat));
% what we call rho-hat
term1=((x.^3)'*x)/t;
help = x'*x/t;
helpDiag=diag(help);
term2=helpDiag(:,ones(n,1)).*sample;
term3=help.*var(:,ones(n,1));
term4=var(:,ones(n,1)).*sample;
thetaMat=term1-term2-term3+term4;
thetaMat(logical(eye(n)))=zeros(n,1);
rho=sum(diag(phiMat))+rBar*sum(sum(((1./sqrtvar)*sqrtvar').*thetaMat));
% what we call gamma-hat
gamma=norm(sample-prior,'fro')^2;
% compute shrinkage constant
kappa=(phi-rho)/gamma;
shrinkage=max(0,min(1,kappa/t));

Viewing all articles
Browse latest Browse all 130

Trending Articles