defining functions in equivalent ways but get different results
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
when i run the following code with different ways of defining the function, I got the different results (the graphs produced are so different).
But I see that both ways of defining the function are equivalent. I dont know why the results come out differently?
Really appreciate any comment or answer.
Thank you
Here is the code and 2 function definitions (at the end of the code)
% HW2
% Solve deterministic growth model by value function iteration + linear
% interpolation
% Housekeeping
clc;
clear all;
close all;
tic
% Parameters
global A alpha beta delta kmat k0 kgrid V0
A=1 ;
alpha=0.6;
beta=0.96;
delta=1;
% first solve the stead state value
kstar = (alpha*beta*A)^(1/(1-alpha)); % steady state k
% Grid points
kmin=0.25*kstar;
kmax=2*kstar;
kgrid = 30;
grid=(kmax-kmin)/(kgrid-1);
kmat = kmin:grid:kmax;
kmat=kmat';
[N,n]=size(kmat);
% Iteration parameters
tol=0.0001;
maxits=300;
its=0;
dif=tol+1;
V0 = zeros(N,1); %initial guess of value function
% Iteration
while dif > tol && its < maxits
for i=1:N
k0=kmat(i,1);
k1=fminbnd(@intlinear,kmin,kmax);
k11(i,1)=k1; %put k' in order in a vector, so it position is ith elment--> it is optimal for k =k(i)
V1(i,1)=-intlinear(k1);
end
% no longer (i) --> move out of for loop
dif = norm(V1-V0);
V0=V1;
its=its+1;
end
Vapp=V0;
% Compute policy function
Papp = k11;
toc;
% True value/policy function
Vtrue = alpha/(1-alpha*beta)*log(kmat) + 1/(1-beta)*(log(1-alpha*beta)+ alpha*beta/(1-alpha*beta)*log(alpha*beta));
Ptrue = alpha*beta*A*(kmat).^alpha;
figure;
kaxis=kmin:grid:kmax;
plot(kaxis, Vtrue, '-', kaxis, Vapp, '--');
title('True vs Approximated Value Function - Linear Interpolation');
legend({'True VF','Approx. VF'},'Location','southeast');
% Plot distance between two functions
distance= Vtrue - Vapp;
distanceP= Ptrue-Papp;
plot(kaxis, distance, '--');
title('Difference between true and approximated VF');
plot(kaxis, distanceP, '--');
title('Difference between true and approximated PF');
% first way of defining function
function val=intlinear(k)
global A alpha beta delta kmat k0 kgrid V0
% v = ln(k0^alpha +(1-delta)*k0 - kmat) + v0, do not need to take this, we
% have it, as zero vector initially, and update everytime
g = interp1(kmat,V0,k,'linear');
c = A*k0^alpha - k;
if c <= 0
val = -9999999 - 999*abs(c);
else
val = log(c) + beta*g;
end
val = -val; % make it negative since we're maximizing and code is to minimize.
% 2nd way of define function
function val=intlinear(k)
global A alpha beta delta kmat k0 kgrid V0
% v = ln(k0^alpha +(1-delta)*k0 - kmat) + v0, do not need to take this, we
% have it, as zero vector initially, and update everytime
g = interp1(kmat,V0,k,'linear');
c = A*k0^alpha - k;
c=max(0,c)
val = log(c) + beta*g; % in matlab log(0)=-inf
val = -val; % make it negative since we're maximizing and code is to minimize.
2 commentaires
Stephen23
le 9 Fév 2019
They are not equivalent:
>> c = -1;
>> val = -9999999 - 999*abs(c) % 1st function
val = -10000998
>> log(max(0,c)) % 2nd function
ans = -Inf
Réponses (0)
Voir également
Catégories
En savoir plus sur Statistics and Machine Learning Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!