SlideShare a Scribd company logo
Empirical methods project (Econ 597)
Oleksii Khvastunov
Abstract
My empirical methods project is based on Per Krusell and Anthony
Smith paper ”Income and Wealth Heterogeneity in the Macroeconomy”
(JPE, 1998).The paper presents a simple benchmark incomplete market
heterogeneous model with two types of risk: idiosyncratic (unemploy-
ment) and aggregate (productivity). In my project I am solving bench-
mark model following the steps presented in the paper.
1
Contents
1 Introduction 3
2 Model 4
3 Algorithm 7
4 My results 9
5 Appendix 12
5.1 Short description of the functions . . . . . . . . . . . . . . . . . . 12
5.2 Main function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
5.3 Function that finds value functions for given aggregate capital
law of motion parameters . . . . . . . . . . . . . . . . . . . . . . 16
5.4 Function that performs value function itteration step . . . . . . . 19
5.5 Simulation routine . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5.6 Auxiliary functions . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2
1 Introduction
A large number of dynamic general equilibrium macroeconomic models rely
on the assumption of representative agent. In some cases it could be a rea-
sonable assumption like in the case of complete markets. However the models
that want to have strong microfoundation require agents heterogeneity. This set
up makes models much more complicated and most of them can not be solved
analytically.
Numerical solution of such models is not an easy task as well. Rational
agents in this type of models make decisions based on the entire distribution of
individuals state variables. In general case this object is infinitely dimensional
that makes problem extremely complicated. The paper by Per Krusell and An-
thony Smith (henceforth KS) presents methodology of numerical approximation
of equilibrium in heterogeneous agent models.
The key assumption that is used to calculate the approximate equilibrium
is the following: agents have a limited ability to predict the evolution of the
distribution of individual state variables. It is shown in the paper that this
bound does not significantly restrict agents. As a result, the law of motion that
agents perceive could be described using finite-dimensional vector of distribu-
tion parameters (moments could be used for this purpose).
The rest of the report is organized as follows: in Model section I briefly
explain the model, intuition behind the methodology and give model parame-
ters that are used. In Algorithm section I will explain in details what are the
steps for equilibrium computation and how I implemented them. In the results
section I present my results and discuss them.
3
2 Model
The benchmark model consists of continuum (measure one) ex-ante iden-
tical consumers who maximize expected discounted utility subject to budget
constraints in every period:
max E0Σ∞
t=0βt
U(ct) (1)
subject to
c + k − (1 − δ)k = y (2)
where y is individual’s income that consists of two parts: return on capital that
individual owns and wage that individual gets if he is employed. U(c) - CRRA
function. Market for capital and labor is competitive so individual gets return
and wage equal to marginal product.
Production function is Cobb-Douglas:
F(k, l) = z¯kα¯l1−α
(3)
where z - aggregate productivity shock, ¯k - aggregate capital in the economy, ¯l
- total amount of labor supplied.
There are two types of uncertainty in the model: aggregate (productivity
shock - z) and idiosyncratic (individual unemployment shock - ). Both shocks
follow first order Markov process with two possible states: zg and zb for aggre-
gate productivity shock and 0 and 1 for idiosyncratic shock (0 means that the
agent is unemployed). Parameters of the model specify the average duration of
boom and recession in the economy. The authors are trying to match durations
to the ones observed in the data (8 quaters for boom and recession). As a result,
the transition matrix for aggregate shock is:
Bad Good
Bad 0.875 0.125
Good 0.125 0.875
The shock are dependent in the following sense: aggregate shock affects indi-
vidial, but individual does not affect aggregate. The intuition behind this fact is
the following: aggregate shock determines the state of the economy, in particular
employment (unemployment in boom ug and recession ub are parameters of the
model) that affects the evolution of the individual shock. The authors impose
additional conditions for the dependence of aggregate and individual shock in
order to identify the joint transition matrix. I wrote the function ”emprpr” to
find this matrix by solving the system of linear equations.As a result, the joint
transion matrix has the following form:
Bad0 Bad1 Good0 Good1
Bad0 0.5250 0.35 0.0312 0.0938
Bad1 0.0389 0.8361 0.0021 0.1229
Good0 0.0937 0.0313 0.2917 0.5833
Good1 0.0091 0.1159 0.0243 0.8507
4
Elements of this matrix satisfy system of the linear equations as I mentioned
above. Four of these equations make sure that unemployment shares remains
consistent with the model parameters ug and ub.
Individual’s Bellman equation has the following form:
v(k, ; Γ, z) = max
c,k
{U(c) + βE[v(k , ; Γ , z )|z, ]} (4)
subject to
c + k = r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k,
Γ = H(Γ, z, z ),
k ≥ 0
where k - individual capital in current period, - current employment, Γ -
current distribution of capital, z - current aggregate shock.
Notice that when agent is employed he supplies exogenously given amount
of labor ˜l and gets wage. Otherwise an agent does not get a wage and return
on capital is the only source of his income.
The key assumption that allows compute equilibrium numerically is that
agents are boundedly rational in their perception of law of motion. Therefore,
current and future prices are assumed to be dependent only on the current
moments of capital distribution.
As a result, agent’s problem becomes:
v(k, ; ¯k, z) = max
c,k
{U(c) + βE[v(k , ; ¯k , z )|z, ]} (5)
subject to
c + k = r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k,
log(¯k ) = a0 + a1log(¯k) if z = zg
log(¯k ) = b0 + b1log(¯k) if z = zb
k ≥ 0
where w(¯k, ¯l, z) = (1 − α)z(¯k/¯l)α
and r(¯k, ¯l, z) = αz(¯k/¯l)α−1
5
For model simulations I have used the following parameters:
Parameter Value Meaning
β 0.99 Discount factor
δ 0.025 Capital depreciation
σ 1 Utility risk-aversion parameter
α 0.36 Capital share in the production
function
zg 1.01 Productivity in a good state
zb 0.99 Productivity in a bad state
ug 0.04 Unemployment in a good state
ub 0.1 Unemployment in a bad state
˜l 0.3271 Individual labor supply
I want to point out that for the draft version I used the value of parameter
˜l = 1.11 that is not the one used in the paper. The reason was that the value
of this parameter is not given in the published version of the paper. Therefore
I looked in the other papers that are based on KS. There ˜l is normalized such
that labor supply in a bad state is equal to one (˜l = 1
1−ub
= 1
0.9 ≈ 1.11). Having
obtained results that are far from the ones in the paper I looked at the working
paper version of KS, where side note 9 on the page 10 is the following:
For the final version of the Empirical Methods paper I am using the value
of exogenous labor supply ˜l = 0.3271 that is the one exploited in KS.
6
3 Algorithm
In this section I discuss equilibrium computation algorithm and it’s imple-
mentation.
Step 1: Compute transition probabilities. There are 4 possible states for in-
dividual in each period: (z, ) ∈ {(b, 0), (b, 1), (g, 0), (g, 1)} and paper contains
16 linear equations that determine these probabilities. As a mentioned in a
Model section evolution of (z, ) follows first order Markov process.
The function ”emprpr” contains these 16 equation and when we apply fsolve
to this function we obtain transition probabilities. These equations reflect the
following facts: individual employment can not affect aggregate economy state,
unemployment for each aggregate state should be consistent with model param-
eters ub and ug, etc.
Step 2: Choose parameters (a0, a1, b0, b1) for the law of motion of aggregate
capital. Chose grid for k and ¯k. KS suggest to have 70-130 points in k dimen-
sion with more points close to 0; and 4-6 points in ¯k dimension. Then starting
with zero initial value function do the following: do the value function iteration
step for the points on the grid, interpolate obtained result and continue these
two procedures until policy functions on a grid for two consecutive iterations
will be close enough. To be more precise the value function iteration step in
this set up is the following (denote f ≡ r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k):
vt+1(k, ; ¯k, z) = max
k ∈[0,f]
{log(k − k ) + β
z ,
π(z , |z, )vt(k , ; ¯k , z )} (6)
Notice that vt(k , ; ¯k , z ) is defined for all k ∈ [0, f], so when we are trying
to find vt+1(k, ; ¯k, z) for the points on the grid we solve continuous optimiza-
tion problem. Having obtained vt+1(k, ; ¯k, z) for the points on the grid we
interpolate this function so that it can be used in the next iteration. We start
with v0(k, ; ¯k, z) = 0 for all k and k’. This procedure allows me to find policy
functions. In fact I need to find four policy functions because there are four
combinations of aggregate and idiosyncratic shocks.
The function ”emprrhs” computes next iteration interpolated value function
given as inputs interpolated value functions from the previous iteration. I did
not maximize value functions in each point of the grid separately. I maximize
sum of value function that should produce the same result given that value
functions at each point of the grid have different arguments. To be more precise
if you denote mij(k ) ≡ log(kj − k ) + β z , π(z , |z, )vt(k , ; ¯ki , z ) (it is
a function that you need to optimize in order to get value function in a grid
point (kj, ¯ki)), then you can find kij ∈ argmax{mij(k )} either by optimizing all
mij(k ) separately or sum them up and maximize jointly. These two procedures
should lead to the same result because all optimizations problems are indepen-
dent, however I joint optimization allows to reduce computation time (it is the
case because I can supply gradient for optimization routine). For interpolation
7
purpose I used not spline routine but pchip (Piecewise Cubic Hermite Inter-
polating Polynomials) because it allows to produces functions without wiggles
even for relatively small number of grid points (it means that value functions
will be increasing and concave).
Step 3: Given policy functions simulate 5000 agents and 11000 periods. This
procedure includes simulation of aggregate and idiosyncratic shocks according
to their conditional distribution. Policy functions give us individual capital ev-
ery period, so we can calculate actual aggregate capital. Then we can regress
log of actual aggregate capital on log of previous period actual aggregate capital
in good and bad times. This procedure gives us new parameters (a0, a1, b0, b1)
of the aggregate law of motion. Then we can go back to the Step 2. We repeat
Steps 2 and 3 until R2
for the regression of the log of actual capital is high
enough or until parameters (a0, a1, b0, b1) don’t change too much. It means
|ai
0 − ai+1
0 | + |ai
1 − ai+1
1 | + |bi
0 − bi+1
0 | + |bi
1 − bi+1
1 | < eps.
The function ”emprsim3” does simulation given policy functions and returns
implied sequence of aggregate capital and shocks. The difference with the func-
tion ”emprsim” that I used in the draft version is the following: I managed to
vectorize computations in the agent’s dimension that reduced required for sim-
ulations time from 3 hours to 1 minute (these numbers reflect computational
time on ”hammer” machine).
8
4 My results
For the results part I started with the following: I took the true values of
the parameters of the law of motion and tried to reproduce Figure 1 and Figure
2 from the paper.
log(¯k ) = 0.095 + 0.962log(¯k) if z = zg
log(¯k ) = 0.085 + 0.965log(¯k) if z = zb
In order to solve agent’s Bellman equation I used the following grid:
k = [0.00001,0.00004,0.00008,0.0001,0.001:0.013:0.04,0.05:0.15:1,1:1:35]; (50 points)
¯k = [7.7 : 4 : 19.7]; (4 points)
I am using less grid points in both dimensions than in the draft version
because Piecewise Cubic Hermite Interpolating Polynomials that I am using
allows me to do that. These polynomials don’t produce wiggly function that
permits me to use less grid points without substantial lose of interpolating preci-
sion. Less grid point allows me to reduce time of one iteration for value functions
iterations. So within the same time period I can perform more iterations and
solve policy function more precisely on a grid. The stopping criteria was that
average absolute change of the policy functions for two consecutive iterations
(across all grid points for all value functions) should be less than 0.00005. In the
draft version it was 0.005. I use average absolute change not maximum absolute
change, but it does not make a significant difference here because convergence is
moreless uniform on a grid points. I have got the following figure that contains
policy function for agent in a good state.
Analog of Figure 2: Tomorrow’s Individual Capital as a function of Today’s
Individual Capital
9
Then I simulate economy with 5000 individuals (who follow obtained policy
functions) for 11000 periods. I started with the good aggregate state where all
individuals hold 10 units of capital. I discarded first 1000 periods and tried to
reproduce Figure 1 in the paper.
Analog of Figure 1: Tomorrow’s Aggregate Capital as a function of Today’s
Aggregate Capital
As we can see this figure is slightly different from the one in the paper
(see Figure 1 from the paper on the next page). When I run regression of the
next period aggregate capital on today’s period aggregate capital I have got the
following results:
In good times:
log(¯k ) = 0.0993 + 0.9601log(¯k); R2
= 0.999941 ˆσ = 0.0186%
In bad times:
log(¯k ) = 0.0877 + 0.9624log(¯k); R2
= 0.999851 ˆσ = 0.0291%
10
Figure 1 from the paper: Tomorrow’s Aggregate Capital as a function of
Today’s Aggregate Capital
As we can see, obtained coefficients are close but not exactly the ones we
started from. In fact the sum of absolute coefficients change is of magnitude
0.01 that can not be considered as very precise.
Then I run the entire procedure - I started with the following guesses for the
aggregate capital law of motion:
log(¯k ) = 0 + log(¯k) if z = zg
log(¯k ) = 0 + log(¯k) if z = zb
The results of the first four iterations are presented in the table below:
The smallest change in coefficients is observed in the step from third to forth
iteration. I did not incude more iterations in the table because the procedure
diverges. The procedure does not converge because at some point iterations
produce range for the aggregate capital that fall outside the range where I have
grid points for the aggregate capital.
11
5 Appendix
5.1 Short description of the functions
Section 5.2:
emprmainbchm - main function that performs iterations for the aggregate cap-
ital law of motion coefficients.
Section 5.3:
emprmain1 - function that finds value functions for given aggregate capital law
of motion coefficients.
Section 5.4:
emprrhs - function that performs value iteration step for given aggregate capital
law of motion coefficients.
Section 5.5:
emprsim3 - function that simulates time series for the aggregate capital given
agents’ policy functions
Section 5.6:
emprcon2 - function that constructs linear constraints for the optimization re-
quired for value function iterations. This constraint insures that we will have
increasing in individual capital value function. This constraint was needed when
I used spline interpolation. For Hermite interpolation this constraint does play
a role.
emprder - function that computes coefficients of the piecewise polynomials
derivative, this function is used to supply optimization routine in the value
iteration step a gradient.
emprderu - function that returns the derivative of the utility function
emprf00, emprf01, emprf10, emprf11 - functions that are optimized in order
to perform value function iteration step.
emprfun - function that computes next period aggregate capital for today’s
corresponding grid points for aggregate capital
emprpr - function that returns vector of zeros when all conditions for tran-
sition probabilitis is satisfied. Applying fsolve to this function allows to get 4x4
transition probabilities matrix.
emprr - function that computes returns on capital.
empru - function that returns value of the utility function.
12
emprvalev - function that computes value function in all grid points.
emprw - function that computes wage.
13
5.2 Main function
clear;
diary(’output6.txt’);
diary on;
global a0 a1 b0 b1;
a0=0;
a1=1;
b0=0;
b1=1;
ccoef=zeros(50,8);
eps=1;
cccount=1;
lll=0.9;
while ((eps>0.01)&&(cccount<50))
display(’Start of outer iteration’);
cccount
a0
a1
b0
b1
emprmain1;
[res,res2]=emprsim3();
stats=regstats(log(res(find(ismember(res2(1:end-1),1))+1)),...
log(res(find(ismember(res2(1:end-1),1)))),’linear’);
stats2=regstats(log(res(find(ismember(res2(1:end-1),0))+1)),...
log(res(find(ismember(res2(1:end-1),0)))),’linear’);
display(’Good state’);
aa0=stats.beta(1,1)
aa1=stats.beta(2,1)
stats.rsquare
(stats.mse)^0.5
ccoef(cccount,1)=aa0;
ccoef(cccount,2)=aa1;
ccoef(cccount,3)=stats.rsquare;
ccoef(cccount,4)=(stats.mse)^0.5;
display(’Bad state’);
bb0=stats2.beta(1,1)
bb1=stats2.beta(2,1)
stats2.rsquare
(stats2.mse)^0.5
14
ccoef(cccount,5)=bb0;
ccoef(cccount,6)=bb1;
ccoef(cccount,7)=stats2.rsquare;
ccoef(cccount,8)=(stats2.mse)^0.5;
clear stats;
clear stats2;
eps=abs(a0-aa0)+abs(b0-bb0)+abs(a1-aa1)+abs(b1-bb1)
a0=lll*aa0+(1-lll)*a0;
b0=lll*bb0+(1-lll)*b0;
a1=lll*aa1+(1-lll)*a1;
b1=lll*bb1+(1-lll)*b1;
display(’End of outer itteration’);
cccount
cccount=cccount+1;
end;
diary off;
15
5.3 Function that finds value functions for given aggregate
capital law of motion parameters
tic
global bbeta delta sig alpha zg zb ug ub prag durub durug mkk mkkb trpr kk
kkb a0 a1 b0 b1 in00 in01 in10 in11 plcov fnorm mc00 mc01 mc10 mc11 limp
con ltild mouse;
% prag(i,j) - probabilities of transition for the aggregate state from i to
% j where 1 - bad, 2 - good
% mkk - matrix that contains grid points for k, each row is a grid for k,
% number of rows is equal to the number of grid points for kbar, all rows
% are the same
% mkkb - matrix that contains grid points for kbar, each column is a grid
% for kbar, number of colums is equal to the number of grid points for k,
% all columns are the same
% trpr - transition probabilities of moving from different conbinations of
% aggregate and idiosyncratic shocks
% orger of rows and columns is the following
% 1 - b0
% 2 - b1
% 3 - g0
% 4 - g1
% kk - grid for k
% kkb - grid for kbar
% a0, a1, b0, b1 - coefficients for the aggregate capital law of motion
% in00, in01, in10, in11 - initial value for the policy function (it is
% needed for maximization routine). The results from the previous step of
% the value function itteration is used here.
% plcov - (policy convergence) this value contains the distance between
% policy functions for the consecutive itterations.
% If it is small then value function iterations converged
plcov=2;
% average duration of bad and good times
durb=8;
durg=8;
prag=[1-1/durb, 1/durb; 1/durg, 1-1/durg];
% average duration of unemployment in good and bad times
durub=2.5;
16
durug=1.5;
ub=0.1;
ug=0.04;
zb=0.99;
zg=1.01;
alpha=0.36;
% see workpaper version
ltild=0.3271;
sig=1;
bbeta=0.99;
delta=0.025;
kk=[0.00001,0.00004,0.00008,0.0001,0.001:0.013:0.04,0.05:0.15:1,1:1:35];
kkb=[7.7:4:19.7];
% mkk(i,j) - amount of capital if kbar corresponds to i-s point on the grid
% and kk corresponds to j-s point on the grid
mkk=repmat(kk,size(kkb,2),1);
mkkb=repmat(kkb’,1,size(kk,2));
in00=zeros(size(mkk));
in01=zeros(size(mkk));
in10=zeros(size(mkk));
in11=zeros(size(mkk));
% calculation of transition probabilities
f=@(p) emprpr(p);
[trpr,fval,exitflag,output] = fsolve(f,ones(4,4));
mc00=emprr(0).*mkk+(1-delta).*mkk;
% 1 - aggregate state is good
% 0 - person is unemployed
mc10=emprr(1).*mkk+(1-delta).*mkk;
mc01=emprr(0).*mkk+emprw(0)*ltild+(1-delta).*mkk;
mc11=emprr(1).*mkk+emprw(1)*ltild+(1-delta).*mkk;
%Zero initial value function
y00=zeros(size(mkk));
v00=pchip(kk,y00);
y01=zeros(size(mkk));
v01=pchip(kk,y01);
y10=zeros(size(mkk));
v10=pchip(kk,y10);
17
y11=zeros(size(mkk));
v11=pchip(kk,y11);
fnorm=10;
con=emprcon2();
limp=1;
ccount=0;
while (((plcov>5*(1e-5)*4*size(mkk,1)*size(mkk,2))&&(ccount<370))
||(ccount==1)||(ccount==0))
[res00,res10,res01,res11]=emprrhs(v00,v10,v01,v11);
ccount=ccount+1;
ccount
v00=res00;
v01=res01;
v10=res10;
v11=res11;
plcov
end;
toc
18
5.4 Function that performs value function itteration step
function [res00,res10,res01,res11]=emprrhs(v00,v10,v01,v11)
% function that computes rhs for the value function iteration
% v00 - array that contains: v00(i) - function of kprime given i-s point on
% the grid for kbar and shocks 0-unemployed and 0-bad times
global kk kkb mkk delta trpr bbeta in00 in01 in10 in11 plcov fnorm
mc00 mc01 mc10 mc11 vp00 vp01 vp10 vp11 limp con mouse;
kkpg=emprfun(1);
kkpb=emprfun(0);
help00=(ppval(v00,kk));
help10=(ppval(v10,kk));
help01=(ppval(v01,kk));
help11=(ppval(v11,kk));
h00=pchip(kkb,help00’);
h10=pchip(kkb,help10’);
h01=pchip(kkb,help01’);
h11=pchip(kkb,help11’);
he00=(ppval(h00,kkpb));
he10=(ppval(h10,kkpg));
he01=(ppval(h01,kkpb));
he11=(ppval(h11,kkpg));
% functions of kprime for every kbarprime that correspond to kbar
vp00=pchip(kk,he00’);
vp10=pchip(kk,he10’);
vp01=pchip(kk,he01’);
vp11=pchip(kk,he11’);
% derivatives of these functions
vp00d=fnder(vp00,1);
vp00d.coefs=emprder(vp00.coefs);
vp01d=fnder(vp01,1);
vp01d.coefs=emprder(vp01.coefs);
vp10d=fnder(vp10,1);
vp10d.coefs=emprder(vp10.coefs);
vp11d=fnder(vp11,1);
vp11d.coefs=emprder(vp11.coefs);
nvar=4*size(mkk,1)*size(mkk,2);
epss=1e-8;
19
options=optimset(’Display’,’off’,’Algorithm’,’interior-point’,’TolFun’,
1e-8,’TolX’,1e-8,’MaxFunEvals’,2000000,’TolCon’,1e-8,’MaxIter’,2000);
emprf00(in00)
ini00=max(min(mc00-epss,in00),epss*ones(size(mc00)));
ini01=max(min(mc01-epss,in01),epss*ones(size(mc01)));
ini10=max(min(mc10-epss,in10),epss*ones(size(mc10)));
ini11=max(min(mc11-epss,in11),epss*ones(size(mc11)));
hf=-epss*ones(size(mkk,1)*(size(mkk,2)-1),1);
[pl00,fval,exitflag,output]=ktrlink(@emprf00,ini00,con,hf,[],[],
epss*ones(size(mkk)),mc00-epss,[],options);
pl00
[pl01,fval,exitflag,output]=ktrlink(@emprf01,ini01,con,hf,[],[],
epss*ones(size(mkk)),mc01-epss,[],options);
pl01
[pl10,fval,exitflag,output]=ktrlink(@emprf10,ini10,con,hf,[],[],
epss*ones(size(mkk)),mc10-epss,[],options);
pl10
[pl11,fval,exitflag,output]=ktrlink(@emprf11,ini11,con,hf,[],[],
epss*ones(size(mkk)),mc11-epss,[],options);
pl11
plcov=(sum(sum(abs(pl00-ini00)))+sum(sum(abs(pl01-ini01)))+
sum(sum(abs(pl10-ini10)))+sum(sum(abs(pl11-ini11))));
in00=pl00;
in01=pl01;
in10=pl10;
in11=pl11;
fv00=(limp*empru(mc00-pl00)+bbeta*(trpr(1,1)*emprvalev(vp00,pl00)+ ...
+trpr(1,2)*emprvalev(vp01,pl00)+trpr(1,3)*emprvalev(vp10,pl00)+...
+trpr(1,4)*emprvalev(vp11,pl00)));
fv01=(limp*empru(mc01-pl01)+bbeta*(trpr(2,1)*emprvalev(vp00,pl01)+ ...
+trpr(2,2)*emprvalev(vp01,pl01)+trpr(2,3)*emprvalev(vp10,pl01)+...
+trpr(2,4)*emprvalev(vp11,pl01)));
fv10=(limp*empru(mc10-pl10)+bbeta*(trpr(3,1)*emprvalev(vp00,pl10)+ ...
+trpr(3,2)*emprvalev(vp01,pl10)+trpr(3,3)*emprvalev(vp10,pl10)+...
+trpr(3,4)*emprvalev(vp11,pl10)));
fv11=(limp*empru(mc11-pl11)+bbeta*(trpr(4,1)*emprvalev(vp00,pl11)+ ...
+trpr(4,2)*emprvalev(vp01,pl11)+trpr(4,3)*emprvalev(vp10,pl11)+...
+trpr(4,4)*emprvalev(vp11,pl11)));
20
res00=pchip([kk],[fv00]);
res01=pchip([kk],[fv01]);
res10=pchip([kk],[fv10]);
res11=pchip([kk],[fv11]);
21
5.5 Simulation routine
function [res,res2]=emprsim3()
% function that simulates nn consumers for tt time periods and we burn
% first burn periods, because aggregate capital there is affected by
% initial wealth distribution
global ub ug kkb in00 in01 in10 in11 trpr prag kk;
nn=5000;
tt=11000;
burn=1000;
% suppose initially everybody has the same level of capital ink and
% aggregate state is good
ink=10;
kap0=ink*ones(nn,1);
kap=zeros(nn,1);
agst=zeros(1,tt);
empl=(rand(nn,1)>ug);
agkap=zeros(1,tt);
agkap(1)=ink;
kap=kap0;
kap2=zeros(nn,1);
empl2=zeros(nn,1);
agst(1,1)=1;
% first index is aggreagate state, second - employment
ii00=pchip(kkb,in00’);
ii01=pchip(kkb,in01’);
ii10=pchip(kkb,in10’);
ii11=pchip(kkb,in11’);
help=1; % good initial state
for i=2:1:tt
i
he00=(ppval(ii00,agkap(i-1)));
he10=(ppval(ii10,agkap(i-1)));
he01=(ppval(ii01,agkap(i-1)));
he11=(ppval(ii11,agkap(i-1)));
i00=pchip(kk,he00’);
i10=pchip(kk,he10’);
i01=pchip(kk,he01’);
i11=pchip(kk,he11’);
help2=rand(1,1);
22
if (help==0)
if (help2<prag(1,1))
help3=0;
else
help3=1;
end;
end;
if (help==1)
if (help2<prag(2,1))
help3=0;
else
help3=1;
end;
end;
agst(1,i)=help3;
help4=rand(nn,1);
if ((help==0) && (help3==0))
thresh=(1-empl)*trpr(1,1)/prag(1,1)+empl*trpr(2,1)/prag(1,1);
empl2=(help4>thresh);
hh=sum(ismember(empl2,1)); % number of employed guys
% adjustment of employment in order to have exact unemployment
% rates
if (hh>nn*(1-ub))
hhh=find(ismember(empl2,1));
emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=0;
end;
if (hh<nn*(1-ub))
hhh=find(ismember(empl2,0));
emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=1;
end;
kap2(find(ismember(empl,1)))=ppval(i01,kap(find(ismember(empl,1))));
kap2(find(ismember(empl,0)))=ppval(i00,kap(find(ismember(empl,0))));
end;
if ((help==1) && (help3==0))
thresh=(1-empl)*trpr(3,1)/prag(2,1)+empl*trpr(4,1)/prag(2,1);
empl2=(help4>thresh);
23
hh=sum(ismember(empl2,1)); % number of employed guys
% adjustment of employment in order to have exact unemployment
% rates
if (hh>nn*(1-ub))
hhh=find(ismember(empl2,1));
emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=0;
end;
if (hh<nn*(1-ub))
hhh=find(ismember(empl2,0));
emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=1;
end;
kap2(find(ismember(empl,1)))=ppval(i11,kap(find(ismember(empl,1))));
kap2(find(ismember(empl,0)))=ppval(i10,kap(find(ismember(empl,0))));
end;
if ((help==0) && (help3==1))
thresh=(1-empl)*trpr(1,3)/prag(1,2)+empl*trpr(2,3)/prag(1,2);
empl2=(help4>thresh);
hh=sum(ismember(empl2,1)); % number of employed guys
% adjustment of employment in order to have exact unemployment
% rates
if (hh>nn*(1-ug))
hhh=find(ismember(empl2,1));
emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=0;
end;
if (hh<nn*(1-ug))
hhh=find(ismember(empl2,0));
emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=1;
end;
kap2(find(ismember(empl,1)))=ppval(i01,kap(find(ismember(empl,1))));
kap2(find(ismember(empl,0)))=ppval(i00,kap(find(ismember(empl,0))));
end;
if ((help==1) && (help3==1))
thresh=(1-empl)*trpr(3,3)/prag(2,2)+empl*trpr(4,3)/prag(2,2);
empl2=(help4>thresh);
24
hh=sum(ismember(empl2,1)); % number of employed guys
% adjustment of employment in order to have exact unemployment
% rates
if (hh>nn*(1-ug))
hhh=find(ismember(empl2,1));
emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=0;
end;
if (hh<nn*(1-ug))
hhh=find(ismember(empl2,0));
emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=1;
end;
kap2(find(ismember(empl,1)))=ppval(i11,kap(find(ismember(empl,1))));
kap2(find(ismember(empl,0)))=ppval(i10,kap(find(ismember(empl,0))));
end;
help=help3;
agkap(1,i)=sum(kap2)/nn;
agkap(1,i)
kap=kap2;
empl=empl2;
end;
res=agkap(1,burn+1:end);
res2=agst(1,burn+1:end);
25
5.6 Auxiliary functions
function res=emprcon2()
global mkk;
h1=ones(size(mkk,1)*(size(mkk,2)-1),1);
h2=eye(size(mkk,1)*size(mkk,2))+diag(-h1,size(mkk,1));
res=h2(1:end-size(mkk,1),:);
*****************************************************************************
function res=emprder(coef)
% coef - each row contains coefficients of piecewise polynomials at some
% interval, for instance [1,0,2] means x^2+2
col=size(coef,2);
row=size(coef,1);
help=repmat(col-1:-1:1,row,1);
res=coef(:,1:end-1).*help;
*****************************************************************************
function res=emprderu(c)
global sig;
if (sig==1)
res=1./c;
else
res=(c.^(-sig));
end;
*****************************************************************************
function [res,res2]=emprf00(kp)
global mc00 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d;
% res - value of the function
%kp=reshape(kp,size(mc00,1),size(mc00,2));
res= -1*(sum(sum(limp*empru(mc00-kp)+bbeta*(trpr(1,1)*emprvalev(vp00,kp)+ ...
+trpr(1,2)*emprvalev(vp01,kp)+trpr(1,3)*emprvalev(vp10,kp)+...+
+trpr(1,4)*emprvalev(vp11,kp)))));
% res2 - drivative (since function has matrix argument derivative is matrix as well)
res2= -1*(-1*limp*emprderu(mc00-kp)+bbeta*(trpr(1,1)*emprvalev(vp00d,kp)+ ...
+trpr(1,2)*emprvalev(vp01d,kp)+trpr(1,3)*emprvalev(vp10d,kp)+...
+trpr(1,4)*emprvalev(vp11d,kp)));
26
function [res,res2]=emprf01(kp)
global mc01 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d;
res= -1*(sum(sum(limp*empru(mc01-kp)+bbeta*(trpr(2,1)*emprvalev(vp00,kp)+ ...
+trpr(2,2)*emprvalev(vp01,kp)+trpr(2,3)*emprvalev(vp10,kp)+...
+trpr(2,4)*emprvalev(vp11,kp)))));
res2=-1*(-1*limp*emprderu(mc01-kp)+bbeta*(trpr(2,1)*emprvalev(vp00d,kp)+ ...
+trpr(2,2)*emprvalev(vp01d,kp)+trpr(2,3)*emprvalev(vp10d,kp)+...
+trpr(2,4)*emprvalev(vp11d,kp)));
*****************************************************************************
function [res,res2]=emprf10(kp)
global mc10 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d;
res=-1*(sum(sum(limp*empru(mc10-kp)+bbeta*(trpr(3,1)*emprvalev(vp00,kp)+ ...
+trpr(3,2)*emprvalev(vp01,kp)+trpr(3,3)*emprvalev(vp10,kp)+...
+trpr(3,4)*emprvalev(vp11,kp)))));
res2=-1*(-1*limp*emprderu(mc10-kp)+bbeta*(trpr(3,1)*emprvalev(vp00d,kp)+ ...
+trpr(3,2)*emprvalev(vp01d,kp)+trpr(3,3)*emprvalev(vp10d,kp)+...
+trpr(3,4)*emprvalev(vp11d,kp)));
*****************************************************************************
function [res,res2]=emprf11(kp)
global mc11 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d;
res= -1*(sum(sum(limp*empru(mc11-kp)+bbeta*(trpr(4,1)*emprvalev(vp00,kp)+ ...
+trpr(4,2)*emprvalev(vp01,kp)+trpr(4,3)*emprvalev(vp10,kp)+...
+trpr(4,4)*emprvalev(vp11,kp)))));
res2= -1*(-1*limp*emprderu(mc11-kp)+bbeta*(trpr(4,1)*emprvalev(vp00d,kp)+ ...
+trpr(4,2)*emprvalev(vp01d,kp)+trpr(4,3)*emprvalev(vp10d,kp)+...
+trpr(4,4)*emprvalev(vp11d,kp)));
27
function res=emprfun(flag)
% function that computes next period aggregate capital for today’s
% correponding grid points for aggregate capital
% kkb - grid for aggregate capital
% flag=0 - bad state
% flag=1 - good state
global a0 a1 b0 b1 kkb;
if (flag==0)
res=exp(b0+b1*log(kkb));
elseif (flag==1)
res=exp(a0+a1*log(kkb));
end;
*****************************************************************************
function res=emprpr(pprob)
% function that returns vector of zeros when all conditions for transition
% probabilitis is satisfied
% pprob is 4x4
% pprob(i,j) is probability of transition from state i to j
% where i,j belongs to 1 - b0, 2 - b1, 3 - g0, 4 - g1
global ug ub prag durub durug;
res=zeros(16,1);
res(1,1)=1-1/durub-pprob(1,1)/prag(1,1);
res(2,1)=1-1/durug-pprob(3,3)/prag(2,2);
res(3,1)=pprob(1,1)+pprob(1,2)-prag(1,1);
res(4,1)=pprob(2,1)+pprob(2,2)-prag(1,1);
res(5,1)=pprob(1,3)+pprob(1,4)-prag(1,2);
res(6,1)=pprob(2,3)+pprob(2,4)-prag(1,2);
res(7,1)=pprob(3,1)+pprob(3,2)-prag(2,1);
res(8,1)=pprob(4,1)+pprob(4,2)-prag(2,1);
res(9,1)=pprob(3,3)+pprob(3,4)-prag(2,2);
res(10,1)=pprob(4,3)+pprob(4,4)-prag(2,2);
res(11,1)=pprob(3,1)*prag(1,1)-1.25*pprob(1,1)*prag(2,1);
res(12,1)=pprob(1,3)*prag(2,2)-0.75*pprob(3,3)*prag(1,2);
res(13,1)=ub*pprob(1,1)+(1-ub)*pprob(2,1)-ub*prag(1,1);
res(14,1)=ub*pprob(1,3)+(1-ub)*pprob(2,3)-ug*prag(1,2);
res(15,1)=ug*pprob(3,1)+(1-ug)*pprob(4,1)-ub*prag(2,1);
res(16,1)=ug*pprob(3,3)+(1-ug)*pprob(4,3)-ug*prag(2,2);
28
function res=emprr(flag)
% function computes return on capital
% flag=0 - bad aggregate state
% flag=1 - good aggregate state
global ub ug alpha zb zg mkkb ltild;
if (flag==0)
res=alpha*zb*(mkkb.^(alpha-1))/((ltild*(1-ub))^(alpha-1));
elseif (flag==1)
res=alpha*zg*(mkkb.^(alpha-1))/((ltild*(1-ug))^(alpha-1));
end;
*****************************************************************************
function res=empru(c)
global sig;
if (sig==1)
res=log(c);
else
res=(c.^(1-sig))/(1-sig);
end;
*****************************************************************************
function res=emprvalev(cs,kpr)
% function that computes values for the value function
% res(i,j) = cs(i)(kpr(i,j))
help=ppval(cs,kpr);
help2=repmat(eye(size(kpr,1),size(kpr,1)),[1,1,size(kpr,2)]);
help3=help.*help2;
help4=sum(help3);
res=squeeze(help4);
*****************************************************************************
function res=emprw(flag)
% function computes wages
% flag=0 - bad aggregate state
% flag=1 - good aggregate state
global ub ug alpha zb zg mkkb ltild;
if (flag==0)
res=(1-alpha)*zb*(mkkb.^(alpha))/((ltild*(1-ub))^(alpha));
elseif (flag==1)
res=(1-alpha)*zg*(mkkb.^(alpha))/((ltild*(1-ug))^(alpha));
end;
29

