楼主: ibicfzks
7897 5

[问答] 求随机动态规划解法的matlab实例,拜托了 [推广有奖]

  • 2关注
  • 1粉丝

已卖:1份资源

博士生

77%

还不是VIP/贵宾

-

威望
0
论坛币
27002 个
通用积分
26.8848
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
2961 点
帖子
127
精华
0
在线时间
472 小时
注册时间
2009-1-17
最后登录
2026-1-13

楼主
ibicfzks 发表于 2011-3-17 23:49:13 |AI写论文
100论坛币
求随机动态规划解法的matlab实例,拜托大家了。

最佳答案

qibbxxt 查看完整内容

找了一个程序,希望对你有所帮助
关键词:MATLAB matla atlab 动态规划 Mat MATLAB 实例 随机 解法 动态规划

沙发
qibbxxt 发表于 2011-3-17 23:49:14
找了一个程序,希望对你有所帮助

  1. clear all;
  2. close all;
  3. % Define the structural parameters of the model,
  4. % i.e. policy invariant preference and technology parameters
  5. % alpha : capital's share of output
  6. % beta  : time discount factor
  7. % delta : depreciation rate
  8. % sigma : risk-aversion parameter, also intertemp. subst. param.
  9. alpha = .35;
  10. beta  = .98;
  11. delta = .025;
  12. sigma = 2;

  13. % Number of exogenous states
  14. gz = 2;
  15. % Values of z
  16. z = [4.95
  17.      5.05];
  18. % Probabilites for z     
  19. prh = .5;
  20. prl = 1-prh;     
  21. % Expected value of z
  22. zbar = prh*z(1) + prl*z(2);

  23. % Find the steady-state level of capital as a function of
  24. %   the structural parameters
  25. kstar = ((1/beta - 1 + delta)/(alpha*zbar))^(1/(alpha-1));
  26. % Define the number of discrete values k can take
  27. gk = 101;
  28. k  = linspace(0.90*kstar,1.10*kstar,gk);

  29. % Compute a (gk x gk x gz) dimensional consumption matrix c
  30. %   for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.
  31. for h = 1 : gk
  32.    for i = 1 : gk
  33.        for j = 1 : gz
  34.             c(h,i,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - k(i);
  35.             if c(h,i,j) < 0
  36.                 c(h,i,j) = 0;
  37.             end
  38.             % h is the counter for the endogenous state variable k_t
  39.             % i is the counter for the control variable k_t+1
  40.             % j is the counter for the exogenous state variable z_t
  41.        end
  42.    end
  43. end
  44. % Compute a (gk x gk x gz) dimensional consumption matrix u
  45. %   for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.
  46. for h = 1 : gk
  47.    for i = 1 : gk
  48.        for j = 1 : gz
  49.             if sigma == 1
  50.                 u(h,i,j) = log(c(h,i,j))
  51.             else
  52.                 u(h,i,j) = (c(h,i,j)^(1-sigma) - 1)/(1-sigma);   
  53.             end
  54.             % h is the counter for the endogenous state variable k_t
  55.             % i is the counter for the control variable k_t+1
  56.             % j is the counter for the exogenous state variable z_t
  57.        end
  58.    end
  59. end

  60. % Define the initial matrix v as a matrix of zeros (could be anything)
  61. v = zeros(gk,gz);
  62. % Set parameters for the loop
  63. convcrit = 1E-6;  % chosen convergence criterion
  64. diff = 1;         % arbitrary initial value greater than convcrit
  65. iter = 0;         % iterations counter
  66. while diff > convcrit
  67.     % for each combination of k_t and gamma_t
  68.     %   find the k_t+1 that maximizes the sum of instantenous utility and
  69.     %   discounted continuation utility
  70.     for h = 1 : gk
  71.         for j = 1 : gz
  72.             Tv(h,j) = max( u(h,:,j) + beta*(prh*v(:,1)' + prl*v(:,2)') );
  73.         end
  74.     end
  75.     iter = iter + 1;
  76.     diff = norm(Tv - v);
  77.     v = Tv;
  78. end

  79. % Find the implicit decision rule for k_t+1 as a function of the state
  80. %   variables k_t and z_t
  81. for h = 1 : gk
  82.     for j = 1 : gz
  83.         % Using the [ ] syntax for max does not only give the value, but
  84.         %   also the element chosen.
  85.         [Tv,gridpoint] = max(u(h,:,j) + beta*(prh*v(:,1)' + prl*v(:,2)'));
  86.         % Find what grid point of the k vector which is the optimal decision
  87.         kgridrule(h,j) = gridpoint;
  88.         % Find what value for k_t+1 which is the optimal decision
  89.         kdecrule(h,j) = k(gridpoint);
  90.     end
  91. end

  92. % Plot it and save it as a jpg-file
  93. figure
  94. plot(k,kdecrule,k,k);
  95. xlabel('k_t')
  96. ylabel('k_{t+1}')
  97. print -djpeg kdecrule.jpg

  98. % Compute the optimal decision rule for c as a function of the state
  99. %    variables
  100. for h = 1 : gk       % counter for k_t
  101.     for j = 1 : gz   % counter for z_t
  102.         cdecrule(h,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - kdecrule(h,j);
  103.     end
  104. end
  105. figure
  106. plot(k,cdecrule)
  107. xlabel('k_t')
  108. ylabel('c_t')
  109. print -djpeg decrulec.jpg
  110. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


  111. % Simulation
  112. % Aribrary starting point
  113. enostate = round(gk/2);
  114. ksim(1) = k(enostate);
  115. % Draw a sequence of 100 random variables and use the optimal decision rule
  116. for i = 1 : 100
  117.     draw = rand;
  118.     if draw < prh
  119.         exostate = 1;
  120.     else
  121.         exostate = 2;
  122.     end
  123.    
  124.     kprimegrid = kgridrule(enostate,exostate);
  125.     kprime = k(kprimegrid);
  126.    
  127.     zsim(i) = z(exostate);
  128.     ksim(i+1) = kprime;
  129.     ysim(i) = z(exostate)*ksim(i)^alpha;
  130.     isim(i) = ksim(i+1) - (1-delta)*ksim(i);
  131.     csim(i) = ysim(i) - isim(i);
  132. end
  133. figure
  134. plot(1:100,ysim/mean(ysim),1:100,csim/mean(csim),1:100,isim/mean(isim),1:100,ksim(1:100)/mean(ksim(1:100)))
  135. legend('y','c','i','k')
  136. title('Simulation 100 draws, devations from mean')
  137. print -djpeg simulation.jpg
复制代码

藤椅
ibicfzks 发表于 2011-3-21 13:41:34
2# qibbxxt 你能提供程序,真是太感谢了。不知道你能不能提供这个程序来源的文章。

板凳
sandy12208 发表于 2011-4-8 15:18:25
怎么给币法?我可以告诉你

报纸
driftgu 发表于 2011-4-27 05:02:55
我也很想知道这个程序的内容,能否告知?

地板
matlab-007 发表于 2016-8-14 19:13:57
这应该是你对所使用的工具箱中某工具的功能了解不全所致

请检查你的toolbox 确定你所使用的工具有你所使用的功能

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-17 14:09