% Summary Calculations 
% June 5, 2015
%


clear all;
small = 1.0e-10;
big = 1.0e+6;                  
rng(63761);
 
  % -- File Directories  
  outdir = '/Users/mwatson/Dropbox/p_comp/ddisk/matlab/out/';
  figdir = '/Users/mwatson/Dropbox/p_comp/ddisk/matlab/fig/';
  matdir = '/Users/mwatson/Dropbox/p_comp/ddisk/matlab/mat/';
  outfile_name = [outdir 'Table_2.out'];
  fileID = fopen(outfile_name,'w');
  
  % -- Read in Data --- 
  load_data = 1;  % 1 if reloading data from Excel, etc 
  pcomp_data_calendar_mtoq;
  
  % Dates
  first_date = [1960 1];
  last_date = [2015 1];
  ismpl_60_end = smpl(calvec,first_date,last_date,4);
  cal_60_end = calvec(ismpl_60_end==1);
  dnobs_60_end = size(cal_60_end,1);
  
  dp_agg_60_end = dp_agg(ismpl_60_end==1,:);
  dp_agg_xe_60_end = dp_agg_xe(ismpl_60_end==1,:);
  dp_agg_xfe_60_end = dp_agg_xfe(ismpl_60_end==1,:);
  dp_disagg_60_end = dp_disagg(ismpl_60_end==1,:);
  share_avg_60_end = share_avg(ismpl_60_end==1,:);
  
