matlab 随机规划,随机动态规划 - MATLAB等数学软件专版 - 经管之家(原人大经济论坛)...

clear all;

close all;

% Define the structural parameters of the model,

% i.e. policy invariant preference and technology parameters

% alpha : capital's share of output

% beta  : time discount factor

% delta : depreciation rate

% sigma : risk-aversion parameter, also intertemp. subst. param.

alpha = .35;

beta  = .98;

delta = .025;

sigma = 2;

% Number of exogenous states

gz = 2;

% Values of z

z = [4.95

5.05];

% Probabilites for z

prh = .5;

prl = 1-prh;

% Expected value of z

zbar = prh*z(1) + prl*z(2);

% Find the steady-state level of capital as a function of

%   the structural parameters

kstar = ((1/beta - 1 + delta)/(alpha*zbar))^(1/(alpha-1));

% Define the number of discrete values k can take

gk = 101;

k  = linspace(0.90*kstar,1.10*kstar,gk);

% Compute a (gk x gk x gz) dimensional consumption matrix c

%   for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.

for h = 1 : gk

for i = 1 : gk

for j = 1 : gz

c(h,i,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - k(i);

if c(h,i,j) < 0

c(h,i,j) = 0;

end

% h is the counter for the endogenous state variable k_t

% i is the counter for the control variable k_t+1

% j is the counter for the exogenous state variable z_t

end

end

end

% Compute a (gk x gk x gz) dimensional consumption matrix u

%   for all the (gk x gk x gz) values of k_t, k_t+1 and z_t.

for h = 1 : gk

for i = 1 : gk

for j = 1 : gz

if sigma == 1

u(h,i,j) = log(c(h,i,j))

else

u(h,i,j) = (c(h,i,j)^(1-sigma) - 1)/(1-sigma);

end

% h is the counter for the endogenous state variable k_t

% i is the counter for the control variable k_t+1

% j is the counter for the exogenous state variable z_t

end

end

end

% Define the initial matrix v as a matrix of zeros (could be anything)

v = zeros(gk,gz);

% Set parameters for the loop

convcrit = 1E-6;  % chosen convergence criterion

diff = 1;         % arbitrary initial value greater than convcrit

iter = 0;         % iterations counter

while diff > convcrit

% for each combination of k_t and gamma_t

%   find the k_t+1 that maximizes the sum of instantenous utility and

%   discounted continuation utility

for h = 1 : gk

for j = 1 : gz

Tv(h,j) = max( u(h,:,j) + beta*(prh*v(:,1)' + prl*v(:,2)') );

end

end

iter = iter + 1;

diff = norm(Tv - v);

v = Tv;

end

% Find the implicit decision rule for k_t+1 as a function of the state

%   variables k_t and z_t

for h = 1 : gk

for j = 1 : gz

% Using the [ ] syntax for max does not only give the value, but

%   also the element chosen.

[Tv,gridpoint] = max(u(h,:,j) + beta*(prh*v(:,1)' + prl*v(:,2)'));

% Find what grid point of the k vector which is the optimal decision

kgridrule(h,j) = gridpoint;

% Find what value for k_t+1 which is the optimal decision

kdecrule(h,j) = k(gridpoint);

end

end

% Plot it and save it as a jpg-file

figure

plot(k,kdecrule,k,k);

xlabel('k_t')

ylabel('k_{t+1}')

print -djpeg kdecrule.jpg

% Compute the optimal decision rule for c as a function of the state

%    variables

for h = 1 : gk       % counter for k_t

for j = 1 : gz   % counter for z_t

cdecrule(h,j) = z(j)*k(h)^alpha + (1-delta)*k(h) - kdecrule(h,j);

end

end

figure

plot(k,cdecrule)

xlabel('k_t')

ylabel('c_t')

print -djpeg decrulec.jpg

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Simulation

% Aribrary starting point

enostate = round(gk/2);

ksim(1) = k(enostate);

% Draw a sequence of 100 random variables and use the optimal decision rule

for i = 1 : 100

draw = rand;

if draw < prh

exostate = 1;

else

exostate = 2;

end

kprimegrid = kgridrule(enostate,exostate);

kprime = k(kprimegrid);

zsim(i) = z(exostate);

ksim(i+1) = kprime;

ysim(i) = z(exostate)*ksim(i)^alpha;

isim(i) = ksim(i+1) - (1-delta)*ksim(i);

csim(i) = ysim(i) - isim(i);

end

figure

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)))

legend('y','c','i','k')

title('Simulation 100 draws, devations from mean')

print -djpeg simulation.jpg

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值