function ftest=pftest_bsr(y,z,i,q,bigt,datevec,robust,x,p,hetdat,hetvar,wl,wh)

% This code modifies pftest.m in Bai Perron (1998) code to accomodate the
% band spectral regression approach suggested by Yamamoto and Perron (2013)

rsub=zeros(i,i+1);
j=1;
while j <= i
    rsub(j,j)=-1;
    rsub(j,j+1)=1;
    j=j+1;
end
rmat=kron(rsub,eye(q));
[ybar zbar zf na]=pyzbar_bsr(y,z,i,datevec(1:i,i),wl,wh);
if p==0
    delta=olsqr(ybar,zbar);
else
    dbdel=olsqr(ybar,[zbar x]);
    delta=dbdel(1:(i+1)*q,1);
end

vdel=pvdel_bsr(y,z,i,q,bigt,datevec(1:i,i),robust,x,p,0,hetdat,hetvar,wl,wh);

fstar=delta'*rmat'*inv(rmat*vdel*rmat')*rmat*delta;
ftest=(sum(na)-(i+1)*q-p)*fstar/(bigt*i);

