Value Function Iteration Code Not Converging
Hello all, I am currently coding a value/policy function iteration code to solve an economic problem. The problem that this is solving for is that there is a certain amount of S and R currently owned (these are the state variables). S and R generate income, which must be allocated to purchase more S and R for the next "round" (S’ and R’, equivalently. S’ and R’ are the control variables). This code solves for the optimal amount of income that should be spent on R’ at any given level of S and R. Right now my code below only works at specific parameter values and does not converge–the error term gets stuck, and I cannot seem to figure out why. My code is below. Any help on what could possibly prevent it from converging would be greatly appreciated. Thank you all in advance!
clc;
clear;
% Model Parameters and initalize matrices
numRisky = 100;
numSafe = 100;
v = (1:numSafe)’ + (1:numRisky);
vnew=ones(numSafe, numRisky);
policy = zeros(numSafe, numRisky);
delta=0.99;
ctol=0.001;
ntol=200;
count=0;
norm=1;
vx = ones(numSafe);
vy = ones(numRisky);
alpha = 1;
while norm>ctol && count<ntol
for R=1:numRisky
riskyReturns = 1:1:3;
numReturns = numel(riskyReturns);
pReturns = (1/numReturns)*ones(1, numReturns);
u = zeros(1, numReturns);
p = zeros(1, numReturns);
for S = 1:numSafe
safeReturn = 2;
for r = 1:numReturns
safeIncome = S*safeReturn;
riskyIncome = R*riskyReturns(r);
posI = safeIncome + riskyIncome;
U = alpha*log(S+R);
W = zeros(1, posI);
for i = 1:posI
if S + (posI-i) > numSafe && R + i <= numRisky
vBar = v(numSafe,R+i) + vx(R+i)*(S+posI-i-numSafe); %conduct linear approximation
W(i) = (1-delta)*U + delta*vBar;
elseif R + i > numRisky && S + (posI-i) <= numSafe
vBar = v(S+posI-i,numRisky) + vy(S+posI-i)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
elseif R + i > numRisky && S + (posI-i) > numSafe
vBar = v(numSafe,numRisky) + vx(numRisky)*(S+posI-i-numSafe) + vy(numSafe)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
else
W(i) = (1-delta)*U + delta*v(S + (posI – i), R + i); %no linear approximation needed here
end
end
[w, ind] = max(W);
u(r) = w;
p(r) = ind/posI;
end
vnew(S, R) = dot(u, pReturns);
policy(S, R) = dot(p, pReturns);
end
end
%calculate slopes for linear approximation if S’ and R’ are greater than numSafe and numRisky, respectively
for n = 1:numSafe
vx(n) = v(numSafe,n)-v(numSafe-1,n);
end
for m = 1:numRisky
vy(m) = v(m,numRisky)-v(m,numRisky-1);
end
%calculate convergence criteria and update value function
count = count + 1;
norm = max(abs(log(vnew(:)) – log(v(:))));
v = vnew;
disp(‘Iteration:’)
disp(count)
disp(‘Error Term:’)
disp(norm)
endHello all, I am currently coding a value/policy function iteration code to solve an economic problem. The problem that this is solving for is that there is a certain amount of S and R currently owned (these are the state variables). S and R generate income, which must be allocated to purchase more S and R for the next "round" (S’ and R’, equivalently. S’ and R’ are the control variables). This code solves for the optimal amount of income that should be spent on R’ at any given level of S and R. Right now my code below only works at specific parameter values and does not converge–the error term gets stuck, and I cannot seem to figure out why. My code is below. Any help on what could possibly prevent it from converging would be greatly appreciated. Thank you all in advance!
clc;
clear;
% Model Parameters and initalize matrices
numRisky = 100;
numSafe = 100;
v = (1:numSafe)’ + (1:numRisky);
vnew=ones(numSafe, numRisky);
policy = zeros(numSafe, numRisky);
delta=0.99;
ctol=0.001;
ntol=200;
count=0;
norm=1;
vx = ones(numSafe);
vy = ones(numRisky);
alpha = 1;
while norm>ctol && count<ntol
for R=1:numRisky
riskyReturns = 1:1:3;
numReturns = numel(riskyReturns);
pReturns = (1/numReturns)*ones(1, numReturns);
u = zeros(1, numReturns);
p = zeros(1, numReturns);
for S = 1:numSafe
safeReturn = 2;
for r = 1:numReturns
safeIncome = S*safeReturn;
riskyIncome = R*riskyReturns(r);
posI = safeIncome + riskyIncome;
U = alpha*log(S+R);
W = zeros(1, posI);
for i = 1:posI
if S + (posI-i) > numSafe && R + i <= numRisky
vBar = v(numSafe,R+i) + vx(R+i)*(S+posI-i-numSafe); %conduct linear approximation
W(i) = (1-delta)*U + delta*vBar;
elseif R + i > numRisky && S + (posI-i) <= numSafe
vBar = v(S+posI-i,numRisky) + vy(S+posI-i)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
elseif R + i > numRisky && S + (posI-i) > numSafe
vBar = v(numSafe,numRisky) + vx(numRisky)*(S+posI-i-numSafe) + vy(numSafe)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
else
W(i) = (1-delta)*U + delta*v(S + (posI – i), R + i); %no linear approximation needed here
end
end
[w, ind] = max(W);
u(r) = w;
p(r) = ind/posI;
end
vnew(S, R) = dot(u, pReturns);
policy(S, R) = dot(p, pReturns);
end
end
%calculate slopes for linear approximation if S’ and R’ are greater than numSafe and numRisky, respectively
for n = 1:numSafe
vx(n) = v(numSafe,n)-v(numSafe-1,n);
end
for m = 1:numRisky
vy(m) = v(m,numRisky)-v(m,numRisky-1);
end
%calculate convergence criteria and update value function
count = count + 1;
norm = max(abs(log(vnew(:)) – log(v(:))));
v = vnew;
disp(‘Iteration:’)
disp(count)
disp(‘Error Term:’)
disp(norm)
end Hello all, I am currently coding a value/policy function iteration code to solve an economic problem. The problem that this is solving for is that there is a certain amount of S and R currently owned (these are the state variables). S and R generate income, which must be allocated to purchase more S and R for the next "round" (S’ and R’, equivalently. S’ and R’ are the control variables). This code solves for the optimal amount of income that should be spent on R’ at any given level of S and R. Right now my code below only works at specific parameter values and does not converge–the error term gets stuck, and I cannot seem to figure out why. My code is below. Any help on what could possibly prevent it from converging would be greatly appreciated. Thank you all in advance!
clc;
clear;
% Model Parameters and initalize matrices
numRisky = 100;
numSafe = 100;
v = (1:numSafe)’ + (1:numRisky);
vnew=ones(numSafe, numRisky);
policy = zeros(numSafe, numRisky);
delta=0.99;
ctol=0.001;
ntol=200;
count=0;
norm=1;
vx = ones(numSafe);
vy = ones(numRisky);
alpha = 1;
while norm>ctol && count<ntol
for R=1:numRisky
riskyReturns = 1:1:3;
numReturns = numel(riskyReturns);
pReturns = (1/numReturns)*ones(1, numReturns);
u = zeros(1, numReturns);
p = zeros(1, numReturns);
for S = 1:numSafe
safeReturn = 2;
for r = 1:numReturns
safeIncome = S*safeReturn;
riskyIncome = R*riskyReturns(r);
posI = safeIncome + riskyIncome;
U = alpha*log(S+R);
W = zeros(1, posI);
for i = 1:posI
if S + (posI-i) > numSafe && R + i <= numRisky
vBar = v(numSafe,R+i) + vx(R+i)*(S+posI-i-numSafe); %conduct linear approximation
W(i) = (1-delta)*U + delta*vBar;
elseif R + i > numRisky && S + (posI-i) <= numSafe
vBar = v(S+posI-i,numRisky) + vy(S+posI-i)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
elseif R + i > numRisky && S + (posI-i) > numSafe
vBar = v(numSafe,numRisky) + vx(numRisky)*(S+posI-i-numSafe) + vy(numSafe)*(R+i-numRisky);
W(i) = (1-delta)*U + delta*vBar; %conduct linear approximation
else
W(i) = (1-delta)*U + delta*v(S + (posI – i), R + i); %no linear approximation needed here
end
end
[w, ind] = max(W);
u(r) = w;
p(r) = ind/posI;
end
vnew(S, R) = dot(u, pReturns);
policy(S, R) = dot(p, pReturns);
end
end
%calculate slopes for linear approximation if S’ and R’ are greater than numSafe and numRisky, respectively
for n = 1:numSafe
vx(n) = v(numSafe,n)-v(numSafe-1,n);
end
for m = 1:numRisky
vy(m) = v(m,numRisky)-v(m,numRisky-1);
end
%calculate convergence criteria and update value function
count = count + 1;
norm = max(abs(log(vnew(:)) – log(v(:))));
v = vnew;
disp(‘Iteration:’)
disp(count)
disp(‘Error Term:’)
disp(norm)
end value function iteration, bellman, dynamic program, matlab, bellman equation MATLAB Answers — New Questions