%
% KFS      Compute transitional probabilities across 
%


%
% Load in data from file with subset from Public_Use_LI_LONG.csv
%
%   Note: Two categories replaced with numbers
%
%     1. status
%        0 = Complete: No Data
%        1 = Refusal
%        2 = Hard Missing Value (because business closed, sold or merged)
%        3 = Out of Business
%        4 = Merged Or Sold
%        5 = Temporarily Stopped
%        6 = Complete
%
%     2. c1z2_legal_status
%        1 = Sole Proprietorship
%        2 = Limited Liability Company
%        3 = Subchapter S Corporation
%        4 = C Corporation
%        5 = General Partnership
%        6 = Limited Partnership
%        7 = Something else?
%

n       = 4928;
x       = xlsread('../RawData/KFS/KFSsubset');
sta     = zeros(8,n);
lfo     = zeros(8,n);
rev     = zeros(8,n);
wgt     = zeros(8,n);
sta(:)  = x(:,1); sta = sta';
lfo(:)  = x(:,2); lfo = lfo';
rev(:)  = x(:,4); rev = rev';
wgt(:)  = x(:,5); wgt = wgt';

%
% In case of refusal, use last LFO
%
disp(' ')
disp('Assignment for businesses that dropped out:')
disp('  1.  Set LFO equal to NaN')
disp('  2.  Use last period LFO')
idropout = input('? ');
disp(' ')
disp('Assignment for located businesses with missing LFO:')
disp('  1.  Set LFO equal to NaN')
disp('  2.  Use last period LFO')
inodata  = input('? ');
disp(' ')

for j=2:8;
  for i=1:n; 
    if (isnan(lfo(i,j)) & (((sta(i,j)==0) & (idropout==2)) | ...
                          (((sta(i,j)==1) & (inodata==2)))));
      lfo(i,j) = lfo(i,j-1); 
    end;
  end;
end;
    
%
% Loop to count all transitions
%

count = zeros(8,9);
pimat = zeros(8,9);
for j=1:7;
  for i=1:n; 
    for k=1:7; 
      for l=1:7; 
        if (lfo(i,j)==k & lfo(i,j+1)==l); 
          count(k,l) = count(k,l) + 1;
          if (~isnan(rev(i,j)));
            pimat(k,l) = pimat(k,l) + wgt(i,j)*rev(i,j);
          end
        end;
      end;
      if ((lfo(i,j)==k) & (isnan(lfo(i,j+1))));
        if ((sta(i,j+1)>=2) & (sta(i,j+1)<=5));
          count(k,8) = count(k,8)+1;
          if (~isnan(rev(i,j)));
            pimat(k,8) = pimat(k,8) + wgt(i,j)*rev(i,j);
          end
        else
          count(k,9) = count(k,9)+1;
          if (~isnan(rev(i,j)));
            pimat(k,9) = pimat(k,9) + wgt(i,j)*rev(i,j);
          end
        end;
      end;
    end;
    if (isnan(lfo(i,j)));
      for l=1:7;
        if (lfo(i,j+1)==l);
          count(8,l) = count(8,l)+1;
          if (~isnan(rev(i,j)));
            pimat(8,l) = pimat(8,l) + wgt(i,j)*rev(i,j);
          end
        end
      end
      if (isnan(lfo(i,j+1)));
        if ((sta(i,j+1)>=2) & (sta(i,j+1)<=5));
          count(8,8) = count(8,8)+1;
          if (~isnan(rev(i,j)));
            pimat(8,8) = pimat(8,8) + wgt(i,j)*rev(i,j);
          end
        else
          count(8,9) = count(8,9)+1;
          if (~isnan(rev(i,j)));
            pimat(8,9) = pimat(8,9) + wgt(i,j)*rev(i,j);
          end
        end;
      end;
    end;
  end;
