Advertisement
Guest User

Untitled

a guest
Dec 11th, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.85 KB | None | 0 0
  1. % Carroll, Overland, and Weil (1997, 2000)
  2. % Matlab programs 2003-2007 © Patrick Toche
  3.  
  4. function cow1
  5. clear all; delete thediary.out; diary thediary.out;
  6. disp('Patrick Toche©');
  7. disp('The Carroll, Overland, and Weil (2000) model');
  8. disp('The habit formation mechanism is defined in terms of consumption');
  9. global rho theta delta;
  10. rho=0.2; theta=0.05; delta=0.05;
  11. fprintf('rho = %7.3f, theta = %7.3f, delta = %7.3f\n',rho,theta,delta);
  12.  
  13. %%% calibrating the initial steady state
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. global sigma psi gam Delta;
  16. %Delta=3; sigma=5; psi=sigma-Delta; gam=psi/(sigma-1);
  17. Delta=3; psi=4; sigma=psi+Delta; gam=psi/(sigma-1);
  18. %sigma=9; gam=0.75; psi=gam*(sigma-1); Delta=sigma-psi;
  19. %sigma=3; psi=1.00; gam=psi/(sigma-1); Delta=sigma-psi;
  20. fprintf('sigma = %7.3f, psi = %7.3f, gamma = %7.3f, Delta = %7.3f\n',sigma,psi,gam,Delta);
  21. global g A;
  22. g=0.02; A=delta+theta+Delta*g;
  23. global xss yss y0 s dsdg;
  24. s=(g+delta)/A;
  25. dsdg=(delta+theta-delta*Delta)/(delta+theta+g*Delta)^2;
  26. fprintf('g = %7.3f, A = %7.3f, s = %7.3f, dsdg = %7.3f\n',g,A,s,dsdg);
  27. xss=1+(A-delta-theta)/(rho*Delta);
  28. yss=xss/(A-delta-rho*(xss-1));
  29. %y0=1.5*yss;
  30. y0=0.5*yss;
  31. fprintf('xss = %7.3f, yss = %7.3f, y0 = %7.3f\n',xss,yss,y0);
  32.  
  33. %%% checking that the computed steady state annulls the ODE equations
  34. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  35. %ess=zeros(2,1); ess(1)=xss; ess(2)=yss; odess=feval(@ODE,1,ess); fprintf('%5.6f\n',odess);
  36. %jss=zeros(2,2); jss=feval(@J,1,ess); tr=trace(jss); dt=det(jss); fprintf('tr = %5.6f, det = %5.6f\n',tr,dt);
  37.  
  38. %%% computing the dynamics using the bvp4c routine
  39. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  40. global T0 T N NMax RelTol AbsTol guess;
  41. T0=10; T=input('Horizon Length:  '); Tmax=min(50,T);
  42. N=10; NMax=500; RelTol=1e-4; AbsTol=1e-2;
  43. guess=[xss y0];
  44. %options = bvpset('Vectorized','on','Stats','on','RelTol',RelTol,'AbsTol',AbsTol','NMax',NMax);
  45. options = bvpset('FJacobian',@J,'Vectorized','on','Stats','on','RelTol',RelTol,'AbsTol',AbsTol','NMax',NMax);
  46. solinit = bvpinit(linspace(0,T0,N),guess);
  47. sol = bvp4c(@ODE,@BC,solinit,options);
  48. t = linspace(0,T0);
  49. e = deval(sol,t);  
  50. e(3,:) = (A-delta-theta+psi*rho.*(e(1,:)-1))/sigma;  %growth rate of consumption
  51. e(4,:) = A-delta-e(1,:)./e(2,:); %growth rate of capital
  52. e(5,:) = 1-e(1,:)./(A.*e(2,:)); %saving rate
  53. fprintf('With T = %g, x(0) = %7.7f, y(0) = %7.7f\n',T0,e(1,1),e(2,1));
  54. fprintf('With T = %g, x(T) = %7.7f, y(T) = %7.7f\n',T0,e(1,end),e(2,end));
  55.  
  56. %%% drawing some figures
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58.  
  59. figure(101);
  60. clf;
  61. plot(t,e(1,:),'-',t(end),e(1,end),'o');
  62. title('consumption/habit ratio');
  63. xlabel('t');
  64. ylabel('x(t)');
  65. hold on;
  66. drawnow;
  67.  
  68. figure(102);
  69. clf;
  70. plot(t,e(2,:),'-',t(end),e(2,end),'o');
  71. title('capital/habit ratio');
  72. xlabel('t');
  73. ylabel('y(t)');
  74. hold on;
  75. drawnow;
  76.  
  77. %%% use continuation to adjust end-point
  78. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  79. for TT = T0+1:T
  80. solinit = bvpinit(sol,[0 TT]);
  81. sol = bvp4c(@ODE,@BC,solinit,options);
  82. t = linspace(0,TT);
  83. e = deval(sol,t);
  84. e(3,:) = (A-delta-theta+psi*rho.*(e(1,:)-1))/sigma;  %growth rate of consumption
  85. e(4,:) = A-delta-e(1,:)./e(2,:); %growth rate of capital
  86. e(5,:) = 1-e(1,:)./(A.*e(2,:)); %saving rate
  87. fprintf('With T = %g, x(0) = %7.7f, y(0) = %7.7f\n',TT,e(1,1),e(2,1));
  88. fprintf('With T = %g, x(T) = %7.7f, y(T) = %7.7f\n',TT,e(1,end),e(2,end));
  89. fprintf('With T = %g, s(0) = %7.7f, s(T) = %7.7f\n',TT,e(5,1),e(5,end));
  90.  
  91. %%% completing the figures
  92. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  93. figure(101);
  94. plot(t,e(1,:),'-',t(end),e(1,end),'o');
  95. figure(102);
  96. plot(t,e(2,:),'-',t(end),e(2,end),'o');
  97. drawnow;
  98. end
  99. hold off;
  100.  
  101. %%% drawing more figures
  102. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%figure(1)
  103.  
  104. figure(1);
  105. clf;
  106. plot(t,e(1,:),'-',t(end),e(1,end),'o',t(1),e(1,1),'o');
  107. grid on;
  108. title('consumption/habit ratio');
  109. xlabel('t');
  110. ylabel('x(t)');
  111. hold on;
  112. drawnow;
  113.  
  114. figure(2);
  115. clf;
  116. plot(t,e(2,:),'-',t(end),e(2,end),'o',t(1),y0,'o',t(1),e(2,1),'o');
  117. grid on;
  118. title('capital/habit ratio');
  119. xlabel('t');
  120. ylabel('y(t)');
  121. hold on;
  122. drawnow;
  123.  
  124. figure(3);
  125. clf;
  126. plot(t,e(3,:),'-',t(1),e(3,1),'o',t(end),e(3,end),'o');
  127. grid on;
  128. title('consumption growth');
  129. xlabel('t');
  130. ylabel('gc');
  131. hold on;
  132. drawnow;
  133.  
  134. figure(4);
  135. clf;
  136. plot(t,e(4,:),'-',t(1),e(4,1),'o',t(end),e(4,end),'o');
  137. grid on;
  138. title('capital growth');
  139. xlabel('t');
  140. ylabel('gk');
  141. hold on;
  142. drawnow;
  143.  
  144. figure(5);
  145. clf;
  146. plot(t,e(5,:),'-',t(1),e(5,1),'o',t(end),e(5,end),'o');
  147. grid on;
  148. title('saving rate');
  149. xlabel('t');
  150. ylabel('s/y');
  151. hold on;
  152. drawnow;
  153.  
  154. figure(6);
  155. clf;
  156. plot(e(2,:),e(1,:),'-',e(2,1),e(1,1),'o',e(2,end),e(1,end),'o');
  157. grid on;
  158. title('consumption/habit against capital/habit ratios');
  159. xlabel('k/h');
  160. ylabel('c/h');
  161. hold on;
  162. drawnow;
  163.  
  164. figure(7);
  165. clf;
  166. plot(e(2,:),e(3,:),'-',e(2,1),e(3,1),'o',e(2,end),e(3,end),'o');
  167. grid on;
  168. title('consumption growth against capital/habit ratios');
  169. xlabel('k/h');
  170. ylabel('gc');
  171. hold on;
  172. drawnow;
  173.  
  174. figure(8);
  175. clf;
  176. plot(e(2,:),e(4,:),'-',e(2,1),e(4,1),'o',e(2,end),e(4,end),'o');
  177. grid on;
  178. title('capital growth against capital/habit ratios');
  179. xlabel('k/h');
  180. ylabel('gk');
  181. hold on;
  182. drawnow;
  183.  
  184. figure(9);
  185. clf;
  186. plot(e(2,:),e(5,:),'-',e(2,1),e(5,1),'o',e(2,end),e(5,end),'o');
  187. grid on;
  188. title('saving rate against capital/habit ratios');
  189. xlabel('k/h');
  190. ylabel('s/y');
  191. hold on;
  192. drawnow;
  193.  
  194. figure(10);
  195. clf;
  196. plot(e(3,:),e(5,:),'-',e(3,1),e(5,1),'o',e(3,end),e(5,end),'o');
  197. grid on;
  198. title('growth rate of consumption against saving rate');
  199. xlabel('gc');
  200. ylabel('s/y');
  201. hold on;
  202. drawnow;
  203.  
  204. hold off; diary off; save('cow.mat');
  205.  
  206. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  207. function rr = ODE(t,e)
  208. global sigma psi rho theta A delta;
  209. rr = [((A-delta-theta)-(sigma-psi)*rho.*(e(1,:)-1)).*e(1,:)/sigma;
  210.       (A-delta-rho*(e(1,:)-1)).*e(2,:)-e(1,:)];
  211.  
  212. function rr = BC(e0,eT)
  213. global  y0 yss;
  214. rr = zeros(2,1);
  215. rr(1) = eT(2)-yss;
  216. rr(2) = e0(2)-y0;
  217.  
  218. function j = J(t,e)
  219. global sigma psi rho theta A delta xss yss;
  220. j = zeros(2,2);
  221. j(1,1) = -(sigma-psi)*rho*xss/sigma;
  222. j(1,2) = 0;
  223. j(2,1) = -1;
  224. j(2,2) = xss/yss;
  225.  
  226. function rr = U(c,z)  %utility function
  227. global sigma psi;
  228. rr = c^(1-sigma)*z^(psi);
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement