function[F]=h1kcsd_gen_det(N,T,breaks,p,q,y,x,w,db,dnb,phi)
b=size(breaks,2);
ndb=size(db,2);
k=[breaks T]';
R=[eye(q) -eye(q)];
if b>1
    for d=2:b
        R=[R zeros((d-1)*q,q); zeros(q,(d-1)*q) eye(q) -eye(q)];
    end
end
qs=(b+1)*q;
WT=zeros(T,qs,N);
for j=1:N
    WT(:,1:q,j)=[w(1:k(1),:,j);zeros(T-k(1),q)];
    WT(:,q*b+1:q*b+q,j)=[zeros(k(b,1),q);w(k(b,1)+1:T,:,j)];
    if b>1
       for i=2:b
            WT(:,q*(i-1)+1:q*i,j)=[zeros(k(i-1,1),q);w(k(i-1,1)+1:k(i,1),:,j);zeros(T-k(i,1),q)];
        end
    end
end
ndbs=(b+1)*ndb;
D=zeros(T,ndbs,1);
D(:,1:ndb,1)=[db(1:k(1),:,1);zeros(T-k(1),ndb)];
D(:,ndb*b+1:ndb*b+ndb,1)=[zeros(k(b,1),ndb);db(k(b,1)+1:T,:,1)];
if b>1
   for i=2:b
        D(:,ndb*(i-1)+1:ndb*i,1)=[zeros(k(i-1,1),ndb);db(k(i-1,1)+1:k(i,1),:,1);zeros(T-k(i,1),ndb)];
    end
end
D=[D dnb];
% D=ones(T,1);
if p>0
    WTtilde=zeros(T,qs,N);
    xtilde=zeros(T,p,N);
    ytilde=zeros(T,1,N);
    I=eye(T);
    Zbar=[D mean(x,3) mean(WT,3)];
    Q=I-Zbar*((Zbar'*Zbar)\Zbar');
    for i=1:N
        xtilde(:,:,i)=Q*x(:,:,i);
        WTtilde(:,:,i)=Q*WT(:,:,i);
        ytilde(:,:,i)=Q*y(:,:,i);
    end
    WW=zeros(qs,qs);
    WX=zeros(qs,p);
    XX=zeros(p,p);
    WY=zeros(qs,1);
    XY=zeros(p,1);
    YY=0;
    for i=1:N
        WW=WW+WTtilde(:,:,i)'*WTtilde(:,:,i);
        WX=WX+WTtilde(:,:,i)'*xtilde(:,:,i);
        XX=XX+xtilde(:,:,i)'*xtilde(:,:,i);
        WY=WY+WTtilde(:,:,i)'*ytilde(:,:,i);
        XY=XY+xtilde(:,:,i)'*ytilde(:,:,i);
        YY=YY+ytilde(:,:,i)'*ytilde(:,:,i);
    end
    deltadenom=WW-WX*(XX\WX');
    deltanum=WY-WX*(XX\XY);
    delta=deltadenom\deltanum;
    if phi>0
        for i=1:N
            epshat(:,i)=ytilde(:,:,i)-xtilde(:,:,i)*(XX\XY)-WTtilde(:,:,i)*delta+xtilde(:,:,i)*(XX\WX')*delta;
            htilda(:,:,i)=WTtilde(:,:,i)-xtilde(:,:,i)*(XX\WX');
            for t=1:T
                h(t,:,i)=htilda(t,:,i)*epshat(t,i);
            end
        end
        pt=floor(4*(T/100)^(2/9));
        Psij=zeros(qs,qs,pt+1);
        for j=1:pt+1
            for i=1:N
                for t=(j-1)+1:T
                    Psij(:,:,j)=Psij(:,:,j)+h(t,:,i)'*h(t-(j-1),:,i)/(N*T);
                end
            end
        end
        PsiV=Psij(:,:,1);
        for j=2:pt+1
            PsiV=PsiV+(1-(j-1)/(pt+1))*(Psij(:,:,j)+Psij(:,:,j)');
        end
        Vartheta=((deltadenom\PsiV)/deltadenom)*(N*T); 
        F=(delta'*R'*((R*Vartheta*R')\(R*delta)))/(b*q);
    else
        SSR=YY-XY'*(XX\XY)-(WY'-XY'*(XX\WX'))*(deltadenom\(WY'-XY'*(XX\WX'))');
        sigma2=SSR/(N*(T-p-qs)-p-qs);
        F=(delta'*R'*((R*(deltadenom\R'))\(R*delta)))/(b*q*sigma2);
    end
else
    WTtilde=zeros(T,qs,N);
    ytilde=zeros(T,1,N);
    I=eye(T);
    Zbar=[D mean(WT,3)];
    Q=I-Zbar*((Zbar'*Zbar)\Zbar');
    for i=1:N
        WTtilde(:,:,i)=Q*WT(:,:,i);
        ytilde(:,:,i)=Q*y(:,:,i);
    end
    WW=zeros(qs,qs);
    WY=zeros(qs,1);
    YY=0;
    for i=1:N
        WW=WW+WTtilde(:,:,i)'*WTtilde(:,:,i);
        WY=WY+WTtilde(:,:,i)'*ytilde(:,:,i);
        YY=YY+ytilde(:,:,i)'*ytilde(:,:,i);
    end
    deltadenom=WW;
    deltanum=WY;
    delta=deltadenom\deltanum;
    if phi>0
        for i=1:N
            epshat(:,i)=ytilde(:,:,i)-WTtilde(:,:,i)*delta;
            htilda(:,:,i)=WTtilde(:,:,i);
            for t=1:T
                h(t,:,i)=htilda(t,:,i)*epshat(t,i);
            end
        end
        pt=floor(4*(T/100)^(2/9));
        Psij=zeros(qs,qs,pt+1);
        for j=1:pt+1
            for i=1:N
                for t=(j-1)+1:T
                    Psij(:,:,j)=Psij(:,:,j)+h(t,:,i)'*h(t-(j-1),:,i)/(N*T);
                end
            end
        end
        PsiV=Psij(:,:,1);
        for j=2:pt+1
            PsiV=PsiV+(1-(j-1)/(pt+1))*(Psij(:,:,j)+Psij(:,:,j)');
        end
        Vartheta=((deltadenom\PsiV)/deltadenom)*(N*T); 
        F=(delta'*R'*((R*Vartheta*R')\(R*delta)))/(b*q);
    else
        SSR=YY-(WY'*(deltadenom\WY));
        sigma2=SSR/(N*(T-qs)-qs);
        F=(delta'*R'*((R*(deltadenom\R'))\(R*delta)))/(b*q*sigma2);
    end
end