More Related Content

What's hot

aaoczc2252
aaoczc2252aaoczc2252
aaoczc2252
Perumal_Gopi42
 
Linear Regression Ordinary Least Squares Distributed Calculation Example
Linear Regression Ordinary Least Squares Distributed Calculation ExampleLinear Regression Ordinary Least Squares Distributed Calculation Example
Linear Regression Ordinary Least Squares Distributed Calculation Example
Marjan Sterjev
 
9781441911933 c1(1)
9781441911933 c1(1)9781441911933 c1(1)
9781441911933 c1(1)
Dr Lendy Spires
 
Lecture 5: Stochastic Hydrology
Lecture 5: Stochastic Hydrology Lecture 5: Stochastic Hydrology
Lecture 5: Stochastic Hydrology
Amro Elfeki
 
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Amro Elfeki
 
Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function
ijscmcj
 
Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss functionEstimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function
ijscmcj
 
Lecture 4: Stochastic Hydrology (Site Characterization)
Lecture 4: Stochastic Hydrology (Site Characterization)Lecture 4: Stochastic Hydrology (Site Characterization)
Lecture 4: Stochastic Hydrology (Site Characterization)
Amro Elfeki
 
20191123 bayes dl-jp
20191123 bayes dl-jp20191123 bayes dl-jp
20191123 bayes dl-jp
Taku Yoshioka
 
