-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistsim.m
147 lines (135 loc) · 6.47 KB
/
distsim.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% Computes impulse response of states and jumps from Klein solution.
% then calls computeirf.m to calculate IRFs of all other variables.
%
% Jumps are: V, C, p.
% States are: z, Phihat.
% z is an exogenous state with shocks; Phihat is a predetermined, endogenous state.
% All state and jump variables represent deviations from the steady state,
% so STATEDYNAMICS must have all eigenvalues <= 1.
%
% Klein solution is:
% states_t+1 = STATEDYNAMICS*states_t + [shocks_t+1 ; 0]
% jumps_t = JUMPS*states_t.
%
%for simplicity, 'time' is same as index of row vectors: starts at time=1.
%SET TIME 1 DISTRIBUTION (both dist and shock can be 0 or nonzero at initial time)
if INITCONDIT==0 %STEADY STATE: this is the baseline case
disp('DISTSIM')
disp('STARTING FROM STEADY STATE DISTRIBUTION')
disp(sprintf('\n'))
shiftdist = Pdist;
elseif INITCONDIT==1 %STARTING FROM SHIFTED DISTRIBUTION in p direction (like $ shock)
shiftsteps = 2 %number of steps in p direction
disp('DISTSIM')
disp(sprintf('START FROM SHIFTED DISTRIBUTION (%d steps in p direction):',shiftsteps))
disp(sprintf(' equivalent to %0.3g pct INCREASE in money supply.',100*pstep*shiftsteps))
disp(sprintf('\n'))
%Note: an INCREASE in money supply is a DECREASE in real prices
shiftdist = Pdist;
if shiftsteps>= 0
shiftdist(1,:) = sum(shiftdist(1:1+shiftsteps,:));
shiftdist(2:nump-shiftsteps,:) = shiftdist(shiftsteps+2:nump,:);
shiftdist(nump-shiftsteps+1:nump,:) = 0;
elseif shiftsteps < 0
error('ERROR! must program this!')
end
elseif INITCONDIT==2 %STARTING FROM SHIFTED DISTRIBUTION in A direction (like tech shock)
shiftsteps = 2 %number of steps in A direction
disp('DISTSIM')
disp(sprintf('START FROM SHIFTED DISTRIBUTION (%d steps in A direction):',shiftsteps))
disp(sprintf(' equivalent to %0.3g pct INCREASE in productivity.',100*pstep*shiftsteps))
disp(sprintf('\n'))
%Note: an INCREASE in productivity is a DECREASE in marginal cost
shiftdist = Pdist;
if shiftsteps>= 0
shiftdist(:,1) = sum(shiftdist(:,1:1+shiftsteps)')'; %#ok<UDIM>
shiftdist(:,2:nums-shiftsteps) = shiftdist(:,shiftsteps+2:nums);
shiftdist(:,nums-shiftsteps+1:nums) = 0;
elseif shiftsteps < 0
error('ERROR! must program this!')
end
elseif INITCONDIT==3 %STARTING ALL FROM SAME PRICE
disp('DISTSIM')
disp(sprintf('ALL FIRMS START FROM SAME PRICE (middle point of price grid)'))
disp(sprintf('\n'))
shiftdist = zeros(size(Pdist));
if rem(nump,2)==1 %nump is odd
midp = ceil(nump/2)
shiftdist(midp,:) = sum(Pdist);
else %nump is even
midp = nump/2
shiftdist(midp,:) = sump(Pdist/2);
shiftdist(midp+1,:) = sump(Pdist/2);
end
elseif INITCONDIT==4 %ALL START FROM SAME PRICE and PRODUCTIVITY (like switching on the idiosyncratic shocks!!)
disp('DISTSIM')
disp(sprintf('SWITCHING ON THE IDIO SHOCKS: all firms start from middle price, middle prod'))
disp(sprintf('\n'))
% perhaps should start from a distribution over prices for same productivity
shiftdist = zeros(size(Pdist));
if (rem(nump,2)==1 && rem(nums,2)==1) %nump and nums are odd
midp = ceil(nump/2)
mids = ceil(nums/2)
shiftdist(midp,mids) = sum(sum(Pdist));
else
error('ERROR! must program this!')
end
elseif INITCONDIT==5 %ALL START FROM OPTIMAL FLEXIBLE PRICE
disp('DISTSIM')
disp(sprintf('SWITCHING ON THE STICKINESS: all firms start from OPTIMAL FLEXIBLE price'))
disp(sprintf('\n'))
Pflex=log(wbar)+sgrid+log(epsilon/(epsilon-1)); %#ok<NASGU>
Pdisteroded = RECURSEMAT*Pdist*TRANSMAT'; % COMBINING TWO STEPS
Pmoves=sum(ones(size(Pdisteroded)).*Pdisteroded); % marginal density (over price) of firms adjusting price
shiftdist = zeros(size(Pdist));
for col=1:nums % loop building the new joint density (p,mc)
OPTindHI=find(Pgrid>Pflex(col),1);
OPTindLO=max(OPTindHI-1,1);
shiftdist(OPTindHI,col)=shiftdist(OPTindHI,col)+ ...
Pmoves(col)*(Pflex(col)-Pgrid(OPTindLO))/pstep;
shiftdist(OPTindLO,col)=shiftdist(OPTindLO,col)+ ...
Pmoves(col)*(Pgrid(OPTindHI)-Pflex(col))/pstep;
end
elseif INITCONDIT==6 %ALL START FROM OPTIMAL STICKY PRICE, as if they had just been forced to adjust (EURO)
disp('DISTSIM')
disp(sprintf('ONE FORCED ADJUSTMENT: all firms start from OPTIMAL STICKY price'))
disp(sprintf('\n'))
%THIS CODE ASSUMES CORRECT Pdist and Pstar are in memory.
shiftdist = zeros(size(Pdist));
%TEXT ADAPTED FROM GE_Pdist_iter:
Pdisteroded = RECURSEMAT*Pdist*TRANSMAT'; % COMBINING TWO STEPS
Pmoves=sum(ones(size(Pdisteroded)).*Pdisteroded); % marginal density (over price) of firms adjusting price
for col=1:nums % loop building the new joint density (p,mc)
OPTindHI=find(Pgrid>pstar(col),1);
OPTindLO=max(OPTindHI-1,1);
shiftdist(OPTindHI,col)=shiftdist(OPTindHI,col)+ ...
Pmoves(col)*(pstar(col)-Pgrid(OPTindLO))/pstep;
shiftdist(OPTindLO,col)=shiftdist(OPTindLO,col)+ ...
Pmoves(col)*(Pgrid(OPTindHI)-pstar(col))/pstep;
end
else
error('ERROR!! must specify initial conditions for simulation!')
end
if abs(sum(sum(shiftdist))-1 > 1e-10)
error('ERROR!! probabilities dont sum to 1!!!')
end
initdist = shiftdist - Pdist; %deviation from steady state
initdist = initdist(:);
initscalarstates = zeros(nss,1); % scalar states start from steady-state
%PUT TOGETHER ALL TIME 1 INITIAL CONDITIONS
initSTATE = [time1Rshock; time1TFPshock; initdist; initscalarstates]; %TIME 1 STATE
initJUMPS = JUMPS*initSTATE;
STATEHISTORY = NaN*zeros(nz+nPhi+nss,TT);
JumpHistory = NaN*zeros(nV+nsj,TT);
STATEHISTORY(:,1) = initSTATE; %time 1 state
JumpHistory(:,1) = initJUMPS; %time 1 jumps
%initialize time 1 state for loop:
STATENOW = initSTATE;
% SPECIFY ZERO SHOCKS for the distribution
zeroshocks = zeros(gridsize+nss,1); % gridsize is the size of Pdist(:)
%NOW SIMULATE DYNAMICS
for time = 2:TT
STATENOW = STATEDYNAMICS*STATENOW + [Rshocks(time); TFPshocks(time); zeroshocks];
JumpHistory(:,time) = JUMPS*STATENOW;
STATEHISTORY(:,time) = STATENOW;
end