%---------------------------------------------%
% Program for the projection of Public Sector
%---------------------------------------------%
clear
PublicSector_INPUT

%---------------------------------------------%
% Variables
%---------------------------------------------%
%
%   KG      -   public capital
%   AG      -   public assets
%   DtG   -   public debt
%   r_debt   -   interest rate for public debt
%   i       -   capital investment rate
%   g       -   rate of growth of GDP
%
%   Yl      -   LaborIncome
%   Ya      -   AssetIncome
%   tau_yl      -   tax rate on labor income
%   tau_ya      -   tax rate on asset income
%   tau_c       -   tax rate on consumption
%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%
%   Read in:
%       - Population by age for all years
%       - Expenditures for different programs in the base year
%       - Labor Income in the base year
%       - Consumption in the base year
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get population data 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load(Country_Population_File)
population_by_age = data/population_constant;
population = sum(population_by_age,2);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get income and consumption data for the base year
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load(LaborIncomeFile);
LaborIncome = data;

load(AssetIncomeFile);
AssetIncome = data;

load(PrivateConsumptionFile);
PrivateConsumption = data;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get expenditure data for the base year
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load(EducationTransfersFile);
PC_Education = data;
load(EducationCashFile);
CT_Education = data;

load(HealthTransfersFile);
PC_Health = data;
load(HealthCashFile);
CT_Health = data;

load(LongTermCareTransfersFile);
PC_LongTermCare = data;
load(LongTermCareCashFile);
CT_LongTermCare = data;

load(PensionCashFile);
CT_Pension = data;