X02 Supervised learning problem linear regression multiple features
X02 Supervised learning problem linear regression multiple featuresX02 Supervised learning problem linear regression multiple features
X02 Supervised learning problem linear regression multiple features
Marco Moldenhauer
 
Ch07 ans
Ch07 ansCh07 ans
Ch07 ans
emanuelfilho00
 
Principal Component Analysis for Tensor Analysis and EEG classification
Principal Component Analysis for Tensor Analysis and EEG classificationPrincipal Component Analysis for Tensor Analysis and EEG classification
Principal Component Analysis for Tensor Analysis and EEG classification
Tatsuya Yokota
 
30120140505004
3012014050500430120140505004
30120140505004
IAEME Publication
 
Batch arrival retrial queuing system with state dependent admission and berno...
Batch arrival retrial queuing system with state dependent admission and berno...Batch arrival retrial queuing system with state dependent admission and berno...
Batch arrival retrial queuing system with state dependent admission and berno...
eSAT Journals
 
Regularization and variable selection via elastic net
Regularization and variable selection via elastic netRegularization and variable selection via elastic net
Regularization and variable selection via elastic net
KyusonLim
 
Dag in mmhc
Dag in mmhcDag in mmhc
Dag in mmhc
KyusonLim
 
Super-twisting sliding mode based nonlinear control for planar dual arm robots
Super-twisting sliding mode based nonlinear control for planar dual arm robotsSuper-twisting sliding mode based nonlinear control for planar dual arm robots
Super-twisting sliding mode based nonlinear control for planar dual arm robots
journalBEEI
 
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
ijaia
 

