function []=graph_decomp(z,shock_names,endo_names,i_var,initial_date,groups,labels)
%function []=graph_decomp(z,varlist,initial_period,freq)

% Copyright (C) 2010-2011 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

% if we specify to aggregate some shock contribution
if isempty(groups)  == 0
	nshocks = size(shock_names,1);
	ngroups = size(groups,2);
	endo_nbr = size(z,1);
	gend=size(z,3);
	zg=zeros(endo_nbr,ngroups+2,gend);
	lab=[];
	
	% keeping the last 2 columns of smoothed and initial values
	zg(:,ngroups+1:end,:) = z(:,nshocks+1:end,:);
	
	% aggregating groups
	for i=1:ngroups
		tempg = groups{i};
		% concatinate names in lab variable
		if( length(groups{i}) > 1 )
			templ = char(tempg{1});
			for j=2:length(groups{i})
				templ = [ templ '+' tempg{j} ];
			end;
			lab = char( lab, char(templ));
		else
			lab = char( lab, char(groups{i}));
		end
		
		% finding id number to sum and store in col_id
		col_id=[];
		for xx = 1:length(tempg)
			for j=1:nshocks 
				if strcmp(tempg{xx},strtrim(shock_names(j,:))) 
					col_id=[col_id j];
					j=nshocks;
				end
			end
		end
		% summing the group
		zg(:,i,:) = sum(z(:,col_id,:),2);
	end
	
	% updating for the new dataset
	z = zg;
	shock_names = lab(2:end,:);
end

% adding label names
labels=char(labels);
if isempty(labels)
	labels = char(shock_names,'Initial values');
elseif ( size(labels,1) > (size(cellstr(shock_names),1)+1) || size(labels,1) < size(cellstr(shock_names),1) )
	labels = char(shock_names,'Initial values');
	warning(['SHOCK DECOMPOSITION: The number of labels should be ' num2str(size(shock_names,1)) ' or ' num2str(size(shock_names,1)+1) '.'])
elseif(size(labels,1) == size(shock_names,1))
	labels = char(labels,'Initial values');
end;
	
% number of components equals number of shocks + 1 (initial conditions)
comp_nbr = size(z,2)-1;

gend = size(z,3);
freq = initial_date.freq;
initial_period = initial_date.period + initial_date.subperiod/freq;
x = initial_period-1/freq:(1/freq):initial_period+(gend-1)/freq;

nvar = length(i_var);

for j=1:nvar 
    z1 = squeeze(z(i_var(j),:,:));
    xmin = x(1);
    xmax = x(end);
    ix = z1 > 0; 
    ymax = max(sum(z1.*ix));
    ix = z1 < 0;
    ymin = min(sum(z1.*ix));
	
    if ymax-ymin < 1e-6
        continue
    end
	
    figure('Name',endo_names(i_var(j),:));
    ax=axes('Position',[0.1 0.1 0.6 0.8]);
    axis(ax,[xmin xmax ymin ymax]);
    plot(ax,x(2:end),z1(end,:),'k-','LineWidth',2)
    hold on;
    for i=1:gend 
        i_1 = i-1;
        yp = 0;
        ym = 0;
        for k = 1:comp_nbr
            zz = z1(k,i);
            if zz > 0 
                fill([x(i) x(i) x(i+1) x(i+1)],[yp yp+zz yp+zz yp],k); 
                yp = yp+zz; 
            else
                fill([x(i) x(i) x(i+1) x(i+1)],[ym ym+zz ym+zz ym],k);
                ym = ym+zz;
            end
            hold on;
        end
    end
    plot(ax,x(2:end),z1(end,:),'k-','LineWidth',2)
    hold off;

    axes('Position',[0.75 0.1 0.2 0.8]);
    axis([0 1 0 1]);
    axis off;
    hold on;
    y1 = 0;
    height = 1/comp_nbr;
	
    for i=1:comp_nbr
        fill([0 0 0.2 0.2],[y1 y1+0.7*height y1+0.7*height y1],i);
        hold on
        text(0.3,y1+0.3*height,labels(i,:),'Interpreter','none');
        hold on
        y1 = y1 + height;
    end
    hold off
end