function[finalbreaks,CIL,CIH]=breakCI_gen_det(N,T,y,x,w,db,dnb,b,p,q,tr)
[finalbreaks]=pcdpcsd_gen_det(N,T,y,x,w,db,dnb,b,p,q,tr);
k=[finalbreaks'; T];
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
WTtilde=zeros(T,qs,N);
xtilde=zeros(T,p,N);
ndb=size(db,2);
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];
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;
Omega=zeros(q,q);
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);
    Omega=Omega+w(:,:,i)'*w(:,:,i)/(N*T);
end
deltadenom=WW-WX*(XX\WX');
deltanum=WY-WX*(XX\XY);
delta=deltadenom\deltanum;
SSR=YY-XY'*(XX\XY)-(WY'-XY'*(XX\WX'))*(deltadenom\(WY'-XY'*(XX\WX'))');
sigma2=SSR/(N*(T-p-qs)-p-qs);
deltaf=reshape(delta,q,b+1);
for s=1:b
    L(s)=(N*(deltaf(:,s+1)-deltaf(:,s))'*Omega*(deltaf(:,s+1)-deltaf(:,s)))/sigma2;
    CIL(s,1)=k(s)-floor(11/L(s))-1;
    CIH(s,1)=k(s)+floor(11/L(s))+1;
end