end;
tran1        = pimat(1:7,:)./(sum(pimat(1:7,:)')'*ones(1,9))*100;
tran2        = zeros(5);
tran2(1,:)   = [pimat(1,1),sum(pimat(1,5:6)),pimat(1,3),pimat(1,2), ...
                pimat(1,4)]/sum(pimat(1,1:6))*100;
tran2(2,:)   = [sum(pimat(5:6,1)),sum(sum(pimat(5:6,5:6))),sum(pimat(5:6,3)), ...
                sum(pimat(5:6,2)),sum(pimat(5:6,4))]/sum(sum(pimat(5:6,1:6)))*100;
tran2(3,:)   = [pimat(3,1),sum(pimat(3,5:6)),pimat(3,3),pimat(3,2), ...
                pimat(3,4)]/sum(pimat(3,1:6))*100;
tran2(4,:)   = [pimat(2,1),sum(pimat(2,5:6)),pimat(2,3),pimat(2,2), ...
                pimat(2,4)]/sum(pimat(2,1:6))*100;
tran2(5,:)   = [pimat(4,1),sum(pimat(4,5:6)),pimat(4,3),pimat(4,2), ...
                pimat(4,4)]/sum(pimat(4,1:6))*100;

ii           = [1,3,5,6];
tran3        = zeros(3);
tran3(1,:)   = [sum(sum(pimat(ii,ii))),sum(pimat(ii,2)),sum(pimat(ii,4))]/ ...
                sum(sum(pimat(ii,1:6)))*100;
tran3(2,:)   = [sum(pimat(2,ii)),pimat(2,[2,4])]/sum(pimat(2,1:6))*100;
tran3(3,:)   = [sum(pimat(4,ii)),pimat(4,[2,4])]/sum(pimat(4,1:6))*100;


clc
disp('Counts')
disp('======')
disp(' ')
disp('                                    In t+1:')
disp('  In t:         Prop  G-Part L-Part  S-Corp  LLC  C-Corp  Other  Exit  Unknown')
ii           = [1,5,6,3,2,4,7,8,9];
disp(sprintf(' Proprietor  %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(1,ii)))
disp(sprintf(' G-Partner   %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(5,ii)))
disp(sprintf(' L-Partner   %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(6,ii)))
disp(sprintf(' S-Corp      %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(3,ii)))
disp(sprintf(' LLC         %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(2,ii)))
disp(sprintf(' C-Corp      %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(4,ii)))
disp(sprintf(' Other       %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(7,ii)))
disp(sprintf(' Unknown     %7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f%7.0f',count(8,ii)))

disp(' ')
disp(' ')
disp('Revenue-weighted Transition Matrix')
disp('==================================')
disp(' ')
disp('                                    In t+1:')
disp('  In t:         Prop  G-Part L-Part  S-Corp  LLC  C-Corp  Other  Exit  Unknown')
disp(sprintf(' Proprietor  %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(1,ii)))
disp(sprintf(' G-Partner   %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(5,ii)))
disp(sprintf(' L-Partner   %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(6,ii)))
disp(sprintf(' S-Corp      %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(3,ii)))
disp(sprintf(' LLC         %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(2,ii)))
disp(sprintf(' C-Corp      %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(4,ii)))
disp(sprintf(' Other       %7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f%7.1f',tran1(7,ii)))


disp(' ')
disp(' ')
disp('Revenue-weighted Transition Matrix (Continuing businesses)')
disp('==========================================================')
disp(' ')
disp('                                    In t+1:')
disp('  In t:          Proprietor    Partner      S-Corp        LLC       C-Corp ')
disp(sprintf(' Proprietor  %12.1f%12.1f%12.1f%12.1f%12.1f',tran2(1,:)))
disp(sprintf(' Partner     %12.1f%12.1f%12.1f%12.1f%12.1f',tran2(2,:)))
disp(sprintf(' S-Corp      %12.1f%12.1f%12.1f%12.1f%12.1f',tran2(3,:)))
disp(sprintf(' LLC         %12.1f%12.1f%12.1f%12.1f%12.1f',tran2(4,:)))
disp(sprintf(' C-Corp      %12.1f%12.1f%12.1f%12.1f%12.1f',tran2(5,:)))


disp(' ')
disp(' ')
disp('Simplified Revenue-weighted Transition Matrix (Continuing businesses)')
disp('====================================================================')
disp(' ')
disp('                                    In t+1:')
disp('  In t:                Pass-through        LLC          C-Corp ')
disp(sprintf(' Pass-through   %15.1f%15.1f%15.1f%',tran3(1,:)))
disp(sprintf(' LLC            %15.1f%15.1f%15.1f%',tran3(2,:)))
disp(sprintf(' C-Corp         %15.1f%15.1f%15.1f%',tran3(3,:)))
disp(' ')
disp(' ')
