%Model to analyse the effects of a Universal Basic Income in an overlapping
%generation world
%Arespa, M and del Rey, E.

%VERSION: "Standard" OLG model
%Date: January 2024

clc
clear
%close all
warning('off','all')

%Define parameters
syms rho phi alp n n_1 gA

%Define variables
syms l w s s_1 k k1 L A A1 y


%create a loop to generate many periods for the economy:

last=100; %number of periods
e(7,last)=zeros; %matrix to save variable values in every loop.
AA(last)=zeros; %create x axis for variable plots over A_t value.
A=1; %initial value for A
gA=.005; % d exogenous technological increase from previous period



for t=1:last
    A=A+gA;
    AA(t)=A;

%parameter values:

par=[.5; %rho subjective discount factor (for future C)
    3.05; %phi preference for leisure
    .5;   %alpha (alp in the code) Cobb-doublas production function parameter
    1;    % n young population at t
    1;    % n_1 young population at t_1
    .01;  % gA exogenous technological growth
        
  ];

%initial guess:

X0=[    
.2;     %w   t wage
1;      %s   t savings (from young people=workers)
1;      %s_1 t-1 savings
1;      %k   t capital
1;      %k1  t+1 capital
.33;    %L   t Labour demand
.33     %l   t labour supply

];




options=optimset('display','off');
% [e(:,t),fval]=lsqnonlin(@(e)UBI1reduced(e,par,A),X0,lb,ub,options); % CALL THE FUNCTION in file UBI1.m
[e(:,t),fval]=fsolve(@(e)UBI1reduced(e,par,A),X0,options);

A=A+gA;
t

end

% Variable definitions to include them in figures after running the
% function
y(last)=zeros; %matrix to drop GDP at the end
c_ave(last)=zeros; %matrix to drop average C (young and old p.)

y=(e(4,:).^(par(3))).*((A(:).*e(6,:))).^(1-par(3));
c_ave=y-e(2,:);
%%%%%%%%%%%%%%%%%% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%
ff=figure;

subplot(3,3,1) %2 rows, 3 columns, first position
plot(1:last,AA(1:last));
title('A');

subplot(3,3,2);
plot(1:last,e(1,:));
title('w');

subplot(3,3,3);
plot(1:last,e(2,:));
title('s');

subplot(3,3,4);
plot(1:last,e(4,:));
title('k');

subplot(3,3,5);
plot(1:last,y(:));
title('y');


subplot(3,3,6);
plot(1:last,c_ave(:));
title('Average C');

subplot(3,3,7);
plot(1:last,e(7,:));
title('l-supply');
