% ================================================================
% Program: entry2quart_ipd_cumtight.m 
% Purpose: Take annual measure of entry wages and translate to a
% quarterly series.
% 
% Date Started: 29 December  2015
% Date Revised: 14 July      2016
% ================================================================
clear all;

% === Add chowlin command to search path ===
% This most likely only has to be run once. 
path(path,'./TD');

dat=readtable('../input/fixed_effects_ipd_cumtight_entry_wage.csv','Delimiter',',','ReadVariableNames',true);
junk=~isnan(dat.lentry_wage);
dat=dat(junk,:);

fred=readtable('../input/fred4uc2quart_data.csv','Delimiter',',','ReadVariableNames',true);    

% === PREPARE DATA FOR CHOW INTERPOLATION ===
short=ismember(fred.year,dat.year); % ismember returns logical
                                    % array indicating whether
                                    % fred.year is also in dat.year 
fredshort=fred(short,:);
% log variables
fredshort.lrgdp=log(fredshort.rgdp);
fredshort.lempl=log(fredshort.empl);
fredshort.lcomp=log(fredshort.comp);
% SYNTAX: res = chowlin(Y,x,ta,sc,type,opC,rl)
% ------------------------------------------------------------
% OUTPUT: res: a structure
% res.meth ='Chow-Lin';
% res.ta = type of disaggregation
% res.type = method of estimation
% res.opC = option related to intercept
% res.N = nobs. of low frequency data
% res.n = nobs. of high-frequency data
% res.pred = number of extrapolations
% res.sc = frequency conversion between low and high freq.
% res.p = number of regressors (including intercept)
% res.Y = low frequency data
% res.x = high frequency indicators
% res.y = high frequency estimate
% res.y_dt = high frequency estimate: standard deviation
% res.y_lo = high frequency estimate: sd - sigma
% res.y_up = high frequency estimate: sd + sigma
% res.u = high frequency residuals
% res.U = low frequency residuals
% res.beta = estimated model parameters
% res.beta_sd = estimated model parameters: standard deviation
% res.beta_t = estimated model parameters: t ratios
% res.rho = innovational parameter
% res.aic = Information criterion: AIC
% res.bic = Information criterion: BIC
% res.val = Objective function used by the estimation method
% res.wls = Weighted least squares as a function of rho
% res.loglik = Log likelihood as a function of rho
% res.r = grid of innovational parameters used by the estimation method
% ------------------------------------------------------------
% INPUT: Y: Nx1 ---> vector of low frequency data
% x: nxp ---> matrix of high frequency indicators (without intercept)
% ta: type of disaggregation
% ta=1 ---> sum (flow)
% ta=2 ---> average (index)
% ta=3 ---> last element (stock) ---> interpolation
% ta=4 ---> first element (stock) ---> interpolation
% sc: number of high frequency data points for each low frequency data points
% sc= 4 ---> annual to quarterly
% sc=12 ---> annual to monthly
% sc= 3 ---> quarterly to monthly
% type: estimation method:
% type=0 ---> weighted least squares
% type=1 ---> maximum likelihood
% opC: 1x1 option related to intercept
% opc = -1 : pretest intercept significance
% opc = 0 : no intercept in hf model
% opc = 1 : intercept in hf model
% rl: innovational parameter (Optional)
% rl: 0x0 ---> rl=[0.5 0.06 0.99] (e.g. rl=[])
% rl: 1x1 ---> fixed value of rho parameter
% rl: 1x3 ---> [r_min: dr : r_max] search is performed
% on this range, using dr as step.
% ------------------------------------------------------------
% INPUT
Y=dat.lentry_wage; % vector of low-freq data
x=[fredshort.ur fredshort.lempl fredshort.lcomp fredshort.lrgdp];
res=chowlin(Y,x,2,4,0,1,[]);

yearx=fredshort.year;
quarterx=fredshort.quarter;
dateq=yearx+(quarterx-1)./4;
plot(dateq,res.y,'k','LineWidth',3);
hold on;
plot(dateq,res.y_lo,'k:','LineWidth',3);
plot(dateq,res.y_up,'k:','LineWidth',3);
title('Interpolated Log User Cost');
% print -depsc2 \\lsa-research03.m.storage.umich.edu\lsa-research03\richryan\nlsy\slides\fig_entry_ipd_interpolated.eps;
    
% === create table to output ===
forstata=fredshort(:,{'year','quarter'});
forstata.lentry_wageq=res.y;
writetable(forstata,'../input/entry_quarterly_interpolated_ipd_cumtight.csv','Delimiter',',');


exit, clear;