function log_like = log_like_kalman(Y,F,Q,H,R,S,A,F1,Q1,H1,R1,S1,A1,Tbreak)
%log_like = log_like_kalman(Y,F,Q,H,R,S,A,F1,Q1,H1,R1,S1,A1,Tbreak)
%Evaluates the log likelihood function at the sample Y using the Kalman filter. The model is:
% x_t+1 = F x_t + v_t+1
% y_t = A'*s_t + H'x_t + w_t 
%var(v_t) = Q
%var(w_t) = R
% x_t is nx by 1
% F is nx by nx
% v_t is nx by 1
% y_t is ny by 1
% H' is ny by nx
% s_t is ns by 1
% A' is ny by ns
% w_t is ny by 1
% Q is nx by nx
% R is ny by ny
%Y is a T by ny matrix containing T observations of y_t
%S is a T by ns matrix containing T observations of s_t
% See Hamilton, page 372
%© Stephanie Schmitt-Grohé and Martín Uribe, 27-Feb-2017.

if nargin<7
%S = 0*Y;
%A = 0*H;
S = 0*Y+1;
A = zeros(size(Y,2));
end
%Number of Observations
T = size(Y,1);

if length(Tbreak)==1
Tbreak(2) = T;
end

%Rows of transition matrix
r = size(F,1);
n = size(Y,2);

%Initial forecast
XI_10 = zeros(r,1);

%MSE of initial forecast
%P_10 = zeros(r,r);
%P_10(:) = (eye(r^2) - kron(F,F))\Q(:);
%Doubling algorithm
F_old=F;
Q_old=Q;
P_10_old=eye(size(F));
diferenz=0.1;
while diferenz>1e-25;
P_10 =F_old*P_10_old*F_old' + Q_old;

diferenz = max(max(abs(P_10-P_10_old)));
Q_old=F_old*Q_old*F_old' + Q_old;
F_old = F_old * F_old;
P_10_old=P_10;
end    %while diferenz

XIold = XI_10;
Pold = P_10;
log_like = 0;

tmp_const = n*0.5 * log(2*pi);

F0=F;Q0=Q;H0=H;R0=R;S0=S;A0=A;

Yprime = Y';

for t=1:T
if t>= Tbreak(1) & t<=Tbreak(2)
F=F1;Q=Q1;H=H1;R=R1;S=S1;A=A1;
else
F=F0;Q=Q0;H=H0;R=R0;S=S0;A=A0;
end
Fprime = F';
Hprime = H';
Aprime = A';
Sprime = S';
    y = Yprime(:,t);
    s = Sprime(:,t);

    dev_y = y - Aprime*s -  Hprime*XIold;

    tmp1 = Hprime*Pold;
    var_y = tmp1*H+R;

    ivar_y = var_y \ eye(n);

    log_like = log_like - tmp_const -0.5 * log(det(var_y)) - 0.5 *dev_y'*ivar_y*dev_y;

    tmp_fp = F * Pold;
    tmp2 = tmp_fp * H * ivar_y;
    XIold = F*XIold  + tmp2 *dev_y;

    Pold = (tmp_fp - tmp2 * tmp1) * Fprime + Q;

end

if ~isreal(log_like) | isnan(log_like) | ~isfinite(log_like)
log_like = -inf;
end
