function vdel=pvdel_trun(y,z,i,q,bigt,b,robust,x,p,withb,hetdat,hetvar)

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

ev=ones(i+1,1);
[ybar zbar zf]=pyzbar_trun(y,z,i,b);
[na jl jh]=pna_trun(i,b,bigt);

if p==0
    delv=olsqr(ybar,zbar);
    res=ybar-zbar*delv;
    reg=zbar;
else
    delv=olsqr(ybar,[zbar x]);
    res=ybar-[zbar x]*delv;
    
    if withb==0
        reg=zbar-x*inv(x'*x)*x'*zbar;
    else
        reg=[x zbar];
    end
end

if robust==0
    % section on testing with no serial correlation in errors
    
    if p==0
        if hetdat==1 && hetvar==0
            sig=res'*res/bigt;
            vdel=sig*inv(reg'*reg);
        end
        if hetdat==1 && hetvar==1
            sig=psigmq(res,b,q,i,bigt);
            vdel=kron(sig,eye(q))*inv(reg'*reg);
        end
        if hetdat==0 && hetvar==0
            lambda=plambda(b,i,bigt);
            sig=res'*res/bigt;
            vdel=sig*inv(kron(lambda,(zf'*zf)));
        end
        if hetdat==0 && hetvar==1
            lambda=plambda(b,i,bigt);
            sig=psigmq(res,b,q,i,bigt);
            vdel=kron(sig,eye(q))*inv(kron(lambda,(zf'*zf)));
        end
        
    else
        
        if hetdat==0
            disp('the case hetdat=0 is not allowed.');
            disp('vdel is returned as zeros.');
            vdel=zeros(q*(i+1),q*(i+1));
        end
        
        if hetdat==1 && hetvar==0
            sig=res'*res/bigt;
            vdel=sig*inv(reg'*reg);
        end
        
        if hetdat==1 && hetvar==1
            [~,~,xf]=pyzbar_trun(y,x,i,b);
            w=[zbar xf];
            wbar=pzbar(w,i,b);       
            ww=wbar'*wbar;
            sig=psigmq(res,b,q,i,bigt);
            gg=zeros((i+1)*q+p*withb, (i+1)*q+p*withb);
            ie=1;
            while ie<=i+1
                gg=gg+sig(ie,ie)*ww((ie-1)*((i+1)*q+p*withb)+1:ie*((i+1)*q+...
                    p*withb),(ie-1)*((i+1)*q+p*withb)+1:ie*((i+1)*q+p*withb));
                ie=ie+1;
            end
            vdel=inv(reg'*reg)*gg*inv(reg'*reg);
        end
    end
    
else
    if hetdat==0
        disp('the case hetdat=0 is not allowed');
        disp('vdel is returned as zeros.');
        vdel=zeros(q*(i+1),q*(i+1));
    end
    
    if p==0
        if hetvar==1
            hc=zeros((i+1)*q,(i+1)*q);
            vdel=zeros((i+1)*q,(i+1)*q);
            hc(1:q,1:q)=na(1,1)*correcthc(zf(1:na(1,1),1:q),res(1:na(1,1),1));
            j=2;
            while j<=i
                regj=zf(sum(na(1:j-1,1))+1:sum(na(1:j,1)),1:q);
                hc((j-1)*q+1:j*q,(j-1)*q+1:j*q)=na(j,1)*correcthc(regj,...
                    res(sum(na(1:j-1,1))+1:sum(na(1:j,1)),1));
                j=j+1;
            end
            regj=zf(sum(na(1:i,1))+1:sum(na(:,1)),1:q);
            hc(i*q+1:(i+1)*q,i*q+1:(i+1)*q)=na(i,1)*correcthc(regj,...
                res(sum(na(1:i,1))+1:sum(na(:,1)),1));
            vdel=inv(reg'*reg)*hc*inv(reg'*reg);
        else
            hc=correcthc(zf,res);
            lambda=plambda(b,i,bigt);
            vdel=bigt*inv(reg'*reg)*kron(lambda,hc)*inv(reg'*reg);
        end
        
    else
        
        hc=correcthc(reg,res);
        vdel=bigt*inv(reg'*reg)*hc*inv(reg'*reg);
        
    end
end
