function [v,k]=DCS(a,rint,beta,x,p) %Discrete Consumption - Savings %a is a column vector of allowable asset values. %rint is the gross real rate of interest. %x is the level of income when employed. %p is the state transition matrix. %Solution is obtained by value function iteration. %The first set of instructions creates a matrix of current period returns. %R1 for unemployed. R2 for employed. R combines the two. %First R computes consumption values. %Rows correspond to current asset values. %Columns correspond to next period asset values, the control variable. %Then we take log(R) to put in utility terms. %The use of max prevents the possibility of choosing negative consumption. %Negative consumption will never be chosen because log(exp(-100)) %is appreximately minus infinity. %Avoids cluttering output with warning messages about designed log of zero. z=size(a); n=z(1); for i=1:n for j=1:n R1(i,j)=max(rint*a(i)-a(j),exp(-100)); end end R1=log(R1); for i=1:n for j=1:n R2(i,j)=max(rint*a(i)+x-a(j),exp(-100)); end end R2=log(R2); R=[R1;R2]; %Now we initialize the value function as all zero's. v1=zeros(n,1); v2=zeros(n,1); %Set initial criterion to assure the process starts. iterations=0; m=10; v0=[v1;v2]; while m>.000000000001 v0=[v1;v2]; A=R+beta*kron(p,ones(n,1))*[v1';v2']; %Calculates posible values of the Bellman equation terms. %Columns are indexed by the choice variable. %Rows are indexed by the current state variables. [v,k]=max(A'); %Need to take transpose to properly utilize max command. v=v'; m=norm(v-v0,2); %Checks for convergence. v1=v(1:n); v2=v(n+1:2*n); iterations=iterations+1; %Prepares to restart at top of while statement. end %Output variables are defined. v=[v1';v2']'; z=zeros(n,2); z(:,1)=k(1:n)'; z(:,2)=k(n+1:2*n)'; %z values tell us the index value of the optimal policy. %We want to output the asset values of the optimal policy. k=zeros(n,2); for j=1:2 for i=1:n k(i,j)=a(z(i,j)); end end iterations %y value indicates number of iterations completed. %First column of v gives value function when unemployed. %Second column of v gives value function when employed. %First column of k gives optimal controls (a's) when unemployed. %Second column of k gives optimal controls (a's) when employed.