
ERROR while running value function iteration code: Index in position 2 exceeds array bounds (must not exceed 2).
1 次查看(过去 30 天)
显示 更早的评论
% Parameters being spesified
sigma = 1; % utility parameter
delta = 0.025; % depreciation rate
beta = 0.99; % discount factor
alpha = 0; % No capital, solo labor in the production function.
se = 0.007; % volality of the shock associated
epsilon = 0.022; % by 1+z
phi = 0.95; % phi = (1+rho)/2, Cooley&Prescott (1995)
theta = 0.36; % Cooley&Prescott (1995)
z = 0.0050; % Annual GDP growth of the US-taken as 2%. Adjusted to quarterly
myu = 2; % Importance of leisure
%
%
%
% Before, in deterministic value function iteration; we only discretized
% the state space. Here, we shall parametrize also the space spanned by the shocks.
ab = 0;
Z = [0, z]; % -Low z given as 0 and high z given as z at the problem set.
Xi = (1/beta - (1 - delta))/alpha;
Pi = [phi 1-phi; 1-phi phi];
nbn = 51; % Number of data points associated with control var labor in the grid
nba = 2; % Number of values associated with the shock
% Steady State calculations will be different what I did in my previous
% homework setup. Here I refer to Cooley's l, suggested as l=0.69. I also form
% grid by enjoying that info later.
nstar=0.31;
elow=exp(0.95*0+0.05*z);
ehigh=exp(0.05*0+0.95*z);
% Now, just like our first homework's value function iteration, we
% discretize the state space
ngrid=linspace(.8*nstar,1.2*nstar,nbn);
utility = zeros(nbn,nba); %building up the array for utility, dependent on labor and states.
cons = zeros(nbn,nba); %building up the array for consumption, dependent on labor and states.
% Let us express the counters, the letters for control, states and the
% shock component z.
% i stands as the counter of control variable n.
% m stands as the counter of component of the shock z.
% For loop for zero or negative
% consumption to be never chosen. tabulate the utility function such that for zero or negative
% consumption utility remains a large negative number so that
% such values will never be chosen as utility maximizing
%
warning off;
for i = 1:nbn
for m =1:nba
cons(i,m) = exp(Z(m))*ngrid(i);
if cons(i,m) < 0
cons(i,m) = 0;
end
utility(i,m)= (theta * log(cons(i,m))- (1-theta) * log(1-ngrid(i))); %Actually it is u(i,m)= psi*(theta * log(c(i,m))- (1-psi)*(1-theta) * log(1-ngrid(i))) but since countries are symettric we can sum the psi and 1-psi and get the left part written in the code.
end
end
v = zeros(nbn,nba); %initial value function
criterion = 1e-11; % convergence criterion chosen
diff =1; % arbitrary initial value greater
iter =0; % counter for iterations
%Not with epsilon because it is already defined above within the row vector Z.
while diff > criterion
diff = 0;
for m =1:nba
for i=1:nbn
objectivefunc ( : , : ,m, i) = utility( : , : ,m, i) +beta*(Pi(m,1)*(v(:,1)*ones(1,nbn))'+Pi(m,2)*(v(:,2)*ones(1,nbn))'+Pi(m,3)*(v(:,3)*ones(1,nbn))'+Pi(m,4)*(v(:,4)*ones(1,nbn))');
Tv(i,m)=max(max(objectivefunc(:,:,m,i)));
end
end
diff = norm(v-Tv); %for the calculation of the Convergence Criterion
v = Tv; %we update the value function
iter = iter + 1;
end
I get the following error when I am trying to run the simple value function iteration, what might be the problem?
Index in position 2 exceeds array bounds (must not exceed 2).
7 个评论
回答(1 个)
per isakson
2020-4-27
编辑:per isakson
2020-4-27
Before trying to fix the problems see: Debug a MATLAB Program
Then start by reading the code carefully. The first thing that catches my eyes is the indexing of the variable utility. First it is preallocated as a 2D-array, then it is refered to with four indicies. That's weird.
utility = zeros(nbn,nba); % building up the array ... 2D array
...
objectivefunc ( : , : ,m, i) = utility( : , : ,m, i) ... 4D indexing
The line
objectivefunc ( : , : ,m, i) = utility( : , : ,m, i) ...
is hard to read and debug because it's so long. The variable, objectivefunc, doesn't exist the first time this line is executed. Thus, it's not allowed to index into it this way. And since it's just a temporary variable used to carry over the value to the next line indexing is not needed.
Now it's time to run the code, but first set "Pause on Errors"