What's hot (18)

aaoczc2252
aaoczc2252aaoczc2252
aaoczc2252
 
Linear Regression Ordinary Least Squares Distributed Calculation Example
Linear Regression Ordinary Least Squares Distributed Calculation ExampleLinear Regression Ordinary Least Squares Distributed Calculation Example
Linear Regression Ordinary Least Squares Distributed Calculation Example
 
9781441911933 c1(1)
9781441911933 c1(1)9781441911933 c1(1)
9781441911933 c1(1)
 
Lecture 5: Stochastic Hydrology
Lecture 5: Stochastic Hydrology Lecture 5: Stochastic Hydrology
Lecture 5: Stochastic Hydrology
 
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
Lecture 6: Stochastic Hydrology (Estimation Problem-Kriging-, Conditional Sim...
 
Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function
 
Estimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss functionEstimation of mean and its function using asymmetric loss function
Estimation of mean and its function using asymmetric loss function
 
Lecture 4: Stochastic Hydrology (Site Characterization)
Lecture 4: Stochastic Hydrology (Site Characterization)Lecture 4: Stochastic Hydrology (Site Characterization)
Lecture 4: Stochastic Hydrology (Site Characterization)
 
20191123 bayes dl-jp
20191123 bayes dl-jp20191123 bayes dl-jp
20191123 bayes dl-jp
 
X02 Supervised learning problem linear regression multiple features
X02 Supervised learning problem linear regression multiple featuresX02 Supervised learning problem linear regression multiple features
X02 Supervised learning problem linear regression multiple features
 
Ch07 ans
Ch07 ansCh07 ans
Ch07 ans
 
Principal Component Analysis for Tensor Analysis and EEG classification
Principal Component Analysis for Tensor Analysis and EEG classificationPrincipal Component Analysis for Tensor Analysis and EEG classification
Principal Component Analysis for Tensor Analysis and EEG classification
 
30120140505004
3012014050500430120140505004
30120140505004
 
Batch arrival retrial queuing system with state dependent admission and berno...
Batch arrival retrial queuing system with state dependent admission and berno...Batch arrival retrial queuing system with state dependent admission and berno...
Batch arrival retrial queuing system with state dependent admission and berno...
 
Regularization and variable selection via elastic net
Regularization and variable selection via elastic netRegularization and variable selection via elastic net
Regularization and variable selection via elastic net
 
Dag in mmhc
Dag in mmhcDag in mmhc
Dag in mmhc
 
Super-twisting sliding mode based nonlinear control for planar dual arm robots
Super-twisting sliding mode based nonlinear control for planar dual arm robotsSuper-twisting sliding mode based nonlinear control for planar dual arm robots
Super-twisting sliding mode based nonlinear control for planar dual arm robots
 
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
MIXTURES OF TRAINED REGRESSION CURVESMODELS FOR HANDRITTEN ARABIC CHARACTER R...
 

Viewers also liked

Alba's PP
Alba's PPAlba's PP
Alba's PP
anasuarez
 
A6 Postcard
A6 PostcardA6 Postcard
A6 Postcard
Victoria Sheedy
 
Medio ambiente 24
Medio ambiente 24Medio ambiente 24
Medio ambiente 24
losprimeros b
 
wesco international Non-Gap%203Q07
wesco international Non-Gap%203Q07wesco international Non-Gap%203Q07
wesco international Non-Gap%203Q07
finance34
 
Indigenous Tweets, Visible Voices & Technology
Indigenous Tweets, Visible Voices & TechnologyIndigenous Tweets, Visible Voices & Technology
Indigenous Tweets, Visible Voices & Technology
Kara Andrade
 
Narración magui..
Narración magui..Narración magui..
Narración magui..
Margarita Rendon Muro
 
Geriatrie
GeriatrieGeriatrie
Geriatrie
Doina Gheorghe
 
Promes kelas9
Promes kelas9Promes kelas9
Promes kelas9
zuhrotunnisa95
 
Successful collaboration and team work
Successful collaboration and team workSuccessful collaboration and team work
Successful collaboration and team work
shon3220
 
Casa Vereda Pantañillo
Casa Vereda PantañilloCasa Vereda Pantañillo
Casa Vereda Pantañillo
Carlos H Jaramillo A
 
Adoleshenca
AdoleshencaAdoleshenca
Adoleshenca
Melissa Cani
 

Viewers also liked (12)

Alba's PP
Alba's PPAlba's PP
Alba's PP
 
ثانيـاً
ثانيـاًثانيـاً
ثانيـاً
 
A6 Postcard
A6 PostcardA6 Postcard
A6 Postcard
 
Medio ambiente 24
Medio ambiente 24Medio ambiente 24
Medio ambiente 24
 
wesco international Non-Gap%203Q07
wesco international Non-Gap%203Q07wesco international Non-Gap%203Q07
wesco international Non-Gap%203Q07
 
Indigenous Tweets, Visible Voices & Technology
Indigenous Tweets, Visible Voices & TechnologyIndigenous Tweets, Visible Voices & Technology
Indigenous Tweets, Visible Voices & Technology
 
Narración magui..
Narración magui..Narración magui..
Narración magui..
 
Geriatrie
GeriatrieGeriatrie
Geriatrie
 
Promes kelas9
Promes kelas9Promes kelas9
Promes kelas9
 
Successful collaboration and team work
Successful collaboration and team workSuccessful collaboration and team work
Successful collaboration and team work
 
Casa Vereda Pantañillo
Casa Vereda PantañilloCasa Vereda Pantañillo
Casa Vereda Pantañillo
 
Adoleshenca
AdoleshencaAdoleshenca
Adoleshenca
 

Similar to Report Final

Programming project
Programming projectProgramming project
Programming project
MuireannSpain
 
EENG519FinalProjectReport
EENG519FinalProjectReportEENG519FinalProjectReport
EENG519FinalProjectReport
Daniel K
 
SLAM of Multi-Robot System Considering Its Network Topology
SLAM of Multi-Robot System Considering Its Network TopologySLAM of Multi-Robot System Considering Its Network Topology
SLAM of Multi-Robot System Considering Its Network Topology
toukaigi
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
anhlodge
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
Scilab
 
Fir 05 dynamics
Fir 05 dynamicsFir 05 dynamics
Fir 05 dynamics
nguyendattdh
 
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
Alexander Litvinenko
 
SBSI optimization tutorial
SBSI optimization tutorialSBSI optimization tutorial
SBSI optimization tutorial
Richard Adams
 
APPLIED MACHINE LEARNING
APPLIED MACHINE LEARNINGAPPLIED MACHINE LEARNING
APPLIED MACHINE LEARNING
Revanth Kumar
 
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHMADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
Wireilla
 
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHMADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ijfls
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
Oğuzhan TAŞ Akademi
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)
NYversity
 
