%%%%%%%%%%%%%%%%%%%%%%%%
%%% DATABASE PREPARATION
%%%%%%%%%%%%%%%%%%%%%%%%
close all;
clear all;

addpath('../infrastructure/model');
addpath('../infrastructure/tools');

% readmodel;
load('../infrastructure/model/model.mat');

%% Load quarterly data
% Command 'processfile' loads the data from a 'csv' file (saved in Excel
% as .csv in the current directory) and converts base and gpm data into qpm 
% data along with a few data transformations. 'limit=' is the date range to 
% be read, and 'dbadd=' specifies any database that should be
% appended to the loaded data (this is necessary when loading gpm data for 
% example as the rand/dollar ER from base is needed to calculate the G3 
% NEER). All the data are available in the database 'd' 

% Get the assumptions on oil, food, commodtities, etc 
d = processfile('data/May_MPC_assumptions.csv' , 'assumptions', ...
    'limit=', qq(1980,1):qq(2020,4));

% Get the base data from the core model
d = processfile('data/May_MPC_base.csv' , 'base', ...
    'limit=', qq(1980,1):qq(2017,4), 'dbadd=', d);

% Get the disaggregated CPI inflation data and weights
d = processfile('data/May_MPC_DIM.csv' , 'dim', ...
    'limit=', qq(1980,1):qq(2020,4), 'dbadd=', d);

% Get the foreign economy forecast from the GPM
d = processfile('data/WGDP_Assumption_May 2018 MPC.csv', 'gpm', ...
    'limit=', qq(2000,1):qq(2020,4), 'dbadd=', d);

% Add the yield curve and CDS data to the database
d = dbmerge(d,dbload('data/May_MPC_yields.csv'));

%% Seasonal adjustment
list = dbnames(d);

for i = 1:length(list)
    if length(list{i})>1
        if strcmp('_u', list{i}(end-1:end))
            d.(list{i}(1:end-2)) = x12(d.(list{i}), Inf, 'mode', 'm');
            d = rmfield(d, list{i});
        end
    end
end

% rebase CPI series REMEMBER SOME ARE SEASONALLY ADJUSTED 
d.cpi_food    = d.cpi_food/d.cpi_food(qq(2016,4))*100;
d.cpi_petr    = d.cpi_petr/d.cpi_petr(qq(2016,4))*100;
d.cpi_elec    = d.cpi_elec/d.cpi_elec(qq(2016,4))*100;
d.cpi_serv    = d.cpi_serv/d.cpi_serv(qq(2016,4))*100;
d.cpi_goodsx  = d.cpi_goodsx/d.cpi_goodsx(qq(2016,4))*100;
d.cpi_statssa = d.cpi_statssa/d.cpi_statssa(qq(2016,4))*100;
            
% calculate CPI multiplicatively from the bottom up
d.cpi = d.cpi_food^d.w_food ...
    * d.cpi_elec^d.w_elec ...
    * d.cpi_petr^d.w_petr ...
    * d.cpi_goodsx^d.w_goodsx ...
    * d.cpi_serv^d.w_serv;

% Determine the difference with the Official CPI series
d.cpi_res = d.cpi_statssa - d.cpi;
                
%% Make log of variables
exceptions = {'rn','rn8','rn20','rn40','prem','x_rn','lx_gdp_gap',...
    'dot_x_gdp_eq'};

list = dbnames(d);

for i = 1:length(list)
    if isempty(strmatch(list{i},exceptions,'exact'))
        d.(['l' list{i}]) = 100*log(d.(list{i}));
    end
end

%% Define the real exchange rate
d.lz = d.ls + d.lx_cpi - d.lcpi;

%% Growth rate qoq, yoy
exceptions = {'rn','x_rn','cpi_ber','lx_gdp_gap', 'dot_x_gdp_eq'};

list = dbnames(d);

for i = 1:length(list)
    if isempty(strmatch(list{i}, exceptions, 'exact'))
        if length(list{i})>1
            if strcmp('l', list{i}(1:1))
                d.(['dot_' list{i}(2:end)])  = 4*(d.(list{i}) - d.(list{i}){-1});
                d.(['dot4_' list{i}(2:end)]) = d.(list{i}) - d.(list{i}){-4};
            end
        end
    end
end

%% Real variables
d.rr       = d.rn      - d.dot4_cpi;
d.x_rr     = d.x_rn    - d.dot4_x_cpi;
d.lqfood   = d.lfood   - d.lx_cpi;
d.lqforcmd = d.lforcmd - d.lx_cpi;
d.lqoil    = d.loil    - d.lx_cpi;

%% Relative prices
d.lrp_serv   = d.lcpi_serv - d.lcpi;
d.lrp_goodsx = d.lcpi_goodsx - d.lcpi;
d.lrp_food   = d.lcpi_food - d.lcpi;
d.lrp_petr   = d.lcpi_petr - d.lcpi;
d.lrp_elec   = d.lcpi_elec - d.lcpi;

%% Inflation target (history)
d.target = tseries(get(d.dot_cpi,'first2last'),4.5);

%% Credibility
% BER 2-year-ahead inflation expectations
d.edot8_cpi_ber = d.lcpi_ber - d.lcpi_ber{-4};

% instantaneous credibility, has a maximum of 1 at d.target=4.5
d.lambda0 = 1-(2/3)*abs(d.edot8_cpi_ber-d.target);

% lambda0 with a floor of zero, lambda=0 above 6 and below 3

d.lambda = tseries(startdate(d.lambda0), max(0,double(d.lambda0)));

%% Save the database
% Database is saved in file 'history.csv'
dbsave(d,'data/history.csv');

rmpath('../infrastructure/model');
rmpath('../infrastructure/tools');