I've run the script half a dozen times and changes to the code to "fix" errors.
"Do you think it is due to the fact that the sizes of left hand side and right hand side do not match?" The sizes of the lhs and the rhs must be compatible. See Compatible Array Sizes for Basic Operations to learn what that means. All the errors I've encounterted breaks that rule.
I've modified the code in four places (including the onces that you discuss in the comments above). I've marked them with the comment % poi:.
Now this script runs to the end without errors. However, it doesn't converge to a solution, which shouldn't surprise since I've made changes without understanding the code.
%%
% Parameters being spesified
sigma = 1; % utility parameter
delta = 0.025; % depreciation rate
beta = 0.99; % discount factor
alpha = 0; % No capital, solo labor in the production function.
se = 0.007; % volality of the shock associated
epsilon = 0.022; % by 1+z
phi = 0.95; % phi = (1+rho)/2, Cooley&Prescott (1995)
theta = 0.36; % Cooley&Prescott (1995)
z = 0.0050; % Annual GDP growth of the US-taken as 2%. Adjusted to quarterly
myu = 2; % Importance of leisure
%
% Before, in deterministic value function iteration; we only discretized
% the state space. Here, we shall parametrize also the space spanned by the shocks.
ab = 0;
% Z = [0, z]; % -Low z given as 0 and high z given as z at the problem set.
Z = [0,z,z,z]; % poi:
Xi = (1/beta - (1 - delta))/alpha;
% Pi = [phi 1-phi; 1-phi phi];
Pi = [ phi 1-phi phi 1-phi
1-phi phi 1-phi phi
phi 1-phi phi 1-phi
1-phi phi 1-phi phi
]; % poi:
nbn = 51; % Number of data points associated with control var labor in the grid
% nba = 2; % Number of values associated with the shock
nba = 4; % poi:
% Steady State calculations will be different what I did in my previous
% homework setup. Here I refer to Cooley's l, suggested as l=0.69. I also form
% grid by enjoying that info later.
nstar=0.31;
elow=exp(0.95*0+0.05*z);
ehigh=exp(0.05*0+0.95*z);
% Now, just like our first homework's value function iteration, we
% discretize the state space
ngrid=linspace(.8*nstar,1.2*nstar,nbn);
utility = zeros(nbn,nba); %building up the array for utility, dependent on labor and states.
cons = zeros(nbn,nba); %building up the array for consumption, dependent on labor and states.
% Let us express the counters, the letters for control, states and the
% shock component z.
% i stands as the counter of control variable n.
% m stands as the counter of component of the shock z.
% For loop for zero or negative
% consumption to be never chosen. tabulate the utility function such that for zero or
% negative consumption utility remains a large negative number so that such values will
% never be chosen as utility maximizing
%
warning off;
for ii = 1:nbn
for m =1:nba
cons(ii,m) = exp(Z(m))*ngrid(ii);
if cons(ii,m) < 0
cons(ii,m) = 0;
end
% Actually it is u(i,m)= psi*(theta * log(c(i,m))- (1-psi)*(1-theta) *
% log(1-ngrid(i))) but since countries are symettric we can sum the psi and 1-psi
% and get the left part written in the code.
utility(ii,m)= (theta * log(cons(ii,m))- (1-theta) * log(1-ngrid(ii)));
end
end
v = zeros(nbn,nba); %initial value function
criterion = 1e-11; % convergence criterion chosen
diff =1; % arbitrary initial value greater
iter =0; % counter for iterations
% Not with epsilon because it is already defined above within the row vector Z.
while diff > criterion
diff = 0;
for m=1:nba
for ii=1:nbn
t1 = utility(ii,m); % poi:
v1 = Pi(m,1)*(v(:,1)*ones(1,nbn))';
v2 = Pi(m,2)*(v(:,2)*ones(1,nbn))';
v3 = Pi(m,3)*(v(:,3)*ones(1,nbn))';
v4 = Pi(m,4)*(v(:,4)*ones(1,nbn))';
t2 = beta*( v1 + v2 + v3 + v4 );
of = t1 + t2;
Tv(ii,m) = max(max(of));
end
end
diff = norm(v-Tv); %for the calculation of the Convergence Criterion
v = Tv; %we update the value function
iter = iter + 1;
end
1 个评论
per isakson
2020-4-27
To make any code work correctly, one needs a certain domain knowledge. Since I know nothing about macroeconomics, I just tried to help you debug the code a bit more methodically.
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!