A few solvers for portfolio selection
A few solvers for portfolio selectionA few solvers for portfolio selection
A few solvers for portfolio selection
Bogusz Jelinski
 
Analysis of Variance 2
Analysis of Variance 2Analysis of Variance 2
Analysis of Variance 2
Mayar Zo
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction Problem
Varad Meru
 
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATIONLOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
orajjournal
 
Different Types of Machine Learning Algorithms
Different Types of Machine Learning AlgorithmsDifferent Types of Machine Learning Algorithms
Different Types of Machine Learning Algorithms
rahmedraj93
 
Project2
Project2Project2
Project2
Linjun Li
 
Fuzzy c means_realestate_application
Fuzzy c means_realestate_applicationFuzzy c means_realestate_application
Fuzzy c means_realestate_application
Cemal Ardil
 

Similar to Report Final (20)

Programming project
Programming projectProgramming project
Programming project
 
EENG519FinalProjectReport
EENG519FinalProjectReportEENG519FinalProjectReport
EENG519FinalProjectReport
 
SLAM of Multi-Robot System Considering Its Network Topology
SLAM of Multi-Robot System Considering Its Network TopologySLAM of Multi-Robot System Considering Its Network Topology
SLAM of Multi-Robot System Considering Its Network Topology
 
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docxSAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
 
Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2Scilab for real dummies j.heikell - part 2
Scilab for real dummies j.heikell - part 2
 
Fir 05 dynamics
Fir 05 dynamicsFir 05 dynamics
Fir 05 dynamics
 
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
Computation of Electromagnetic Fields Scattered from Dielectric Objects of Un...
 
SBSI optimization tutorial
SBSI optimization tutorialSBSI optimization tutorial
SBSI optimization tutorial
 
APPLIED MACHINE LEARNING
APPLIED MACHINE LEARNINGAPPLIED MACHINE LEARNING
APPLIED MACHINE LEARNING
 
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHMADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
 
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHMADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
ADAPTIVE FUZZY KERNEL CLUSTERING ALGORITHM
 
Genetic Algorithms
Genetic AlgorithmsGenetic Algorithms
Genetic Algorithms
 
Machine learning (7)
Machine learning (7)Machine learning (7)
Machine learning (7)
 
A few solvers for portfolio selection
A few solvers for portfolio selectionA few solvers for portfolio selection
A few solvers for portfolio selection
 
Analysis of Variance 2
Analysis of Variance 2Analysis of Variance 2
Analysis of Variance 2
 
Kakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction ProblemKakuro: Solving the Constraint Satisfaction Problem
Kakuro: Solving the Constraint Satisfaction Problem
 
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATIONLOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
LOGNORMAL ORDINARY KRIGING METAMODEL IN SIMULATION OPTIMIZATION
 
Different Types of Machine Learning Algorithms
Different Types of Machine Learning AlgorithmsDifferent Types of Machine Learning Algorithms
Different Types of Machine Learning Algorithms
 
Project2
Project2Project2
Project2
 
Fuzzy c means_realestate_application
Fuzzy c means_realestate_applicationFuzzy c means_realestate_application
Fuzzy c means_realestate_application
 