load(OtherTransfersFile) ;
PC_Other = data;
load(OtherCashFile) ;
CT_Other = data;
load(OtherSocialBenefitsCashFile);
CT_OtherSocialBenefits = data;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Sum expenditures by programs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Education_by_age(1,:) = PC_Education + CT_Education;
Health_by_age(1,:) = PC_Health + CT_Health;
LongTermCare_by_age(1,:) = PC_LongTermCare + CT_LongTermCare;
Pension_by_age(1,:) = CT_Pension;
Other_by_age(1,:) = CT_Other + CT_OtherSocialBenefits + PC_Other;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   Base year data to simulation matrices
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Yl_by_age(1,:) = LaborIncome;
Ya_by_age(1,:) = AssetIncome;
C_by_age(1,:) = PrivateConsumption;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   compute population growth rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gn = population(2:numel(population))./population(1:numel(population)-1)-ones(numel(population)-1,1);
gn=gn';
gn(numel(population))=gn(numel(population)-1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   GDP growth rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g_GDP = (ones(1,numel(gn))+gn)*(1+gy)-ones(1,numel(gn));

%---------------------------------------------%
% Setting initial values     
%---------------------------------------------%
year_initial = time_index(YEAR_INITIAL,YEAR_INITIAL,STEP);
year_final = time_index(YEAR_INITIAL,YEAR_FINAL,STEP);
year_span = year_final;

Yl(year_initial) = sum(Yl_by_age(year_initial,:).*population_by_age(year_initial,:));
yl(year_initial) = Yl(year_initial)/population(year_initial);
Ya(year_initial) = sum(Ya_by_age(year_initial,:).*population_by_age(year_initial,:));
asset_income_ratio = Ya(year_initial)/Yl(year_initial);
C(year_initial) = sum(C_by_age(year_initial,:).*population_by_age(year_initial,:)); 
consumption2income_ratio = C(year_initial)/Yl(year_initial);
GDP(year_initial) = 3/2*Yl(year_initial);          
DtG(year_initial) = debt_to_GDP_ratio*GDP(year_initial);       
KG(year_initial) = capital_to_GDP_ratio*GDP(year_initial);
AG(year_initial) = KG(year_initial) + DtG(year_initial);

%---------------------------------------------%
%   Tax calculation in initial year
%---------------------------------------------%
Income_Taxes(year_initial) = tau_yl(year_initial)*Yl(year_initial);
Income_TaxesPLANNED(year_initial) = Income_Taxes(year_initial); 
Asset_Taxes(year_initial) = tau_ya(year_initial)*Ya(year_initial);
Asset_TaxesPLANNED(year_initial) = Asset_Taxes(year_initial);
Consumption_Taxes(year_initial) = tau_c(year_initial)*C(year_initial);
Consumption_TaxesPLANNED(year_initial) = Consumption_Taxes(year_initial);
Taxes(year_initial) = Income_Taxes(year_initial) + Asset_Taxes(year_initial) + Consumption_Taxes(year_initial);
TaxesPLANNED(year_initial) = Taxes(year_initial);
%---------------------------------------------%
%   Benefits calculation in initial year
%---------------------------------------------%
Education_Expenditures(year_initial) = sum(Education_by_age(year_initial,:) .*population_by_age(year_initial,:));
Health_Expenditures(year_initial) = sum(Health_by_age(year_initial,:) .*population_by_age(year_initial,:));
Pension_Expenditures(year_initial) = sum(Pension_by_age(year_initial,:) .*population_by_age(year_initial,:));
LongTermCare_Expenditures(year_initial) = sum(LongTermCare_by_age(year_initial,:) .*population_by_age(year_initial,:));
Other_Expenditures(year_initial) = sum(Other_by_age(year_initial,:) .*population_by_age(year_initial,:));
Total_Expenditures(year_initial) = Other_Expenditures(year_initial) + LongTermCare_Expenditures(year_initial) + Education_Expenditures(year_initial) + Health_Expenditures(year_initial) + Pension_Expenditures(year_initial);
Total_ExpendituresPLANNED(year_initial) = Total_Expenditures(year_initial);

surplus(year_initial) = Taxes(year_initial) - Total_Expenditures(year_initial);
surplusPLANNED(year_initial) = g_GDP(year_initial)*AG(year_initial) - r_debt*DtG(year_initial);
    
    if surplus(year_initial)  < surplusPLANNED(year_initial)
    signal(year_initial) = 1;
    %--------------------------------------
    %   increase taxes
    %   no reduction of benefits in the base year; they have to match real values 
    %--------------------------------------
    taxes_rate_increase = -(surplus(year_initial)-surplusPLANNED(year_initial))/Taxes(year_initial);
    tau_yl(year_initial)= tau_yl(year_initial)*(1+taxes_rate_increase);
    tau_ya(year_initial)= tau_ya(year_initial)*(1+taxes_rate_increase);
    tau_c(year_initial)= tau_c(year_initial)*(1+taxes_rate_increase);
    Income_Taxes(year_initial) = tau_yl(year_initial)*Yl(year_initial);
    Asset_Taxes(year_initial) =tau_ya(year_initial)*Ya(year_initial);
    Consumption_Taxes(year_initial) = tau_c(year_initial)*C(year_initial);
    Taxes(year_initial) = Income_Taxes(year_initial) + Asset_Taxes(year_initial) + Consumption_Taxes(year_initial);
    surplus(year_initial) = Taxes(year_initial) - Total_Expenditures(year_initial);
elseif surplus(year_initial)  > surplusPLANNED(year_initial)
    signal(year_initial) = 2;
    %--------------------------------------
    %   decrease taxes
    %   no increasing of benefits in the base year; they have to match real values 
    %--------------------------------------
    taxes_rate_decrease = (surplus(year_initial)-surplusPLANNED(year_initial))/Taxes(year_initial);
    tau_yl(year_initial)= tau_yl(year_initial)*(1-taxes_rate_decrease);
    tau_ya(year_initial)= tau_ya(year_initial)*(1-taxes_rate_decrease);
    tau_c(year_initial)= tau_c(year_initial)*(1-taxes_rate_decrease);
    Income_Taxes(year_initial) = tau_yl(year_initial)*Yl(year_initial);
    Asset_Taxes(year_initial) =tau_ya(year_initial)*Ya(year_initial);
    Consumption_Taxes(year_initial) = tau_c(year_initial)*C(year_initial);
    Taxes(year_initial) = Income_Taxes(year_initial) + Asset_Taxes(year_initial) + Consumption_Taxes(year_initial);
    surplus(year_initial) = Taxes(year_initial) - Total_Expenditures(year_initial);    
end
%---------------------------------------------%
% Iterative projection for the future years     
%---------------------------------------------%
for year = year_initial+1:year_final
    % GDP grows at g_GDP rate
    GDP(year)= GDP(year-1)*(1+g_GDP(year-1));       
    %   public debt and public capital are a constant ratio of GDP
    DtG(year) = debt_to_GDP_ratio*GDP(year);    
    KG(year) = capital_to_GDP_ratio*GDP(year);
    AG(year) = KG(year) + DtG(year);
   
    % labor income index grows at the rate of technological growth
    yl(year) = yl(year-1)*(1+gy);
    Yl(year) = yl(year)*population(year);
    %   asset income is a constant ratio of labor income
    Ya(year) = Yl(year)*asset_income_ratio;
    C(year) = Yl(year)*consumption2income_ratio;
    %---------------------------------------------%
    %   Tax calculation
    %---------------------------------------------%
    tau_yl(year)= tau_yl(year-1);
    tau_ya(year)= tau_ya(year-1);
    tau_c(year)= tau_c(year-1);
    Income_Taxes(year) = tau_yl(year)*Yl(year);
    Income_TaxesPLANNED(year) = Income_Taxes(year);
    Asset_Taxes(year) =tau_ya(year)*Ya(year);
    Asset_TaxesPLANNED(year) = Asset_Taxes(year);
    Consumption_Taxes(year) = tau_c(year)*C(year);
    Consumption_TaxesPLANNED(year) = Consumption_Taxes(year);
    Taxes(year) = Income_Taxes(year) + Asset_Taxes(year) + Consumption_Taxes(year);
    %---------------------------------------------%
    %   Benefits calculation
    %---------------------------------------------%
    Education_by_age(year,:) = Education_by_age(year-1,:)*(1+lambda);
    Pension_by_age(year,:) = Pension_by_age(year-1,:)*(1+lambda);
    Health_by_age(year,:) = Health_by_age(year-1,:)*(1+lambda);
    LongTermCare_by_age(year,:) = LongTermCare_by_age(year-1,:)*(1+lambda);
    Other_by_age(year,:) = Other_by_age(year-1,:)*(1+lambda);
    
    Education_Expenditures(year) = sum(Education_by_age(year,:) .*population_by_age(year,:));
    Health_Expenditures(year) = sum(Health_by_age(year,:) .*population_by_age(year,:));
    Pension_Expenditures(year) = sum(Pension_by_age(year,:) .*population_by_age(year,:));
    LongTermCare_Expenditures(year) = sum(LongTermCare_by_age(year,:) .*population_by_age(year,:));
    Other_Expenditures(year) = sum(Other_by_age(year,:) .*population_by_age(year,:));
    
    Total_Expenditures(year) = Other_Expenditures(year) + LongTermCare_Expenditures(year) + Education_Expenditures(year) + Health_Expenditures(year) + Pension_Expenditures(year);
    Total_ExpendituresPLANNED(year) = Total_Expenditures(year);
    surplus(year) = Taxes(year) - Total_Expenditures(year);
    surplusPLANNED(year) = g_GDP(year)*AG(year) - r_debt*DtG(year);

    if surplus(year) < surplusPLANNED(year)
        signal(year) = 1;
        %--------------------------------------
        %   increase taxes
        %--------------------------------------
        taxes_rate_increase = -0.5*(surplus(year)-surplusPLANNED(year))/Taxes(year);
        tau_yl(year)= tau_yl(year)*(1+taxes_rate_increase);
        tau_ya(year)= tau_ya(year)*(1+taxes_rate_increase);
        tau_c(year)= tau_c(year)*(1+taxes_rate_increase);
        Income_Taxes(year) = tau_yl(year)*Yl(year);
        Asset_Taxes(year) =tau_ya(year)*Ya(year);
        Consumption_Taxes(year) = tau_c(year)*C(year);
        Taxes(year) = Income_Taxes(year) + Asset_Taxes(year) + Consumption_Taxes(year);
        %--------------------------------------
        %   reduce benefits 
        %--------------------------------------
        benefits_proportion_decrease = 1+0.5*(surplus(year)-surplusPLANNED(year))/Total_Expenditures(year);
        Education_by_age(year,:) = Education_by_age(year,:)*benefits_proportion_decrease;
        Health_by_age(year,:) = Health_by_age(year,:)*benefits_proportion_decrease;
        Pension_by_age(year,:) = Pension_by_age(year,:)*benefits_proportion_decrease;
        LongTermCare_by_age(year,:) = LongTermCare_by_age(year,:)*benefits_proportion_decrease;
        Other_by_age(year,:) = Other_by_age(year,:)*benefits_proportion_decrease;
    
        Education_Expenditures(year) = sum(Education_by_age(year,:) .*population_by_age(year,:));
        Health_Expenditures(year) = sum(Health_by_age(year,:) .*population_by_age(year,:));
        Pension_Expenditures(year) = sum(Pension_by_age(year,:) .*population_by_age(year,:));
        LongTermCare_Expenditures(year) = sum(LongTermCare_by_age(year,:) .*population_by_age(year,:));
        Other_Expenditures(year) = sum(Other_by_age(year,:) .*population_by_age(year,:));
    
        Total_Expenditures(year) = Other_Expenditures(year) + LongTermCare_Expenditures(year) + Education_Expenditures(year) + Health_Expenditures(year) + Pension_Expenditures(year);
        Total_ExpendituresCheck(year) = Total_ExpendituresPLANNED(year)*benefits_proportion_decrease;
        surplus(year) = Taxes(year) - Total_Expenditures(year);
        sur = surplus(year);
        sur2=sur;
    elseif surplus(year) > surplusPLANNED(year)
        signal(year) = 2;
        %--------------------------------------
        %   decrease taxes
        %--------------------------------------
        taxes_rate_decrease = 0.5*(surplus(year)-surplusPLANNED(year))/Taxes(year);
        tau_yl(year)= tau_yl(year)*(1-taxes_rate_decrease);
        tau_ya(year)= tau_ya(year)*(1-taxes_rate_decrease);
        tau_c(year)= tau_c(year)*(1-taxes_rate_decrease);
        Income_Taxes(year) = tau_yl(year)*Yl(year);
        Asset_Taxes(year) =tau_ya(year)*Ya(year);
        Consumption_Taxes(year) = tau_c(year)*C(year);
        Taxes(year) = Income_Taxes(year) + Asset_Taxes(year) + Consumption_Taxes(year);
        %--------------------------------------
        %   increase benefits 
        %--------------------------------------
        benefits_proportion_increase = 1+0.5*(surplus(year)-surplusPLANNED(year))/Total_Expenditures(year);
        Education_by_age(year,:) = Education_by_age(year,:)*benefits_proportion_increase;
        Health_by_age(year,:) = Health_by_age(year,:)*benefits_proportion_increase;
        Pension_by_age(year,:) = Pension_by_age(year,:)*benefits_proportion_increase;
        LongTermCare_by_age(year,:) = LongTermCare_by_age(year,:)*benefits_proportion_increase;
        Other_by_age(year,:) = Other_by_age(year,:)*benefits_proportion_increase;
    
        Education_Expenditures(year) = sum(Education_by_age(year,:) .*population_by_age(year,:));
        Health_Expenditures(year) = sum(Health_by_age(year,:) .*population_by_age(year,:));
        Pension_Expenditures(year) = sum(Pension_by_age(year,:) .*population_by_age(year,:));
        LongTermCare_Expenditures(year) = sum(LongTermCare_by_age(year,:) .*population_by_age(year,:));
        Other_Expenditures(year) = sum(Other_by_age(year,:) .*population_by_age(year,:));
    
        Total_Expenditures(year) = Other_Expenditures(year) + LongTermCare_Expenditures(year) + Education_Expenditures(year) + Health_Expenditures(year) + Pension_Expenditures(year);
        Total_ExpendituresCheck(year) = Total_ExpendituresPLANNED(year)*benefits_proportion_increase;
        surplus(year) = Taxes(year) - Total_Expenditures(year);
    end

    i(year-1)=(KG(year) - (1-delta)*KG(year-1))/GDP(year-1);  
end






