# Intermediate variables value at the optimisation achievement

2 views (last 30 days)
Sid S on 16 Jul 2011
Answered: Daniel Frisch on 7 Oct 2020
Hello,
I'am trying unsuccessfuly since several weeks to get the value of intermediate variables at the achievement of fmincon optimisation. I want to have the value of "call" and "e", so please what may I add to the code ??? please I very need help !
My code :
function monOptimisation
%Contraintes
A = [ 1 1 1 1 ; -1 0 0 0 ; 0 -1 0 0 ; 0 0 0 0 ; 0 0 0 -1];
b = [1 ; 0 ; 0 ; 0 ; 0];
x0=[0;0;0;0;0];
prix=data;
strike_vec = data2;
sp = data3;
sig0 = 0.0005;
r = 0;
nbr_sim = 300;
options=optimset('algorithm','interior-point','display','iter');
par= fmincon (@(x)erreursfun(x,prix,strike_vec,sp,sig0,r,nbr_sim),x0,A,b,[],[],[],[],[],options)
function f = erreursfun (par,prix,strike_vec,sp,sig0,r,nbr_sim)
nbr_strike = 36; %size(strike_vec,1)
mat = 257; %size(prix,1)
epsil = randn (nbr_sim,mat);
% Calcul des calls via MC
som_var = zeros(nbr_sim,mat);
som_et = zeros(nbr_sim,mat);
sigma = zeros(nbr_sim,mat);
st = zeros(nbr_sim,mat);
payoff = zeros(nbr_sim,mat);
som_payoff= zeros(mat,nbr_strike);
call= zeros(mat,nbr_strike);
for i = 1: nbr_strike
strike = strike_vec (i);
for j =1 : nbr_sim
sigma (j,1)=(par(1) + par(2) *( epsil(j,1)-par(3)^2)*sig0^2+par(4)*sig0^2)^0.5;
for k = 2 : mat
sigma (j,k)=(par(1) + par(2) * (epsil(j,k)-par(3)^2)*sigma(j,k-1)^2+par(4)*sigma(j,k- 1)^2)^0.5;
end
som_var(j,mat)= sigma (j,mat); %%%
som_et(j,mat)=sigma (j,mat)^0.5*epsil(j,mat); %%%
for k = mat -1 : 1:-1
som_var(j,k)= som_var(j,k+1)+sigma (j,k+1); %%%
som_et(j,k)=som_et(j,k+1)+sigma (j,k+1)^0.5*epsil(j,k+1); %%%
end
for k = 1 : mat
st(j,k)= sp(k)*(exp(r*(mat-k))-(0.5*som_var(j,k))+som_et(j,k)*epsil(j,k));
payoff(j,k)= max(st(j,k)-strike,0);
som_payoff(k,i)=som_payoff(k,i)+payoff(j,k); %%%
end
end
for k = 1 : mat
call(k,i)=exp(r*(mat-k))*((som_payoff(k,i)/nbr_sim));
end
end
% Calcul des erreurs
e=zeros(mat,nbr_strike);
ee=zeros(mat);
eee=0;
for i = 1 : mat
for j = 1 : nbr_strike
e(i,j)=(prix(i,j)- call(i,j))^2;
ee(i)=ee(i)+e(i,j);
end
eee=eee+ee(i);
end
f= eee;

Andrew Newell on 17 Jul 2011
The newsgroup thread Intermediate results of Optimization has a nice solution to this problem.
Andrew Newell on 17 Jul 2011
You reply below that you don't have the resources to calculate the parameters. But if you can't calculate them, how can you expect to save them? Anyway, my impression is that you do calculate them during the optimization.
To implement growdata, just create the function - in its own file or below erreursfun - and call it from erreursfun.

Sid S on 17 Jul 2011
Thanks for your answer, but I don't see how to use growdata where puting it, adding to this growdata takes additional CPU ressources, I need to estimate 4 parameters with 5000 simulations of 10000 observations and my CPU is not powerful.
Andrew Newell on 17 Jul 2011
This should be a comment on my answer. See my answer above for a response.

Daniel Frisch on 7 Oct 2020
You can use my function
outputFcn_global
for this purpose. It stores the intermediate results in a global variable, so you can inspect it afterwards.