%HGAINS    plot holding gains and the market value of businesses from 
%          the US Flow of Funds.

%          Ellen McGrattan, 2-4-2012


%
% NIPA Table 1.1.5 nominal GDP and Table 1.1.9 deflator 
%
load ../BEA/NIPA/dgdp4711.dat
i            = find(dgdp4711(1,:)>1951);
y            = dgdp4711(1,i)';
q            = dgdp4711(2,i)';
gdp          = dgdp4711(3,i)';
def          = dgdp4711(4,i)';
gdpa         = zeros(4,length(gdp)/4);
defa         = gdpa;
gdpa(:)      = gdp;
gdpa         = mean(gdpa)';
defa(:)      = def;
defa         = mean(defa)'/100;
rgdpa        = gdpa./defa;
T            = length(gdpa);

%
% FOF Table R100 Change in household net worth (Quarterly, Bil$)
% (Guess of 4th quarter reversal from 3rd quarter in 2011:4)
%

load ../FOF/btabs/btab100d.dat
load ../FOF/btabs/rtab100d.dat
load ../FOF/ltabs/ltab213d.dat
mvbus        = (ltab213d(:,25)+btab100d(:,30))/1000;
nwlessre     = (btab100d(:,43)-btab100d(:,4)-btab100d(:,8))/1000;
nholdgain    = (rtab100d(:,10)-rtab100d(:,11))/1000;

nholdgain    = [nholdgain;-nholdgain(length(nholdgain))];
nwlessre     = [nwlessre;nwlessre(length(nwlessre))];
mvbus        = [mvbus;mvbus(length(mvbus)-3)*1.021];

nholdgaina   = zeros(4,length(nholdgain)/4);
nholdgaina(:)= nholdgain;
nholdgaina   = sum(nholdgaina)';
Vnholdgain   = cumsum(nholdgaina);

nwlessrea    = zeros(4,length(nwlessre)/4);
nwlessrea(:) = nwlessre;
nwlessrea    = nwlessrea(4,:)';

mvbusa       = zeros(4,length(mvbus)/4);
mvbusa(:)    = mvbus;
mvbusa       = mvbusa(4,:)';


ngain        = [Vnholdgain(2:T)-Vnholdgain(1:T-1)]./gdpa(1:T-1);
rgain        = [Vnholdgain(2:T)./defa(2:T)-Vnholdgain(1:T-1)./ ...
                defa(1:T-1)]./rgdpa(1:T-1);

t            = 1953:2011; 
figure(1)
plot(t,rgain)
title('Real Holding Gains Relative to GDP (Excluding Real Estate)', ...
      'Fontsize',16)

t            = 1952:2011;  
figure(2)
plot(t,mvbusa./gdpa)
title('Market Value of US Businesses Relative to GDP','Fontsize',16)

