%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                   %
%         Godley and Lavoie         %
%        MONETARY ECONOMICS         %
%             2nd edition           %
%                                   %
%          SIMEX model              %
%           Chapter 3               %
%                                   %
%  code by  Anthony Bonen           %
%           March 2014              %
%                                   %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

close all
clear all

T = 30;            % Number of periods (e.g. years)

% Parameters
theta = 0.2;        % tax rate
alpha1 = 0.6;       % marginal propensity to consume from dispoable income
alpha2 = 0.4;       % marginal propensity to consume from assets

% Endogenous Variables - preallocated
C = zeros(T,1);     % Consumption
H = zeros(T,1);     % Stock of wealth
Y = zeros(T,1);     % Gross Output/Income (expenditure defition)
YD = zeros(T,1);    % Disposable income
YDe = zeros(T,1);   % Expected Disposable Income
tax = zeros(T,1);   % Tax revenue

dHs = zeros(T,1);   % Financial assets - supply  flow
dHd = zeros(T,1);   % Financial assets - desired flow
dHh = zeros(T,1);   % Financial assets - holding flow
dH  = zeros(T,1);   % Financial assets - actual change

% Exogenous Variable - preallocated
G = 20*ones(T,1);   % Government expenditure

% Initialization
YDe(2) = (1-theta)*G(1);



fprintf('\n');
fprintf('______________________________________________________________________________________\n');
fprintf('     | Income  |Disposable| Expected  |   Tax   |Consumption|       Financial        |\n');
fprintf('iter.|         |  Income  |Disp. Inc. | Revenue |           | stock    flows  desired|\n');
fprintf('  [t]|  [Y]    |   [YD]   |   [YDe]   |  [tax]  |    [C]    |  [H]      [dH]   [dHd] |\n');
fprintf('-----|---------|----------|-----------|---------|-----------|------------------------|\n');
fprintf('%3d  |  %6.2f |   %3.2f   |   %6.2f  |  %5.2f  |   %6.2f  | %6.2f  %6.2f  %6.2f |', 0, ...
      Y(1), YD(1), YDe(1), tax(1), C(1), H(1), dH(1), dHd(1));
fprintf('\n')


for t = 2:T
    YI   = G(t) + alpha2*H(t-1);                % (3.11A) & (3.4) YS = W*N 
    
    % Recursion to find goods market equilibrium (Y == YI)    
    while abs(Y(t)-YI) > 0.00001
        Y(t) = YI;
        tax(t) = theta*Y(t);
        C(t) = alpha1*YDe(t) + alpha2*H(t-1);   % (3.7E) - Expected Consumption
        YI   = C(t) + G(t);                                     % (3.10)
    end
    
    % Actual/Realized Disposable Income
    YD(t)  = Y(t) - tax(t);                                     % (3.5)
    if t < T
        YDe(t+1) = YD(t);                                       % (3.20)
        %YDe(t+1) = G(t)*(1-theta)/theta + randn(1); % alternate (rational) expectations
    end

    % flows of financial assets
    dHs(t) = G(t) - tax(t);                                     % (3.8) Fiscal deficit
    dHh(t) = YD(t) - C(t);                                      % (3.9)
    dH(t) = dHh(t);  %= dHs(t)                                  % redundant
    dHd(t) = YDe(t) - C(t);                                     % (3.18) Desired holdings
    
    % update stock of financial wealth
    H(t) = H(t-1) + dH(t);

% Display
fprintf('%3d  |  %6.2f |  %3.2f   |   %6.2f  |  %5.2f  |   %6.2f  | %6.2f  %6.2f  %6.2f |', t-1, ...
      Y(t), YDe(t), YD(t), tax(t), C(t), H(t), dH(t), dHd(t));
fprintf('\n')
end

% Plots
periods = (1:T)';
figure('Name', 'The Long Run', 'units', 'normalized', 'position', [0.1,0.2, 0.35, 0.8])
subplot(3,1,1)
plot(periods, Y, '-+r', periods, tax, '--g', 'linewidth', 2)
legend('Income', 'Tax Revenue')
legend('location', 'east')
ylim([0, Y(T)+G(T)])

subplot(3,1,2)
plot(periods, YD, '-+b', periods, C, '--c', 'linewidth', 2)
legend('Disposable Income', 'Consumption')
legend('location', 'southeast')
ylim([0,YD(T)+G(T)])

subplot(3,1,3)
plot(periods, H, '--k', periods, dH, '-.g', 'linewidth', 2)
legend('Financial Assets: Stock', 'Financial Assets: Flow')
legend('location', 'east')
ylim([0, H(T)+G(T)])
