From: Philip M on
Hey guys,

I'm using the following program code, to calculate a 5x1 vector called spread. Actually I only get values for the first two entries and the last three are always zero, regardless of my parameters and I don't know why. I think that it has something to do with the random numbers, but I'm not sure.

Thanks in advance,
Philip



varomikron=661/2309; % Parameter
a=5254/6853; % Parameter
b=1420/2709; % Parameter
mu= -755/2358; % Parameter

q=100;
l=zeros([20 125]);
Portfolioverlust=zeros(20,q);
Epsilon=zeros(20,1);
Omikron=zeros(20,1);
X=zeros(20,1);
Z=zeros(20,1);
v=zeros(q,1);
s=zeros(q,1);
pvprot=zeros(21,q);
pvdef=zeros(21,q);
LA=zeros(20,q);
LB=zeros(20,q);
LTranche=zeros(21,q);
spread=zeros(5,1);

randZ=randn(q,5);
randEps=randn(q,625);
randOmik=normrnd(0,sqrt(varomikron),q,625);


r2007=xlsread('C:\Universitaet\Diplomarbeit\Daten\Spotrates','Tabelle1','A2:A21');
stetverzinsung=zeros(20,1);
for i = 1:20
stetverzinsung(i)=exp(-r2007(i)*(i/4));
end
k=1:20;
p=1-exp((-0.003514966*k)/4); % Berechnung anhand des durch. CDS Spreads
c = norminv(p);

Attachment=[0.00001 0.03 0.06 0.09 0.12]; % Attachment Punkte der einzelnen Tranchen
Detachment=[0.03000 0.06 0.09 0.12 0.22]; % Detachment Punkte der einzelnen Tranchen

for d=1:5
A=Attachment(d); %Attachment Punkt
B=Detachment(d); %Detachment Punkt
for i=1:q
Z(1)=randZ(i,d);
for j=1:125
Epsilon(j)=randEps(i,(d-1)*125+j);
Omikron(j)=randOmik(i,(d-1)*125+j);
X(j)=a*Z(1)+sqrt(1-a^2)*Epsilon(j);
if (X(j)<=c(1))
l(1,j)=1-normcdf(mu-b*Z(1)+Omikron(j));
else l(1,j)=0;
end
end
Portfolioverlust(1,i)=sum(l(1,:))/125;
for r=2:20
Z(r)=randZ(i,d);
for j=1:125
if (l(r-1,j)>0)
l(r,j)=l(r-1,j);
else
Epsilon(j)=randEps(i,(d-1)*125+j);
Omikron(j)=randOmik(i,(d-1)*125+j);
X(j)=a*Z(r)+sqrt(1-a^2)*Epsilon(j);
if (X(j)<=c(r))
l(r,j)=1-normcdf(mu-b*Z(r)+Omikron(j));
else l(r,j)=0;
end
end
end
Portfolioverlust(r,i)=sum(l(r,:))/125;
LA(r,i)=max(Portfolioverlust(r,i)-A,0);
LB(r,i)=max(Portfolioverlust(r,i)-B,0);
LTranche(r+1,i)=(1/(B-A))*(LA(r,i)-LB(r,i));
end
for h=2:21
pvprot(h-1,i)=stetverzinsung(h-1)*(LTranche(h,i)-LTranche(h-1,i));
pvdef(h-1,i)=0.25*stetverzinsung(h-1)*(1-LTranche(h,i));
end
if (d==1)
v(i)= -sum(pvdef(:,i))*0.05+sum(pvprot(:,i));
else s(i)=sum(pvprot(:,i))/sum(pvdef(:,i));
end

end
if (d==1)
spread(1)=sum(v)/q;
else spread(d)=sum(s)/q;
end

end
From: Philip M on
Sorry for double post, but if I use the following code, I get five values for my vector. It's all the same. I just changed the way how the randnom numbers are generated. So why does the first one not work?
----------------------------------------------------------------------
q=100; %Anzahl der Wiederholungen


l=zeros([20 125]);
Portfolioverlust=zeros(20,q);
Epsilon=zeros(20,1);
Omikron=zeros(20,1);
X=zeros(20,1);
Z=zeros(20,1);
s=zeros(q,1);
v=zeros(q,1);
pvprot=zeros(21,q);
pvdef=zeros(21,q);
LA=zeros(20,q);
LB=zeros(20,q);
LTranche=zeros(21,q);
spread=zeros(5,1);


r2007=xlsread('C:\Universitaet\Diplomarbeit\Daten\Spotrates','Tabelle1','A2:A21');
stetverzinsung=zeros(20,1);
for i = 1:20
stetverzinsung(i)=exp(-r2007(i)*(i/4));
end
k=1:20;
c = norminv(1-exp((-0.003514966*k)/4));

varomikron=661/2309; % Parameter
a=5254/6853; % Parameter
b=1420/2709; % Parameter
mu= -755/2358; % Parameter

Attachment=[0.00001 0.03 0.06 0.09 0.12];
Detachment=[0.03000 0.06 0.09 0.12 0.22];
for d=1:5
A=Attachment(d); %Attachment Punkt
B=Detachment(d); %Detachment Punkt
for i=1:q
Z(1)=randn(1);
for j=1:125
Epsilon(j)=randn(1);
Omikron(j)=normrnd(0,sqrt(varomikron));
X(j)=a*Z(1)+sqrt(1-a^2)*Epsilon(j);
if (X(j)<=c(1))
l(1,j)=1-normcdf(mu-b*Z(1)+Omikron(j));
else l(1,j)=0;
end
end
Portfolioverlust(1,i)=sum(l(1,:))/125;
for r=2:20
Z(r)=randn(1);
for j=1:125
if (l(r-1,j)>0)
l(r,j)=l(r-1,j);
else
Epsilon(j)=randn(1);
Omikron(j)=normrnd(0,varomikron);
X(j)=a*Z(r)+sqrt(1-a^2)*Epsilon(j);
if (X(j)<=c(r))
l(r,j)=1-normcdf(mu-b*Z(r)+Omikron(j));
else l(r,j)=0;
end
end
end
Portfolioverlust(r,i)=sum(l(r,:))/125;
LA(r,i)=max(Portfolioverlust(r,i)-A,0);
LB(r,i)=max(Portfolioverlust(r,i)-B,0);
LTranche(r+1,i)=(1/(B-A))*(LA(r,i)-LB(r,i));
end
for h=2:21
pvprot(h-1,i)=stetverzinsung(h-1)*(LTranche(h,i)-LTranche(h-1,i));
pvdef(h-1,i)=0.25*stetverzinsung(h-1)*(1-LTranche(h,i));
end
if (d==1)
v(i)= -sum(pvdef(:,i))*0.05+sum(pvprot(:,i));
else s(i)=sum(pvprot(:,i))/sum(pvdef(:,i));
end
if (d==1)
spread(1)=sum(v)/q;
else spread(d)=sum(s)/q;
end
end
end
From: Jan Simon on
Dear Philip,

it would be much easier, if you post only the relevant part of your function.
You can go through your program step by step using the debugger and look, why "sum(s)" is 0 if d is 3, 4 and 5.
Do you know how to use the debugger?

Kind regards, Jan