Question regarding Stochastic Value Function Iteration
이전 댓글 표시
Hi Everyone, I am trying to solve a stochastic neoclassical-growth model with value function iteration. For some reason which I do not under stand, I get the following error message
if true
Subscript indices must either be real positive integers or logicals.
Error in valfun_stoch (line 22)
c = amat(j,1)*k0^alpha - k + (1-delta)*k0; % consumption
Error in fminbnd (line 119)
x= xf; fx = feval(funfcn,x,varargin{:});
Error in ericsims_vfi_stoch (line 54)
k1 = fminbnd(@valfun_stoch,kmin,kmax);
end
what does this error message with the incorrent matrix indexing mean? I looked it up but it was not clear to me in this context here...
The main code is as follows
if true
clear all;
close all;
tic
global v0 beta delta alpha kmat k0 a0 s kgrid amat prob
% set parameters
alpha = 0.33; % capital's share
beta = 0.95;
delta = 0.1; % depreciation rate (annual)
s=2;
tol = 0.01;
maxits = 300;
dif = tol+1000;
its=0;
kgrid = 4; % grid points + 1
% first solve for steady state
kstar = (alpha/(1/beta - (1-delta)))^(1/(1-alpha)) % steady state k
cstar = kstar^(alpha) - delta*kstar;
istar = delta*kstar;
ystar = kstar^(alpha);
kmin = 0.25*kstar; % minimum
kmax = 1.75*kstar; % maximum
grid = (kmax-kmin)/kgrid; % grid
kmat = kmin:grid:kmax
kmat = kmat';
[N,n] = size(kmat);
amat = [1; 1; 1]
prob=[1/3 1/3 1/3;
1/3 1/3 1/3;
1/3 1/3 1/3;
]
v0 = zeros(N,1);
while dif>tol & its < maxits
for j = 1:3
for i = 1:N
k0 = kmat(i,1);
a0 = amat(j,1);
k1 = fminbnd(@valfun_stoch,kmin,kmax);
v1(i,j) = -valfun_stoch(k1);
k11(i,j) = k1;
end
end
ggg = v1 - v0;
gg = vec(ggg);
dif = norm(v1 - v0)
v0 = v1;
its = its+1
end
toc
end
and the valfun_stoch function is defined as
if true
function val=valfun_stoch(k)
% This program gets the value function for a neoclassical growth model with
% no uncertainty and CRRA utility
global v0 beta delta alpha kmat amat prob k0 s kgrid
klo=max(sum(k>kmat),1); % identify the gridpoint that falls just below . .
%..the choice fork
khi=klo+1;
% do the interpolation
g = v0(klo,:) + (k-kmat(klo))*(v0(khi,:) - v0(klo,:))/(kmat(khi) - kmat(klo))
gg = interp1(kmat,v0,k,'linear')
c = amat(j,1)*k0^alpha - k + (1-delta)*k0; % consumption
if c<0
val = -8888888888888888-800*abs(c);
else
val = (1/(1-s))*(c^(1-s)-1) + beta*(gg*prob(j,:)')
end
val = -val; % make it negative since we're maximizing and code is to minimize.
end
If anyone has a clue that would be awesome, thanks a lot Cheers
답변 (1개)
Roger Stafford
2014년 2월 8일
The problem would appear to be the variable 'j' which you have in the line
c = amat(j,1)*k0^alpha - k + (1-delta)*k0; % consumption
The function 'valfun_stoch' does not know what its value is. You didn't make it a global variable and it doesn't carry over automatically from the for-loop where it is an index. The error message is a bit of an understatement, since the trouble is even more fundamental than not being "real positive integers or logicals". It is totally unknown to 'valfun_stoch'!
댓글 수: 2
derrick
2014년 2월 8일
Stephanie Parsons
2018년 9월 21일
Did you figure out why you were getting the error with 'fminbnd'? I'm having the same problem.
카테고리
도움말 센터 및 File Exchange에서 Simscape에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!