Report Final

  • 1. Empirical methods project (Econ 597) Oleksii Khvastunov Abstract My empirical methods project is based on Per Krusell and Anthony Smith paper ”Income and Wealth Heterogeneity in the Macroeconomy” (JPE, 1998).The paper presents a simple benchmark incomplete market heterogeneous model with two types of risk: idiosyncratic (unemploy- ment) and aggregate (productivity). In my project I am solving bench- mark model following the steps presented in the paper. 1
  • 2. Contents 1 Introduction 3 2 Model 4 3 Algorithm 7 4 My results 9 5 Appendix 12 5.1 Short description of the functions . . . . . . . . . . . . . . . . . . 12 5.2 Main function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.3 Function that finds value functions for given aggregate capital law of motion parameters . . . . . . . . . . . . . . . . . . . . . . 16 5.4 Function that performs value function itteration step . . . . . . . 19 5.5 Simulation routine . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.6 Auxiliary functions . . . . . . . . . . . . . . . . . . . . . . . . . . 26 2
  • 3. 1 Introduction A large number of dynamic general equilibrium macroeconomic models rely on the assumption of representative agent. In some cases it could be a rea- sonable assumption like in the case of complete markets. However the models that want to have strong microfoundation require agents heterogeneity. This set up makes models much more complicated and most of them can not be solved analytically. Numerical solution of such models is not an easy task as well. Rational agents in this type of models make decisions based on the entire distribution of individuals state variables. In general case this object is infinitely dimensional that makes problem extremely complicated. The paper by Per Krusell and An- thony Smith (henceforth KS) presents methodology of numerical approximation of equilibrium in heterogeneous agent models. The key assumption that is used to calculate the approximate equilibrium is the following: agents have a limited ability to predict the evolution of the distribution of individual state variables. It is shown in the paper that this bound does not significantly restrict agents. As a result, the law of motion that agents perceive could be described using finite-dimensional vector of distribu- tion parameters (moments could be used for this purpose). The rest of the report is organized as follows: in Model section I briefly explain the model, intuition behind the methodology and give model parame- ters that are used. In Algorithm section I will explain in details what are the steps for equilibrium computation and how I implemented them. In the results section I present my results and discuss them. 3
  • 4. 2 Model The benchmark model consists of continuum (measure one) ex-ante iden- tical consumers who maximize expected discounted utility subject to budget constraints in every period: max E0Σ∞ t=0βt U(ct) (1) subject to c + k − (1 − δ)k = y (2) where y is individual’s income that consists of two parts: return on capital that individual owns and wage that individual gets if he is employed. U(c) - CRRA function. Market for capital and labor is competitive so individual gets return and wage equal to marginal product. Production function is Cobb-Douglas: F(k, l) = z¯kα¯l1−α (3) where z - aggregate productivity shock, ¯k - aggregate capital in the economy, ¯l - total amount of labor supplied. There are two types of uncertainty in the model: aggregate (productivity shock - z) and idiosyncratic (individual unemployment shock - ). Both shocks follow first order Markov process with two possible states: zg and zb for aggre- gate productivity shock and 0 and 1 for idiosyncratic shock (0 means that the agent is unemployed). Parameters of the model specify the average duration of boom and recession in the economy. The authors are trying to match durations to the ones observed in the data (8 quaters for boom and recession). As a result, the transition matrix for aggregate shock is: Bad Good Bad 0.875 0.125 Good 0.125 0.875 The shock are dependent in the following sense: aggregate shock affects indi- vidial, but individual does not affect aggregate. The intuition behind this fact is the following: aggregate shock determines the state of the economy, in particular employment (unemployment in boom ug and recession ub are parameters of the model) that affects the evolution of the individual shock. The authors impose additional conditions for the dependence of aggregate and individual shock in order to identify the joint transition matrix. I wrote the function ”emprpr” to find this matrix by solving the system of linear equations.As a result, the joint transion matrix has the following form: Bad0 Bad1 Good0 Good1 Bad0 0.5250 0.35 0.0312 0.0938 Bad1 0.0389 0.8361 0.0021 0.1229 Good0 0.0937 0.0313 0.2917 0.5833 Good1 0.0091 0.1159 0.0243 0.8507 4
  • 5. Elements of this matrix satisfy system of the linear equations as I mentioned above. Four of these equations make sure that unemployment shares remains consistent with the model parameters ug and ub. Individual’s Bellman equation has the following form: v(k, ; Γ, z) = max c,k {U(c) + βE[v(k , ; Γ , z )|z, ]} (4) subject to c + k = r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k, Γ = H(Γ, z, z ), k ≥ 0 where k - individual capital in current period, - current employment, Γ - current distribution of capital, z - current aggregate shock. Notice that when agent is employed he supplies exogenously given amount of labor ˜l and gets wage. Otherwise an agent does not get a wage and return on capital is the only source of his income. The key assumption that allows compute equilibrium numerically is that agents are boundedly rational in their perception of law of motion. Therefore, current and future prices are assumed to be dependent only on the current moments of capital distribution. As a result, agent’s problem becomes: v(k, ; ¯k, z) = max c,k {U(c) + βE[v(k , ; ¯k , z )|z, ]} (5) subject to c + k = r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k, log(¯k ) = a0 + a1log(¯k) if z = zg log(¯k ) = b0 + b1log(¯k) if z = zb k ≥ 0 where w(¯k, ¯l, z) = (1 − α)z(¯k/¯l)α and r(¯k, ¯l, z) = αz(¯k/¯l)α−1 5
  • 6. For model simulations I have used the following parameters: Parameter Value Meaning β 0.99 Discount factor δ 0.025 Capital depreciation σ 1 Utility risk-aversion parameter α 0.36 Capital share in the production function zg 1.01 Productivity in a good state zb 0.99 Productivity in a bad state ug 0.04 Unemployment in a good state ub 0.1 Unemployment in a bad state ˜l 0.3271 Individual labor supply I want to point out that for the draft version I used the value of parameter ˜l = 1.11 that is not the one used in the paper. The reason was that the value of this parameter is not given in the published version of the paper. Therefore I looked in the other papers that are based on KS. There ˜l is normalized such that labor supply in a bad state is equal to one (˜l = 1 1−ub = 1 0.9 ≈ 1.11). Having obtained results that are far from the ones in the paper I looked at the working paper version of KS, where side note 9 on the page 10 is the following: For the final version of the Empirical Methods paper I am using the value of exogenous labor supply ˜l = 0.3271 that is the one exploited in KS. 6
  • 7. 3 Algorithm In this section I discuss equilibrium computation algorithm and it’s imple- mentation. Step 1: Compute transition probabilities. There are 4 possible states for in- dividual in each period: (z, ) ∈ {(b, 0), (b, 1), (g, 0), (g, 1)} and paper contains 16 linear equations that determine these probabilities. As a mentioned in a Model section evolution of (z, ) follows first order Markov process. The function ”emprpr” contains these 16 equation and when we apply fsolve to this function we obtain transition probabilities. These equations reflect the following facts: individual employment can not affect aggregate economy state, unemployment for each aggregate state should be consistent with model param- eters ub and ug, etc. Step 2: Choose parameters (a0, a1, b0, b1) for the law of motion of aggregate capital. Chose grid for k and ¯k. KS suggest to have 70-130 points in k dimen- sion with more points close to 0; and 4-6 points in ¯k dimension. Then starting with zero initial value function do the following: do the value function iteration step for the points on the grid, interpolate obtained result and continue these two procedures until policy functions on a grid for two consecutive iterations will be close enough. To be more precise the value function iteration step in this set up is the following (denote f ≡ r(¯k, ¯l, z)k + w(¯k, ¯l, z)˜l + (1 − δ)k): vt+1(k, ; ¯k, z) = max k ∈[0,f] {log(k − k ) + β z , π(z , |z, )vt(k , ; ¯k , z )} (6) Notice that vt(k , ; ¯k , z ) is defined for all k ∈ [0, f], so when we are trying to find vt+1(k, ; ¯k, z) for the points on the grid we solve continuous optimiza- tion problem. Having obtained vt+1(k, ; ¯k, z) for the points on the grid we interpolate this function so that it can be used in the next iteration. We start with v0(k, ; ¯k, z) = 0 for all k and k’. This procedure allows me to find policy functions. In fact I need to find four policy functions because there are four combinations of aggregate and idiosyncratic shocks. The function ”emprrhs” computes next iteration interpolated value function given as inputs interpolated value functions from the previous iteration. I did not maximize value functions in each point of the grid separately. I maximize sum of value function that should produce the same result given that value functions at each point of the grid have different arguments. To be more precise if you denote mij(k ) ≡ log(kj − k ) + β z , π(z , |z, )vt(k , ; ¯ki , z ) (it is a function that you need to optimize in order to get value function in a grid point (kj, ¯ki)), then you can find kij ∈ argmax{mij(k )} either by optimizing all mij(k ) separately or sum them up and maximize jointly. These two procedures should lead to the same result because all optimizations problems are indepen- dent, however I joint optimization allows to reduce computation time (it is the case because I can supply gradient for optimization routine). For interpolation 7
  • 8. purpose I used not spline routine but pchip (Piecewise Cubic Hermite Inter- polating Polynomials) because it allows to produces functions without wiggles even for relatively small number of grid points (it means that value functions will be increasing and concave). Step 3: Given policy functions simulate 5000 agents and 11000 periods. This procedure includes simulation of aggregate and idiosyncratic shocks according to their conditional distribution. Policy functions give us individual capital ev- ery period, so we can calculate actual aggregate capital. Then we can regress log of actual aggregate capital on log of previous period actual aggregate capital in good and bad times. This procedure gives us new parameters (a0, a1, b0, b1) of the aggregate law of motion. Then we can go back to the Step 2. We repeat Steps 2 and 3 until R2 for the regression of the log of actual capital is high enough or until parameters (a0, a1, b0, b1) don’t change too much. It means |ai 0 − ai+1 0 | + |ai 1 − ai+1 1 | + |bi 0 − bi+1 0 | + |bi 1 − bi+1 1 | < eps. The function ”emprsim3” does simulation given policy functions and returns implied sequence of aggregate capital and shocks. The difference with the func- tion ”emprsim” that I used in the draft version is the following: I managed to vectorize computations in the agent’s dimension that reduced required for sim- ulations time from 3 hours to 1 minute (these numbers reflect computational time on ”hammer” machine). 8
  • 9. 4 My results For the results part I started with the following: I took the true values of the parameters of the law of motion and tried to reproduce Figure 1 and Figure 2 from the paper. log(¯k ) = 0.095 + 0.962log(¯k) if z = zg log(¯k ) = 0.085 + 0.965log(¯k) if z = zb In order to solve agent’s Bellman equation I used the following grid: k = [0.00001,0.00004,0.00008,0.0001,0.001:0.013:0.04,0.05:0.15:1,1:1:35]; (50 points) ¯k = [7.7 : 4 : 19.7]; (4 points) I am using less grid points in both dimensions than in the draft version because Piecewise Cubic Hermite Interpolating Polynomials that I am using allows me to do that. These polynomials don’t produce wiggly function that permits me to use less grid points without substantial lose of interpolating preci- sion. Less grid point allows me to reduce time of one iteration for value functions iterations. So within the same time period I can perform more iterations and solve policy function more precisely on a grid. The stopping criteria was that average absolute change of the policy functions for two consecutive iterations (across all grid points for all value functions) should be less than 0.00005. In the draft version it was 0.005. I use average absolute change not maximum absolute change, but it does not make a significant difference here because convergence is moreless uniform on a grid points. I have got the following figure that contains policy function for agent in a good state. Analog of Figure 2: Tomorrow’s Individual Capital as a function of Today’s Individual Capital 9
  • 10. Then I simulate economy with 5000 individuals (who follow obtained policy functions) for 11000 periods. I started with the good aggregate state where all individuals hold 10 units of capital. I discarded first 1000 periods and tried to reproduce Figure 1 in the paper. Analog of Figure 1: Tomorrow’s Aggregate Capital as a function of Today’s Aggregate Capital As we can see this figure is slightly different from the one in the paper (see Figure 1 from the paper on the next page). When I run regression of the next period aggregate capital on today’s period aggregate capital I have got the following results: In good times: log(¯k ) = 0.0993 + 0.9601log(¯k); R2 = 0.999941 ˆσ = 0.0186% In bad times: log(¯k ) = 0.0877 + 0.9624log(¯k); R2 = 0.999851 ˆσ = 0.0291% 10
  • 11. Figure 1 from the paper: Tomorrow’s Aggregate Capital as a function of Today’s Aggregate Capital As we can see, obtained coefficients are close but not exactly the ones we started from. In fact the sum of absolute coefficients change is of magnitude 0.01 that can not be considered as very precise. Then I run the entire procedure - I started with the following guesses for the aggregate capital law of motion: log(¯k ) = 0 + log(¯k) if z = zg log(¯k ) = 0 + log(¯k) if z = zb The results of the first four iterations are presented in the table below: The smallest change in coefficients is observed in the step from third to forth iteration. I did not incude more iterations in the table because the procedure diverges. The procedure does not converge because at some point iterations produce range for the aggregate capital that fall outside the range where I have grid points for the aggregate capital. 11
  • 12. 5 Appendix 5.1 Short description of the functions Section 5.2: emprmainbchm - main function that performs iterations for the aggregate cap- ital law of motion coefficients. Section 5.3: emprmain1 - function that finds value functions for given aggregate capital law of motion coefficients. Section 5.4: emprrhs - function that performs value iteration step for given aggregate capital law of motion coefficients. Section 5.5: emprsim3 - function that simulates time series for the aggregate capital given agents’ policy functions Section 5.6: emprcon2 - function that constructs linear constraints for the optimization re- quired for value function iterations. This constraint insures that we will have increasing in individual capital value function. This constraint was needed when I used spline interpolation. For Hermite interpolation this constraint does play a role. emprder - function that computes coefficients of the piecewise polynomials derivative, this function is used to supply optimization routine in the value iteration step a gradient. emprderu - function that returns the derivative of the utility function emprf00, emprf01, emprf10, emprf11 - functions that are optimized in order to perform value function iteration step. emprfun - function that computes next period aggregate capital for today’s corresponding grid points for aggregate capital emprpr - function that returns vector of zeros when all conditions for tran- sition probabilitis is satisfied. Applying fsolve to this function allows to get 4x4 transition probabilities matrix. emprr - function that computes returns on capital. empru - function that returns value of the utility function. 12
  • 13. emprvalev - function that computes value function in all grid points. emprw - function that computes wage. 13
  • 14. 5.2 Main function clear; diary(’output6.txt’); diary on; global a0 a1 b0 b1; a0=0; a1=1; b0=0; b1=1; ccoef=zeros(50,8); eps=1; cccount=1; lll=0.9; while ((eps>0.01)&&(cccount<50)) display(’Start of outer iteration’); cccount a0 a1 b0 b1 emprmain1; [res,res2]=emprsim3(); stats=regstats(log(res(find(ismember(res2(1:end-1),1))+1)),... log(res(find(ismember(res2(1:end-1),1)))),’linear’); stats2=regstats(log(res(find(ismember(res2(1:end-1),0))+1)),... log(res(find(ismember(res2(1:end-1),0)))),’linear’); display(’Good state’); aa0=stats.beta(1,1) aa1=stats.beta(2,1) stats.rsquare (stats.mse)^0.5 ccoef(cccount,1)=aa0; ccoef(cccount,2)=aa1; ccoef(cccount,3)=stats.rsquare; ccoef(cccount,4)=(stats.mse)^0.5; display(’Bad state’); bb0=stats2.beta(1,1) bb1=stats2.beta(2,1) stats2.rsquare (stats2.mse)^0.5 14
  • 16. 5.3 Function that finds value functions for given aggregate capital law of motion parameters tic global bbeta delta sig alpha zg zb ug ub prag durub durug mkk mkkb trpr kk kkb a0 a1 b0 b1 in00 in01 in10 in11 plcov fnorm mc00 mc01 mc10 mc11 limp con ltild mouse; % prag(i,j) - probabilities of transition for the aggregate state from i to % j where 1 - bad, 2 - good % mkk - matrix that contains grid points for k, each row is a grid for k, % number of rows is equal to the number of grid points for kbar, all rows % are the same % mkkb - matrix that contains grid points for kbar, each column is a grid % for kbar, number of colums is equal to the number of grid points for k, % all columns are the same % trpr - transition probabilities of moving from different conbinations of % aggregate and idiosyncratic shocks % orger of rows and columns is the following % 1 - b0 % 2 - b1 % 3 - g0 % 4 - g1 % kk - grid for k % kkb - grid for kbar % a0, a1, b0, b1 - coefficients for the aggregate capital law of motion % in00, in01, in10, in11 - initial value for the policy function (it is % needed for maximization routine). The results from the previous step of % the value function itteration is used here. % plcov - (policy convergence) this value contains the distance between % policy functions for the consecutive itterations. % If it is small then value function iterations converged plcov=2; % average duration of bad and good times durb=8; durg=8; prag=[1-1/durb, 1/durb; 1/durg, 1-1/durg]; % average duration of unemployment in good and bad times durub=2.5; 16
  • 17. durug=1.5; ub=0.1; ug=0.04; zb=0.99; zg=1.01; alpha=0.36; % see workpaper version ltild=0.3271; sig=1; bbeta=0.99; delta=0.025; kk=[0.00001,0.00004,0.00008,0.0001,0.001:0.013:0.04,0.05:0.15:1,1:1:35]; kkb=[7.7:4:19.7]; % mkk(i,j) - amount of capital if kbar corresponds to i-s point on the grid % and kk corresponds to j-s point on the grid mkk=repmat(kk,size(kkb,2),1); mkkb=repmat(kkb’,1,size(kk,2)); in00=zeros(size(mkk)); in01=zeros(size(mkk)); in10=zeros(size(mkk)); in11=zeros(size(mkk)); % calculation of transition probabilities f=@(p) emprpr(p); [trpr,fval,exitflag,output] = fsolve(f,ones(4,4)); mc00=emprr(0).*mkk+(1-delta).*mkk; % 1 - aggregate state is good % 0 - person is unemployed mc10=emprr(1).*mkk+(1-delta).*mkk; mc01=emprr(0).*mkk+emprw(0)*ltild+(1-delta).*mkk; mc11=emprr(1).*mkk+emprw(1)*ltild+(1-delta).*mkk; %Zero initial value function y00=zeros(size(mkk)); v00=pchip(kk,y00); y01=zeros(size(mkk)); v01=pchip(kk,y01); y10=zeros(size(mkk)); v10=pchip(kk,y10); 17
  • 19. 5.4 Function that performs value function itteration step function [res00,res10,res01,res11]=emprrhs(v00,v10,v01,v11) % function that computes rhs for the value function iteration % v00 - array that contains: v00(i) - function of kprime given i-s point on % the grid for kbar and shocks 0-unemployed and 0-bad times global kk kkb mkk delta trpr bbeta in00 in01 in10 in11 plcov fnorm mc00 mc01 mc10 mc11 vp00 vp01 vp10 vp11 limp con mouse; kkpg=emprfun(1); kkpb=emprfun(0); help00=(ppval(v00,kk)); help10=(ppval(v10,kk)); help01=(ppval(v01,kk)); help11=(ppval(v11,kk)); h00=pchip(kkb,help00’); h10=pchip(kkb,help10’); h01=pchip(kkb,help01’); h11=pchip(kkb,help11’); he00=(ppval(h00,kkpb)); he10=(ppval(h10,kkpg)); he01=(ppval(h01,kkpb)); he11=(ppval(h11,kkpg)); % functions of kprime for every kbarprime that correspond to kbar vp00=pchip(kk,he00’); vp10=pchip(kk,he10’); vp01=pchip(kk,he01’); vp11=pchip(kk,he11’); % derivatives of these functions vp00d=fnder(vp00,1); vp00d.coefs=emprder(vp00.coefs); vp01d=fnder(vp01,1); vp01d.coefs=emprder(vp01.coefs); vp10d=fnder(vp10,1); vp10d.coefs=emprder(vp10.coefs); vp11d=fnder(vp11,1); vp11d.coefs=emprder(vp11.coefs); nvar=4*size(mkk,1)*size(mkk,2); epss=1e-8; 19
  • 20. options=optimset(’Display’,’off’,’Algorithm’,’interior-point’,’TolFun’, 1e-8,’TolX’,1e-8,’MaxFunEvals’,2000000,’TolCon’,1e-8,’MaxIter’,2000); emprf00(in00) ini00=max(min(mc00-epss,in00),epss*ones(size(mc00))); ini01=max(min(mc01-epss,in01),epss*ones(size(mc01))); ini10=max(min(mc10-epss,in10),epss*ones(size(mc10))); ini11=max(min(mc11-epss,in11),epss*ones(size(mc11))); hf=-epss*ones(size(mkk,1)*(size(mkk,2)-1),1); [pl00,fval,exitflag,output]=ktrlink(@emprf00,ini00,con,hf,[],[], epss*ones(size(mkk)),mc00-epss,[],options); pl00 [pl01,fval,exitflag,output]=ktrlink(@emprf01,ini01,con,hf,[],[], epss*ones(size(mkk)),mc01-epss,[],options); pl01 [pl10,fval,exitflag,output]=ktrlink(@emprf10,ini10,con,hf,[],[], epss*ones(size(mkk)),mc10-epss,[],options); pl10 [pl11,fval,exitflag,output]=ktrlink(@emprf11,ini11,con,hf,[],[], epss*ones(size(mkk)),mc11-epss,[],options); pl11 plcov=(sum(sum(abs(pl00-ini00)))+sum(sum(abs(pl01-ini01)))+ sum(sum(abs(pl10-ini10)))+sum(sum(abs(pl11-ini11)))); in00=pl00; in01=pl01; in10=pl10; in11=pl11; fv00=(limp*empru(mc00-pl00)+bbeta*(trpr(1,1)*emprvalev(vp00,pl00)+ ... +trpr(1,2)*emprvalev(vp01,pl00)+trpr(1,3)*emprvalev(vp10,pl00)+... +trpr(1,4)*emprvalev(vp11,pl00))); fv01=(limp*empru(mc01-pl01)+bbeta*(trpr(2,1)*emprvalev(vp00,pl01)+ ... +trpr(2,2)*emprvalev(vp01,pl01)+trpr(2,3)*emprvalev(vp10,pl01)+... +trpr(2,4)*emprvalev(vp11,pl01))); fv10=(limp*empru(mc10-pl10)+bbeta*(trpr(3,1)*emprvalev(vp00,pl10)+ ... +trpr(3,2)*emprvalev(vp01,pl10)+trpr(3,3)*emprvalev(vp10,pl10)+... +trpr(3,4)*emprvalev(vp11,pl10))); fv11=(limp*empru(mc11-pl11)+bbeta*(trpr(4,1)*emprvalev(vp00,pl11)+ ... +trpr(4,2)*emprvalev(vp01,pl11)+trpr(4,3)*emprvalev(vp10,pl11)+... +trpr(4,4)*emprvalev(vp11,pl11))); 20
  • 22. 5.5 Simulation routine function [res,res2]=emprsim3() % function that simulates nn consumers for tt time periods and we burn % first burn periods, because aggregate capital there is affected by % initial wealth distribution global ub ug kkb in00 in01 in10 in11 trpr prag kk; nn=5000; tt=11000; burn=1000; % suppose initially everybody has the same level of capital ink and % aggregate state is good ink=10; kap0=ink*ones(nn,1); kap=zeros(nn,1); agst=zeros(1,tt); empl=(rand(nn,1)>ug); agkap=zeros(1,tt); agkap(1)=ink; kap=kap0; kap2=zeros(nn,1); empl2=zeros(nn,1); agst(1,1)=1; % first index is aggreagate state, second - employment ii00=pchip(kkb,in00’); ii01=pchip(kkb,in01’); ii10=pchip(kkb,in10’); ii11=pchip(kkb,in11’); help=1; % good initial state for i=2:1:tt i he00=(ppval(ii00,agkap(i-1))); he10=(ppval(ii10,agkap(i-1))); he01=(ppval(ii01,agkap(i-1))); he11=(ppval(ii11,agkap(i-1))); i00=pchip(kk,he00’); i10=pchip(kk,he10’); i01=pchip(kk,he01’); i11=pchip(kk,he11’); help2=rand(1,1); 22
  • 23. if (help==0) if (help2<prag(1,1)) help3=0; else help3=1; end; end; if (help==1) if (help2<prag(2,1)) help3=0; else help3=1; end; end; agst(1,i)=help3; help4=rand(nn,1); if ((help==0) && (help3==0)) thresh=(1-empl)*trpr(1,1)/prag(1,1)+empl*trpr(2,1)/prag(1,1); empl2=(help4>thresh); hh=sum(ismember(empl2,1)); % number of employed guys % adjustment of employment in order to have exact unemployment % rates if (hh>nn*(1-ub)) hhh=find(ismember(empl2,1)); emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=0; end; if (hh<nn*(1-ub)) hhh=find(ismember(empl2,0)); emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=1; end; kap2(find(ismember(empl,1)))=ppval(i01,kap(find(ismember(empl,1)))); kap2(find(ismember(empl,0)))=ppval(i00,kap(find(ismember(empl,0)))); end; if ((help==1) && (help3==0)) thresh=(1-empl)*trpr(3,1)/prag(2,1)+empl*trpr(4,1)/prag(2,1); empl2=(help4>thresh); 23
  • 24. hh=sum(ismember(empl2,1)); % number of employed guys % adjustment of employment in order to have exact unemployment % rates if (hh>nn*(1-ub)) hhh=find(ismember(empl2,1)); emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=0; end; if (hh<nn*(1-ub)) hhh=find(ismember(empl2,0)); emppl2(hhh(1:(hh-nn*(1-ub)),1),1)=1; end; kap2(find(ismember(empl,1)))=ppval(i11,kap(find(ismember(empl,1)))); kap2(find(ismember(empl,0)))=ppval(i10,kap(find(ismember(empl,0)))); end; if ((help==0) && (help3==1)) thresh=(1-empl)*trpr(1,3)/prag(1,2)+empl*trpr(2,3)/prag(1,2); empl2=(help4>thresh); hh=sum(ismember(empl2,1)); % number of employed guys % adjustment of employment in order to have exact unemployment % rates if (hh>nn*(1-ug)) hhh=find(ismember(empl2,1)); emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=0; end; if (hh<nn*(1-ug)) hhh=find(ismember(empl2,0)); emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=1; end; kap2(find(ismember(empl,1)))=ppval(i01,kap(find(ismember(empl,1)))); kap2(find(ismember(empl,0)))=ppval(i00,kap(find(ismember(empl,0)))); end; if ((help==1) && (help3==1)) thresh=(1-empl)*trpr(3,3)/prag(2,2)+empl*trpr(4,3)/prag(2,2); empl2=(help4>thresh); 24
  • 25. hh=sum(ismember(empl2,1)); % number of employed guys % adjustment of employment in order to have exact unemployment % rates if (hh>nn*(1-ug)) hhh=find(ismember(empl2,1)); emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=0; end; if (hh<nn*(1-ug)) hhh=find(ismember(empl2,0)); emppl2(hhh(1:(hh-nn*(1-ug)),1),1)=1; end; kap2(find(ismember(empl,1)))=ppval(i11,kap(find(ismember(empl,1)))); kap2(find(ismember(empl,0)))=ppval(i10,kap(find(ismember(empl,0)))); end; help=help3; agkap(1,i)=sum(kap2)/nn; agkap(1,i) kap=kap2; empl=empl2; end; res=agkap(1,burn+1:end); res2=agst(1,burn+1:end); 25
  • 26. 5.6 Auxiliary functions function res=emprcon2() global mkk; h1=ones(size(mkk,1)*(size(mkk,2)-1),1); h2=eye(size(mkk,1)*size(mkk,2))+diag(-h1,size(mkk,1)); res=h2(1:end-size(mkk,1),:); ***************************************************************************** function res=emprder(coef) % coef - each row contains coefficients of piecewise polynomials at some % interval, for instance [1,0,2] means x^2+2 col=size(coef,2); row=size(coef,1); help=repmat(col-1:-1:1,row,1); res=coef(:,1:end-1).*help; ***************************************************************************** function res=emprderu(c) global sig; if (sig==1) res=1./c; else res=(c.^(-sig)); end; ***************************************************************************** function [res,res2]=emprf00(kp) global mc00 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d; % res - value of the function %kp=reshape(kp,size(mc00,1),size(mc00,2)); res= -1*(sum(sum(limp*empru(mc00-kp)+bbeta*(trpr(1,1)*emprvalev(vp00,kp)+ ... +trpr(1,2)*emprvalev(vp01,kp)+trpr(1,3)*emprvalev(vp10,kp)+...+ +trpr(1,4)*emprvalev(vp11,kp))))); % res2 - drivative (since function has matrix argument derivative is matrix as well) res2= -1*(-1*limp*emprderu(mc00-kp)+bbeta*(trpr(1,1)*emprvalev(vp00d,kp)+ ... +trpr(1,2)*emprvalev(vp01d,kp)+trpr(1,3)*emprvalev(vp10d,kp)+... +trpr(1,4)*emprvalev(vp11d,kp))); 26
  • 27. function [res,res2]=emprf01(kp) global mc01 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d; res= -1*(sum(sum(limp*empru(mc01-kp)+bbeta*(trpr(2,1)*emprvalev(vp00,kp)+ ... +trpr(2,2)*emprvalev(vp01,kp)+trpr(2,3)*emprvalev(vp10,kp)+... +trpr(2,4)*emprvalev(vp11,kp))))); res2=-1*(-1*limp*emprderu(mc01-kp)+bbeta*(trpr(2,1)*emprvalev(vp00d,kp)+ ... +trpr(2,2)*emprvalev(vp01d,kp)+trpr(2,3)*emprvalev(vp10d,kp)+... +trpr(2,4)*emprvalev(vp11d,kp))); ***************************************************************************** function [res,res2]=emprf10(kp) global mc10 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d; res=-1*(sum(sum(limp*empru(mc10-kp)+bbeta*(trpr(3,1)*emprvalev(vp00,kp)+ ... +trpr(3,2)*emprvalev(vp01,kp)+trpr(3,3)*emprvalev(vp10,kp)+... +trpr(3,4)*emprvalev(vp11,kp))))); res2=-1*(-1*limp*emprderu(mc10-kp)+bbeta*(trpr(3,1)*emprvalev(vp00d,kp)+ ... +trpr(3,2)*emprvalev(vp01d,kp)+trpr(3,3)*emprvalev(vp10d,kp)+... +trpr(3,4)*emprvalev(vp11d,kp))); ***************************************************************************** function [res,res2]=emprf11(kp) global mc11 trpr vp00 vp01 vp10 vp11 bbeta limp vp00d vp01d vp10d vp11d; res= -1*(sum(sum(limp*empru(mc11-kp)+bbeta*(trpr(4,1)*emprvalev(vp00,kp)+ ... +trpr(4,2)*emprvalev(vp01,kp)+trpr(4,3)*emprvalev(vp10,kp)+... +trpr(4,4)*emprvalev(vp11,kp))))); res2= -1*(-1*limp*emprderu(mc11-kp)+bbeta*(trpr(4,1)*emprvalev(vp00d,kp)+ ... +trpr(4,2)*emprvalev(vp01d,kp)+trpr(4,3)*emprvalev(vp10d,kp)+... +trpr(4,4)*emprvalev(vp11d,kp))); 27
  • 28. function res=emprfun(flag) % function that computes next period aggregate capital for today’s % correponding grid points for aggregate capital % kkb - grid for aggregate capital % flag=0 - bad state % flag=1 - good state global a0 a1 b0 b1 kkb; if (flag==0) res=exp(b0+b1*log(kkb)); elseif (flag==1) res=exp(a0+a1*log(kkb)); end; ***************************************************************************** function res=emprpr(pprob) % function that returns vector of zeros when all conditions for transition % probabilitis is satisfied % pprob is 4x4 % pprob(i,j) is probability of transition from state i to j % where i,j belongs to 1 - b0, 2 - b1, 3 - g0, 4 - g1 global ug ub prag durub durug; res=zeros(16,1); res(1,1)=1-1/durub-pprob(1,1)/prag(1,1); res(2,1)=1-1/durug-pprob(3,3)/prag(2,2); res(3,1)=pprob(1,1)+pprob(1,2)-prag(1,1); res(4,1)=pprob(2,1)+pprob(2,2)-prag(1,1); res(5,1)=pprob(1,3)+pprob(1,4)-prag(1,2); res(6,1)=pprob(2,3)+pprob(2,4)-prag(1,2); res(7,1)=pprob(3,1)+pprob(3,2)-prag(2,1); res(8,1)=pprob(4,1)+pprob(4,2)-prag(2,1); res(9,1)=pprob(3,3)+pprob(3,4)-prag(2,2); res(10,1)=pprob(4,3)+pprob(4,4)-prag(2,2); res(11,1)=pprob(3,1)*prag(1,1)-1.25*pprob(1,1)*prag(2,1); res(12,1)=pprob(1,3)*prag(2,2)-0.75*pprob(3,3)*prag(1,2); res(13,1)=ub*pprob(1,1)+(1-ub)*pprob(2,1)-ub*prag(1,1); res(14,1)=ub*pprob(1,3)+(1-ub)*pprob(2,3)-ug*prag(1,2); res(15,1)=ug*pprob(3,1)+(1-ug)*pprob(4,1)-ub*prag(2,1); res(16,1)=ug*pprob(3,3)+(1-ug)*pprob(4,3)-ug*prag(2,2); 28
  • 29. function res=emprr(flag) % function computes return on capital % flag=0 - bad aggregate state % flag=1 - good aggregate state global ub ug alpha zb zg mkkb ltild; if (flag==0) res=alpha*zb*(mkkb.^(alpha-1))/((ltild*(1-ub))^(alpha-1)); elseif (flag==1) res=alpha*zg*(mkkb.^(alpha-1))/((ltild*(1-ug))^(alpha-1)); end; ***************************************************************************** function res=empru(c) global sig; if (sig==1) res=log(c); else res=(c.^(1-sig))/(1-sig); end; ***************************************************************************** function res=emprvalev(cs,kpr) % function that computes values for the value function % res(i,j) = cs(i)(kpr(i,j)) help=ppval(cs,kpr); help2=repmat(eye(size(kpr,1),size(kpr,1)),[1,1,size(kpr,2)]); help3=help.*help2; help4=sum(help3); res=squeeze(help4); ***************************************************************************** function res=emprw(flag) % function computes wages % flag=0 - bad aggregate state % flag=1 - good aggregate state global ub ug alpha zb zg mkkb ltild; if (flag==0) res=(1-alpha)*zb*(mkkb.^(alpha))/((ltild*(1-ub))^(alpha)); elseif (flag==1) res=(1-alpha)*zg*(mkkb.^(alpha))/((ltild*(1-ug))^(alpha)); end; 29