% makes transition matrices out of ghx and ghu for Kalman filter
% still needs to eliminate unobserved static variables

function [A,B] = kalman_transition_matrix(dr)
  global ykmin_ exo_nbr 
  k1 = dr.nfwrd+dr.nstatic;
  nx = size(dr.ghx,2)+k1;
  kstate = dr.kstate;
  ikx = [dr.nstatic+1:dr.nstatic+dr.npred];
  
  A = zeros(nx,nx);
  k0 = kstate(find(kstate(:,2) <= ykmin_+1),:);
  i0 = find(k0(:,2) == ykmin_+1);
  i1 = [1:size(dr.ghx,1)];
  A(i1,k1+1:nx) = dr.ghx;
  B = zeros(nx,exo_nbr);
  B(i1,:) = dr.ghu;
  for i=ykmin_:-1:2
    i1 = find(k0(:,2) == i);
    n1 = size(i1,1);
    j = zeros(n1,1);
    for j1 = 1:n1
      j(j1) = find(k0(i0,1)==k0(i1(j1),1));
    end
    A(k1+i1,k1+i0(j))=eye(n1);
    i0 = i1;
  end
