| 1 |
// This mod file test the bgp.write function by characterizing the Balanced Growth Path of the Solow model. This is done 10000 times in a Monte Carlo loop |
| 2 |
// randomizing the initial guess of the nonlinear solver. |
| 3 |
|
| 4 |
var Efficiency |
| 5 |
EfficiencyGrowth |
| 6 |
Population |
| 7 |
PopulationGrowth |
| 8 |
Output |
| 9 |
PhysicalCapitalStock ; |
| 10 |
|
| 11 |
varexo e_x |
| 12 |
e_n ; |
| 13 |
|
| 14 |
parameters alpha |
| 15 |
delta |
| 16 |
s |
| 17 |
rho_x |
| 18 |
rho_n |
| 19 |
EfficiencyGrowth_ss |
| 20 |
PopulationGrowth_ss ; |
| 21 |
|
| 22 |
alpha = .33; |
| 23 |
delta = .02; |
| 24 |
s = .20; |
| 25 |
rho_x = .90; |
| 26 |
rho_n = .95; |
| 27 |
EfficiencyGrowth_ss = 1.01; |
| 28 |
PopulationGrowth_ss = 1.01; |
| 29 |
|
| 30 |
model; |
| 31 |
Efficiency = EfficiencyGrowth*Efficiency(-1); |
| 32 |
EfficiencyGrowth/EfficiencyGrowth_ss = (EfficiencyGrowth(-1)/EfficiencyGrowth_ss)^(rho_x)*exp(e_x); |
| 33 |
Population = PopulationGrowth*Population(-1); |
| 34 |
PopulationGrowth/PopulationGrowth_ss = (PopulationGrowth(-1)/PopulationGrowth_ss)^(rho_n)*exp(e_n); |
| 35 |
Output = PhysicalCapitalStock(-1)^alpha*(Efficiency*Population)^(1-alpha); |
| 36 |
PhysicalCapitalStock = (1-delta)*PhysicalCapitalStock(-1) + s*Output; |
| 37 |
end; |
| 38 |
|
| 39 |
if ~isoctave |
| 40 |
% The levenberg-marquardt algorithm is not available in octave's |
| 41 |
% implementation of fsolve, so we skip this check for Octave |
| 42 |
verbatim; |
| 43 |
|
| 44 |
bgp.write(M_); |
| 45 |
MC = 10000; |
| 46 |
KY = NaN(MC,1); |
| 47 |
GY = NaN(MC,1); |
| 48 |
GK = NaN(MC,1); |
| 49 |
EG = NaN(MC,1); |
| 50 |
if isoctave |
| 51 |
options = optimset('Display', 'off', 'MaxFunEvals', 1000000,'MaxIter',100000,'Jacobian','on','TolFun',1e-8,'TolX',1e-8); |
| 52 |
else |
| 53 |
options = optimoptions('fsolve','Display','off','Algorithm','levenberg-marquardt','MaxFunctionEvaluations',1000000,'MaxIterations',100000,'SpecifyObjectiveGradient',true,'FunctionTolerance',1e-8,'StepTolerance',1e-8); |
| 54 |
end |
| 55 |
reverseStr = ''; |
| 56 |
for i=1:MC |
| 57 |
y = 1+(rand(6,1)-.5)*.2; |
| 58 |
g = ones(6,1); |
| 59 |
[y, fval, exitflag] = fsolve(@solow.bgpfun, [y;g], options); |
| 60 |
if exitflag>0 |
| 61 |
KY(i) = y(6)/y(5); |
| 62 |
GY(i) = y(11); |
| 63 |
GK(i) = y(12); |
| 64 |
EG(i) = y(2); |
| 65 |
end |
| 66 |
if mod(i,100)==0 |
| 67 |
% Display the progress |
| 68 |
percentDone = 100 * i / MC; |
| 69 |
msg = sprintf('Percent done: %3.1f', percentDone); |
| 70 |
fprintf([reverseStr, msg]); |
| 71 |
reverseStr = repmat(sprintf('\b'), 1, length(msg)); |
| 72 |
end |
| 73 |
end |
| 74 |
fprintf('\n'); |
| 75 |
% Compute the physical capital stock over output ratio along the BGP as |
| 76 |
% a function of the deep parameters... |
| 77 |
theoretical_long_run_ratio = s*EfficiencyGrowth_ss*PopulationGrowth_ss/(EfficiencyGrowth_ss*PopulationGrowth_ss-1+delta); |
| 78 |
% ... and compare to the average ratio in the Monte Carlo |
| 79 |
mu = mean(KY(~isnan(KY))); |
| 80 |
s2 = var(KY(~isnan(KY))); |
| 81 |
nn = sum(isnan(KY))/MC; |
| 82 |
disp(sprintf('Theoretical K/Y BGP ratio = %s.', num2str(theoretical_long_run_ratio))); |
| 83 |
disp(sprintf('mean(K/Y) = %s.', num2str(mu))); |
| 84 |
disp(sprintf('var(K/Y) = %s.', num2str(s2))); |
| 85 |
disp(sprintf('Number of failures: %u (over %u problems).', sum(isnan(KY)), MC)); |
| 86 |
assert(abs(mu-theoretical_long_run_ratio)<1e-8); |
| 87 |
assert(s2<1e-16); |
| 88 |
assert(abs(mean(GY(~isnan(GY)))-EfficiencyGrowth_ss*PopulationGrowth_ss)<1e-8) |
| 89 |
assert(var(GY(~isnan(GY)))<1e-16); |
| 90 |
assert(abs(mean(GK(~isnan(GK)))-EfficiencyGrowth_ss*PopulationGrowth_ss)<1e-8) |
| 91 |
assert(var(GK(~isnan(GK)))<1e-16); |
| 92 |
assert(abs(mean(EG(~isnan(EG)))-EfficiencyGrowth_ss)<1e-8) |
| 93 |
assert(var(EG(~isnan(EG)))<1e-16); |
| 94 |
|
| 95 |
end; |
| 96 |
|
| 97 |
end; |