Errors in declaring the function to minimize

1 view (last 30 days)
Sid S
Sid S on 18 Jun 2011
[EDIT: 20110618 10:01 CDT - reformat - WDR]
Hello,
I am trying to write a code to calibrate a GARCH option pricing to market data and I have the following errors :
Function keyword use is invalid here.
Parse error at ')' : usage might be invalid MATLAB syntax.
I have a vector of maturities, for each maturity, I have a vector of strikes and a matrix of market prices, I use cell arrays to deal with them,
In the arguments can I put the cell arrays of data, or have I to declare the matrixes and vectors one by one ?
I am also bloqued with the use of fmincon.
Please help !
om = par(1);
alpha=par(2);
gam =par(3);
beta=par(4);
function f = f (par,prix_array,strike_array,mat_vec,s0_vec,sig0_vec,rest_vec,r,nbr_sim)
nbr_mat = size(mat_vec,1);
eps2=cell(nbr_mat);
for i = 1:nbr_mat
mat = mat_vec(i);
eps1=cell(nbr_strike);
for j = 1: nbr_strike
eps1{j}=randn(nbr_sim,mat);
end
eps2{i}= eps1{j};
end
e=zeros(nbr_mat,nbr_obs);
c=zeros(nbr_mat,nbr_strike);
for i = 1:nbr_mat
vec_strike = strike_array{i};
prix=prix_array{i};
nbr_strike = size(vec_strike,1);
mat = mat_vec(i);
so=s0_vec(i);
sig0=sig0_vec(i);
nbr_obs=mat_vec(i)-rest_vec(i);
eps1=eps2{i};
for j =1 : nbr_strike
strike = vec_strike (j);
eps=eps1{j};
payoff=zeros(nbr_sim);
for k = 1 : nbr_sim
sigma=zeros(nbr_sim,mat+1);
Z = zeros(nbr_sim,mat);
for l = 2 : mat+1
sigma (1,1)=sig0;
Z(1)=1;
sigma (k,l)=(om + (alpha * (eps(k,l-1)-(0.5+ gam))^2)*sigma(k,l-1)^2+beta*sig(l- 1)^2)^0.5;
Z(k,l) = Z(k,l-1)*Exp((-0.5 *(cig(k,l)^2))+(sig(k,l)*eps(k,l)));
end
st = sz * (Exp(r * mat)) * Z(mat);
payoff(k)= max(st-strike,0);
end
c (i,j)= mean (payoff);
%%%%%%%Errors
for l = i : nbr_obs
e(i,j)=e(j)+(prix(l,j)-(c(i,j)*exp(r*(l-mat))))^2;
end
end
c (i,j)% stocker dans un cell array
e(i,j) % stocker dans un cell array
end
ee =zeros(nbr_mat);
for i =1 : nbr_mat
for j =1 :nbr_strike
ee(i)=ee+e(i,j);
end
eee=eee+ ee(i);
end
f = eee;
%Optimisation
% Constraintes
A = [1 1 1 ; -1 0 0 ; 0 -1 0 ; 0 0 -1];
b = [1 ; 0 ; 0 ; 0];
fmincon

Answers (2)

Walter Roberson
Walter Roberson on 18 Jun 2011
The name of your output ("f") cannot be the same as the name of your function ("f")
If you are defining functions in a .m file, then the first non-comment line must be a function definition. It is not allowed to define functions in a script file.
You have to pass parameters to fmincon.
If f is your function to be minimized, then the call to fmincon cannot be inside of f.
You can pass a cell array or a struct to a function. Keep in mind, though, that your function to be minimized must accept a parameter of specific values to be evaluated at.
Any value that you wish to pass in to your function to be minimized must be defined or calculated outside of the function to be minimized.

Gerald
Gerald on 29 Jul 2011
Thanks Walter

Categories

Find more on Conditional Variance Models in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!