% coporate bonds market value calc
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
%%%%%%%%%%%%%%%%
global z N;
load baa.txt -ascii         %monthly corporate bond yields BAA  Long Term Composite  (starts in Jan 19)
load aab10.txt -ascii       %monthly corporate bond yields BAA on 10 year benchmark (starts in April 53)

%-------------------------------------------------------------------------
%10 year yield on corporates constructed using the spread on corp added to 
%Treasury benchmark
%----------------------------------------------------------------------------



rate=baa(:,2);     %moody's average corporate bond yield BAA  (Jan 19) in percentage points
rate_2=aab10(:,2); %10 year Treasury yield + spread (Apr 53) in percentage points


i=145;            %(Jan/1931)
n=1;



%constructing quarterly rates prior to April 53
%---------------------------------------------
while i<=411  %(March/53)
   rateq(n)=rate(i);
i=i+3;
n=n+1;
end;
m=n

%constructing quarterly rates  starting in April 53
%-------------------------------------------------
j=1;
while j<=561 
   rateq(n)=rate_2(j);
j=j+3;
n=n+1;
end;







%quarterly net issuance of corp bonds starting in Jan 52 (quarterly data)
load change.txt -ascii     %code 103163003


%---------------------------------------------------------------------------
%to start the algorithm, the initial level of corp bonds at the end of 1945
%was spread out equally across the 15 preceding years
%-----------------------------------------------------------------------------
L=15;

level=23.9;   %level of outstanding corp bonds at the end of 45   

chan=23.9/L;

%spread out '45 stock of corporate bonds over previous 15 years
%--------------------------------------------------------------
for l=1:L
   corpq(l)=chan;     %starting in 1931
end   
%annual net issuance of corp bonds prior to 52 in billions of dollars (no quarterly data available)
%-------------------------------------------------------------------------------------------------
corpq(L+1)=1.0    %1946
corpq(L+2)=2.8    %1947
corpq(L+3)=4.3    %1948
corpq(L+4)=2.9    %1949
corpq(L+5)=1.6;   %1950
corpq(L+6)=3.3;   %1951



%construcing quarterly net issuance of bonds for years preceding 46, starting in 1931
%-------------------------------------------------------------------------------------
for n=1:6+L
   for i=1:4
   j=(n-1)*4+i
   corp_1(j)=(corpq(n)/4)*1000  ;
  end
end
   

M=275;    %total number of quarters
N=40;     %10 year maturity



%tax-exempt securities are added-------------------------------------------
%these are also referred to as municipal securities/industrial revenue bonds
%no tax-exempt bonds prior to 52
%--------------------------------------------------------------------------
corp_2=change(:,2);
corp(1:24+L*4)=corp_1;
corp(25+L*4:M)=corp_2;


%------------------------------------------
%constructing book market measure of bonds
%summing up the flows
%------------------------------------------
corp_ls=0*1000;
corp_l(1)=corp_ls+corp(1);
for n=2:M
   corp_l(n)=corp_l(n-1)+corp(n);
end   




%----------------------------------------------
%contructing gross issuance of corporate bonds
%as net issuance + principal repayment on bond issued N periods ago
%------------------------------------------------------------------
gr_corp=corp;
for t=N+1:M
   gr_corp(t)=corp(t)+corp(t-N);
end   
%----------------------------------------------------
%constructing coupon payments on the bond issued at t
%----------------------------------------------------
yield=rateq/100;

for t=1:M
   c(t)=gr_corp(t)*yield(t);
end

coupon=zeros(N+M,N+M);
coupon_net=zeros(M+N,1);



%------------------------------------------------------------
%constructing total coupon payments on bonds outstanding at t
%------------------------------------------------------------
for t=1:M 
   n=4;
   while n<=N
     coupon(t+n,t)=coupon(t+n,t)+c(t);     %coupon payments at t+n for bonds issued at t
     coupon_net(t+n)=coupon_net(t+n)+c(t); %this does not include the principal at t+N
  if n==N
    coupon(t+n,t)=coupon(t+n,t)+gr_corp(t);  %this includes the principal at t+N
   end
  n=n+4;
  end
end



%------------------------------------------------------------------------------------
%constructing value of outstanding bonds using PDV of total future coupon payments of
%all out currently outstanding bonds at the current yield
%------------------------------------------------------------------------------------
value=zeros(M,1);
for t=1:M %time at which values are evaluated
   for r=1:t  %time at which bonds were issued
     for s=t+1:t+N  %time at which coupon is paid
        value(t)=( coupon(s,r)/(1+yield(t))^((s-t)/4) )+ value(t); %PDV of coupon payments
     end   
  end 
end





coupon_net2=coupon_net(L*4+1:M) %only values from 1946:1999
value_n=value(L*4+1:M)          %only values from 1946:1999
corp_n=corp_l(L*4+1:M)'         %only values from 1946:1999
 

save int_bond  coupon_net2   -ascii
save val_bond    value_n  -ascii
save bval_bond corp_n -ascii