function [newshocklowalt,newshockhighalt] = tvpnewsperc(impulses_unanticipated,IRFmean,VVVmean,M,t,maxh,nhor,p)


IRFlow = zeros(M^2,nhor,t);
IRFhigh = zeros(M^2,nhor,t);
newshocklowalt = zeros(M,maxh,t);
newshockhighalt = zeros(M,maxh,t);
% DECOMP_low = zeros(M^2,nhor,t);
% DECOMP_high = zeros(M^2,nhor,t);
% NEWSDECOMP_low = zeros(M,nhor,t);
% NEWSDECOMP_high = zeros(M,nhor,t);
pp=16;
for irep = 1:t
    if mod(irep,round(t./12))==0           
        disp([num2str(100*(irep/t)) '% completed'])
    end    
    IRFFc = squeeze(impulses_unanticipated(:,:,irep,:));
%     IRFmean = squeeze(median(impulses_unanticipated(:,:,irep,:),4));
    [SEDalt, low, high] = impln(IRFFc,M,maxh,pp);
    
    IRFlow(:,:,irep) = IRFmean(:,1:nhor,irep) - SEDalt;
    IRFhigh(:,:,irep) = IRFmean(:,1:nhor,irep) + SEDalt;
    
%     [DECOMPlow,TOTlow] = vardecomp_eric(IRFlow(:,:,irep),nhor,p,M);
%     [DECOMPhigh,TOThigh] = vardecomp_eric(IRFhigh(:,:,irep),nhor,p,M);

    V9low = low((M+1:M:(M-1)*M+1),1:maxh)' ;
    V9high = high((M+1:M:(M-1)*M+1),1:maxh)';

    WWWlow = zeros(M-1);
    WWWhigh = zeros(M-1);

    for j = 1:maxh
        JJlow = (maxh+1-j)*V9low(j,:)'*V9low(j,:);   
        JJhigh = (maxh+1-j)*V9high(j,:)'*V9high(j,:);
            
        WWWlow = WWWlow+JJlow;   
        WWWhigh = WWWhigh+JJhigh;
    end
    
    [Qlow,QQlow] = eig(WWWlow);
    [Qhigh,QQhigh] = eig(WWWhigh);
    
    RQlow = diag(QQlow);
    RQhigh = diag(QQhigh);

    [maxvlow,maxilow] = max(RQlow);
    [maxvhigh,maxihigh] = max(RQhigh);

    xlow = [0 Qlow(:,maxilow)'];
    xhigh = [0 Qhigh(:,maxihigh)'];

    for i = 1:M
        newshocklowalt(i,:,irep) = IRFlow((0:M:(M-1)*M)+i,1:nhor,irep)'*VVVmean(irep,:)';    
        newshockhighalt(i,:,irep) = IRFhigh((0:M:(M-1)*M)+i,1:nhor,irep)'*VVVmean(irep,:)';
    end
    
%     if newshocklowalt(1,maxh,irep)<0
%         newshocklowalt(:,:,irep) = -newshocklowalt(:,:,irep);
%     end
%     
%     if newshockhighalt(1,maxh,irep)<0
%         newshockhighalt(:,:,irep) = -newshockhighalt(:,:,irep);
%     end
    % calculate variance decomposition
    
%     SIRFlow = newshocklowalt.^2;
%     SIRFhigh = newshockhighalt.^2;
%     for i = 1:M           
%         SIRFlow(i,:) = cumsum(SIRFlow(i,:));
%         SIRFhigh(i,:) = cumsum(SIRFhigh(i,:));
%     end
%         
%     NEWDECOMPlow = zeros(M,maxh);
%     NEWDECOMPhigh = zeros(M,maxh);
%     for i = 1:M
%         for j = 1:maxh           
%             NEWDECOMPlow(i,j) = SIRFlow(i,j)/TOTlow(j,i);
%             NEWDECOMPhigh(i,j) = SIRFhigh(i,j)/TOThigh(j,i); 
%         end
%     end
%     DECOMP_low(:,:,irep) = DECOMPlow(:,1:nhor);
%     DECOMP_high(:,:,irep) = DECOMPhigh(:,1:nhor);
%     NEWSDECOMP_low(:,:,irep) = NEWDECOMPlow(:,1:nhor); 
%     NEWSDECOMP_high(:,:,irep) = NEWDECOMPhigh(:,1:nhor); 


end