mlabel = '_mucsv_tvp_17c';
  str_tmp = [matdir 'calvec_ismpl' mlabel]; load(str_tmp);
  % check dates
  tmp = calvec_ismpl-cal_60_end;
  if sum(tmp) ~= 0;
    error('Dates not synchronized');
  end;  
  str_tmp = [matdir 'agg_f_tau_mean_sd' mlabel]; load(str_tmp);
  str_tmp = [matdir 'agg_tau_mean_sd' mlabel]; load(str_tmp);
  tau_f_17c = agg_f_tau_mean_sd(:,1);
  tau_s_17c = agg_tau_mean_sd(:,1);
  str_tmp = [matdir 'agg_f_tau_mean_pct' mlabel]; load(str_tmp);
  str_tmp = [matdir 'agg_tau_mean_pct' mlabel]; load(str_tmp);
  tau_f_mean_pct_17c = agg_f_tau_mean_pct;
  tau_s_mean_pct_17c = agg_tau_mean_pct;
  str_tmp = [matdir 'disagg_f_tau_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'disagg_tau_mean' mlabel]; load(str_tmp);
  dis_tau_f_17c = disagg_f_tau_mean;
  dis_tau_s_17c = disagg_tau_mean;
  % Compute 3-Components from 17
  s = share_avg(ismpl_60_end==1,:);
  ifood = 5;
  ienergy = [7 10];
  icore = [1:4 6 8:9 11:17];
  s_energy = s(:,ienergy);
  s_energy = s_energy./repmat(sum(s_energy,2),1,2);
  s_core = s(:,icore);
  s_core = s_core./repmat(sum(s_core,2),1,14);
  tau_f_17c_food = dis_tau_f_17c(:,ifood);
  tau_f_17c_energy = sum(dis_tau_f_17c(:,ienergy).*s_energy,2);
  tau_f_17c_core = sum(dis_tau_f_17c(:,icore).*s_core,2);
  tau_s_17c_food = dis_tau_s_17c(:,ifood);
  tau_s_17c_energy = sum(dis_tau_s_17c(:,ienergy).*s_energy,2);
  tau_s_17c_core = sum(dis_tau_s_17c(:,icore).*s_core,2); 
  % -- Mean Values of Parameters
  str_tmp = [matdir 'alpha_eps_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'alpha_tau_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'alpha_eps_mean_pct' mlabel]; load(str_tmp);
  str_tmp = [matdir 'alpha_tau_mean_pct' mlabel]; load(str_tmp);
  str_tmp = [matdir 'sigma_eps_common_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'sigma_eps_common_total_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'sigma_eps_unique_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'sigma_eps_unique_total_mean' mlabel]; load(str_tmp); 
  str_tmp = [matdir 'sigma_dtau_common_mean' mlabel]; load(str_tmp); 
  str_tmp = [matdir 'sigma_dtau_unique_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'var_eps_common_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'var_eps_common_total_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'var_eps_unique_mean' mlabel]; load(str_tmp);
  str_tmp = [matdir 'var_eps_unique_total_mean' mlabel]; load(str_tmp); 
  str_tmp = [matdir 'var_y_eps_unique_mean_pct' mlabel]; load(str_tmp); 
  str_tmp = [matdir 'var_dtau_common_mean' mlabel]; load(str_tmp); 
  str_tmp = [matdir 'var_dtau_unique_mean' mlabel]; load(str_tmp);
  alpha_eps_17c = alpha_eps_mean;
  alpha_tau_17c = alpha_tau_mean;
  alpha_eps_pct_17c = alpha_eps_mean_pct;
  alpha_tau_pct_17c = alpha_tau_mean_pct;
  sigma_eps_common_17c = sigma_eps_common_mean;
  sigma_eps_common_total_17c = sigma_eps_common_total_mean;
  sigma_eps_unique_17c = sigma_eps_unique_mean;
  sigma_eps_unique_total_17c = sigma_eps_unique_mean;
  sigma_dtau_common_17c = sigma_dtau_common_mean;
  sigma_dtau_unique_17c = sigma_dtau_unique_mean;
  
  var_eps_common_17c = var_eps_common_mean;
  var_eps_common_total_17c = var_eps_common_total_mean;
  var_eps_unique_17c = var_eps_unique_mean;
  var_eps_unique_total_17c = var_eps_unique_total_mean;
  var_eps_unique_pct_17c = var_y_eps_unique_mean_pct;
  var_dtau_common_17c = var_dtau_common_mean;
  var_dtau_unique_17c = var_dtau_unique_mean;
  
% % -- Figures 7-9
% % --- Figure 7
% j = 14;
% %j = 5;
% %j = 2;
% alpha_eps_j = reshape(alpha_eps_pct_17c(:,:,j),dnobs_60_end,6);
% alpha_tau_j = reshape(alpha_tau_pct_17c(:,:,j),dnobs_60_end,6);
% sigma_eps_j = sqrt(reshape(var_eps_unique_pct_17c(:,:,j),dnobs_60_end,6));
% figure;
% subplot(2,2,1);
%    plot(cal_60_end,dp_agg_60_end,': b','LineWidth',2);
%    hold on;
%      plot(cal_60_end,dp_disagg_60_end(:,j),'- k','LineWidth',1);
%      plot(cal_60_end,dis_tau_s_17c(:,j),'-- r','LineWidth',3);
%    hold off;
%  
%   tmp = char(labelvec_short_disagg(j));
%   titstr =['(a) ' tmp ': Inflation rates and trend']; 
%   title(titstr);
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,2);
%   plot(cal_60_end,alpha_tau_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,alpha_tau_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,alpha_tau_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(b) \alpha_{\tau}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,3);
%   plot(cal_60_end,alpha_eps_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,alpha_eps_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,alpha_eps_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(c) \alpha_{\epsilon}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,4);
%   plot(cal_60_end,sigma_eps_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,sigma_eps_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,sigma_eps_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(d) \sigma_{\epsilon,{\iti}}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
  
 
% % -- Figure 8 ---------
% j = 5;
% alpha_eps_j = reshape(alpha_eps_pct_17c(:,:,j),dnobs_60_end,6);
% alpha_tau_j = reshape(alpha_tau_pct_17c(:,:,j),dnobs_60_end,6);
% sigma_eps_j = sqrt(reshape(var_eps_unique_pct_17c(:,:,j),dnobs_60_end,6));
% figure;
% subplot(2,2,1);
%    plot(cal_60_end,dp_agg_60_end,': b','LineWidth',2);
%    hold on;
%      plot(cal_60_end,dp_disagg_60_end(:,j),'- k','LineWidth',1);
%      plot(cal_60_end,dis_tau_s_17c(:,j),'-- r','LineWidth',3);
%    hold off;
%  
%   tmp = char(labelvec_short_disagg(j));
%   titstr =['(a) ' tmp ': Inflation rates and trend']; 
%   title(titstr);
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,2);
%   plot(cal_60_end,alpha_tau_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,alpha_tau_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,alpha_tau_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(b) \alpha_{\tau}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,3);
%   plot(cal_60_end,alpha_eps_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,alpha_eps_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,alpha_eps_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(c) \alpha_{\epsilon}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
% subplot(2,2,4);
%   plot(cal_60_end,sigma_eps_j(:,1),'- k','LineWidth',3);
%    hold on;
%      plot(cal_60_end,sigma_eps_j(:,3),'-- k','LineWidth',3);
%      plot(cal_60_end,sigma_eps_j(:,5),'-- k','LineWidth',3);
%    hold off;
%   title('(d) \sigma_{\epsilon,{\iti}}');
%   ax = gca;
%   ax.FontSize = 20;
%   ax.XLim = [1960 2015];
  
  
% --- Figure 9 ----  
j = 2;
alpha_eps_j = reshape(alpha_eps_pct_17c(:,:,j),dnobs_60_end,6);
alpha_tau_j = reshape(alpha_tau_pct_17c(:,:,j),dnobs_60_end,6);
sigma_eps_j = sqrt(reshape(var_eps_unique_pct_17c(:,:,j),dnobs_60_end,6));
figure;
subplot(2,2,1);
   plot(cal_60_end,dp_agg_60_end,': b','LineWidth',2);
   hold on;
     plot(cal_60_end,dp_disagg_60_end(:,j),'- k','LineWidth',1);
     plot(cal_60_end,dis_tau_s_17c(:,j),'-- r','LineWidth',3);
   hold off;
 
  tmp = char(labelvec_short_disagg(j));
  titstr =['(a) ' tmp ': Inflation rates and trend']; 
  title(titstr);
  ax = gca;
  ax.FontSize = 20;
  ax.XLim = [1960 2015];
subplot(2,2,2);
  plot(cal_60_end,alpha_tau_j(:,1),'- k','LineWidth',3);
   hold on;
     plot(cal_60_end,alpha_tau_j(:,3),'-- k','LineWidth',3);
     plot(cal_60_end,alpha_tau_j(:,5),'-- k','LineWidth',3);
   hold off;
  title('(b) \alpha_{\tau}');
  ax = gca;
  ax.FontSize = 20;
  ax.XLim = [1960 2015];
subplot(2,2,3);
  plot(cal_60_end,alpha_eps_j(:,1),'- k','LineWidth',3);
   hold on;
     plot(cal_60_end,alpha_eps_j(:,3),'-- k','LineWidth',3);
     plot(cal_60_end,alpha_eps_j(:,5),'-- k','LineWidth',3);
   hold off;
  title('(c) \alpha_{\epsilon}');
  ax = gca;
  ax.FontSize = 20;
  ax.XLim = [1960 2015];
subplot(2,2,4);
  plot(cal_60_end,sigma_eps_j(:,1),'- k','LineWidth',3);
   hold on;
     plot(cal_60_end,sigma_eps_j(:,3),'-- k','LineWidth',3);
     plot(cal_60_end,sigma_eps_j(:,5),'-- k','LineWidth',3);
   hold off;
  title('(d) \sigma_{\epsilon,{\iti}}');
  ax = gca;
  ax.FontSize = 20;
  ax.XLim = [1960 2015];
  error('tm');

  